Advanced Deep Learning with Keras

fourpersent2020
from fourpersent2020 More from this publisher
16.03.2021 Views

Chapter 3# Mean Square Error (MSE) loss function, Adam optimizerautoencoder.compile(loss='mse', optimizer='adam')# train the autoencoderautoencoder.fit(x_train_noisy,x_train,validation_data=(x_test_noisy, x_test),epochs=10,batch_size=batch_size)# predict the autoencoder output from corrupted test imagesx_decoded = autoencoder.predict(x_test_noisy)# 3 sets of images with 9 MNIST digits# 1st rows - original images# 2nd rows - images corrupted by noise# 3rd rows - denoised imagesrows, cols = 3, 9num = rows * colsimgs = np.concatenate([x_test[:num], x_test_noisy[:num], x_decoded[: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('Original images: top rows, ''Corrupted Input: middle rows, ''Denoised Input: third rows')plt.imshow(imgs, interpolation='none', cmap='gray')Image.fromarray(imgs).save('corrupted_and_denoised.png')plt.show()Automatic colorization autoencoderWe're now going to work on another practical application of autoencoders. In thiscase, we're going to imagine that we have a grayscale photo and that we want tobuild a tool that will automatically add color to them. We would like to replicate thehuman abilities in identifying that the sea and sky are blue, the grass field and treesare green, while clouds are white, and so on.[ 89 ]

AutoencodersAs shown in Figure 3.4.1, if we are given a grayscale photo of a rice field on theforeground, a volcano in the background and sky on top, we're able to add theappropriate colors.Figure 3.4.1: Adding color to a grayscale photo of the Mayon Volcano. A colorization network should replicatehuman abilities by adding color to a grayscale photo. Left photo is grayscale. The right photo is color. Originalcolor photo can be found on the book GitHub repository, https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter3-autoencoders/README.md.A simple automatic colorization algorithm seems like a suitable problem forautoencoders. If we can train the autoencoder with a sufficient number of grayscalephotos as input and the corresponding colored photos as output, it could possiblydiscover the hidden structure on properly applying colors. Roughly, it is the reverseprocess of denoising. The question is, can an autoencoder add color (good noise)to the original grayscale image?Listing 3.4.1 shows the colorization autoencoder network. The colorizationautoencoder network is a modified version of denoising autoencoder that weused for the MNIST dataset. Firstly, we need a dataset of grayscale to coloredphotos. The CIFAR10 database, which we have used before, has 50,000 trainingand 10,000 testing 32 × 32 RGB photos that can be converted to grayscale. As shownin the following listing, we're able to use the rgb2gray() function to apply weightson R, G, and B components to convert from color to grayscale.Listing 3.4.1, colorization-autoencoder-cifar10-3.4.1.py, shows usa colorization autoencoder using the CIFAR10 dataset:from keras.layers import Dense, Inputfrom keras.layers import Conv2D, Flatten[ 90 ]

Chapter 3

# Mean Square Error (MSE) loss function, Adam optimizer

autoencoder.compile(loss='mse', optimizer='adam')

# train the autoencoder

autoencoder.fit(x_train_noisy,

x_train,

validation_data=(x_test_noisy, x_test),

epochs=10,

batch_size=batch_size)

# predict the autoencoder output from corrupted test images

x_decoded = autoencoder.predict(x_test_noisy)

# 3 sets of images with 9 MNIST digits

# 1st rows - original images

# 2nd rows - images corrupted by noise

# 3rd rows - denoised images

rows, cols = 3, 9

num = rows * cols

imgs = np.concatenate([x_test[:num], x_test_noisy[:num], x_

decoded[: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('Original images: top rows, '

'Corrupted Input: middle rows, '

'Denoised Input: third rows')

plt.imshow(imgs, interpolation='none', cmap='gray')

Image.fromarray(imgs).save('corrupted_and_denoised.png')

plt.show()

Automatic colorization autoencoder

We're now going to work on another practical application of autoencoders. In this

case, we're going to imagine that we have a grayscale photo and that we want to

build a tool that will automatically add color to them. We would like to replicate the

human abilities in identifying that the sea and sky are blue, the grass field and trees

are green, while clouds are white, and so on.

[ 89 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!