python RGB自动编码器输出空白图像

57hvy0tb  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(138)

我正在做一个大学作业,任务是做一个去噪自动编码器来编码和解码有噪声的图像。我已经实现了一切权利,但当试图预测它输出白色图像。
层数:

input_img = keras.Input(shape=(32, 32, 3))
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
encoded = x
x = layers.Conv2DTranspose(32, (3, 3), strides=(2, 2), activation='relu', padding='same')(encoded)
x = layers.Conv2DTranspose(32, (3, 3), strides=(2, 2), activation='relu', padding='same')(x)
decoded = layers.Conv2D(3, (3, 3), activation='relu', padding='same')(x)
autoencoder = keras.Model(input_img, decoded)
s = 20 * 5000
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(0.01, s, 0.1)
autoencoder.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=lr_schedule), loss='mean_squared_error',metrics=['accuracy'])
autoencoder.summary()

负载数据:

(x_train, _), (x_test, _) = cifar100.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), 32, 32, 3))
x_test = np.reshape(x_test, (len(x_test), 32, 32, 3))
x_train = x_train[:5000]
x_test = x_test[6000:7000]

noise_factor = 0.2
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.)

拟合和预测:

autoencoder.fit(x_train_noisy, x_train,
                epochs=50,
                batch_size=128,
                shuffle=True)
decoded_imgs = autoencoder.predict(x_test_noisy)

for i in range(5):
    # Display original
    ax = plt.subplot(2, 5, i + 1)
    plt.imshow(x_test_noisy[i])
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    # Display reconstruction
    ax = plt.subplot(2, 5, i + 1 + 5)
    plt.imshow(decoded_imgs[i])
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

输出:

c9qzyr3d

c9qzyr3d1#

生成的输出可能是由于最大池,它通过减少前一个卷积层输出中的像素数量来减少图像的维度。请尝试像下面这样更改模型。

input_img = keras.Input(shape=(32, 32, 3))
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
encoded = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)

x = layers.Conv2DTranspose(16, (3, 3), strides=(2, 2), activation='relu', padding='same')(encoded)
x = layers.Conv2DTranspose(16, (3, 3), strides=(2, 2), activation='relu', padding='same')(encoded)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
decoded = layers.Conv2D(3, (3, 3), activation='relu', padding='same')(x)

请查找完整的代码here。谢谢!

相关问题