Advanced Deep Learning with Keras

fourpersent2020
from fourpersent2020 More from this publisher
16.03.2021 Views

Chapter 4discriminator.trainable = False# adversarial = generator + discriminatoradversarial = Model(inputs,discriminator(generator(inputs)),name=model_name)adversarial.compile(loss='binary_crossentropy',optimizer=optimizer,metrics=['accuracy'])adversarial.summary()# train discriminator and adversarial networksmodels = (generator, discriminator, adversarial)params = (batch_size, latent_size, train_steps, model_name)train(models, x_train, params)Listing 4.2.4 shows the function dedicated to training the discriminator andadversarial networks. Due to custom training, the usual fit() function is not goingto be used. Instead, train_on_batch() is called up to run a single gradient updatefor the given batch of data. The generator is then trained via an adversarial network.The training first randomly picks a batch of real images from the dataset. This islabeled as real (1.0). Then a batch of fake images will be generated by the generator.This is labeled as fake (0.0). The two batches are concatenated and are used to trainthe discriminator.After this is completed, a new batch of fake images will be generated by thegenerator and labeled as real (1.0). This batch will be used to train the adversarialnetwork. The two networks are trained alternately for about 40,000 steps. At regularintervals, the generated MNIST digits based on a certain noise vector are saved onthe filesystem. At the last training step, the network has converged. The generatormodel is also saved on a file so we can easily reuse the trained model for futureMNIST digits generation. However, only the generator model is saved since thatis the useful part of GANs in the generation of new MNIST digits. For example,we can generate new and random MNIST digits by executing:python3 dcgan-mnist-4.2.1.py --generator=dcgan_mnist.h5Listing 4.2.4, dcgan-mnist-4.2.1.py shows us the function to train thediscriminator and adversarial networks:def train(models, x_train, params):"""Train the Discriminator and Adversarial NetworksAlternately train Discriminaor and Adversarial networks by batch.Discriminator is trained first with properly real and fake images.Adversarial is trained next with fake images pretending to be realGenerate sample images per save_interval.[ 111 ]

Generative Adversarial Networks (GANs)# Argumentsmodels (list): Generator, Discriminator, Adversarial modelsx_train (tensor): Train imagesparams (list) : Networks parameters"""# the GAN modelsgenerator, discriminator, adversarial = models# network parametersbatch_size, latent_size, train_steps, model_name = params# the generator image is saved every 500 stepssave_interval = 500# noise vector to see how the generator output evolves# during trainingnoise_input = np.random.uniform(-1.0, 1.0, size=[16, latent_size])# number of elements in train datasettrain_size = x_train.shape[0]for i in range(train_steps):# train the discriminator for 1 batch# 1 batch of real (label=1.0) and fake images (label=0.0)# randomly pick real images from datasetrand_indexes = np.random.randint(0, train_size, size=batch_size)real_images = x_train[rand_indexes]# generate fake images from noise using generator# generate noise using uniform distributionnoise = np.random.uniform(-1.0, 1.0, size=[batch_size, latent_size])# generate fake imagesfake_images = generator.predict(noise)# real + fake images = 1 batch of train datax = np.concatenate((real_images, fake_images))# label real and fake images# real images label is 1.0y = np.ones([2 * batch_size, 1])# fake images label is 0.0y[batch_size:, :] = 0.0# train discriminator network, log the loss and accuracyloss, acc = discriminator.train_on_batch(x, y)log = "%d: [discriminator loss: %f, acc: %f]" % (i, loss, acc)network# train the adversarial network for 1 batch# 1 batch of fake images with label=1.0# since the discriminator weights are frozen in adversarial[ 112 ]

Generative Adversarial Networks (GANs)

# Arguments

models (list): Generator, Discriminator, Adversarial models

x_train (tensor): Train images

params (list) : Networks parameters

"""

# the GAN models

generator, discriminator, adversarial = models

# network parameters

batch_size, latent_size, train_steps, model_name = params

# the generator image is saved every 500 steps

save_interval = 500

# noise vector to see how the generator output evolves

# during training

noise_input = np.random.uniform(-1.0, 1.0, size=[16, latent_size])

# number of elements in train dataset

train_size = x_train.shape[0]

for i in range(train_steps):

# train the discriminator for 1 batch

# 1 batch of real (label=1.0) and fake images (label=0.0)

# randomly pick real images from dataset

rand_indexes = np.random.randint(0, train_size, size=batch_

size)

real_images = x_train[rand_indexes]

# generate fake images from noise using generator

# generate noise using uniform distribution

noise = np.random.uniform(-1.0, 1.0, size=[batch_size, latent_

size])

# generate fake images

fake_images = generator.predict(noise)

# real + fake images = 1 batch of train data

x = np.concatenate((real_images, fake_images))

# label real and fake images

# real images label is 1.0

y = np.ones([2 * batch_size, 1])

# fake images label is 0.0

y[batch_size:, :] = 0.0

# train discriminator network, log the loss and accuracy

loss, acc = discriminator.train_on_batch(x, y)

log = "%d: [discriminator loss: %f, acc: %f]" % (i, loss, acc)

network

# train the adversarial network for 1 batch

# 1 batch of fake images with label=1.0

# since the discriminator weights are frozen in adversarial

[ 112 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!