In [1]:
from keras.datasets import mnist
import numpy as np
import keras
import sys
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.info(str("本次运行使用的包版本为:keras.__version__" + keras.__version__ + "np.__version__" + np.__version__ + "Python 版本为" + sys.version))
In [2]:
logging.info('# 加载MNIST数据,不需要对应的标签,将像素值归一化到0至1,即张量,1表示颜色通道,即灰度图')
(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))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
In [3]:
logging.info('# 添加随机白噪声,并限制加噪后像素值仍处于0至1之间')
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.)
In [4]:
logging.info('# 看一下加噪后的效果')
import matplotlib.pyplot as plt
%matplotlib inline
n = 20
plt.figure(figsize=(20, 2))
for i in range(n):
ax = plt.subplot(1, n, i + 1)
plt.imshow(x_test_noisy[i*5+17].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
In [5]:
logging.info('# 定义模型的输入')
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model, load_model
input_img = Input(shape=(28, 28, 1,))
logging.info('# 实现encoder部分,由两个 3 X 3 X 32 的卷积和两个 2 X 2 的最大池化组成')
x = Conv2D(32, (3, 3), padding='same', activation='relu')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), padding='same', activation='relu')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
logging.info('# 实现decoder部分,由两个 3 X 3 X 32 的卷积和两个 2 X 2 的上采样组成')
# 7 * 7 * 32
x = Conv2D(32, (3, 3), padding='same', activation='relu')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), padding='same', activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), padding='same', activation='sigmoid')(x)
logging.info('# 将输入和输出连接,构成自编码器并compile')
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
logging.info('# 使用x_train作为输入和输出进行训练,使用x_test进行校验')
logging.info('# 先训练5次看效果')
autoencoder.fit(x_train_noisy, x_train,
epochs=5,
batch_size=512,
shuffle=True,
validation_data=(x_test_noisy, x_test))
decoded_imgs = autoencoder.predict(x_test_noisy)
n = 20
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*5+17].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*5+17].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
In [6]:
autoencoder.save('autoencoder.h5')
logging.info('''
# autoencoder.save('autoencoder.h5')
# 在CPU上训练比较慢,有条件的话可以用GPU,速度快上几十倍
# 这里将训练后的模型保存下来,之后或在其他地方都可以直接加载使用
# 使用自编码器对x_test_noisy预测,绘制预测结果,和原始加噪图像进行对比,便可以得到一开始的对比效果图
# autoencoder = load_model('autoencoder.h5')
''')
autoencoder = load_model('autoencoder.h5')
In [7]:
logging.info('# 再训练20次看效果')
autoencoder.fit(x_train_noisy, x_train,
epochs=20,
batch_size=512,
shuffle=True,
validation_data=(x_test_noisy, x_test))
decoded_imgs = autoencoder.predict(x_test_noisy)
n = 20
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*5+17].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*5+17].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()