Face Recognition Using Transfer Learning

Face Recognition Using Transfer Learning

The objective of the 4th task is: 

Problem Statement: Create a project using transfer learning solving various problems like Face Recognition, Image Classification, using existing Deep Learning models like VGG16, VGG19, ResNet, etc.


So, let us create a face recognition model.


The very first step is to collect dataset. And for collecting dataset, I have used the Haar Cascade Model. 

Here I have first imported the haar cascade model and then I have created a code in which the face in the image will be detected and will give the output as the cropped faces. 

No alt text provided for this image


Then, I have used the cv2 library in Python to click pictures using my webcam. 

Here, I have created two similar datasets. One is of my own face and other is of my friend’s face. A total of 100 images are clicked and then saved in the desired location.

No alt text provided for this image


And then, I took the dataset from the location and divided into training and validation set.

And thus, the dataset has been collected. Now, it is time to train the model:


At first, I have imported some of the important libraries and modules that are to be used in this task. And then, I have used a pre-created model called “MobileNet” in order to train the face recognition model through transfer learning.

No alt text provided for this image


Here, I have printed the layers of the model:

No alt text provided for this image


The next step is to get general details about the model such as: which layers are trainable, what is the output layer, etc. Then, I have frozen the layers that are not trainable. Then, the output layer is stored in a variable and then, it is flattened. 

No alt text provided for this image


In the next step, I have added the extra layers (my own layers). And then, again defined the input as well as the output layers, and then finally, compiled the model:

No alt text provided for this image


The next step is to import the dataset and train the model after fitting the dataset. 

No alt text provided for this image


You can notice that I have achieved 97+ accuracy here.

The next step is optional where I have saved this model. It is recommended to save the model in case of future use and load it whenever you need it.

No alt text provided for this image


In the next step, we have to load the testing dataset, and then do prediction on that test data. I have done the same by using some functions:

No alt text provided for this image


No alt text provided for this image


And thus, you can see that the model is able to recognize the face of my friend. 

No alt text provided for this image



Thus, I have successfully created a face recognition model. Thanks for reading!!

To view or add a comment, sign in

More articles by Shubham Mehta

Others also viewed

Explore content categories