Advanced Deep Learning with Keras
After undergoing two Conv2DTranspose with strides = 2, the feature mapswill have a size of 28 × 28 × number of filters. Each Conv2DTranspose is precededby batch normalization and ReLU. The final layer has sigmoid activation thatgenerates the 28 × 28 × 1 fake MNIST images. Each pixel is normalized to[0.0, 1.0] corresponding to [0, 255] grayscale levels. Following listing shows theimplementation of the generator network in Keras. A function is defined to buildthe generator model. Due to the length of the entire code, we will limit the listingto the particular lines being discussed.Chapter 4The complete code is available on GitHub: https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras.Listing 4.2.1, dcgan-mnist-4.2.1.py shows us the generator network builderfunction for DCGAN:def build_generator(inputs, image_size):"""Build a Generator ModelStack of BN-ReLU-Conv2DTranpose to generate fake images.Output activation is sigmoid instead of tanh in [1].Sigmoid converges easily.# Argumentsinputs (Layer): Input layer of the generator (the z-vector)image_size: Target size of one side (assuming square image)# ReturnsModel: Generator Model"""image_resize = image_size // 4# network parameterskernel_size = 5layer_filters = [128, 64, 32, 1]x = Dense(image_resize * image_resize * layer_filters[0])(inputs)x = Reshape((image_resize, image_resize, layer_filters[0]))(x)for filters in layer_filters:# first two convolution layers use strides = 2# the last two use strides = 1if filters > layer_filters[-2]:strides = 2[ 107 ]
Generative Adversarial Networks (GANs)else:strides = 1x = BatchNormalization()(x)x = Activation('relu')(x)x = Conv2DTranspose(filters=filters,kernel_size=kernel_size,strides=strides,padding='same')(x)x = Activation('sigmoid')(x)generator = Model(inputs, x, name='generator')return generatorThe discriminator is similar to many CNN-based classifiers. The input is a 28 × 28 × 1MNIST image that is classified as either real (1.0) or fake (0.0). There are four CNNlayers. Except for the last convolution, each Conv2D uses strides = 2 to downsample the feature maps by two. Each Conv2D is then preceded by a Leaky ReLUlayer. The final filter size is 256, while the initial filter size is 32 and doubles everyconvolution layer. The final filter size of 128 also works. However, we'll find thatthe generated images look better with 256. The final output layer is flattened, anda single unit Dense layer generates the prediction between 0.0 to 1.0 after scalingby the sigmoid activation layer. The output is modeled as a Bernoulli distribution.Hence, the binary cross-entropy loss function is used.After building the generator and discriminator models, the adversarial model ismade by concatenating the generator and discriminator networks. Both discriminatorand adversarial networks use the RMSprop optimizer. The learning rate for thediscriminator is 2e-4 while for the adversarial network, it is 1e-4. RMSprop decayrates of 6e-8 for discriminator and 3e-8 for the adversarial network are applied.Setting the learning rate of the adversarial equal to half of the discriminatorwill result in a more stable training. We'll recall from Figure 4.1.3 and 4.1.4, thatthe GAN training has two parts: discriminator training and generator training,which is adversarial training, with discriminator weights frozen.Listing 4.2.2 shows the implementation of the discriminator in Keras. A functionis defined to build the discriminator model. In Listing 4.2.3, we'll illustrate how tobuild GAN models. Firstly, the discriminator model is built and following on fromthat the generator model is instantiated. The adversarial model is just the generatorand the discriminator put together. Across many GANs, the batch size of 64 appearsto be the most common. The network parameters are shown in Listing 4.2.3.[ 108 ]
- Page 73 and 74: Deep Neural NetworksTransition Laye
- Page 75 and 76: Deep Neural NetworksThere are some
- Page 77 and 78: Deep Neural NetworksResNet v2 is al
- Page 79 and 80: Deep Neural Networks…if version =
- Page 81 and 82: Deep Neural NetworksTo prevent the
- Page 83 and 84: Deep Neural NetworksAverage Pooling
- Page 85 and 86: Deep Neural Networks# orig paper us
- Page 88 and 89: AutoencodersIn the previous chapter
- Page 90 and 91: Chapter 3The autoencoder has the te
- Page 92 and 93: Chapter 3Firstly, we're going to im
- Page 94 and 95: Chapter 3# reconstruct the inputout
- Page 96 and 97: Chapter 3Figure 3.2.2: The decoder
- Page 98 and 99: batch_size=32,model_name="autoencod
- Page 100 and 101: Chapter 3Figure 3.2.6: Digits gener
- Page 102 and 103: Chapter 3As shown in Figure 3.3.2,
- Page 104 and 105: Chapter 3image_size = x_train.shape
- Page 106 and 107: Chapter 3# Mean Square Error (MSE)
- Page 108 and 109: Chapter 3from keras.layers import R
- Page 110 and 111: Chapter 3# build the autoencoder mo
- Page 112 and 113: Chapter 3x_train,validation_data=(x
- Page 114: Chapter 3ConclusionIn this chapter,
- Page 117 and 118: Generative Adversarial Networks (GA
- Page 119 and 120: Generative Adversarial Networks (GA
- Page 121 and 122: Generative Adversarial Networks (GA
- Page 123: Generative Adversarial Networks (GA
- Page 127 and 128: Generative Adversarial Networks (GA
- Page 129 and 130: Generative Adversarial Networks (GA
- Page 131 and 132: Generative Adversarial Networks (GA
- Page 133 and 134: Generative Adversarial Networks (GA
- Page 135 and 136: Generative Adversarial Networks (GA
- Page 137 and 138: Generative Adversarial Networks (GA
- Page 139 and 140: Generative Adversarial Networks (GA
- Page 141 and 142: Generative Adversarial Networks (GA
- Page 143 and 144: Improved GANsIn summary, the goal o
- Page 145 and 146: Improved GANsThe intuition behind E
- Page 147 and 148: Improved GANsThis makes sense since
- Page 149 and 150: Improved GANsIn the context of GANs
- Page 151 and 152: Improved GANsFigure 5.1.3: Top: Tra
- Page 153 and 154: Improved GANsThe functions include:
- Page 155 and 156: Improved GANsmodels = (generator, d
- Page 157 and 158: Improved GANsfor layer in discrimin
- Page 159 and 160: Improved GANsFollowing figure shows
- Page 161 and 162: Improved GANsThe preceding table sh
- Page 163 and 164: Improved GANsFollowing figure shows
- Page 165 and 166: Improved GANsEssentially, in CGAN w
- Page 167 and 168: Improved GANslayer = Dense(layer_fi
- Page 169 and 170: Improved GANsx = BatchNormalization
- Page 171 and 172: Improved GANsdiscriminator.compile(
- Page 173 and 174: Improved GANssize=batch_size)real_i
Generative Adversarial Networks (GANs)
else:
strides = 1
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same')(x)
x = Activation('sigmoid')(x)
generator = Model(inputs, x, name='generator')
return generator
The discriminator is similar to many CNN-based classifiers. The input is a 28 × 28 × 1
MNIST image that is classified as either real (1.0) or fake (0.0). There are four CNN
layers. Except for the last convolution, each Conv2D uses strides = 2 to down
sample the feature maps by two. Each Conv2D is then preceded by a Leaky ReLU
layer. The final filter size is 256, while the initial filter size is 32 and doubles every
convolution layer. The final filter size of 128 also works. However, we'll find that
the generated images look better with 256. The final output layer is flattened, and
a single unit Dense layer generates the prediction between 0.0 to 1.0 after scaling
by the sigmoid activation layer. The output is modeled as a Bernoulli distribution.
Hence, the binary cross-entropy loss function is used.
After building the generator and discriminator models, the adversarial model is
made by concatenating the generator and discriminator networks. Both discriminator
and adversarial networks use the RMSprop optimizer. The learning rate for the
discriminator is 2e-4 while for the adversarial network, it is 1e-4. RMSprop decay
rates of 6e-8 for discriminator and 3e-8 for the adversarial network are applied.
Setting the learning rate of the adversarial equal to half of the discriminator
will result in a more stable training. We'll recall from Figure 4.1.3 and 4.1.4, that
the GAN training has two parts: discriminator training and generator training,
which is adversarial training, with discriminator weights frozen.
Listing 4.2.2 shows the implementation of the discriminator in Keras. A function
is defined to build the discriminator model. In Listing 4.2.3, we'll illustrate how to
build GAN models. Firstly, the discriminator model is built and following on from
that the generator model is instantiated. The adversarial model is just the generator
and the discriminator put together. Across many GANs, the batch size of 64 appears
to be the most common. The network parameters are shown in Listing 4.2.3.
[ 108 ]