Advanced Deep Learning with Keras
Chapter 5Stack of LeakyReLU-Conv2D to discriminate real from fakeThe network does not converge with BN so it is not used hereunlike in [1]# Argumentsinputs (Layer): Input layer of the discriminator (the image)activation (string): Name of output activation layernum_labels (int): Dimension of one-hot labels for ACGAN &InfoGANnum_codes (int): num_codes-dim Q network as outputif StackedGAN or 2 Q networks if InfoGAN# 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)# default output is probability that the image is realoutputs = Dense(1)(x)if activation is not None:print(activation)outputs = Activation(activation)(outputs)if num_labels:# ACGAN and InfoGAN have 2nd output# 2nd output is 10-dim one-hot vector of label[ 149 ]
Improved GANslayer = Dense(layer_filters[-2])(x)labels = Dense(num_labels)(layer)labels = Activation('softmax', name='label')(labels)if num_codes is None:outputs = [outputs, labels]else:# InfoGAN have 3rd and 4th outputs# 3rd output is 1-dim continous Q of 1st c given xcode1 = Dense(1)(layer)code1 = Activation('sigmoid', name='code1')(code1)# 4th output is 1-dim continuous Q of 2nd c given xcode2 = Dense(1)(layer)code2 = Activation('sigmoid', name='code2')(code2)outputs = [outputs, labels, code1, code2]elif num_codes is not None:# z0_recon is reconstruction of z0 normal distributionz0_recon = Dense(num_codes)(x)z0_recon = Activation('tanh', name='z0')(z0_recon)outputs = [outputs, z0_recon]return Model(inputs, outputs, name='discriminator')The discriminator is then built by calling:discriminator = gan.discriminator(inputs, num_labels=num_labels)The generator is the same as the one in ACGAN. To recall, the generator builder isshown in the following listing. We should note that both Listings 5.3.1 and 5.3.2 arethe same builder functions used by WGAN and LSGAN in the previous sections.Listing 5.3.2, gan.py shows the generator model builder is the same as in CGAN:def generator(inputs,image_size,activation='sigmoid',labels=None,codes=None):"""Build a Generator ModelStack of BN-ReLU-Conv2DTranpose to generate fake images.Output activation is sigmoid instead of tanh in [1].Sigmoid converges easily.# Arguments[ 150 ]
- 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 and 126: 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: Improved GANsEssentially, in CGAN w
- 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
- Page 177 and 178: Improved GANsConclusionIn this chap
- Page 179 and 180: Disentangled Representation GANsIn
- Page 181 and 182: Disentangled Representation GANsInf
- Page 183 and 184: Disentangled Representation GANsFol
- Page 185 and 186: Disentangled Representation GANs# A
- Page 187 and 188: Disentangled Representation GANsif
- Page 189 and 190: Disentangled Representation GANsLis
- Page 191 and 192: Disentangled Representation GANsdat
- Page 193 and 194: Disentangled Representation GANsy[b
- Page 195 and 196: Disentangled Representation GANspyt
- Page 197 and 198: Disentangled Representation GANsThe
- Page 199 and 200: Disentangled Representation GANsSta
- Page 201 and 202: Disentangled Representation GANs( )
- Page 203 and 204: Disentangled Representation GANsThe
- Page 205 and 206: Disentangled Representation GANsfea
- Page 207 and 208: Disentangled Representation GANs# f
- Page 209 and 210: Disentangled Representation GANslat
- Page 211 and 212: Disentangled Representation GANsDis
- Page 213 and 214: Disentangled Representation GANsz_d
- Page 215 and 216: Disentangled Representation GANs2.
Improved GANs
layer = Dense(layer_filters[-2])(x)
labels = Dense(num_labels)(layer)
labels = Activation('softmax', name='label')(labels)
if num_codes is None:
outputs = [outputs, labels]
else:
# InfoGAN have 3rd and 4th outputs
# 3rd output is 1-dim continous Q of 1st c given x
code1 = Dense(1)(layer)
code1 = Activation('sigmoid', name='code1')(code1)
# 4th output is 1-dim continuous Q of 2nd c given x
code2 = Dense(1)(layer)
code2 = Activation('sigmoid', name='code2')(code2)
outputs = [outputs, labels, code1, code2]
elif num_codes is not None:
# z0_recon is reconstruction of z0 normal distribution
z0_recon = Dense(num_codes)(x)
z0_recon = Activation('tanh', name='z0')(z0_recon)
outputs = [outputs, z0_recon]
return Model(inputs, outputs, name='discriminator')
The discriminator is then built by calling:
discriminator = gan.discriminator(inputs, num_labels=num_labels)
The generator is the same as the one in ACGAN. To recall, the generator builder is
shown in the following listing. We should note that both Listings 5.3.1 and 5.3.2 are
the same builder functions used by WGAN and LSGAN in the previous sections.
Listing 5.3.2, gan.py shows the generator model builder is the same as in CGAN:
def generator(inputs,
image_size,
activation='sigmoid',
labels=None,
codes=None):
"""Build a Generator Model
Stack of BN-ReLU-Conv2DTranpose to generate fake images.
Output activation is sigmoid instead of tanh in [1].
Sigmoid converges easily.
# Arguments
[ 150 ]