Face Recognition using transfer learning
Hritick Goyal
Data Engineer @ Jio | Immediate Joiner l Serving Notice Period | Microsoft azure data engineer associate DP203
I am writing this article to explain all the things which can used for face recognition using transfer learning
Transfer learning: Basically it is the way of using weights of pretrained model to trained for more objects. For more details go to this link .
https://machinelearningmastery.com/transfer-learning-for-deep-learning/
The pretrained model which we used is very famous model called VGG16.
VGG16: VGG16 is a convolutional neural network model proposed by K. Simonyan and A. Zisserman . The model achieves 92.7% top-5 test accuracy in ImageNet, which is a dataset of over 14 million images belonging to 1000 classes
The dataset which is used is ImageNet.
Dataset: ImageNet is a dataset of over 15 million labeled high-resolution images belonging to roughly 22,000 categories.
Steps for creating face detection model
- First we have to import vgg16 form keras as from keras.applications import vgg16
- Now we have to download the weights form vgg16 and
model= vgg16.VGG16(weights = "imagenet")
here VGG16 is a function which will download the weights from imagenet dataset
and this model has now all the layers which is used in VGG16 with their weights.
3. To see all the layers present in vgg16 use this
model.layers
4. As we dont want to tarined all these layers as these were trained already so we can
freeze all layers by making their trainable features as false
for layer in model.layers :
layer.trainable=False
5. Now we have to remove last layer of VGG16 which is the output layer because
now we want to add our layers so there is no need of vgg16 output layer. we can remove
layer while downloading the weights so write include_top = 'false' in Vgg16 function .
6. To create our own fully connected layer first import Dense function from keras and
create layers as many as anyone want to use .
In this code we have imported Dense for creating FCl (fully connected layer)
and the optimizer used for fully connected layer is RMSprop we can use any other
optimizer also such as adam. here we used model.output to get last layer of VGG16
it is not the output layer. now we add our fcl to that last layer but not using add function
as we mostly do the reason is their we create model from scratch but here our model is
created already we just add some more layers to train them for our new object.
'Gloabalavergaepooling2d ' function was used for converting the weights to 1D .
and we used activation function 'relu '. The number of neuron units can vary to any
number . Last layer of any model should be output layer so we used number of units in
this layer is 1 because we want to do binary classification similarly the activation
function used is sigmoid . Now here Model function is used for attaching vgg16 layers
with our own created fcl.
6. Now we have to compiled the model.
here we used binary_crossentropy optimizer for finding loss as its used for binary
classification. and metrics is used to find the train accuracy of dataset.
7. Now we have to train the model as
Before train the model we can do augmentation on dataset if dataset is small using Image
generator function . In this fit function we passed validation dataset to find testing accuracy
of test dataset and as we know accuracy of model also depend on number of epochs so we
should give sufficient number of epochs .In this model we have achieved 98.95% training
accuracy and 60 % testing accuracy. we can imporve it either increasing epochs or using
other methods.
8. Now last step to predict image or face for that we used Image function of keras to load
the image .and size of image we used to predict should be same as to train then convert it
into numpy array and after that expand the dimension of image from 3D to 4D as CNN
take dataset as 4 D .
Output
there is two values present result so we have to see class_indices to check which value
associated to which person and write one logic to show the image to with person name
Finally our model predict right person's face.