Face Recognition using Transfer learning

Keshav Sharma
Analytics Vidhya
Published in
3 min readJul 11, 2020

Transfer Learning

Transfer learning makes use of the knowledge gained while solving one problem and applying it to a different but related problem.I’m using the Inception model as it is quite capable of extracting useful information from an image.

Transfer Learning

WHY TRANSFER LEARNING?

It’s well known that convolutional networks require significant amounts of data and resources to train.

It has become the norm for researchers and practitioners alike to use transfer learning and fine-tuning (that is, transferring the network weights trained on a previous project such as ImageNet to a new task).

You can take two approaches.

  • Transfer learning: You can take a CNN that has been pretrained on ImageNet, remove the last fully connected layer, and then treat the rest of the CNN as a feature extractor for the new data set. Once you extract the features for all images, you train a classifier for the new data set.
  • Fine-tuning: You can replace and retrain the classifier on top of the CNN and also fine-tune the weights of the pretrained network via backpropagation.

Importing Modules And Freezing Layers

First of all, we import the required modules and libraries. Keras is one of the libraries of python used in machine learning. So here we have to import different sub-modules and functions which are required in our program. As we are using VGG16 architecture so we have imported VGG16 function. Then we freeze the layers.

Now we will create a function called new in which we will define the new layers to be added to the existing model.

Now we will define the path where the training and validation data are present.

train_generator will take the input from the directories and resize them according to VGG. Similarly, validation_generator will do.

Here we are using PMSprop as the optimizer.

we will use Model Checkpoint to save the model. Only the best model will be selected according to value loss.

Then we compile and save the model.

Testing the Model

Now we are at the final step i.e. testing our model. On this step, we provide those images to our model which are neither provided in training set nor in the test set, so that we can check that is our model is predicting right or wrong. So for this first, we’ll import required modules and after that, we load the model that we trained previously. And finally, we predict using predict() function and decode its output.

--

--