Training the generator network

The steps given in this section show how to train the generator network. This is a continuation of the last series of steps:

  1. Again, sample a batch of high-resolution and low-resolution images and normalize them:
    high_resolution_images, low_resolution_images = sample_images(data_dir=data_dir,                         batch_size=batch_size,low_resolution_shape=low_resolution_shape,                                                                                    high_resolution_shape=high_resolution_shape)

# Normalize images
high_resolution_images = high_resolution_images / 127.5 - 1.
low_resolution_images = low_resolution_images / 127.5 - 1.
  1. Use the VGG19 network to extract feature maps (internal representations) of real high-resolution images:
    image_features = vgg.predict(high_resolution_images)
  1. Finally, train the adversarial model and provide it with appropriate inputs, as follows:
    g_loss = adversarial_model.train_on_batch([low_resolution_images, high_resolution_images],
[real_labels, image_features])

  1. After the completion of each epoch, write the losses to TensorBoard to visualize them:
    write_log(tensorboard, 'g_loss', g_loss[0], epoch)
write_log(tensorboard, 'd_loss', d_loss[0], epoch)
  1. After every 100 epochs, generate high-resolution fake images using the generator network and save them to visualize them:
    if epoch % 100 == 0:
high_resolution_images, low_resolution_images = sample_images(data_dir=data_dir, batch_size=batch_size,low_resolution_shape=low_resolution_shape,
high_resolution_shape=high_resolution_shape)
# Normalize images
high_resolution_images = high_resolution_images / 127.5 - 1.
low_resolution_images = low_resolution_images / 127.5 - 1.

# Generate fake high-resolution images
generated_images = generator.predict_on_batch(low_resolution_images)

# Save
for index, img in enumerate(generated_images):
save_images(low_resolution_images[index], high_resolution_images[index], img,
path="results/img_{}_{}".format(epoch, index))

These images will help you to decide whether to continue the training or to stop it early. Stop the training if the quality of the generated high-resolution images is good. Alternatively, continue the training until your model becomes good.

We have now successfully trained an SRGAN network on the CelebA dataset. After the training is complete, generating high-resolution images is very easy. Take a low-resolution image with a dimension of 64 x 64x3 and pass it to the generator.predict() function, which will generate a high-resolution image.