Age and Gender Prediction

Keras Functional API offers a more flexible and creative way to make models of higher complexity. One such Complexity arises when making a model that does more than one type of supervised prediction (Regression and Classification predictions). We will unravel a similar scenario to create a model that can perform Regression and Classification as well.

Dataset

We will use the Age, Gender (Face Data) CSV dataset for our purpose and to achieve this we will use a Convolutional Neural Network (CNN). CNN is a powerful tool in Deep Learning that helps the user classify an Image and has the most usage in Computer Vision. It enables the Machine to visualize and interpret Images and Image data. For an in-depth explanation of CNN and its architecture.

Prerequisite

The essential libraries we have used are:

  • Pandas – An open-source library to read and manipulate datasets. Here it was used to read the CSV file which contained pixel values for the image
  • Numpy – An open-source library with functions for high-level mathematical calculations as well as handling data that spans multiple dimensions
  • Matlplotlib– An open source library which is used to visualize our data and losses in our prediction model
  • Sklearn – This library consists of pre-defined functions and evaluation metrics that help in data preprocessing, model performance evaluation and model initialization.
  • Tensorflow – Developed by Google, this open-source library is used for creating Deep learning models. It provides many methods to interpret data but mainly focuses on training and inference of Neural Networks

Importing main libraries

Python3




# importing necessary libraries
import matplotlib.pyplot as plt
import tensorflow as tf
import random
import keras
from keras import layers
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping


Reading the Dataset

The dataset used here was in a csv format. We used Pandas to read the csv file and print the shape and information about the dataset.

Python3




# reading the dataset
df= pd.read_csv('age_gender.csv')
print(df.head())


Output:

   age  ethnicity  gender                        img_name  \
0    1          2       0  20161219203650636.jpg.chip.jpg   
1    1          2       0  20161219222752047.jpg.chip.jpg   
2    1          2       0  20161219222832191.jpg.chip.jpg   
3    1          2       0  20161220144911423.jpg.chip.jpg   
4    1          2       0  20161220144914327.jpg.chip.jpg   

                                              pixels  
0  129 128 128 126 127 130 133 135 139 142 145 14...  
1  164 74 111 168 169 171 175 182 184 188 193 199...  
2  67 70 71 70 69 67 70 79 90 103 116 132 145 155...  
3  193 197 198 200 199 200 202 203 204 205 208 21...  
4  202 205 209 210 209 209 210 211 212 214 218 21...  

Check the data basic informations

Python3




# information about the dataset
df.info()


Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 23705 entries, 0 to 23704
Data columns (total 5 columns):
 #   Column     Non-Null Count  Dtype 
---  ------     --------------  ----- 
 0   age        23705 non-null  int64 
 1   ethnicity  23705 non-null  int64 
 2   gender     23705 non-null  int64 
 3   img_name   23705 non-null  object
 4   pixels     23705 non-null  object
dtypes: int64(3), object(2)
memory usage: 926.1+ KB

Data Preprocessing

We are concerned about the pixels column in the dataframe. But the values in that column are an object type data type. To convert the datatype of the column, we will define a function which adheres to our purpose at hand and then apply it to create a new column.

Python3




# creating a fuctio which will change the datatype to an array
def str_to_array(ob):
    return np.array(ob.split(' '), dtype='int')
  
# apply the function to pixels column and create a new column 'new_pixels'
df['new_pixels'] = df['pixels'].apply(str_to_array)


Check the input Face images

To visualize an Image, we are first generating a list of random indices value from the dataset and using that to plot a subplot of images. To plot the image we have to first reshape the data into a (48,48,1) shape.

Python3




# creating an empty 3x3 subplot
fig, ax = plt.subplots(3, 5, figsize=(12, 7))
ax = ax.ravel()
  
# generating a random list of integers
res = random.sample(range(0, df.shape[0]), 15)
  
# creating the subplot for random images and printing the corresponding gender and age
for i, id in enumerate(res):
    ax[i].imshow(df['new_pixels'].loc[id].reshape(48, 48))
    ax[i].set_title(f'Age:{df.age.loc[id]}, Gender:{df.gender.loc[id]}')
    ax[i].axis('off')
plt.savefig('image_visualization_subplot.png')


Output:

Age and Gender

Age and Gender Prediction using CNN

In this article, we will create an Age and Gender Prediction model using Keras Functional API, which will perform both Regression to predict the Age of the person and Classification to predict the Gender from face of the person.

Similar Reads

Age and Gender Prediction

Keras Functional API offers a more flexible and creative way to make models of higher complexity. One such Complexity arises when making a model that does more than one type of supervised prediction (Regression and Classification predictions). We will unravel a similar scenario to create a model that can perform Regression and Classification as well....

Model Building

...

Conclusion

...

Contact Us