Age And Gender Prediction

Age and Gender has always been an important feature of our identity. It is also an important factor in our social life. Predictions of age and Gender made with AI can be applied to many areas such as intelligent human-machine interface development, security, cosmetics, electronic commerce.

Age And Gender Prediction
Photo by Zhu Eason on Dribbble

Table of Content

  • Introduction to cAInvas
  • Importing the Dataset
  • Data Analysis
  • Trainset-TestSet Creation
  • Model Architecture and Model Training
  • Introduction to DeepC
  • Compilation with DeepC

Introduction to cAInvas

cAInvas is an integrated development platform to create intelligent edge devices. Not only we can train our deep learning model using Tensorflow, Keras, or Pytorch, we can also compile our model with its edge compiler called DeepC to deploy our working model on edge devices for production.

The Age and Gender Prediction model which we are going to talk about, is also developed on cAInvas. All the dependencies which you will be needing for this project are also pre-installed.

cAInvas also offers various other deep learning notebooks in its gallery which one can use for reference or to gain insight about deep learning. It also has GPU support and which makes it the best in its kind.

Importing the dataset

While working on cAInvas one of its key features is UseCases Gallary.When working on any of its UseCases you don’t have to look for data manually.The data is in the of table and is present as csv format.We will load the dataset through pandas as a dataframe in our workspace.

Data Analysis

The dataset contains age, ethnicity, gender, image name, and pixel values as columns. We can get a description of the dataset by running pandas describe function and then we have to get the pixel values from the pixel column and store them in a numpy array.

And we will create age categories rather than having all discrete ages. We can get an insight about the different ages the dataset have by plotting a histogram which is shown below:

Histogram

TrainSet-TestSet Creation

Once we have extracted the pixel values and stores them in a numpy array, we will extract two separate label variable: one for age and the other for gender. So, in short we will create two models for age and gender prediction. For creation of TestSet and TrainSet we will run the following commands:

Model Architecture and Model Training

After creating the dataset next step is to pass our training data to our Deep Learning model to learn to classify age and sex of the image data. The model architecture used for both the models are:

Model: "AgeModel"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 46, 46, 32)        320       
_________________________________________________________________
batch_normalization (BatchNo (None, 46, 46, 32)        128       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 23, 23, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 21, 21, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 10, 10, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 6400)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                409664    
_________________________________________________________________
dropout (Dropout)            (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 65        
=================================================================
Total params: 428,673
Trainable params: 428,609
Non-trainable params: 64
_________________________________________________________________
Model: "GenderModel"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 46, 46, 32)        320       
_________________________________________________________________
batch_normalization (BatchNo (None, 46, 46, 32)        128       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 23, 23, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 21, 21, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 10, 10, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 6400)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                409664    
_________________________________________________________________
dropout (Dropout)            (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 65        
=================================================================
Total params: 428,673
Trainable params: 428,609
Non-trainable params: 64
_________________________________________________________________

The loss function used is “mean sqaured error” and “Adam” optimizer for age prediction and “binary_crossentropy” and “Adam” optimizer for gender prediction. For training the model we used Keras API with tensorflow at backend. Both the models showed good performance. Here are the training plots for both the models:

a) Gender Model Plot b) Age Model Plot

Training Plots

Training plots
a) Gender Model Plot b) Age Model Plot

Introduction to DeepC

DeepC Compiler and inference framework is designed to enable and perform deep learning neural networks by focussing on features of small form-factor devices like micro-controllers, eFPGAs, cpus and other embedded devices like raspberry-pi, odroid, arduino, SparkFun Edge, risc-V, mobile phones, x86 and arm laptops among others.

DeepC also offers ahead of time compiler producing optimized executable based on LLVM compiler tool chain specialized for deep neural networks with ONNX as front end.

Compilation with DeepC

After we have trained both the models we will be saving both the models in Kears H5 format as it easily stores the weights and model configuration in a single file.

After saving the files in H5 format we can easily compile our model using DeepC compiler which comes as a part of cAInvas platform so that it converts our saved model to a format which can be easily deployed to edge devices. And all this can be done very easily using a simple command.

And that’s it, your Age and Gender Prediction Model is ready for deployment.

Link for the cAInvas Notebook: https://cainvas.ai-tech.systems/use-cases/age-and-gender-prediction-app/

Credit: Ashish Arya

Also Read: Article category classification — on cAInvasfake rolex