This project will be all about defining and training a Convolutional Neural Network to perform facial keypoint detection, and using Computer Vision techniques to transform images of faces.
Facial keypoints (also called facial landmarks) are the small magenta dots shown on the faces in the image below. In each training and test image, there is a single face and 68 keypoints, with coordinates (x, y), for that face. These keypoints mark important areas of the face: the eyes, corners of the mouth, the nose, etc. These keypoints are relevant for a variety of tasks, such as face filters, emotion recognition, pose recognition, and so on. Here they are, numbered, and you can see that specific ranges of points match different portions of the face.
The first step in working with any dataset is to become familiar with your data; you'll need to load in the images of faces and their keypoints and visualize them! This set of image data has been extracted from the YouTube Faces Dataset, which includes videos of people in YouTube videos. These videos have been fed through some processing steps and turned into sets of image frames containing one face and the associated keypoints.
This facial keypoints dataset consists of 5770 color images. All of these images are separated into either a training or a test set of data.
- 3462 of these images are training images, for you to use as you create a model to predict keypoints.
- 2308 are test images, which will be used to test the accuracy of your model.
The information about the images and keypoints in this dataset are summarized in CSV files, which we can read in using pandas
.
All the images are not of the same size, and neural networks often expect images that are standardized; a fixed size, with a normalized range for color ranges and coordinates, and (for PyTorch) converted from numpy lists and arrays to Tensors.
Therefore, the following preprocessing steps are performed:
Normalize
: to convert a color image to grayscale values with a range of [0,1] and normalize the keypoints to be in a range of about [-1, 1]Rescale
: to rescale an image to a desired size.RandomCrop
: to crop an image randomly.ToTensor
: to convert numpy images to torch images.Resize
: to resize an image to a desired size.
In the models.py
file, we have defined our neural network using pytorch
and the following steps are performed:
- Define a CNN with images as input and keypoints as output
- Construct the transformed FaceKeypointsDataset
- Train the CNN on the training data, tracking loss
- See how the trained model performs on test data
- If necessary, modify the CNN structure and model hyperparameters, so that it performs well
- Save the best model locally.
The following steps are performed here:
- Loading the previous saved model(best model) from training phase.
- Using test images and predicting the facial keypoints/landmarks on the test images.
- loss function used is
mean squared error
. - optimizer used is
Adam
. - Visualization is done using
matplotlib
andOpenCv
libraries.