Juggling between coding languages? Let our Code Converter help. Your one-stop solution for language conversion. Start now!
In the real world, it is rare to train a Convolutional Neural Network (CNN) from scratch, as it is hard to collect a massive dataset to get better performance. Instead, it is common to use a pre-trained network on a large dataset and tune it for your classification problem; this process is called Transfer Learning.
It is a machine learning method where a model is trained on a task that can be trained (or tuned) for another task; it is very popular nowadays, especially in computer vision and natural language processing problems. Transfer learning is very handy, given the enormous resources required to train deep learning models. Here are the most important benefits of transfer learning:
For these reasons, it is better to use transfer learning for image classification problems instead of creating your model and training from scratch, models such as ResNet, InceptionV3, Xception, and MobileNet are trained on a massive dataset called ImageNet which contains more than 14 million images that classify 1000 different objects.
Related: How to Perform YOLO Object Detection using OpenCV and PyTorch in Python.
We gonna be using the flower photos dataset, which consists of 5 types of flowers (daisy, dandelion, roses, sunflowers and tulips).
After you have everything installed by the following command:
Open up a new Python file and import the necessary modules:
The dataset comes with inconsistent image sizes, as a result, we gonna need to resize all the images to a shape that is acceptable by MobileNet (the model that we gonna use):
Let's load the dataset:
The above function downloads and extracts the dataset, and then use the ImageDataGenerator Keras utility class to wrap the dataset in a Python generator (so the images only loads to memory by batches, not in one shot).
After that, we scale and resize the images to a fixed shape and then split the dataset by 80% for training and 20% for validation.
I also encourage you to change this function to use tf.data
API instead, the dataset is already in Tensorflow datasets and you can load it as we did in this tutorial.
Building a real-time automatic number plate recognition system using YOLO and OpenCV library in Python
Download EBookWe are going to use the MobileNetV2 model, it is not a very heavy model but does a good job in the training and testing process.
As mentioned earlier, this model is trained to classify different 1000 objects, we need a way to tune this model so it can be suitable for just our flower classification. As a result, we are going to remove that last fully connected layer, and add our own final layer that consists of 5 units with softmax activation function:
The above function will first download the model weights (if not available) and then remove the last layer.
After that, we freeze the last layers, that's because it is pre-trained, we don't wanna modify these weights. However, it is a good practice to retrain the last convolutional layer as this dataset is quite similar to the original ImageNet dataset, so we won't ruin the weights (that much).
Finally, we construct our own dense layer that consists of five neurons and connect it to the last layer of the MobileNetV2 model. The following figure demonstrates the architecture:
Note that you can use the TensorFlow hub to load this model very easily, check this link to use their code snippet for creating the model.
Let's use the above two functions to start training:
Nothing fancy here, loading the data, constructing the model, and then using some callbacks for tracking and saving the best models.
As soon as you execute the script, the training process begins, you'll notice that not all weights are being trained:
It'll take several minutes depending on your hardware.
I used tensorboard to experiment a little bit, for example, I tried freezing all the weights except for the last classification layer, decreasing the optimizer learning rate, used some image flipping, zooming, and general augmentation, here is a screenshot:
MobileNetV2
was the model I froze all its weights (except for the last 5 unit dense layer of course).MobileNetV2_augmentation
uses some image augmentation.MobileNetV2_finetune_last5
the model we're using right now, which does not freeze the last 4 layers of the MobileNetV2 model.MobileNetV2_finetune_last5_less_lr
was dominant for almost 86% accuracy, that's because once you don't freeze the trained weights, you need to decrease the learning rate so you can slowly adjust the weights to your dataset. This was an Adam optimizer with a 0.0005 learning rate.Note: to modify the learning rate, you can import Adam
optimizer from keras.optimizers package, and then compile the model with optimizer=Adam(lr=0.0005) parameter.
Related: Mastering YOLO: Build an Automatic Number Plate Recognition System with OpenCV in Python.
Now to evaluate our model, we need to load the optimal weights via the model.load_weights()
method, you need to choose the weights that has the least loss value, in my case, it's 0.63 loss:
Make sure to use the optimal weights, the one which has the lower loss and higher accuracy.
Output:
Okay, let's visualize a little bit, we are going to plot a complete batch of images with its corresponding predicted and correct labels:
Once you run it, you'll get something like this:
Awesome! As you can see, out of 30 images, 25 were correctly predicted. That's a good result, though, as some flower images are slightly ambiguous.
Alright, that's it. In this tutorial, you discovered how to use transfer learning to quickly develop and use state-of-the-art models using Tensorflow and Keras in Python.
I highly encourage you to use other models that were mentioned above, try to fine-tune them as well, good luck!
Even though in the real world it's not suggested to train image classifiers models from scratch (except for different types of images such as human skins, etc.), I have a tutorial that does that, check it out: How to Make an Image Classifier in Python using Keras.
Also, if you're interested in medicine, we have a tutorial in which we used Transfer learning on skin cancer detection using Tensorflow!
Happy Training ♥
Take the stress out of learning Python. Meet our Python Code Assistant – your new coding buddy. Give it a whirl!
View Full Code Build My Python Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!