Facial Data-Based Deep Learning: Emotion, Age and Gender Prediction

How can we draw information from facial data using deep learning?

Abhijit Roy
Towards Data Science
9 min readJul 25, 2020

--

Deep Learning has found huge applications in the fields of Computer vision. Some of the most important applications of computer vision are in the fields that deal with facial data. Face Detection and recognition are being widely used in security-based applications. If you want to explore these two areas feel free to go through:

  1. Face Detection: In this article, I have talked about an application based on face detection, and
  2. Face Recognition: This article talks about how we can implement a security mechanism using face recognition.

I have tried to give a total explanation of how the mechanisms work in the above articles.

In this article, we are going to talk about two of the other most important applications of face-based deep learning, emotion detection, or facial expression detection, and age and gender prediction from a facial image.

So, let’s jump right in.

Emotion Detection

First, let us talk about Emotion detection or prediction.

For this part, we will be using Kaggle’s CKPlus Dataset.

Data Preprocessing

The dataset has 981 images in total. These images are classified into seven labels based on seven different expressions: Anger, Contempt, Disgust, Fear, Happy, Sadness, and Surprise. Each expression has a folder. So, there are seven folders in which the 981 images are stored.

First, we list the classes or folders in the order using listdir().

So, we save the expression in the order in a list which we will refer to create the labels.

Next, we move to preprocess the images.

The above snippet opens the image reads it using OpenCV, resizes it to (48 x 48) dimension. I have converted it to an RGB image, so, it is having three channels. The size of each image is (48 x 48 x 3). We will append the images in the ‘images’ list, and the corresponding labels in the ‘labels’ list. Let’s visualize a few examples after preprocessing.

Using the above snippet, we convert the labels and images to NumPy arrays and normalize the images array by dividing it with 255. I have used one-hot encoding to encode the labels into vectors. We will be using a test split of 25%.

Model

We are going to use the above model to predict the expressions.

The above snippet will train the model.

The model gives an accuracy of 100% on the test data.

Note: Whenever a model gives a 100% accuracy on test data, we need to check the training accuracy, if that is also 100%. It means the model is actually overfitting and the test set is having a very close distribution to the train set. So, it is showing great results. I think in these circumstances, it’s better to use cross-validation to get the correct intuition of how the model actually works.

Let’s continue with the evaluation.

Evaluation

The two curves show the learning of the model. The first curve shows the loss function decrease and the second shows the accuracy growth with epochs.

We obtain predictions as shown, Let us check the classification report and Confusion Matrix.

Classification report:

Confusion Matrix:

We have seen the confusion matrix and classification report for our model.

The above snippet will let us have a look at some of the images, their true labels, and the predicted labels.

So, we have seen how to predict Emotion using Deep Learning. Let’s check out Age and Gender Prediction.

Age and Gender Prediction

We will use Kaggle’s UTKFace Dataset for predicting age and Gender.

Data Preprocessing

Here I have used the dataset having 9780 files. It has 9780 images of faces belonging to both males and females with ages ranging from 0 to 116. Each image has labels that show the corresponding age and gender. Male is given by 0 and Female is given by 1.

The above snippet helps to get the data and prepare the training sets. The ‘images’ list contains all the 9780 images, each image of size (48 x 48 x 3). The ‘ages’ list has the corresponding ages and the ‘genders’ list has the corresponding genders.

Let us look at the images after preprocessing.

The 1st image has ‘age: 62 and gender: 0’ and the second image has ‘age: 67 and gender:0

Now, we need to check the distribution of our sets.

The first bar graph shows the distribution of gender. It seems well balanced. The second line graph shows the variation of samples of different ages. We can see that the samples with age less than 40 is much more than the number of samples with age more than 40. This creates a skewness in the train set distribution.

We have seen in this case, we will actually need to predict both age and gender using the same model. So, to create the actual labels for our training set, we will need to do some processing.

The above snippet takes the age and gender for each image sample index wise and converts each one into a list and appends them to the labels list. This is done to create the one-dimensional label vectors.

So, the shape of the ‘labels’ list will be:

[[[age(1)],[gender(1)]],

[[age(2)],[gender(2)]], ……………….[[age(n)],[gender(n)]]]

Next, we convert the labels and images list into NumPy arrays, normalize the images, and create the training and test data splits. We will use a 25% test split.

Currently, our Y_train and Y_test are of the form:

We need to transform them in a way such that Y_train[0] denotes the gender labels vector, and Y_train[1] denotes the age labels vector.

The simple snippet does the work for us.

Now, we are ready to proceed and design our model.

Model

We are going to use the above model to predict both the sex and age

The above is a schematic diagram of our model. After the ‘flatten’ layer we are going to use two different dense layers and dropouts corresponding to the corresponding outputs. Now, gender prediction is a classification problem, while age prediction is a regression problem, so, we will use sigmoid as output activation for gender prediction and ReLU as the activation function for Age prediction. Similarly, we will use ‘binary cross-entropy’ as the loss function for gender and ‘mean absolute error’ as the loss function for the age.

The above snippet will train the model.

The model gives an accuracy of 82% for the gender classification.

Evaluation

Let us look at the model’s loss curve.

This is the generated loss curve for our model.

Let’s look at the evaluation for age prediction:

The above curve shows the model traced linear regression line in black and the blue dots show the distribution of test samples. So, we can see our predicted line almost passes through the middle of the distribution. Above the age of 80, there were very few samples, so, maybe owing to that there our model didn’t perform so well.

Evaluation for gender prediction:

The above curve shows the increase in gender accuracy with epochs.

Classification Matrix for gender classification:

Our model obtained an F1 score of 0.84 for the female gender and 0.78 for Male gender. So, it classifies female gender better than males.

Let’s look at some samples from our set and their corresponding predicted age and gender.

The above snippet helps to generate our samples:

For the last sample, the actual age label was 62, and the predicted age label is 60.

Thus we can predict gender and age using face data.

Conclusion

We have seen how to predict emotion, gender, and sex from a facial image in this article.

The GitHub link is here.

I hope this article helps.

--

--