There are two crucial methods in this class: the block and model methods. The following steps will show you how to create those methods:
- Define a method called block that takes in a layer input, filter size, and kernel size:
def block(self,first_layer,filter_size=512,kernel_size=(3,3,3)):
x = Conv3D(filters=filter_size, kernel_size=kernel_size,
kernel_initializer='glorot_normal',
bias_initializer='zeros', padding='same')(first_layer)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
return x
This block represents a common building block that we'll use throughout the discriminator model method.
- Let's define a model method with the input as the internal class variable, INPUT_SHAPE:
def model(self):
input_layer = Input(shape=self.INPUT_SHAPE)
- Using the model method we defined, create a few blocks with multiple filter sizes:
x = self.block(input_layer,filter_size=8)
x = self.block(x,filter_size=16,)
x = self.block(x,filter_size=32)
x = self.block(x,filter_size=64)
- As with the Generator class, the last block has a few changes, so we define it explicitly:
x = Conv3D(filters=1, kernel_size=(3,3,3),
strides=(1,1,1), kernel_initializer='glorot_normal',
bias_initializer='zeros', padding='valid')(x)
x = BatchNormalization()(x)
x = Flatten()(x)
- At the end of the method, we define our singular output and define the model to return:
output_layer = Dense(1, activation='sigmoid')(x)
model = Model(inputs=input_layer, outputs=output_layer)
return model
- In the final step, define the summary helper function:
def summary(self):
return self.Discriminator.summary()
Now, the next recipe will bring the discriminator and generator together in adversarial training!