Advanced Deep Learning with Keras
Chapter 4As can be seen in Listing 4.2.1 and 4.2.2, the DCGAN models are straightforward.What makes it difficult to build is small changes in the network design can easilybreak the training convergence. For example, if batch normalization is used in thediscriminator or if strides = 2 in the generator is transferred to the latter CNNlayers, DCGAN will fail to converge.Listing 4.2.2, dcgan-mnist-4.2.1.py shows us the discriminator network builderfunction for DCGAN:def build_discriminator(inputs):"""Build a Discriminator ModelStack of LeakyReLU-Conv2D to discriminate real from fake.The network does not converge with BN so it is not used hereunlike in [1] or original paper.# Argumentsinputs (Layer): Input layer of the discriminator (the image)# ReturnsModel: Discriminator Model"""kernel_size = 5layer_filters = [32, 64, 128, 256]x = inputsfor filters in layer_filters:# first 3 convolution layers use strides = 2# last one uses strides = 1if filters == layer_filters[-1]:strides = 1else:strides = 2x = LeakyReLU(alpha=0.2)(x)x = Conv2D(filters=filters,kernel_size=kernel_size,strides=strides,padding='same')(x)x = Flatten()(x)x = Dense(1)(x)x = Activation('sigmoid')(x)discriminator = Model(inputs, x, name='discriminator')return discriminator[ 109 ]
Generative Adversarial Networks (GANs)Listing 4.2.3, dcgan-mnist-4.2.1.py: Function to build DCGAN models and callthe training routine:def build_and_train_models():# load MNIST dataset(x_train, _), (_, _) = mnist.load_data()# reshape data for CNN as (28, 28, 1) and normalizeimage_size = x_train.shape[1]x_train = np.reshape(x_train, [-1, image_size, image_size, 1])x_train = x_train.astype('float32') / 255model_name = "dcgan_mnist"# network parameters# the latent or z vector is 100-dimlatent_size = 100batch_size = 64train_steps = 40000lr = 2e-4decay = 6e-8input_shape = (image_size, image_size, 1)# build discriminator modelinputs = Input(shape=input_shape, name='discriminator_input')discriminator = build_discriminator(inputs)# [1] or original paper uses Adam,# but discriminator converges easily with RMSpropoptimizer = RMSprop(lr=lr, decay=decay)discriminator.compile(loss='binary_crossentropy',optimizer=optimizer,metrics=['accuracy'])discriminator.summary()# build generator modelinput_shape = (latent_size, )inputs = Input(shape=input_shape, name='z_input')generator = build_generator(inputs, image_size)generator.summary()# build adversarial modeloptimizer = RMSprop(lr=lr * 0.5, decay=decay * 0.5)# freeze the weights of discriminator# during adversarial training[ 110 ]
- 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 and 124: Generative Adversarial Networks (GA
- Page 125: 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
- Page 175 and 176: Improved GANsUnlike CGAN, the sampl
Chapter 4
As can be seen in Listing 4.2.1 and 4.2.2, the DCGAN models are straightforward.
What makes it difficult to build is small changes in the network design can easily
break the training convergence. For example, if batch normalization is used in the
discriminator or if strides = 2 in the generator is transferred to the latter CNN
layers, DCGAN will fail to converge.
Listing 4.2.2, dcgan-mnist-4.2.1.py shows us the discriminator network builder
function for DCGAN:
def build_discriminator(inputs):
"""Build a Discriminator Model
Stack of LeakyReLU-Conv2D to discriminate real from fake.
The network does not converge with BN so it is not used here
unlike in [1] or original paper.
# Arguments
inputs (Layer): Input layer of the discriminator (the image)
# Returns
Model: Discriminator Model
"""
kernel_size = 5
layer_filters = [32, 64, 128, 256]
x = inputs
for filters in layer_filters:
# first 3 convolution layers use strides = 2
# last one uses strides = 1
if filters == layer_filters[-1]:
strides = 1
else:
strides = 2
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same')(x)
x = Flatten()(x)
x = Dense(1)(x)
x = Activation('sigmoid')(x)
discriminator = Model(inputs, x, name='discriminator')
return discriminator
[ 109 ]