Age Detection of Indian Actors using Deep Learning

Source: Deep Learning on Medium

##inspecting the distribution of classesplt.figure(figsize = (16,6))
plt.style.use("fivethirtyeight")
train_csv['Class'].value_counts(dropna = False).plot(kind = 'bar',grid = True)
plt.title("Distribtuion of class counts")
plt.xticks(rotation = 0)
bar graph for class count

From the above graph we can say that there are more actors of middle aged and least with old aged.

To classify we will use the below techniques:
– Basic Convolutional neural network (CNN)
– Resnet50

Preparing data for train-test

We will use ImageDataGenerator API from keras to split data, here images directly from folders. flow_from_directory method helps to do so.Also ImageDataGenerator helps in augmentation.
Image augmentation artificially creates training images through different ways of processing or combination of multiple processing, such as random rotation, shifts, shear and flips, etc.
Image augmentation improves model performance very well, as test images may not be same as in trained images.

batch_size=32# To handle image loading problem
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)training_set =
train_datagen.flow_from_directory('dataset/training',
target_size = (64, 64),
batch_size = batch_size,
class_mode = 'categorical')
validation_set = test_datagen.flow_from_directory('dataset/validation_set',
target_size = (64, 64),
batch_size = batch_size,
class_mode = 'categorical')
print(training_set.class_indices)

Basic CNN

In neural networks, Convolutional neural network (ConvNets or CNNs) is one of the main categories to do images recognition, images classifications. Objects detections, recognition faces etc., are some of the areas where CNNs are widely used.

Convolution is the first layer to extract features from an input image. Convolution preserves the relationship between pixels by learning image features using small squares of input data. It is a mathematical operation that takes two inputs such as image matrix and a filter or kernal

The Keras library in Python makes it pretty simple to build a CNN. We have used sequential model from keras as the sequential API allows us to create models layer-by-layer for most problems.

Also we have used max pooling, batch normalization, dropouts and a simple hack i.e leaky relu as activation.

model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape = (64, 64, 3)))
model.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3)))
model.add(LeakyReLU(alpha=0.3))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size = (2, 2)))
# Dropout
model.add(Dropout(0.4))
#Max Poolingmodel.add(Conv2D(32, (3, 3)))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(LeakyReLU(alpha=0.3))
model.add(BatchNormalization())
# Dropout
model.add(Dropout(0.3))
#Flatten
model.add(Flatten())
model.add(Dense(128))
model.add(LeakyReLU(alpha=0.3))
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Dense(3, activation = 'softmax'))

We have used accuracy as metrics and adam as optimizer. We run the model for 30 epochs.

# compiling the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
#fitting model
history = model.fit_generator(training_set,
steps_per_epoch = len(training_set),
epochs = 30,
validation_data = validation_set,
validation_steps = len(validation_set),
callbacks=[tensorboard1],
verbose=2)