First introduction to GAN

1. Introduction to Generative Adversarial Network GAN


1.1 Generator

        G(Z) accepts random noise Z as input to generate imitations, and trains itself to fool the discriminator D into thinking that any data generated by G(Z) is real.

1.2 Discriminator

        D(Y) can judge the degree of simulation of counterfeit products based on genuine products and imitation products. Usually, the closer the value is to 0, the more authentic it is (and the closer the value is to 1, which means imitation). The goal is to maximize the D(Y) value of images within each real data distribution and minimize the D(Y) value of images outside the real data distribution.

1.3 Training principles

        The generator and discriminator play an opposing game, hence the name adversarial training. Generally, G and D are trained in an alternating manner, and their objective function is expressed as a loss function and optimized using the gradient descent algorithm. Both D and G adjust the parameters of the generator through backpropagation, so that G can learn how to deceive D in more and more situations, and finally G will learn how to generate fake images that can look real.

2. Implement DCGAN network

tips:

(1) LeakyReLU allows a small gradient when the unit is not activated. In many cases, it can improve the performance of GAN.

(2) BatchNormalization() Batch normalization is a technique that helps stabilize learning by normalizing the input of each unit to 0 mean and unit variance. Speeds up training in many cases , reduces problems with poor initialization , and more generally produces more accurate results.

2.1 Define generator G

from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.core import Activation, Flatten, Dense, Dropout
from keras.datasets import cifar10
from keras.utils import np_utils
from keras.optimizers import RMSprop

def generator_model():
    model = Sequential()
    model.add(Dense(input_dim=100, output_dim=1024))
    model.add(Activation('tanh'))
    model.add(Dense(128*7*7))
    model.add(BatchNormalization())
    model.add(Activation('tanh'))
    model.add(Reshape((128, 7, 7), input_shape=(128*7*7,)))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(Conv2D(64, 5, 5, border_mode='same'))
    model.add(Activation('tanh'))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(Conv2D(1, 5, 5, border_mode='same'))
    model.add(Activation('tanh'))
    return model
    # 注意:这里的卷积网络没有任何池化操作

2.2 Define the discriminator D

def discriminator_model():
    model = Sequential()
    model.add(Dense(input_dim=100, output_dim=1024))
    model.add(Conv2D(64, 5, 5, border_mode='same', input_shape=(1, 28, 28)))
    model.add(Activation('tanh'))
    model.add(MaxPooling2D(pool_size(2, 2)))
    model.add(Conv2D(128, 5, 5))
    model.add(Activation('tanh'))
    model.add(MaxPooling2D(pool_size(2, 2)))
    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation('tanh'))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))
    return model

2.3 Combination

# 一个对抗模型,其 G和 D基于相同的模型 M
adversarial_model = AdversarialModel(base_model=M,
                                     player_params=[generator.trainable_weights, discriminator.trainable_weights],
                                     player_names=['generator', 'discriminator'])
# G和D基于两个不同的模型
adversarial_model = AdversarialModel(player_models=[gan_g, gan_d],
                                     player_params=[generator.trainable_weights, discriminator.trainable_weights],
                                     player_names=['generator', 'discriminator'])

Guess you like

Origin blog.csdn.net/MusicDancing/article/details/132905006