keras实战-入门之去噪卷积自编码器

keras实战-入门之去噪卷积自编码器

  • 去噪卷积自编码器

去噪卷积自编码器

只是在图片预处理时加了噪声,其他没什么改变,若要提升效果,可以修改CNN的一些参数试试。可以看到自编码器还有去噪的功能,应该归功于特征提取,转化为特征向量后,已经无噪声,然后复原到原来的数字,我想应该是这个原理吧。

import numpy as np
np.random.seed(11)
from keras.datasets import mnist
import matplotlib.pyplot as plt
Using TensorFlow backend.
(x_train_image,y_train_label),(x_test_image,y_test_label)=mnist.load_data()
plt.imshow(x_test_image[0],cmap='binary')

keras实战-入门之去噪卷积自编码器_第1张图片

print('x_train_image',len(x_train_image))
print('x_test_image',len(x_test_image))
x_train_image 60000
x_test_image 10000
print('y_train_label',x_train_image.shape)
print('y_train_label',y_train_label.shape)
y_train_label (60000, 28, 28)
y_train_label (60000,)
x_train4D=x_train_image.reshape(x_train_image.shape[0],28,28,1).astype('float32')
x_test4D=x_test_image.reshape(x_test_image.shape[0],28,28,1).astype('float32')
print(x_train4D.shape)
print(x_test4D.shape)
(60000, 28, 28, 1)
(10000, 28, 28, 1)
x_train_normalize=x_train4D/255
x_test_normalize=x_test4D/255
#噪声影响程度,太大就全是噪声了
factor=0.3
x_train_noisy = x_train_normalize +  factor*np.random.normal(0, 1,x_train_normalize.shape) 
x_test_noisy = x_test_normalize + factor*np.random.normal(0,1, x_test_normalize.shape) 

x_train_noisy = np.clip(x_train_noisy, 0,1)
x_test_noisy = np.clip(x_test_noisy,0,1)
def show_noisy_images(start=0,end=5):
    plt.figure(figsize=(20, 4))
    for i in range(start,end):
        ax = plt.subplot(2,end, i+1)
        plt.imshow(x_test_noisy[i].reshape(28, 28),cmap='binary')
    plt.show()
show_noisy_images(0,10)

在这里插入图片描述

from keras import Model
from keras.layers import Dense,Input,MaxPooling2D,Conv2D,UpSampling2D
from keras.callbacks import TensorBoard
#函数式要定义输入
input_image=Input((28,28,1))
#编码器 2个简单的卷积,2个最大池化,缩小为原来的1/4了
encoder=Conv2D(16,(3,3),padding='same',activation='relu')(input_image)
encoder=MaxPooling2D((2,2))(encoder)
encoder=Conv2D(8,(3,3),padding='same',activation='relu')(encoder)
encoder_out=MaxPooling2D((2,2))(encoder)
                                                                 
# 构建编码模型,可以提取特征图片,展开了就是特征向量
encoder_model = Model(inputs=input_image, outputs=encoder_out)

#解码器,反过来                                                             
decoder=UpSampling2D((2,2))(encoder_out)                                                                 
decoder=Conv2D(8,(3,3),padding='same',activation='relu')(decoder)                                               
decoder=UpSampling2D((2,2))(decoder)
decoder=Conv2D(16,(3,3),padding='same',activation='relu')(decoder)
#转成原始图片尺寸
decoder_out=Conv2D(1, (3, 3), padding='same',activation='sigmoid')(decoder)
                                                                
autoencoder=Model(input_image,decoder_out)
autoencoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 28, 28, 16)        160       
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 14, 14, 16)        0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 14, 14, 8)         1160      
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 7, 7, 8)           0         
_________________________________________________________________
up_sampling2d_5 (UpSampling2 (None, 14, 14, 8)         0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 14, 14, 8)         584       
_________________________________________________________________
up_sampling2d_6 (UpSampling2 (None, 28, 28, 8)         0         
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 28, 28, 16)        1168      
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 28, 28, 1)         145       
=================================================================
Total params: 3,217
Trainable params: 3,217
Non-trainable params: 0
_________________________________________________________________
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

autoencoder.fit(x_train_noisy, x_train_normalize, epochs=20, batch_size=256, shuffle=True,verbose=1)
Epoch 1/20
60000/60000 [==============================] - 5s 81us/step - loss: 0.2156
Epoch 2/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.1075
Epoch 3/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0999
Epoch 4/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0957
Epoch 5/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0935
Epoch 6/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0923
Epoch 7/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0913
Epoch 8/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0906
Epoch 9/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0899
Epoch 10/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0893
Epoch 11/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0889
Epoch 12/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0884
Epoch 13/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0881
Epoch 14/20
60000/60000 [==============================] - 4s 69us/step - loss: 0.0877
Epoch 15/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0875
Epoch 16/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0872
Epoch 17/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0869
Epoch 18/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0868
Epoch 19/20
60000/60000 [==============================] - 4s 69us/step - loss: 0.0865
Epoch 20/20
60000/60000 [==============================] - 4s 68us/step - loss: 0.0863






decoded_imgs = autoencoder.predict(x_test_noisy)
print(decoded_imgs.shape)
(10000, 28, 28, 1)
#显示编码器生成的图和真实的图
def show_images(index):
    image=decoded_imgs[index,:]
    image=image.reshape(28,28)
    plt.figure(figsize=(10,10))
    plt.subplot(1,2,1)
    plt.title('encoder')
    plt.imshow(image,cmap='binary')
    plt.subplot(1,2,2)
    plt.title('noisy')
    plt.imshow(x_test_noisy[index].reshape(28, 28),cmap='binary')
    plt.show()
show_images(0)

keras实战-入门之去噪卷积自编码器_第2张图片

show_images(1)

keras实战-入门之去噪卷积自编码器_第3张图片

def show_num_images(start=0,end=5):
    plt.figure(figsize=(20, 4))
    for i in range(start,end):
        ax = plt.subplot(2,end, i+1)
        plt.imshow(x_test_noisy[i].reshape(28, 28),cmap='binary')

        ax = plt.subplot(2, end, i+1 + end)
        plt.imshow(decoded_imgs[i].reshape(28, 28),cmap='binary')
    plt.show()
show_num_images(0,10)

keras实战-入门之去噪卷积自编码器_第4张图片

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵,侵删。

你可能感兴趣的:(keras实战,深度学习)