FaceRecognisation Using Transfer Learning

Govind Bhardwaj
3 min readJul 16, 2020

Hello everyone this is my homework from MlOps class under @vimal daga sir

OBJECTIVE:-

We have to do face recognition using the transfer learning for model trainning…

  1. First load the pretrained model VGG16 having all weights and bias.

2. Freeze all the layers so that weight get fix and remove the topmost layer..

3. Using the VGG16 model as a basis, we now build a final classification layer on top to predict our defined classes. We then print a model summary, lisiting the number of parameters of the model. If you decide to “freeze” some of the layers, you will notice that the number of “Trainable parameters” below will be lower.

4. We then need to define some functions that read images from our folders and feeds them to the image classifier model. As a part of this we also add some basic image preprocessing, where the input images are scaled to have pixel values in the range [0,1], (from 0–255 in the original images).

5. just train our model and start predicting..

from keras_preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
import numpy as np
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(
‘testing/himanshu_set/train/’,
target_size=(224,224),
batch_size=6,
class_mode=’binary’)

test_set = test_datagen.flow_from_directory(
‘testing/himanshu_set/test/’,
target_size=(224,224),
batch_size=6,
class_mode=’binary’)

new_model.fit(
training_set,
steps_per_epoch= 2175,
epochs=2,
#callbacks = callbacks,
validation_data=test_set,
validation_steps=138)
new_model.save_weights(‘mymodel.h5’)

From the output shown above, we see that the loss decreases while the accuracy increases during the training process. Each time the validation accuracy reaches a new maximum value, the checkpoint file is saved (output: “mymodel.h5”. After the training has completed, we then load the checkpoint file which had the best validation accuracy during training…

--

--