Defining the Generator

The generator that we are using here is a simple convolution autoencoder which is a combination of two parts, an encoder, and a decoder. 

In the encoder

  • The 1st layer is a Convolution 2D layer with 32 filters of size 3*3, followed by batch normalization, with activation as relu, followed by downsampling done with AveragePooling2D of size 2*2. 
  • The 2nd layer is a Convolution 2D layer with 64 filters of size 3*3, followed by batch normalization, with activation as relu, followed by downsampling with AveragePooling2D of size 2*2.
  • The 3rd layer or the final layer in this encoder part is again a Convolution 2D layer with 128 filters of size 3*3, batch normalization, with activation as relu.

In the decoder

  • The 1st layer is a Convolution 2D layer with 128 filters of size 3*3 with activation as relu, followed by upsampling done with UpSampling2D. 
  • The 2nd layer is a Convolution 2D layer with 64 filters of size 3*3 with activation as relu, followed by upsampling with UpSampling2D.
  • The 3rd layer or the final layer in this decoder part is again a Convolution 2D layer with 1 filters of size 3*3 with activation as tanh.

Remember, in the encoder if you have 32, 64, 128 filters, it should be followed by 128, 64, image_channels filters in the decoder. The image_channel is the number of channels in the input image which is one in the MNIST dataset. If you have 64, 128, 256, 512 filters in the 1st, 2nd, 3rd and 4th layer of the encoder, the following filters in the decoder should be 256,128,64, image_channels.

 

def img_generator(input_shape):
generator = Sequential()
generator.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape)) # 32 filters
generator.add(BatchNormalization())
generator.add(Activation('relu'))
generator.add(AveragePooling2D(pool_size=(2, 2)))

generator.add(Conv2D(64, (3, 3), padding='same')) # 64 filters
generator.add(BatchNormalization())
generator.add(Activation('relu'))
generator.add(AveragePooling2D(pool_size=(2, 2)))

generator.add(Conv2D(128, (3, 3), padding='same')) # 128 filters
generator.add(BatchNormalization())
generator.add(Activation('relu'))

generator.add(Conv2D(128, (3, 3), padding='same')) # 128 filters
generator.add(Activation('relu'))
generator.add(UpSampling2D((2,2)))

generator.add(Conv2D(64, (3, 3), padding='same')) # 64 filters
generator.add(Activation('relu'))
generator.add(UpSampling2D((2,2)))

generator.add(Conv2D(1, (3, 3), activation='tanh', padding='same')) # 1 filter
return generator

Two important things to remember here about the final convolution layer in the generator. One is, to use 'tanh' as the activation function since the dataset range is between -1 and 1, and the other is, to use the same number of filter/s as the number of channels in the input image. This is to make sure that the image being generated has the same number of channels as the input image.

If you decide to center and scale your data like we have done in this exercise, you need to use batch normalization in the generator during downsampling. Else, the loss will not converge. You can witness the effects of not using the batch normalization by training the generator without the batch normalization layer.

In the summary of the generator below, if you refer to the output shape,  you see the downscaling or compression of the image in the 1st half of the network and the upscaling of the images in the 2nd half of the network.

# print generator summary
img_generator(input_shape).summary()
Figure 14.7: Summary of the Generator (Autoencoder)
Consider the following when you are not obtaining good results with the autoencoder. Use AveragePooling2D first and then check out MaxPooling2D for downsampling. Use LeakyRelu first and then Relu next. For all the convolution layers except the final one use either LeakyRelu or Relu activation. Try using a deeper autoencoder. Feel free to use more filters in the convolution layers, play with the filter sizes and the pooling sizes. 
..................Content has been hidden....................

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