The generator network

We have already explored the architecture of the generator network earlier in this chapter in the The architecture of the generator network section. Let's start by writing the layers for the generator 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 generator network in Keras:

  1. Start by defining the hyperparameters required for the generator network, as follows:
input_shape = (128, 128, 3)
residual_blocks = 6
  1. Next, create an input layer to feed the input to the network, as follows:
input_layer = Input(shape=input_shape)
  1. Add the first convolution block with the hyperparameters specified previously in the The architecture of the generator network section, as follows:
x = Conv2D(filters=32, kernel_size=7, strides=1, padding="same")(input_layer)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)
  1. Add the second convolution block, as follows:
x = Conv2D(filters=64, kernel_size=3, strides=2, padding="same")(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)
  1. Add the third convolution block, as follows:
x = Conv2D(filters=128, kernel_size=3, strides=2, padding="same")(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)
  1. Define a residual block, as follows:
def residual_block(x):
"""
Residual block
"""
res = Conv2D(filters=128, kernel_size=3, strides=1, padding="same")(x)
res = BatchNormalization(axis=3, momentum=0.9, epsilon=1e-5)(res)
res = Activation('relu')(res)

res = Conv2D(filters=128, kernel_size=3, strides=1, padding="same")(res)
res = BatchNormalization(axis=3, momentum=0.9, epsilon=1e-5)(res)

return Add()([res, x])

Now, use the residual_block() function to add six residual blocks to the model, as shown in the following example:

for _ in range(residual_blocks):
x = residual_block(x)
  1. Next, add an upsampling block, as follows:
x = Conv2DTranspose(filters=64, kernel_size=3, strides=2, padding='same', use_bias=False)(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)
  1. Add another upsampling block, as follows:
x = Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding='same', use_bias=False)(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)
  1. Finally, add the output convolution layer, as follows:
x = Conv2D(filters=3, kernel_size=7, strides=1, padding="same")(x)
output = Activation('tanh')(x)

This is the last layer of the generator network. It generates an image with a shape of (128, 128, 3).

  1. Now, create a Keras model by specifying the inputs and outputs for the network, as follows:
model = Model(inputs=[input_layer], outputs=[output])

The entire code for the generator network appears as follows:

def build_generator():
"""
Create a generator network using the hyperparameter values defined below
"""
input_shape = (128, 128, 3)
residual_blocks = 6
input_layer = Input(shape=input_shape)

# First Convolution block
x = Conv2D(filters=32, kernel_size=7, strides=1, padding="same")(input_layer)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)

# 2nd Convolution block
x = Conv2D(filters=64, kernel_size=3, strides=2, padding="same")(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)

# 3rd Convolution block
x = Conv2D(filters=128, kernel_size=3, strides=2, padding="same")(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)

# Residual blocks
for _ in range(residual_blocks):
x = residual_block(x)

# Upsampling blocks

# 1st Upsampling block
x = Conv2DTranspose(filters=64, kernel_size=3, strides=2, padding='same', use_bias=False)(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)

# 2nd Upsampling block
x = Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding='same', use_bias=False)(x)
x = InstanceNormalization(axis=1)(x)
x = Activation("relu")(x)

# Last Convolution layer
x = Conv2D(filters=3, kernel_size=7, strides=1, padding="same")(x)
output = Activation('tanh')(x)

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

We have successfully created a Keras model for the generator network. In the next section, we will create a Keras model for the discriminator network.

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

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