There's more...

In a simple autoencoder, the decoder and encoder networks have fully connected dense layers. A convolutional autoencoder extends this underlying autoencoder architecture by replacing its dense layers with convolutional layers. Like simple autoencoders, the size of the input layer is the same as the output layers in a convolutional autoencoder. The encoder network of this autoencoder has convolutional layers, while the decoder network has transposed convolutional layers or an upsampling layer coupled with a convolutional layer.

In the following code block, we have implemented a convolutional autoencoder, where the decoder network consists of an upsampling layer combined with a convolutional layer. This approach scales up the input and then applies a convolution operation. In the Denoising autoencoders recipe, we have implemented an autoencoder with a transposed convolutional layer. 

The following code shows an implementation of a convolutional autoencoder:

x_train = x_train/ 255
x_test = x_test / 255

x_train = array_reshape(x_train, c(nrow(x_train), 28,28,1))
x_test = array_reshape(x_test, c(nrow(x_test), 28,28,1))

input_img = layer_input(shape=c(28, 28, 1))

x = input_img %>% layer_conv_2d(32, c(3, 3), activation='relu', padding='same')
x = x %>% layer_max_pooling_2d(c(2, 2), padding='same')
x = x %>% layer_conv_2d(18, c(3, 3), activation='relu', padding='same')
x = x %>%layer_max_pooling_2d(c(2, 2), padding='same')
x = x %>% layer_conv_2d(8, c(3, 3), activation='relu', padding='same')
encoded = x %>% layer_max_pooling_2d(c(2, 2), padding='same')


x = encoded %>% layer_conv_2d(8, c(3, 3), activation='relu', padding='same')
x = x %>% layer_upsampling_2d(c(2, 2))
x = x %>% layer_conv_2d(8, c(3, 3), activation='relu', padding='same')
x = x %>% layer_upsampling_2d(c(2, 2))
x = x %>% layer_conv_2d(16, c(3, 3), activation='relu')
x = x %>% layer_upsampling_2d(c(2, 2))
decoded = x %>% layer_conv_2d(1, c(3, 3), activation='sigmoid', padding='same')

autoencoder = keras_model(input_img, decoded)
summary(autoencoder)

autoencoder %>% compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder %>% fit(x_train, x_train,
epochs=20,
batch_size=128,
validation_data=list(x_test, x_test))
predicted <- autoencoder %>% predict(x_test)

Here are some sample test images reconstructed using a convolutional autoencoder:

From the preceding screenshot, we can say that our model did a great job of reconstructing the original images. 

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

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