【发布时间】:2020-07-21 20:45:59
【问题描述】:
我有一些从图像中裁剪出来的面孔,我想通过去噪自动编码器运行它们,我从here 获得的代码。当我在 MNIST 数据集上运行代码时,结果看起来很好,就像网站上的一样。但是,当我在自己的图像上运行它时,我得到一个大部分或完全黑色的图像,而不是简单的没有噪点的相同图像。
这是我调整大小之前的original image for reference,所以你可以知道它的样子。
这是image after resizing,为了将其提供给自动编码器,我必须这样做。我将其缩小为 28x28。
这些是results plotted。对于第一个结果,我实际上希望我的原始灰度图像在我将其输入自动编码器之前出现。对于第二行,我希望它是相同的图像,但没有噪点。如您所见,我得到了这些奇怪的输出,我不知道为什么。
这是我在 MNIST 数据集上尝试过的代码。对于我的数据集,我跳过了 MNIST 数据集的预处理,而是对我自己的图像进行了预处理(缩小尺寸,使它们成为灰度......它们的尺寸是(28、28、1),就像原始代码一样。我尝试改变Epochs 的数量(我经历了 10、50 和 100),但没有明显差异。我考虑过更改层,但在查看了一些论文和其他代码后,层似乎与呈现的相同.我尝试查找自动编码器在像我这样的常规图像上工作的教程,而不仅仅是 MNIST 数据集,但我真的找不到任何东西。我也很困惑为什么,当我绘制原始数组时,我会变黑正方形,即使当我使用 cv2_imshow 中继它时,我得到了调整大小后显示的图像。我真的不知道这是否是同一个问题。我也尝试在我自己的数据集上训练自动编码器(其中有 785 个类似的图像到我上面显示的那些),但无济于事。我已经显示了我使用的代码,如果有什么需要理解我的问题,请告诉我。
import keras
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
from keras.callbacks import TensorBoard
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
noise_factor = 0.5
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
n = 10
plt.figure(figsize=(20, 2))
for i in range(1, n):
ax = plt.subplot(1, n, i)
plt.imshow(x_test_noisy[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
input_img = Input(shape=(28, 28, 1)) # 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(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(grayscale, grayscale,
epochs=100,
batch_size=128,
shuffle=True,
validation_data=(grayscale, grayscale),
callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=True)])
这是我用来将图像输入自动编码器并显示结果的代码。
arr= cv2.imread('/content/FramesResized/frame0000sec.jpg')
#Converting the image to grayscale
gray = cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY)
#Adding an axis as when the image was converted to grayscale, it become (28,28) and I need it to be (28,28,1)
if gray.ndim == 2:
gray = np.expand_dims(gray, axis=2)
#Making a new array to take my images, currently of which there is only one
grayscale = np.zeros([785, 28, 28, 1], dtype=np.uint8)
grayscale[0] = gray
#Feeding my image into the autoencoder
decoded_imgs = autoencoder.predict(grayscale)
#Plotting the before and after images
plt.figure(figsize=(20, 4))
for i in range(1, n):
# display original
ax = plt.subplot(2, n, i)
plt.imshow(grayscale[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
ax = plt.subplot(2, n, i + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
【问题讨论】:
标签: python image-processing keras autoencoder