Latent vector optimization

In the preceding two steps, we successfully trained the generator network, the discriminator network, and the encoder network. In this section, we will improve the encoder and the generator networks. In these steps, we will be using the face recognition (FR) network, which generates a 128-dimensional embedding of a particular input fed to it, to improve the generator and the encoder network.  

Perform the following steps:

  1. Start by building and loading the weights for the encoder network and the generator network:
encoder = build_encoder()
encoder.load_weights("encoder.h5")

# Load the generator network
generator.load_weights("generator.h5")
  1. Next, create a network to resize the images from a shape of (64, 64, 3) to a shape of (192, 192, 3), as follows:
# Define all functions before you make a call to them
def
build_image_resizer():
input_layer = Input(shape=(64, 64, 3))

resized_images = Lambda(lambda x: K.resize_images(x, height_factor=3, width_factor=3,
data_format='channels_last'))(input_layer)

model = Model(inputs=[input_layer], outputs=[resized_images])
return model
image_resizer = build_image_resizer()
image_resizer.compile(loss=loss, optimizer='adam')

To work with FaceNet, the height and width of our images should be greater than 150 pixels. The preceding network will help us in converting our images to the desired format. 

  1. Build the face recognition model:
# Face recognition model
fr_model = build_fr_model(input_shape=fr_image_shape)
fr_model.compile(loss=loss, optimizer="adam")

Refer to https://github.com/PacktPublishing/Generative-Adversarial-Networks-Projects/Age-cGAN/main.py for the build_fr_model() function.

  1. Next, create another adversarial model. In this adversarial model, we will have three networks: the encoder, the generator, and the face recognition model:
# Make the face recognition network as non-trainable
fr_model.trainable = False

# Input layers
input_image = Input(shape=(64, 64, 3))
input_label = Input(shape=(6,))

# Use the encoder and the generator network
latent0 = encoder(input_image)
gen_images = generator([latent0, input_label])

# Resize images to the desired shape
resized_images = Lambda(lambda x: K.resize_images(gen_images, height_factor=3, width_factor=3,
data_format='channels_last'))(gen_images)
embeddings = fr_model(resized_images)

# Create a Keras model and specify the inputs and outputs to the network
fr_adversarial_model = Model(inputs=[input_image, input_label], outputs=[embeddings])

# Compile the model
fr_adversarial_model.compile(loss=euclidean_distance_loss, optimizer=adversarial_optimizer)
  1. Add an epoch loop and a batch steps loop inside the first loop, as follows:
for epoch in range(epochs):
print("Epoch:", epoch)

number_of_batches = int(len(loaded_images) / batch_size)
print("Number of batches:", number_of_batches)
for index in range(number_of_batches):
print("Batch:", index + 1)
  1. Next, sample a batch of images from the list of real images:
        # Sample and normalize
images_batch = loaded_images[index * batch_size:(index + 1) * batch_size]
images_batch = images_batch / 255.0
images_batch = images_batch.astype(np.float32)

# Sample a batch of age one-hot encoder vectors
y_batch = y[index * batch_size:(index + 1) * batch_size]
  1. Next, generate embeddings for the real images using the FR network:
        images_batch_resized = image_resizer.predict_on_batch(images_batch)
real_embeddings = fr_model.predict_on_batch(images_batch_resized)
  1. Finally, train the adversarial model, which will train the encoder model and the generator model:
        reconstruction_loss = fr_adversarial_model.train_on_batch([images_batch, y_batch], real_embeddings)
  1. Also, write the reconstruction loss to TensorBoard for further visualization:
        # Write the reconstruction loss to Tensorboard
write_log(tensorboard, "reconstruction_loss", reconstruction_loss, index)
  1. Save the weights for both of the networks:
# Save improved weights for both of the networks    
generator.save_weights("generator_optimized.h5")
encoder.save_weights("encoder_optimized.h5")

Congratulations! We have now successfully trained the Age-cGAN for face aging. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.178