Creating the Model
It is time to create the Learner. The Learner is the model that is going to learn from the dataset that is provided. It will then be able to predict the output (independent variable) when supplied with an image that was not a part of the training set. The Learner that is used here is called ‘Resnet18’. It is already pretrained, meaning the weights are tweaked such that the model should be able to reasonable predictions without further tweaks. This idea is called transfer learning.
Python3
learn = cnn_learner(dls, resnet18, metrics = error_rate) learn.fine_tune( 8 ) |
fine_tune(8) means the learning takes place for 8 epochs. This number can be played with. The tradeoff between accuracy and the computation power/time would be something to consider.
The model is now trained, and the result can be visualized by looking at the confusion matrix.
Python3
interp = ClassificationInterpretation.from_learner(learn) interp.plot_confusion_matrix() |
Ideally, only the diagonal elements of the confusion matrix should be non-zero. It can be seen that in the model’s predictions, there are some misclassifications present.
The images with the top losses can be seen. These are usually the images which the model predicts incorrectly with great certainty or correctly with less certainty.
Python3
interp.plot_top_losses( 5 , nrows = 5 ) |
Identify Members of BTS — An Image Classifier
BTS is an eminent K-Pop band comprising of 7 members. This article looks at an image classifier that would recognize the name of the band member from a picture. The image classifier would be built using fastai. It is a deep learning library that aims to democratize deep learning. It is built on top of PyTorch, and has plethora of models with optimized weights that are ready-to-use. The application would be hosted on Binder, and the end product would look like this: