Solving the Challenge using Transfer Learning I’ve touched on this above and I’ll reiterate it here – we will be using the VGG16 pre-trained model trained on the ImageNet dataset.
Let’s look at the steps we will be following to train the model using transfer learning: First, we will load the weights of the pre-trained model – VGG16 in our case Then we will fine tune the model as per the problem at hand Next, we will use these pre-trained weights and extract features for our images Finally, we will train the fine tuned model using the extracted features So, let’s start by loading the weights of the model: View the code on Gist.
We will now fine tune the model.
We will not be training the layers of the VGG16 model and hence let’s freeze the weights of these layers: View the code on Gist.
Since we only have 2 classes to predict and VGG16 is trained on ImageNet which has 1000 classes, we need to update the final layer as per our problem: View the code on Gist.
Since we will be training only the last layer, I have set the requires_grad as True for the last layer.
Let’s set the training to GPU: View the code on Gist.
We’ll now use the model and extract features for both the training and validation images.
I will set the batch_size as 128 (again, you can increase or decrease this batch_size per your requirement): View the code on Gist.
Similarly, let’s extract features for our validation images: View the code on Gist.
Next, we will convert these data into torch format: View the code on Gist.
We also have to define the optimizer and the loss function for our model: View the code on Gist.
It’s time to train the model.
We will train it for 30 epochs with a batch_size set to 128: View the code on Gist.
Here is a summary of the model.
You can see that the loss has decreased and hence we can say that the model is improving.
Let’s validate this by looking at the training and validation accuracies: View the code on Gist.
We got an accuracy of ~ 84% on the training set.
Let’s now check the validation accuracy: View the code on Gist.
The validation accuracy of the model is also similar, i,e, 83%.
The training and validation accuracies are almost in sync and hence we can say that the model is generalized.
Here is the summary of our results: Model Training Accuracy Validation Accuracy CNN 81.
26% VGG16 83.
47% We can infer that the accuracies have improved by using the VGG16 pre-trained model as compared to the CNN model.
Got to love the art of transfer learning!. End Notes In this article, we learned how to use pre-trained models and transfer learning to solve an image classification problem.
We first understood what pre-trained models are and how to choose the right pre-trained model depending on the problem at hand.
Then we took a case study of classifying images of vehicles as emergency or non-emergency.
We solved this case study using a CNN model first and then we used the VGG16 pre-trained model to solve the same problem.
We found that using the VGG16 pre-trained model significantly improved the model performance and we got better results as compared to the CNN model.
I hope you now have a clear understanding of how to use transfer learning and the right pre-trained model to solve problems using PyTorch.
I encourage you to take other image classification problems and try to apply transfer learning to solve them.
This will help you to grasp the concept much more clearly.
As always, if you have some feedback or doubts related to this tutorial, feel free to post them in the comments section below and I will be happy to answer them.
You can also read this article on Analytics Vidhyas Android APP Share this:Click to share on LinkedIn (Opens in new window)Click to share on Facebook (Opens in new window)Click to share on Twitter (Opens in new window)Click to share on Pocket (Opens in new window)Click to share on Reddit (Opens in new window) Related Articles (adsbygoogle = window.
adsbygoogle || ).