This model structure is intuitive, so we'll use convolutional layers to trigger a binary classification about the input image: real or fake, as shown in the following code block:
def dc_model(self):
model = Sequential()
model.add(Convolution2D(64, 5, 5, subsample=(2,2), input_shape=
(self.W,self.H,self.C),
border_mode='same', activation=LeakyReLU(alpha=0.2)))
model.add(Dropout(0.3))
model.add(BatchNormalization())
model.add(Convolution2D(128, 5, 5, subsample=(2,2),
border_mode='same', activation=LeakyReLU(alpha=0.2)))
model.add(Dropout(0.3))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
return model
Take the following steps while structuring the discriminator:
- Instantiate a sequential model and start with a 2D convolutional layer with 64 filters, a 5 x 5 kernel, a sub-sample of 2 x 2, and an input shape of the image
- Drop enforced sparcity at each step; in this example, we'll drop 30% of the learned weights after each pass to ensure that the model doesn't overfit and can learn the key features
- Perform BatchNormalization
- Repeat the process with a larger number of filters (such as 128)
- Finally, flatten the model and ensure the output is a prediction (either 1 for real or 0 for fake)
After building the model, check the structure with the saveModel function to ensure that the layers are connected in the manner illustrated in the following diagram:
You can copy the exact GAN.py file from Chapter 3, My First GAN in Under 100 Lines, and use it in this DCGAN architecture.
Now, let's move on to training!