Introduction to Generative Adversarial Networks (GANs) (对抗生成网络)
Combined by a forger network and an expert network.
Generator network
Input: random vector (a random point at a latent space)
Output: synthetic image
Discriminator Network
Input: image (real or synthetic)
Output: predict where this image comes from.
What is a latent space?
A vector space that cannot be observed.
Difference
The GANS different forms of other training setting up, the optimization minimum is not fixed. With the GANS, every step tries to take down the hill changes the entire landscape. There exists an equilibrium balance between two processes or forces, not a minimum optimization process.
Also, in this case, GANS hard to train.
Schematic Implement
This is a deep convolutional GANS (DCGANs).
generator maps vectors (latent_dim) to shape of image (32, 32, 3).
discriminator maps image shape (32, 32, 3) to a binary ‘real’ probability.
gan chains the generator and discriminator together. gan(x) = discriminator(generator(x)).
train the discriminator by real and fake images and labels.
train the generator to fool the discriminator. Move the weight of the generator that makes discriminator classify generated image as a real image.
Tricks
tanh as the last activation in the generator.
sample point the latent space using a normal distribution.
Stochasticity improves robustness. GANs may be stuck in any sort of way because of the resulting balanced equilibrium.
dropout in discriminator (and generator).
adding random noise to labels of the discriminator.
sparse gradient hinder GANs training different from other deep learning technology. Here are two methods to decrease:
change maxpooling to stried convolution for downsampling.
change ReLu to LeakyReLu which relax sparsity constants by allowing small negative activation value.
To fix the checkerboard artifact caused by unequal coverage of the pixel space in the generator, the stridden Conv2DTranpose or Conv2D in both generator and discriminator.
When adversarial loss increases and discriminative loss tends to 0 (discriminator end up dominating the generator), reduce the discriminator learning rate, and increase the dropout level of the discriminator.
discriminator_input = layers.Input(shape=(height, width, channels)) x = layers.Conv2D(128, 3)(discriminator_input) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Flatten()(x)
# dropout layer x = layers.Dropout(0.4)(x)
# output layer x = layers.Dense(1, activation='sigmoid')(x)
# using gradient clipping by value in the optimizer / to stabilize, use learning rate decay discriminator_optimizer = keras.optimizers.RMSprop(lr=0.0008, clipvalue=1.0, decay=1e-8) discriminator.compile(optimizer=discriminator_optimizer, loss='binary_crossentropy')
The GANs
1 2 3 4 5 6 7 8 9
# only applies to gan model discriminator.trainable = False