Few Shot Learning — A Case Study (2)
In the previous blog, we looked into the fact why Few Shot Learning is essential and what are the applications of it. In this article, I will be explaining the Relation Network for Few-Shot Classification (especially for image classification) in the simplest way possible. Moreover, I will be analyzing the Relation Network in terms of:
- Effectiveness of different architectures such as Residual and Inception Networks
- Effects of transfer learning via using pre-trained classifier on ImageNet dataset
Moreover, effectiveness will be evaluated on the accuracy, time required for training, and the number of required training parameters.
Please watch the GitHub repository to check out the implementations and keep updated with further experiments.
Introduction to Few-Shot Classification
In few shot classification, our objective is to design a method which can identify any object images by analyzing few sample images of the same class. Let’s the take one example to understand this. Suppose Bob has a client project to design a 5 class classifier, where 5 classes can be anything and these 5 classes can even change with time. As discussed in previous blog, collecting the huge amount of data is very tedious task. Hence, in such cases, Bob will rely upon few shot classification methods where his client can give few set of example images for each classes and after that his system can perform classification young these examples with or without the need of additional training.
In general, in few shot classification four terminologies (N way, K shot, support set, and query set) are used.
- N way: It means that there will be total N classes which we will be using for training/testing, like 5 classes in above example.
- K shot: Here, K means we have only K example images available for each classes during training/testing.
- Support set: It represents a collection of all available K examples images from each classes. Therefore, in support set we have total N*K images.
- Query set: This set will have all the images for which we want to predict the respective classes.
At this point, someone new to this concept will have doubt regarding the need of support and query set. So, let’s understand it intuitively. Whenever humans sees any object for the first time, we get the rough idea about that object. Now, in future if we see the same object second time then we will compare it with the image stored in memory from the when we see it for the first time. This applied to all of our surroundings things whether we see, read, or hear. Similarly, to recognise new images from query set, we will provide our model a set of examples i.e., support set to compare.
And this is the basic concept behind Relation Network as well. In next sections, I will be giving the rough idea behind Relation Network and I will be performing different experiments on 102-flower dataset.
About Relation Network
The Core idea behind Relation Network is to learn the generalized image representations for each classes using support set such that we can compare lower dimensional representation of query images with each of the class representations. And based on this comparison decide the class of each query images. Relation Network has two modules which allows us to perform above two tasks:
- Embedding module: This module will extract the required underlying representations from each input images irrespective of the their classes.
- Relation Module: This module will score the relation of embedding of query image with each class embedding.
Training/Testing procedure:
We can define the whole procedure in just 5 steps.
- Use the support set and get underlying representations of each images using embedding module.
- Take the average of between each class images and get the single underlying representation for each class.
- Then get the embedding for each query images and concatenate them with each class’ embedding.
- Use the relation module to get the scores. And class with highest score will be the label of respective query image.
- [Only during training] Use MSE loss functions to train both (embedding + relation) modules.
Few things to know during the training is that we will use only images from the set of selective class, and during the testing, we will be using images from unseen classes. For example, from the 102-flower dataset, we will use 50% classes for training, and rest will be used for validation and testing. Moreover, in each episode, we will randomly select 5 classes to create the support and query set and follow the above 5 steps.
That is all need to know about the implementation point of view. Although the whole process is simple and easy to understand, I’ll recommend reading the published research paper, Learning to Compare: Relation Network for Few-Shot Learning, for better understanding.
Results and Analysis
The initial experiment was performed for 5 way and 1/3/5/10 shot classification problem. Also, the same architecture described in the paper was used for both the modules. The graph of validation accuracy after each epoch is shown below.
From the above figure, we can observe that for 1 shot classification, we are getting almost 50 % accuracy, which is pretty good as we are only using a single image for comparison. Furthermore, we can observe that by increasing K from 1 to 10 we are getting more accurate.
One can ask what type of architectures will help increase performance. Such as Inception and ResNet based architectures have shown excellent performance for classification tasks. So, to extend this analysis, I use Inception and ResNet in the embedding module. The following figure shows the results of this modification for 1/5 shot classification.
Here, we can observe that Inception based model performs quite well and shows an improvement of ~10%. Moreover, the amount of increase in accuracy decreases as K increasing. However, we can observe that ResNet based architecture performs poorly compared to the baseline. The reason behind the better performance of Inception is that it can extract the features with different sparsity in the single-layer because different sized kernels are used in each layer to extract sparse features.
Furthermore, one with experience of transfer learning strategies might doubt whether it can improve the results or at least get similar performance easily in lesser time. The above figure also shows the results of this experiment, where pre-trained DenseNet161 is used. Here, we can observe that the transfer learning-based model performs very poorly at the beginning and takes too much time to get good enough accuracy irrespective of the value of K.
Moreover, the below table shows computational complexity and the required time for training the models. After observing every detail, we can say that the Inception based Relation Network is always a better choice.
Conclusion
Following are the few conclusion points to keep in mind:
Relation Network mainly relies upon embedding module to extract the relevant underlying representations for each image.
Inception based embedding module performs good because of it’s ability extract sparse features.
Transfer learning based embedding module does perform good enough when time constraint is applied because these models are trained for classification purpose only instead of extracting required lower dimensional representations of images for few shot classification.
Implementations regarding all of above experiments alongside the different result plots are provided in GitHub repository.
Next week I will be implementing same Relation Network for text classification task. Moreover, I will be analyzing different strategies to learn the required embedding for the same. Initial results using BERT based embedding and it’s implementation can be found in same GitHub repository for the early reference.
So stay tuned! Subscribe here to get notification of upcoming new articles to stay put with current research trends.
References:
- Sung, Flood, Yongxin Yang, Li Zhang, Tao Xiang, Philip HS Torr, and Timothy M. Hospedales. “Learning to compare: Relation network for few-shot learning.” In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pp. 1199–1208. 2018.
- Szegedy, Christian, Vincent Vanhoucke, Sergey Ioffe, Jon Shlens, and Zbigniew Wojna. “Rethinking the inception architecture for computer vision.” In Proceedings of the IEEE conference on computer vision and pattern recognition, pp. 2818–2826. 2016.
- He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. “Deep residual learning for image recognition.” In Proceedings of the IEEE conference on computer vision and pattern recognition, pp. 770–778. 2016.