top of page

Age-Gender-Detection Using UTKFace Dataset

Import Necessary Packages

import tensorflow as tf
import cv2
import numpy as np
from matplotlib import pyplot as plt
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Dense, MaxPool2D, Conv2D
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Activation, Add
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import Adam, Adagrad, Adadelta, Adamax, RMSprop

Load Dataset

fldr="./UTKFace"
import os
flies=os.listdir(fldr)
ages=[]
genders=[]
images=[]

for fle in flies:
    age=int(fle.split('_')[0])
    gender=int(fle.split('_')[1])
    total=fldr+'/'+fle
    print(total)
    image=cv2.imread(total)

    image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    image=cv2.resize(image,(48,48))
    images.append(image)

Output:

./UTKFace/100_0_0_20170112213500903.jpg.chip.jpg
./UTKFace/100_0_0_20170112215240346.jpg.chip.jpg
./UTKFace/100_1_0_20170110183726390.jpg.chip.jpg
./UTKFace/100_1_0_20170112213001988.jpg.chip.jpg
./UTKFace/100_1_0_20170112213303693.jpg.chip.jpg
./UTKFace/100_1_0_20170112215032192.jpg.chip.jpg
./UTKFace/100_1_0_20170117195420803.jpg.chip.jpg
./UTKFace/100_1_0_20170119212053665.jpg.chip.jpg
./UTKFace/100_1_2_20170105174847679.jpg.chip.jpg
./UTKFace/100_1_2_20170112213615815.jpg.chip.jpg
./UTKFace/100_1_2_20170112222336458.jpg.chip.jpg
./UTKFace/101_0_0_20170112213500903.jpg.chip.jpg
...
...
...
for fle in flies:
    age=int(fle.split('_')[0])
    gender=int(fle.split('_')[1])
    ages.append(age)
    genders.append(gender)
plt.imshow(images[87])

Output:










print(ages[87])
print(genders[87])
images_f=np.array(images)
ages_f=np.array(ages)
genders_f=np.array(genders)
np.save(fldr+'image.npy',images_f)
np.save(fldr+'ages.npy',ages_f)
np.save(fldr+'genders.npy',genders_f)


#Finding the no. of Male and Female Samples respectively

values, counts=np.unique(genders_f, return_counts=True)
print(counts)

output:

[12391 11317]



#Plotting the No. of Male and Female Samples

fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
gender=['Male','Female']
values=[12391,11317]
ax.bar(gender,values)
plt.show()

Output:












#Finding the no. of samples in each age

values, counts=np.unique(ages_f,return_counts=True)
print(counts)

output:

[1123  482  289  273  196  131  139  263  166  156   65  130   81  157
  177  247  158  262   98  284  346  395  426  859  734 2197  615  918
  570  724  350  664  143  409  880  483  293  325  266  526  132  266
  157  100  440  153  170  153  148  381  138  232  241  353  268  236
   97  271   82  293  161  125  103   50  259   77   94  100   56  147
   33   98   63   32  148   58   28   69   23  133   22   40   18   24
  155   35   10   34   33   82    2   13    5    9   17    9   11    2
    1    5    5    1    3    4]

#Plotting the samples age wise

#Converting Age counts to lists

val=values.tolist()
cnt=counts.tolist()

#Plotting the samples agewise

plt.plot(counts)
plt.xlabel=('Ages')
plt.ylabel=('Distribution')
plt.show()

output:










#Defining labels for Output

len(genders)

output:

23708
labels=[]
i=0
while i<len(ages):
    label=[]
    label.append(ages[i])
    label.append(genders[i])
    labels.append(label)
    i=i+1

#Creating another list of images from existing one

for better learning


images_f_2=images_f/255
images_f_2.shape

output:

(23708, 48, 48, 3)



labels_f=np.array(labels)

Splitting Data set into Train

from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test=train_test_split(images_f_2,labels_f,test_size=0.25)
Y_train[0:5]

output:

array([[68, 0], [ 2, 1], [61, 0], [34, 1], [30, 0]])


Splitting Y_test and Y_train further for better understanding

Y_train_2=[Y_train[:,1],Y_train[:,0]]
Y_test_2=[Y_test[:,1],Y_test[:,0]]
Y_train_2[0][0:5]

output:

array([0, 1, 0, 1, 0])


Y_train_2[1][0:5]

output:

array([68, 2, 61, 34, 30])



Model Evaluation:

from tensorflow.keras.layers import Conv2D, MaxPooling2D


If you Looking Complete Solution of above problem or Looking for other image processing assignment help then send your reqirement details at:



Here you get solution by top rated professionals and expert with an affordable price.




bottom of page