Reader small image

You're reading from  Generative Adversarial Networks Projects

Product typeBook
Published inJan 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789136678
Edition1st Edition
Languages
Right arrow
Author (1)
Kailash Ahirwar
Kailash Ahirwar
author image
Kailash Ahirwar

Kailash Ahirwar is a machine learning and deep learning enthusiast. He has worked in many areas of Artificial Intelligence (AI), ranging from natural language processing and computer vision to generative modeling using GANs. He is a co-founder and CTO of Mate Labs. He uses GANs to build different models, such as turning paintings into photos and controlling deep image synthesis with texture patches. He is super optimistic about AGI and believes that AI is going to be the workhorse of human evolution.
Read more about Kailash Ahirwar

Right arrow

Face Aging Using Conditional GAN

Conditional GANs (cGANs) are an extension of the GAN model. They allow for the generation of images that have certain conditions or attributes and have proved to be better than vanilla GANs as a result. In this chapter, we will implement a cGAN that, once trained, can perform automatic face aging. The cGAN network that we will implement was first introduced by Grigory Antipov, Moez Baccouche, and Jean-Luc Dugelay, in their paper titled Face Aging With Conditional Generative Adversarial Networks, which can be found at the following link: https://arxiv.org/pdf/1702.01983.pdf.

In this chapter, we will cover the following topics:

  • Introducing cGANs for face aging
  • Setting up the project
  • Preparing the data
  • A Keras implementation of a cGAN
  • Training a cGAN
  • Evaluation and hyperparameter tuning
  • Practical applications of face aging
...

Introducing cGANs for face aging

So far, we have implemented different GAN networks for different use cases. Conditional GANs extend the idea of vanilla GANs and allow us to control the output of the generator network. Face aging is all about changing the age of a person's face without changing their identity. In most other models (including GANs), the appearance or identity of a person is lost by 50% because facial expressions and facial accessories, such as sunglasses or beards, are not taken into account. Age-cGANs consider all of these attributes. In this section, we will explore cGANs for face aging.

Understanding cGANs

cGANs are a type of GAN that are conditioned on some extra information. We feed the extra information...

Setting up the project

If you haven't already cloned the repository with the complete code for all the chapters, clone the repository now. The cloned repository has a directory called Chapter03, which contains the entire code for this chapter. Execute the following commands to set up the project:

  1. Start by navigating to the parent directory, as follows:
cd Generative-Adversarial-Networks-Projects
  1. Now, change the directory from the current directory to Chapter03:
cd Chapter03
  1. Next, create a Python virtual environment for this project:
virtualenv venv
virtualenv venv -p python3 # Create a virtual environment using python3 interpreter
virtualenv venv -p python2 # Create a virtual environment using python2 interpreter

We will be using this newly created virtual environment for this project. Each chapter has its own separate virtual environment.

  1. Next, activate the newly created...

Preparing the data

In this chapter, we will be using the Wiki-Cropped dataset, which contains more than 64, 328 images of various people's faces. The authors have also made a dataset available that contains only the cropped faces, so we don't need to crop faces.

The authors of the paper titled Deep expectation of real and apparent age from a single image without facial landmarks, which is available at https://www.vision.ee.ethz.ch/en/publications/papers/articles/eth_biwi_01299.pdf, have scraped these images from Wikipedia and made them available for academic purposes. If you intend to use the dataset for commercial purposes, contact the authors at rrothe@vision.ee.ethz.ch.

You can manually download the dataset from the following link and place all the compressed files in the directory inside the Age-cGAN project at https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki...

A Keras implementation of an Age-cGAN

Like vanilla GANs, the implementation of cGANs is straightforward. Keras provides enough flexibility to code complex generative adversarial networks. In this section, we will implement the generator network, the discriminator network, and the encoder network used in cGANs. Let's start by implementing the encoder network.

Before starting to write the implementations, create a Python file called main.py and import the essential modules, as follows:

import math
import os
import time
from datetime import datetime

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from keras import Input, Model
from keras.applications import InceptionResNetV2
from keras.callbacks import TensorBoard
from keras.layers import Conv2D, Flatten, Dense, BatchNormalization, Reshape, concatenate, LeakyReLU, Lambda, \
K, Conv2DTranspose, Activation...

Training the cGAN

Training the cGAN for face aging is a three-step process:

  1. Training the cGAN
  2. Initial latent vector approximation
  3. Latent vector optimization

We will cover these steps one by one in the following sections.

Training the cGAN

This is the first step of the training process. In this step, we train the generator and the discriminator networks. Perform the following steps:

  1. Start by specifying the parameters required for the training:
# Define hyperparameters
data_dir = "/path/to/dataset/directory/"
wiki_dir = os.path.join(data_dir, "wiki_crop")
epochs = 500
batch_size = 128
image_shape = (64, 64, 3)
z_shape = 100
TRAIN_GAN = True
TRAIN_ENCODER = False
TRAIN_GAN_WITH_FR = False
fr_image_shape = (192,...

Practical applications of Age-cGAN

Age synthesis and age progression have many industrial and consumer applications:

  • Cross-age face recognition: This can be incorporated into security applications, such as mobile device unlocking or desktop unlocking. The problem with current face recognition systems is that they need to be updated with time. With Age-cGAN networks, the lifespan of cross-age face recognition systems will be longer.
  • Finding lost children: This is an interesting application of an Age-cGAN. As the age of a child increases, their facial features change, and it becomes much harder to identify them. An Age-cGAN can simulate a person's face at a specified age.
  • Entertainment: For example, in mobile applications, to show and share a friend's pictures at a specified age.
  • Visual effects in movies: It's a tedious and lengthy process to manually simulate...

Summary

In this chapter, we were introduced to Age Conditional Generative Adversarial Networks (Age-cGANs). We then studied the architecture of Age-cGANs. After that, we learned how to set up our project and looked at the Keras implementation of Age-cGAN. Then we trained Age-cGAN on a wiki-cropped dataset, and went through all three stages of the Age-cGAN network. Finally, we discussed practical applications of Age-cGANs.

In the next chapter, we will be generating anime characters using another variant of GANs: DCGANs.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Generative Adversarial Networks Projects
Published in: Jan 2019Publisher: PacktISBN-13: 9781789136678
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Kailash Ahirwar

Kailash Ahirwar is a machine learning and deep learning enthusiast. He has worked in many areas of Artificial Intelligence (AI), ranging from natural language processing and computer vision to generative modeling using GANs. He is a co-founder and CTO of Mate Labs. He uses GANs to build different models, such as turning paintings into photos and controlling deep image synthesis with texture patches. He is super optimistic about AGI and believes that AI is going to be the workhorse of human evolution.
Read more about Kailash Ahirwar