CNN Model for Gender and Ethnicity Prediction with Tensorflow

Hargurjeet
2 min readApr 5, 2021

Key Learning while working on this datasets

  1. Building a CNN model using Tensorflow
  2. Working with Image Generators
  3. Use of Dropout, Batch Normalizations and different Optimizer(Adam, rmsprop…etc.)
  4. Identify the Best parameters for the model using the tensorflow build in hyperparameter tuning.

About the Dataset

The dataset contains facial images with labels as Age, Gender and Ethnicity. A total of 27305 images has been provided in the dataset. Based on the models needs it can be divided into Training, validation and testing sets. The source file is CSV file with 5 columns. The dataset can be accessed by clicking here

Let us start with model building

Data Pre Processing

  1. Importing all the required library and downloading the dataset

2. Having a look at the sample records

3. It is always a good practice to perform a null check

4. Converting array to images and looking a sample images

5. Removing the unwanted columns and assigning X as inputs and creating variables to carry output values

6. Reshaping the images so that it can be feed to the model

7. Dividing the data into Training and Testing Sets

Setting up the CNN Model

1. CNN Model For Gender

2. Setting up the Image Generator in Tensorflow

3. Configuring the Train and Validation sets

4. Running the Model for 15 epochs

5. Validating loss and accuracy

6. Evaluating model on the test set

An accuracy of 76% is achieved, Which is not too bad for a beginner. We will later see in the post how this can be improved.

Model for predicting ethnicity

  1. Setting up the image generator

2. Building the CNN model. Please note way Dropout, MaxPooling is being used.

3. Configuring the training and validations sets and running the model for 15 epochs

4. Observing the results

5. On Evaluating the model on the test set

As the accuracy is pretty low. Let us try improving the accuracy using the tensorflow build in lib for identifying the best parameters.

Parameter Optimization

Firstly, We will identify the dropouts, optimizer and units which will best suit our model. There is no single way to identifying this.

Below is the way u can configure the above mentioned parameters into your model

Let us run the model based on the parameter we defined

By running the above steps we identify the best paraments to be as follows

Dropout — 0.2

Optimizer — Adam

no of units — 32

Now let us re run the model with these parameter and observe the accuracy

You observe now the our evaluation results have slightly improved.

Plotting the values

Conclusion — In this model we learned creating CNN model using image generator, Dropout etc. Also we learned using the build in function to identify the best parameters.

Happy learning

--

--

Hargurjeet

Data Science Practitioner | Machine Learning | Neural Networks | PyTorch | TensorFlow