Building the DCGAN structure

  1. First, as we did in an earlier neural network model, we need to instantiate the Sequential model type and add the first dense layer, as follows:
def dc_model(self):
model = Sequential()
model.add(Dense(256*8*8,activation=LeakyReLU(0.2),
input_dim=self.LATENT_SPACE_SIZE))
model.add(BatchNormalization()

This first dense layer represents the input layer with a LeakyReLU activation. Also, notice that the input is the latent space size. The first number in the dense layer is the number of filters initialized; however, in the future, you may want to experiment with changing the network's starting value. The paper here starts with 1,024 filters, but starting out with more can help the GAN's convergence.

  1. Next, we need to reshape the tensor so that it can be built back up in an image. (Notice that the number of filters, 256 x 8 x 8, equals the multiplication of each channel in the reshape layer). Each layer must match the original information of the last—the previous tensor's sizing information must match. After the reshape to (8, 8, 256), we'll upsample it and then look at a generator image of 16 x 16 (by three channels). From there, will do two of the same blocks, as follows:
model.add(Reshape((8, 8, 256)))
model.add(UpSampling2D())

# 16x16
model.add(Convolution2D(128, 5, 5,
border_mode='same', activation=LeakyReLU(0.2)))
model.add(BatchNormalization())
model.add(UpSampling2D())

# 32x32
model.add(Convolution2D(64, 5, 5,
border_mode='same', activation=LeakyReLU(0.2)))
model.add(BatchNormalization())
model.add(UpSampling2D())

The preceding blocks are simple in their design thanks to the following features:

    • Two-dimensional convolution across any intermediate generated images allows you to choose the filter size, stride, border mode (how you handle the edges with a convolutional filter), and the activation function
    • Using LeakyReLU works well in practice
    • BatchNormalization on the preceding convolutional layer helps to ensure there are neither very high nor very low activations while also speeding up training and reducing overfitting
    • UpSampling2D scales are increased twice by default, such as 16 x 16 -> 32 x 32 -> 64 x 64
  1. The output tensor for this model is structured in a similar way to previous layers, with the exception of the activation. tanh is recommended by authors and appears to be necessary in this particular architecture; see it in action as follows:
# 3x64x64
model.add(Convolution2D(self.C, 5, 5, border_mode='same',
activation='tanh'))
return model
  1. After returning the model, save the model structure into a PNG, as shown in the following diagram:

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

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