自编码器(AutoEncoder)是深度学习中的一类无监督学习模型,由 encoder 和 decoder 两部分组成。
• encoder 将原始表示编码成隐层表示;
• decoder 将隐层表示解码成原始表示;
• 训练目标为最小化重构误差;
• 隐层特征维度一般低于原始特征维度,降维的同时学习更稠密更有意 义的表示。
自编码器主要是一种思想,encoder 和 decoder 可以由全连接层、CNN 或 RNN 等模型实现。
以下使用 Keras,用 CNN 实现自编码器,通过学习从加噪图片到原始 图片的映射,完成图像去噪任务。
用到的数据是 MNIST,手写数字识别数据集,Keras 中自带。 训练集 5W 条,测试集 1W 条,都是 28 × 28 的灰度图。实现如下:
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model, load_model
def get_data():
data = np.load('mnist.npz')
x_train, y_train = data['x_train'], data['y_train']
x_test, y_test = data['x_test'], data['y_test']
# 处理成0-1之间的值
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
# 重新构造一个N × 1 × 28 × 28 的四维tensor
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
return x_train,x_test
def add_noise(x_train,x_test):
"""随机添加噪音"""
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)
# 值仍在0-1之间
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
return x_train_noisy,x_test_noisy
def remove_noisy_model(x_train_noisy,x_test_noisy):
"""去燥"""
input_img = Input(shape=(28, 28, 1,)) # N * 28 * 28 * 1
# 实现 encoder 部分,由两个 3 × 3 × 32 的卷积和两个 2 × 2 的最大池化组 成。
x = Conv2D(32, (3, 3), padding='same', activation='relu')(input_img) # 28 * 28 * 32
x = MaxPooling2D((2, 2), padding='same')(x) # 14 * 14 * 32
x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) # 14 * 14 * 32
encoded = MaxPooling2D((2, 2), padding='same')(x) # 7 * 7 * 32
# 实现 decoder 部分,由两个 3 × 3 × 32 的卷积和两个 2 × 2 的上采样组成。
# 7 * 7 * 32
x = Conv2D(32, (3, 3), padding='same', activation='relu')(encoded) # 7 * 7 * 32
x = UpSampling2D((2, 2))(x) # 14 * 14 * 32
x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) # 14 * 14 * 32
x = UpSampling2D((2, 2))(x) # 28 * 28 * 32
decoded = Conv2D(1, (3, 3), padding='same', activation='sigmoid')(x) # 28 * 28 *
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(x_train_noisy, x_train,
epochs=100,
batch_size=128,
shuffle=True,
validation_data=(x_test_noisy, x_test))
autoencoder.save('autoencoder.h5')
def remove_noisy(x_test_noisy):
autoencoder = load_model('autoencoder.h5')
decoded_imgs = autoencoder.predict(x_test_noisy)
return decoded_imgs
def plot1(x_data):
"""画图"""
n = 10
plt.figure(figsize=(20, 2))
for i in range(n):
ax = plt.subplot(1, n, i + 1)
plt.imshow(x_data[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
def plot2(x_test_noisy,decoded_imgs):
"""画图"""
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
# display original
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test_noisy[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 + 1 + 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()
x_train,x_test =get_data()
x_train_noisy, x_test_noisy = add_noise(x_train,x_test)
decoded_imgs = remove_noisy(x_test_noisy)
plot2(x_test_noisy,decoded_imgs)
最后结果展示:
https://github.com/littlemesie/AI-project/tree/master/image_denoise
————————————————
版权声明:本文为CSDN博主「mesie美希」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baidu_28610773/article/details/84666918