The discriminator network

Similarly, to implement the discriminator network, we need to create a Keras model and add the neural network layers to it. The steps required to implement the discriminator network are as follows:

  1. Start by specifying the values for different hyperparameters:
dis_input_shape = (64, 64, 64, 1)
dis_filters = [64, 128, 256, 512, 1]
dis_kernel_sizes = [4, 4, 4, 4, 4]
dis_strides = [2, 2, 2, 2, 1]
dis_paddings = ['same', 'same', 'same', 'same', 'valid']
dis_alphas = [0.2, 0.2, 0.2, 0.2, 0.2]
dis_activations = ['leaky_relu', 'leaky_relu', 'leaky_relu', 'leaky_relu', 'sigmoid']
dis_convolutional_blocks = 5

  1. Next, create an input layer to allow the network to take input. The input to the discriminator network is a 3D image that has a shape of 64x64x64x1:
dis_input_layer = Input(shape=dis_input_shape)
  1. Then, add the first 3D convolution block, shown as follows:
# The first 3D Convolution block
a = Conv3D(filters=dis_filters[0],
kernel_size=dis_kernel_sizes[0],
strides=dis_strides[0],
padding=dis_paddings[0])(dis_input_layer)
a = BatchNormalization()(a, training=True)
a = LeakyReLU(alphas[0])(a)
  1. After that, add four more 3D convolution blocks, shown as follows:
# The next 4 3D Convolutional Blocks
for i in range(dis_convolutional_blocks - 1):
a = Conv3D(filters=dis_filters[i + 1],
kernel_size=dis_kernel_sizes[i + 1],
strides=dis_strides[i + 1],
padding=dis_paddings[i + 1])(a)
a = BatchNormalization()(a, training=True)
if dis_activations[i + 1] == 'leaky_relu':
a = LeakyReLU(dis_alphas[i + 1])(a)
elif dis_activations[i + 1] == 'sigmoid':
a = Activation(activation='sigmoid')(a)
  1. Next, create a Keras model and specify the inputs and the outputs for the discriminator network:
dis_model = Model(inputs=dis_input_layer, outputs=a)
  1. Wrap the complete code for the discriminator network inside a function, given as follows:
def build_discriminator():
"""
Create a Discriminator Model using hyperparameters values defined as follows
:return: Discriminator network
"""

dis_input_shape = (64, 64, 64, 1)
dis_filters = [64, 128, 256, 512, 1]
dis_kernel_sizes = [4, 4, 4, 4, 4]
dis_strides = [2, 2, 2, 2, 1]
dis_paddings = ['same', 'same', 'same', 'same', 'valid']
dis_alphas = [0.2, 0.2, 0.2, 0.2, 0.2]
dis_activations = ['leaky_relu', 'leaky_relu', 'leaky_relu',
'leaky_relu', 'sigmoid']
dis_convolutional_blocks = 5

dis_input_layer = Input(shape=dis_input_shape)

# The first 3D Convolutional block
a = Conv3D(filters=dis_filters[0],
kernel_size=dis_kernel_sizes[0],
strides=dis_strides[0],
padding=dis_paddings[0])(dis_input_layer)
a = BatchNormalization()(a, training=True)
a = LeakyReLU(dis_alphas[0])(a)

# Next 4 3D Convolutional Blocks
for i in range(dis_convolutional_blocks - 1):
a = Conv3D(filters=dis_filters[i + 1],
kernel_size=dis_kernel_sizes[i + 1],
strides=dis_strides[i + 1],
padding=dis_paddings[i + 1])(a)
a = BatchNormalization()(a, training=True)
if dis_activations[i + 1] == 'leaky_relu':
a = LeakyReLU(dis_alphas[i + 1])(a)
elif dis_activations[i + 1] == 'sigmoid':
a = Activation(activation='sigmoid')(a)

dis_model = Model(inputs=dis_input_layer, outputs=a)
print(dis_model.summary())
return dis_model

In this section, we created a Keras model for the discriminator network. We are now ready to train a 3D-GAN.

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

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