1. 自动编码器简介
自动编码器是一种特殊类型的神经网络,经过训练后可将其输入复制到输出。例如,给定一张手写数字图像,自动编码器首先将该图像编码为较低维度的潜在表示,然后将潜在表示解码回图像。自动编码器学习压缩数据,同时最小化重构误差。
书接上回 【自动编码器1 重建图像 全python代码】,自动编码器还可以通过训练来去除图像中的噪声。您将通过对每幅图像应用随机噪声来创建 Fashion MNIST 数据集的噪声版本。然后,您将使用噪声图像作为输入,并使用原始图像作为目标来训练自动编码器。
2. Python 实现
让我们重新导入数据集。
2.1 导入 TensorFlow 和其他库
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model
2.2 加载数据集
导入 Fashion MNIST 数据集训练基本自动编码器。此数据集中的每幅图像均为 28x28 像素。
(x_train, _), (x_test, _) = fashion_mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]
print(x_train.shape)
2.3 自动编码器:图像去噪
先通过对每幅图像应用随机噪声来创建 Fashion MNIST 数据集的噪声版本。然后,您将使用噪声图像作为输入,并使用原始图像作为目标来训练自动编码器。
- 向图像添加随机噪声
noise_factor = 0.2
x_train_noisy = x_train + noise_factor * tf.random.normal(shape=x_train.shape)
x_test_noisy = x_test + noise_factor * tf.random.normal(shape=x_test.shape)
x_train_noisy = tf.clip_by_value(x_train_noisy, clip_value_min=0., clip_value_max=1.)
x_test_noisy = tf.clip_by_value(x_test_noisy, clip_value_min=0., clip_value_max=1.)
- 绘制噪声图像
n = 10
plt.figure(figsize=(20, 2))
for i in range(n):
ax = plt.subplot(1, n, i + 1)
plt.title("original + noise")
plt.imshow(tf.squeeze(x_test_noisy[i]))
plt.gray()
plt.show()
3. 定义卷积自动编码器
您将使用 中的Conv2D层encoder和中的Conv2DTranspose层训练卷积自动编码器 decoder。
class Denoise(Model):
def __init__(self):
super(Denoise, self).__init__()
self.encoder = tf.keras.Sequential([
layers.Input(shape=(28, 28, 1)),
layers.Conv2D(16, (3, 3), activation='relu', padding='same', strides=2),
layers.Conv2D(8, (3, 3), activation='relu', padding='same', strides=2)])
self.decoder = tf.keras.Sequential([
layers.Conv2DTranspose(8, kernel_size=3, strides=2, activation='relu', padding='same'),
layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation='relu', padding='same'),
layers.Conv2D(1, kernel_size=(3, 3), activation='sigmoid', padding='same')])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
autoencoder = Denoise()
autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())
autoencoder.fit(x_train_noisy, x_train,
epochs=10,
shuffle=True,
validation_data=(x_test_noisy, x_test))
查看编码器的摘要。注意图像从 28x28 降采样到 7x7。
autoencoder.encoder.summary()
同样解码器将图像从 7x7 上采样到 28x28。
autoencoder.decoder.summary()
4. 绘制自动编码器产生的噪声图像和去噪图像
encoded_imgs = autoencoder.encoder(x_test_noisy).numpy()
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
# display original + noise
ax = plt.subplot(2, n, i + 1)
plt.title("original + noise")
plt.imshow(tf.squeeze(x_test_noisy[i]))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
bx = plt.subplot(2, n, i + n + 1)
plt.title("reconstructed")
plt.imshow(tf.squeeze(decoded_imgs[i]))
plt.gray()
bx.get_xaxis().set_visible(False)
bx.get_yaxis().set_visible(False)
plt.show()
上图的第二行即为自动编码器图像去噪的效果。
3. Reference
[1] https://www.tensorflow.org/tutorials/generative/autoencoder
[2] https://www.deeplearningbook.org