吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:去噪型编码网络

#为图像像素点增加高斯噪音
noise = np.random.normal(loc=0.5, scale = 0.5, size = x_train.shape)
x_train_noisy = x_train + noise
noise = np.random.normal(loc=0.5, scale = 0.5, size = x_test.shape)
x_test_noisy = x_test + noise
#添加噪音值后,像素点值可能会超过1或小于0,我们把这些值调整到[0,1]之间
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
autoencoder = Model(inputs, decoder(encoder(inputs)), name = 'autoencoder')
autoencoder.compile(loss='mse', optimizer='adam')
autoencoder.fit(x_train_noisy, x_train, validation_data = (x_test_noisy, x_test),
                                                          epochs = 10,
                                                          batch_size = batch_size)

吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:去噪型编码网络

#获取去噪后的图片
x_decode = autoencoder.predict(x_test_noisy)
'''
将去噪前和去噪后的图片显示出来,第一行是原图片,第二行时增加噪音后的图片,
第三行时去除噪音后的图片
'''
rows , cols = 3, 9
num = rows * cols
imgs = np.concatenate([x_test[:num], x_test_noisy[:num], x_decode[:num]])
imgs = imgs.reshape((rows * 3, cols, image_size, image_size))
imgs = np.vstack(np.split(imgs, rows, axis = 1))
imgs = imgs.reshape((rows * 3, -1, image_size, image_size))
imgs = np.vstack([np.hstack(i) for i in imgs])
imgs = (imgs * 255).astype(np.uint8)
plt.figure()
plt.axis('off')
plt.title('first row: original image , middle row: noised image, third row: denoised image')
plt.imshow(imgs, interpolation='none', cmap='gray')
plt.show()

吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:去噪型编码网络

 

上一篇:PhotoSwipe手机端图片弹出显示插件【使用笔记】


下一篇:面向对象实现无缝轮播