Denoising images using DAE

In this section, we will learn how to denoise the images using DAE. We use CAE for denoising the images. The code for DAE is just the same as CAE, except that here, we use noisy images in the input. Instead of looking at the whole code, we will see only the respective changes. The complete code is available on GitHub at https://github.com/PacktPublishing/Hands-On-Deep-Learning-Algorithms-with-Python.

Set the noise factor:

noise_factor = 1

Add noise to the train and test images:

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)

Clip the train and test set by 0 and 1:

x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)

Let's train the model. Since, we want the model to learn to remove noise in the image, input to the model is the noisy images, that is, x_train_noisy and output is the denoised images, that is, x_train:

model.fit(x_train_noisy, x_train, epochs=50,batch_size=128, shuffle=True, validation_data=(x_test_noisy, x_test))

Reconstruct images using our trained model:

reconstructed_images = model.predict(x_test_noisy)

First, let us plot the input image which is the corrupted image:

n = 7
plt.figure(figsize=(20, 4))
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()

The plot of the input noisy image is shown as follows:

Now, let us plot the reconstructed images by the model:

n = 7
plt.figure(figsize=(20, 4))
for i in range(n):
ax = plt.subplot(2, n, i + n + 1)
plt.imshow(reconstructed_images[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

plt.show()

As you can see, our model has learned to remove noise from the image:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset