Age, Gender and Race Prediction

Coursesteach
7 min readJun 2, 2024

--

Introduction

In the rapidly evolving world of artificial intelligence (AI), computer vision stands out as one of the most intriguing and impactful fields. Among its many applications, age, gender, and race prediction is particularly fascinating due to its potential in various sectors like marketing, security, and healthcare. This blog delves into the using of a computer vision project aimed at predicting these demographic attributes from images.

Sections

Applications
Import library
Load Dataset
Data Preprocessing
Age Prediction Model
Gender Prediction Model
Ethnicity Prediction
Conclusion

📚Applications

  1. Marketing: Understanding demographic details helps in targeted advertising and personalized content.
  2. Security: Enhancing facial recognition systems in surveillance for better identification.
  3. Healthcare: Assisting in demographic-based medical research and providing age or gender-specific health recommendation

📚Import library

# Importing Libraries
import numpy as np
import pandas as pd
import os
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator as imgen
from keras.models import load_model, Sequential
from keras.layers import Conv2D,MaxPooling2D,Dropout,Flatten,Dense,BatchNormalization
from keras.preprocessing import image
from keras.optimizers import Adam

from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,confusion_matrix,mean_absolute_error

ImageDataGenerator is a class that provides a variety of methods for augmenting image data, such as rotating, zooming, and flipping images. This is particularly useful for deep learning models as it helps to prevent overfitting by creating more diverse training data from your existing images.

📚Load Dataset

from google.colab import drive
drive.mount('/content/gdrive')
data = pd.read_csv("/content/gdrive/MyDrive/Datasets (1)/Age, Gender and Race Prediction/age_gender.csv")
data.head()

#shape
data.shape
data.info()

📚Data Preprocessing

# Getting Image data
def toPixels(pixels):
arr = np.array(pixels.split(),"float64")
arr = arr.reshape(48,48)
return arr
%%time
data["pixels"] = data["pixels"].apply(toPixels)

pixels = np.reshape(data["pixels"].to_list(), (data.shape[0],48,48,1))

pixels.shape

📚Age Prediction Model

Generating Datasets for Age Prediction

x_train_age,x_test_age, y_train_age,y_test_age = train_test_split(pixels,np.array(data["age"]),random_state = 42, test_size = 0.2)
x_train_age,x_val_age, y_train_age,y_val_age = train_test_split(x_train_age,y_train_age,random_state = 21, test_size = 0.15)
print(x_train_age.shape,y_train_age.shape, x_test_age.shape,x_val_age.shape)

Image data Generator.

traingen = imgen(rescale=1./255,
zoom_range=0.2,
shear_range=0.2,
horizontal_flip= True
)
valgen = imgen(rescale=1./255,
zoom_range=0.2,
shear_range=0.2,
horizontal_flip= True
)
testgen = imgen(rescale=1./255)
age_train_ds = traingen.flow(x_train_age,y_train_age,
batch_size = 32
)
age_val_ds = valgen.flow(x_val_age,y_val_age,
batch_size = 32
)
age_test_ds = testgen.flow(x_test_age,y_test_age,
batch_size = 32,
shuffle=False
)

#Visualizing one batch.
def showImagesAge(img,label):
plt.figure(figsize=[22,15])
for i in range(25):
plt.subplot(5,5,i+1)
plt.imshow(img[i])
plt.title("Age is {}".format(label[i]))
plt.axis('off')
plt.show()

X,Y = next(age_train_ds)
showImagesAge(X,Y)

Model

image_input = keras.Input(shape=(48,48,1))
l1 = Conv2D(32,(3,3), activation="relu")(image_input)
l2 = Conv2D(32,(3,3), activation="relu")(l1)
l3 = MaxPooling2D(pool_size=(2, 2))(l2)
l4 = Dropout(0.30)(l3)
l5 = Conv2D(64,(3,3), activation="relu")(l4)
l6 = Conv2D(128,(3,3), activation="relu")(l5)
#l7 = BatchNormalization()(l6)
l8 = Flatten()(l6)
l9 = Dense(256, activation= "relu")(l8)
image_output = Dense(1)(l9)
model_age = keras.Model(image_input, image_output)
model_age.summary()
# Compiling the model
model_age.compile(optimizer='adam', loss = 'mse', metrics=[keras.metrics.mean_absolute_error])

Defining callbacks.

my_calls = [keras.callbacks.EarlyStopping(monitor='val_mean_absolute_error',patience=3),
keras.callbacks.ModelCheckpoint("Model_age.h5",verbose=1,save_best_only=True)]

Training the model for age.

hist_age = model_age.fit(age_train_ds,epochs=35,validation_data=age_val_ds,callbacks=my_calls)

Test for age prediction

model_age.evaluate(age_test_ds,verbose=1)

Loss and MAE

plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
plt.plot(hist_age.epoch,hist_age.history['mean_absolute_error'],label = 'Training')
plt.plot(hist_age.epoch,hist_age.history['val_mean_absolute_error'],label = 'validation')
plt.title("Accuracy")
plt.legend()
plt.subplot(1,2,2)
plt.plot(hist_age.epoch,hist_age.history['loss'],label = 'Training')
plt.plot(hist_age.epoch,hist_age.history['val_loss'],label = 'validation')
plt.title("Loss")
plt.legend()
plt.show()

Verifying the Predictions

pred_Age = model_age.predict(age_test_ds, verbose=1)

Plotting predicted v/s actual ages

pred_age = []
for i in pred_Age:
pred_age.append(np.round(i[0]))

def plotAgePA(image,pred,actual):
plt.figure(figsize=[22,15])
for i in range(500,525):
plt.subplot(5,5,(i%25)+1)
plt.xticks([])
plt.yticks([])
plt.imshow(x_test_age[i])
plt.xlabel("Actual Age is {}".format(actual[i]))
plt.ylabel("Prediced is {}".format(pred[i]))
plt.show()

plotAgePA(x_test_age,pred_age,y_test_age)

📚Gender Prediction Model

Dataset for Gender Prediction

x_train_gen,x_test_gen, y_train_gen,y_test_gen = train_test_split(pixels,np.array(data["gender"]),random_state = 42, test_size = 0.2)
x_train_gen,x_val_gen, y_train_gen,y_val_gen = train_test_split(x_train_gen,y_train_gen,random_state = 21, test_size = 0.15)

print(x_train_gen.shape,y_train_gen.shape,x_val_gen.shape,y_val_gen.shape)

Data Generator

gender = ["Male","Female"]

gen_train_ds = traingen.flow(x_train_gen,y_train_gen,
batch_size = 32
)

gen_val_ds = valgen.flow(x_val_gen,y_val_gen,
batch_size = 32
)

gen_test_ds = testgen.flow(x_test_gen,y_test_gen,
batch_size = 32,
shuffle=False
)
#one batch
def showImagesGender(img,label):
plt.figure(figsize=[22,15])
for i in range(25):
plt.subplot(5,5,i+1)
plt.imshow(img[i])
plt.title("Gender is {}".format(gender[label[i]]))
plt.axis('off')
plt.show()

A,b = next(gen_train_ds)
showImagesGender(A,b)

Model


model_gender = Sequential([
Conv2D(32,(3,3), activation = "relu", input_shape = (48,48,1)),
MaxPooling2D(2,2),

Conv2D(32,(3,3), activation = "relu"),
MaxPooling2D(2,2),

Dropout(0.3),

Conv2D(64,(3,3), activation = "relu"),
MaxPooling2D(2,2),

#Conv2D(128,(3,3), activation = "relu"),
#MaxPooling2D(2,2),

Flatten(),

#Dense(256,activation = 'relu'),
#Dropout(0.5),

Dense(64,activation = 'relu'),
Dropout(0.5),


Dense(1,activation='sigmoid')
])

model_gender.summary()

compile the model

model_gender.compile(optimizer='adam',loss = "binary_crossentropy",metrics=['accuracy'])

Callbacks

my_calls_1 = [keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=3),
keras.callbacks.ModelCheckpoint("Model_Gender.h5",verbose=1,save_best_only=True)]

Train

hist_gender = model_gender.fit(gen_train_ds,epochs = 23, validation_data = gen_val_ds, callbacks = my_calls_1)

Test

model_gender.evaluate(gen_test_ds)

Loss and Accuracy

plt.figure(figsize=(15,6))

plt.subplot(1,2,1)
plt.plot(hist_gender.epoch,hist_gender.history['accuracy'],label = 'Training')
plt.plot(hist_gender.epoch,hist_gender.history['val_accuracy'],label = 'validation')

plt.title("Accuracy")
plt.legend()

plt.subplot(1,2,2)
plt.plot(hist_gender.epoch,hist_gender.history['loss'],label = 'Training')
plt.plot(hist_gender.epoch,hist_gender.history['val_loss'],label = 'validation')

plt.title("Loss")
plt.legend()
plt.show()

Predictions

pred_gender = model_gender.predict(gen_test_ds)
pred_gen = []
for i in pred_gender:
pred_gen.append(int(np.round(i[0])))

pred_gen[:5]

print(classification_report(pred_gen,y_test_gen))

sns.heatmap(confusion_matrix(pred_gen,y_test_gen),annot = True, fmt = 'd', cmap = "BuPu");
def testGender(image,pred,actual):
plt.figure(figsize=[22,15])
for i in range(500,525):
plt.subplot(5,5,(i%25)+1)
plt.xticks([])
plt.yticks([])
plt.imshow(image[i])
plt.xlabel("Actual Gender is {}".format(gender[actual[i]]))
plt.ylabel("Prediced is {}".format(gender[pred[i]]))
plt.show()

testGender(x_test_gen,pred_gen, y_test_gen)

📚 Ethnicity Prediction

Data for ethnicity prediction

x_train_et,x_test_et, y_train_et,y_test_et = train_test_split(pixels,np.array(data["ethnicity"]),random_state = 42, test_size = 0.2)
x_train_et,x_val_et, y_train_et,y_val_et = train_test_split(x_train_et,y_train_et,random_state = 21, test_size = 0.15)

print(x_train_et.shape,y_train_et.shape)

et_train_ds = traingen.flow(x_train_et,y_train_et,
batch_size = 32
)

et_val_ds = valgen.flow(x_val_et,y_val_et,
batch_size = 32
)
et_test_ds = testgen.flow(x_test_et,y_test_et,
batch_size = 32,
shuffle=False
)
#one batch
def showImagesEthnicity(img,label):
plt.figure(figsize=[22,15])
for i in range(25):
plt.subplot(5,5,i+1)
plt.imshow(img[i])
plt.title("Race is {}".format(label[i]))
plt.axis('off')
plt.show()

c,d = next(et_train_ds)
showImagesEthnicity(c,d)

Model


model_et = Sequential([
Conv2D(32,(3,3), activation = "relu", input_shape = (48,48,1)),
MaxPooling2D(2,2),

Conv2D(32,(3,3), activation = "relu"),
MaxPooling2D(2,2),

Dropout(0.3),

Conv2D(64,(3,3), activation = "relu"),
MaxPooling2D(2,2),

Conv2D(128,(3,3), activation = "relu"),
MaxPooling2D(2,2),

Flatten(),

Dense(256,activation = 'relu'),
Dropout(0.5),

#Dense(64,activation = 'relu'),
#Dropout(0.5),

Dense(5,activation='softmax')
])
model_et.summary()

Compile the model

model_et.compile(optimizer='adam',loss = "sparse_categorical_crossentropy",metrics=['accuracy'])

Callbacks

my_calls_2 = [keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=3),
keras.callbacks.ModelCheckpoint("Model_Ethnicity.h5",verbose=1,save_best_only=True)]

Train

hist_et = model_et.fit(et_train_ds,epochs=22,validation_data=et_val_ds,callbacks=my_calls_2)

Test

model_et.evaluate(et_test_ds)

Loss and Accuracy

plt.figure(figsize=(15,6))

plt.subplot(1,2,1)
plt.plot(hist_et.epoch,hist_et.history['accuracy'],label = 'Training')
plt.plot(hist_et.epoch,hist_et.history['val_accuracy'],label = 'validation')

plt.title("Accuracy")
plt.legend()

plt.subplot(1,2,2)
plt.plot(hist_et.epoch,hist_et.history['loss'],label = 'Training')
plt.plot(hist_et.epoch,hist_et.history['val_loss'],label = 'validation')

plt.title("Loss")
plt.legend()
plt.show()

Predictions

pred_et = model_et.predict(et_test_ds)

pred_et = [np.argmax(i) for i in pred_et]
pred_et[:5]

pd.DataFrame(pred_et).value_counts()

pd.DataFrame(y_test_et).value_counts()

print(classification_report(pred_et,y_test_et))

plt.figure(figsize=[12,7])
sns.heatmap(confusion_matrix(pred_et,y_test_et),annot=True,fmt='d',cmap="Blues");

Conclusion

Predicting age, gender, and race using computer vision is a challenging yet rewarding project. By meticulously collecting data, carefully designing and training models, and considering ethical implications, we can create systems that not only perform well but also serve society responsibly. The future of computer vision holds immense possibilities, and projects like these pave the way for more intelligent and inclusive technologies.

Please Follow and 👏 Clap for the story courses teach to see latest updates on this story

🚀 Elevate Your Data Skills with Coursesteach! 🚀

Ready to dive into Python, Machine Learning, Data Science, Statistics, Linear Algebra, Computer Vision, and Research? Coursesteach has you covered!

🔍 Python, 🤖 ML, 📊 Stats, ➕ Linear Algebra, 👁️‍🗨️ Computer Vision, 🔬 Research — all in one place!

Don’t Miss Out on This Exclusive Opportunity to Enhance Your Skill Set! Enroll Today 🌟 at

Machine Learning projects course

🔍 Explore Free world top University computer Vision ,NLP, Machine Learning , Deep Learning , Time Series and Python Projects, access insightful slides and source code, and tap into a wealth of free online websites, github repository related Machine Learning Projects. Connect with like-minded individuals on Reddit, Facebook, and beyond, and stay updated with our YouTube channel and GitHub repository. Don’t wait — enroll now and unleash your Machine Learning projects potential!”

Stay tuned for our upcoming articles because we reach end to end ,where we will explore specific topics related to Deep Learning in more detail!

Remember, learning is a continuous process. So keep learning and keep creating and Sharing with others!💻✌️

📚GitHub Repository

Ready to dive into data science and AI but unsure how to start? I’m here to help! Offering personalized research supervision and long-term mentoring. Let’s chat on Skype: themushtaq48 or email me at mushtaqmsit@gmail.com. Let’s kickstart your journey together!

Contribution: We would love your help in making coursesteach community even better! If you want to contribute in some courses , or if you have any suggestions for improvement in any coursesteach content, feel free to contact and follow.

Together, let’s make this the best AI learning Community! 🚀

👉WhatsApp

👉 Facebook

👉Github

👉LinkedIn

👉Youtube

👉Twitter

📚References

1-Age, Gender and Race Prediction

--

--