Discriminator

As mentioned in the Architecture of DCGAN section, the discriminator network has three 2D convolutional layers, each followed by an activation function followed by two max pooling layers. The tail of the network contains two fully-connected (dense) layers that work as a classification layer. Before anything else, let's have a look at the different layers in the discriminator network:

  • All convolutional layers have LeakyReLU as the activation function with an alpha value of 0.2
  • The convolutional layers have 128, 256, and 512 filters, respectively. Their kernel sizes are (5, 5), (3, 3), and (3, 3), respectively.
  • After the convolutional layers, we have a flatten layer, which flattens the input to a one-dimensional tensor.
  • Following this, the network has two dense layers with 1,024 neurons and one neuron, respectively.
  • The first dense layer has LeakyReLU as the activation function, while the second layer has sigmoid as the activation function. Sigmoid activation is used for binary classification. We are training the discriminator network to classify between real or fake images.

Perform the following steps to create a discriminator network:

  1. Let's start by creating a Sequential Keras model:
dis_model = Sequential()
  1. Add a 2D convolutional layer that takes an input image of a shape of (64, 64, 3). The hyperparameters for this layer are the following. Also, add LeakyReLU with an alpha value of 0.2 as the activation function:
    • Filters: 128
    • Kernel Size: (5, 5)
    • Padding: Same:
dis_model.add(Conv2D(filters=128, kernel_size=5, padding='same', 
input_shape=(64, 64, 3)))
dis_model.add(LeakyReLU(alpha=0.2))
  1. Next, add a 2D max pooling layer with a pool size of (2, 2). Max pooling is used to downsample an image representation and it is applied by using a max-filter over non-overlapping sub-regions of the representation:
dis_model.add(MaxPooling2D(pool_size=(2, 2)))

The shape of the output tensor from the first layer will be (batch_size, 32, 32, 128).

  1. Next, add another 2D convolutional layer with the following configurations:
    • Filters: 256
    • Kernel size: (3, 3)
    • Activation function: LeakyReLU with alpha 0.2
    • Pool size in 2D max pooling: (2, 2):
dis_model.add(Conv2D(filters=256, kernel_size=3))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))

The shape of the output tensor from this layer will be (batch_size, 30, 30, 256).

  1. Next, add the third 2D convolutional layer with the following configurations:
    • Filters: 512
    • Kernel size: (3, 3)
    • Activation function: LeakyReLU with alpha 0.2
    • Pool size in 2D Max Pooling: (2, 2):
dis_model.add(Conv2D(512, (3, 3)))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))

The shape of the output tensor from this layer will be (batch_size, 13, 13, 512).

  1. Next, add a flatten layer. This flattens the input without affecting the batch size. It produces a two-dimensional tensor:
dis_model.add(Flatten())

The output shape of the tensor from the flattened layer will be (batch_size, 18432,).

  1. Next, add a dense layer with 1024 neurons and LeakyReLU with alpha 0.2 as the activation function:
dis_model.add(Dense(1024))
dis_model.add(LeakyReLU(alpha=0.2))
  1. Finally, add a dense layer with one neuron for binary classification. The sigmoid function is the best for binary classification, as it gives the probability of the classes:
dis_model.add(Dense(1))
dis_model.add(Activation('tanh'))

The network will generate an output tensor of a shape of (batch_size, 1). The output tensor contains the probability of the classes. 

The complete code for the discriminator network wrapped inside a Python method looks as follows:

def get_discriminator():
dis_model = Sequential()
dis_model.add(
Conv2D(128, (5, 5),
padding='same',
input_shape=(64, 64, 3))
)
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))

dis_model.add(Conv2D(256, (3, 3)))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))

dis_model.add(Conv2D(512, (3, 3)))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))

dis_model.add(Flatten())
dis_model.add(Dense(1024))
dis_model.add(LeakyReLU(alpha=0.2))

dis_model.add(Dense(1))
dis_model.add(Activation('sigmoid'))

return dis_model

In this section, we have successfully implemented the discriminator and generator networks. In the next section, we will train the model on the dataset that we prepared in the Downloading and preparing the anime characters dataset section.

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

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