无监督的CNN keras模型

时间:2019-02-22 09:59:40

标签: python-3.x keras conv-neural-network unsupervised-learning

我是在python中使用无监督的CNN模型的新手。我正在尝试使用CNN模型对无监督的频谱图输入图像进行图像分类。每个图像的大小为523宽度和393高度。我尝试了以下代码

    X_data = []
files = glob.glob ("C:/train/*.png")
for myFile in files:
    image = cv2.imread (myFile)
    image_resized = misc.imresize(image, (523,393))
    image_resi = misc.imresize(image_resized, (28, 28))
    assert image_resized.shape == (523,393, 3), "img %s has shape %r" % (myFile, image_resized.shape)
    X_data.append (image_resi)

X_datatest = []
files = glob.glob ("C:/test/*.png")
for myFile in files:
    image = cv2.imread (myFile)
    image_resized = misc.imresize(image, (523,393))
    image_resi = misc.imresize(image_resized, (28, 28))
    assert image_resized.shape == (523,393, 3), "img %s has shape %r" % (myFile, image_resized.shape)
    X_datatest.append (image_resi)

X_data = np.array(X_data)
X_datatest = np.array(X_datatest)    

X_data= X_data.astype('float32') / 255.
X_datatest = X_datatest.astype('float32') / 255.
X_data = np.reshape(X_data, (len(X_data), 28, 28, 3))  # adapt this if using `channels_first` image data format
X_datatest = np.reshape(X_datatest, (len(X_datatest), 28, 28, 3))  # adapt this if using `channels_first` image data format

noise_factor = 0.5
x_train_noisy = X_data + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_data.shape) 
x_test_noisy = X_datatest + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_datatest.shape) 

x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)


input_img = Input(shape=(28, 28, 3))  # adapt this if using `channels_first` image data format

x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (7, 7, 32)

x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy', metrics=['accuracy'] )

autoencoder.fit(x_train_noisy, X_data,
                epochs=100,
                batch_size=128,
                verbose = 2,
                validation_data=(x_test_noisy, X_datatest),
                callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)])

我试图制造噪音并将其作为标签输入,因为我没有标签,因为这是无监督的频谱图数据。但是,仅出于精度考虑,输出为33%。我不知道为什么谁能帮我这个忙,并尝试让我理解过滤器,内核的数量以及基于什么的28 * 28调整大小?为什么我们只使用523宽度和393高度的图像大小?

0 个答案:

没有答案