In deep learning, autoencoder is a very useful unsupervised learning model. Autoencoder is composed of encoder and decoder. The former encoder encodes the original representation into the hidden representation, while the latter decoder decodes the hidden representation into the original representation. The training objective is to minimize reconstruction error.

Autoencoder is just an idea. In the specific implementation, encoder and decoder can be composed of a variety of deep learning models, such as full connection layer, convolution layer or LSTM, etc. Keras is used to realize the convolution autoencoder for image denoising.

1 the results

Let’s take a look at the final result, using a handwritten digital MNIST dataset, with the noised image in the top line and the noised image in the bottom line.

Denoising effect. PNG

2 code

Keras is used to implement autoencoder, encoder and decoder are used to implement CNN.

Load Keras and Numpy.

from keras.datasets import mnist
import numpy as npCopy the code

The MNIST data set was obtained, and the pixel values were converted to 0-1 interval, and reconstructed into a four-dimensional tensor of N×1×28×28.

(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), 1.28.28))
x_test = np.reshape(x_test, (len(x_test), 1.28.28))Copy the code

Add noise, that is, add a random white Gaussian noise, and limit the value after adding noise is still in the range of 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.)Copy the code

Let’s see what happens when we add noise.

import matplotlib.pyplot as plt
n = 10
plt.figure(figsize=(20.2))
for i in range(n):
    ax = plt.subplot(1, 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)
plt.show()Copy the code

It looks something like this.

PNG after adding noise

Define the inputs to the model.

input_img = Input(shape=(1.28.28))Copy the code

The encoder part is defined, which consists of two 32×3×3 convolution layers and two 2×2 maximum pooling layers.

x = Convolution2D(32.3.3, activation='relu', border_mode='same')(input_img)
x = MaxPooling2D((2.2), border_mode='same')(x)
x = Convolution2D(32.3.3, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2.2), border_mode='same')(x)Copy the code

Define the decoder section, which consists of two 32×3×3 convolution layers and two 2×2 upsampling layers.

# at this point the representation is (32, 7, 7)
x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)Copy the code

Connect the input and output to form autoencoder and compile.

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')Copy the code

Train our Autoencoder using X_train as input and output, and validation using X_test.

autoencoder.fit(x_train_noisy, x_train, nb_epoch=100, batch_size=128, shuffle=True, validation_data=(x_test_noisy, x_test))Copy the code

Autoencoder is used to predict X_test, and the prediction results are drawn and compared with the original noised image.

decoded_imgs = autoencoder.predict(x_test_noisy)

import matplotlib.pyplot as plt

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()Copy the code

This generates the result you saw at the beginning. The full code is here.

3 Other Contents

In addition to the above convolutional autoencoder for denoising, there are several other codes:

  • 1 _simplest_possible_autoencoder: Only one layer of Dense was used as encoder and decoder respectively to constitute the autoencoder, and MNIST data set was trained and reconstructed. After 50 epoch, loss was 0.1068 and VAL_loss was 0.1051.
  • 2_DEEP_autoencoder: Encoder and decoder increased from Dense layer 1 to Dense layer 3. After 100 epoch, loss was 0.0974 and Val_loss was 0.0966.
  • 3_convolutional_deep_autoencoder: Encoder and decoder are respectively implemented by CNN. After 100 epoch, loss is 0.0958 and val_loss is 0.0946.
  • 4_denoising_autoencoder: denoising_autoencoder, the denoising_autoencoder discussed in detail above;
  • 5_variational_autoencoder (VAE) : Implicit layer adds additional constraints, i.e. the training objective is to minimize reconstruction errors, and the cross entropy (KL Divergence) of the implicit and original representation distributions.

VAE in the last code maps MNIST original image data to a two-dimensional hidden layer. The following is the clustering result in the representation of the hidden layer. It can be seen that in the representation space of the hidden layer, images corresponding to the same number are aggregated together.

Hidden layer spatial clustering. PNG

If the points in the hidden layer are decoded to the original image representation space, corresponding numbers will appear in each cluster center, and “new numbers” will appear in the position between classes, that is, the transitional form between different numbers.

Transition state number. PNG

If you think the article is good, you might as well click on the lower left like ~