The discriminator network

We have already explored the architecture of the discriminator network in the The architecture of the discriminator network section. Let's start by writing the layers for the discriminator network in the Keras framework and then create a Keras model, using the functional API of the Keras framework.

Perform the following steps to implement the discriminator network in Keras:

  1. Start by defining the hyperparameters required for the discriminator network, as follows:
input_shape = (128, 128, 3)
hidden_layers = 3
  1. Next, add an input layer to feed the input to the network, as follows:
input_layer = Input(shape=input_shape)
  1. Next, add a 2D zero padding layer, as follows:
x = ZeroPadding2D(padding=(1, 1))(input_layer)

This layer will add padding to the input tensor on both the x and y axes.

  1. Next, add a convolution block using the hyperparameters specified previously in the The architecture of the discriminator network section, as follows:
x = Conv2D(filters=64, kernel_size=4, strides=2, padding="valid")(x)
x = LeakyReLU(alpha=0.2)(x)
  1. Next, add another 2D zero padding layer, as follows:
x = ZeroPadding2D(padding=(1, 1))(x)
  1. Next, add three convolution blocks using the hyperparameters specified previously in the The architecture of the discriminator network section, as follows:
for i in range(1, hidden_layers + 1):
x = Conv2D(filters=2 ** i * 64, kernel_size=4, strides=2, padding="valid")(x)
x = InstanceNormalization(axis=1)(x)
x = LeakyReLU(alpha=0.2)(x)
x = ZeroPadding2D(padding=(1, 1))(x)

Each convolution block has two convolution layers, an instance normalization layer, an activation layer, and a 2D zero padding layer.

  1. Now, add the final (output) convolution layer to the network, as follows:
output = Conv2D(filters=1, kernel_size=4, strides=1, activation="sigmoid")(x)
  1. Finally, create a Keras model by specifying inputs and outputs for the network, as follows:
model = Model(inputs=[input_layer], outputs=[output])

The entire code for the discriminator network appears as follows:

def build_discriminator():
"""
Create a discriminator network using the hyperparameter values defined below
"""
input_shape = (128, 128, 3)
hidden_layers = 3

input_layer = Input(shape=input_shape)

x = ZeroPadding2D(padding=(1, 1))(input_layer)

# 1st Convolutional block
x = Conv2D(filters=64, kernel_size=4, strides=2, padding="valid")(x)
x = LeakyReLU(alpha=0.2)(x)

x = ZeroPadding2D(padding=(1, 1))(x)

# 3 Hidden Convolution blocks
for i in range(1, hidden_layers + 1):
x = Conv2D(filters=2 ** i * 64, kernel_size=4, strides=2, padding="valid")(x)
x = InstanceNormalization(axis=1)(x)
x = LeakyReLU(alpha=0.2)(x)

x = ZeroPadding2D(padding=(1, 1))(x)

# Last Convolution layer
output = Conv2D(filters=1, kernel_size=4, strides=1, activation="sigmoid")(x)

model = Model(inputs=[input_layer], outputs=[output])
return model

We have successfully created a Keras model for the discriminator network too. In the next section, we'll train the network.

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

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