{'fraction_non_alphanumeric': 0.030973785600966988, 'fraction_numerical': 0.009934275137871118, 'mean_word_length': 4.661890504704876, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Few-shot Classification via Adaptive Attention

Training a neural network model that can quickly adapt to a new task is highly desirable yet challenging for few-shot learning problems. Recent few-shot learning methods mostly concentrate on developing various meta-learning strategies from two aspects, namely optimizing an initial model or learning a distance metric. In this work, we propose a novel few-shot learning method via optimizing and fast adapting the query sample representation based on very few reference samples. To be specific, we devise a simple and efficient meta-reweighting strategy to adapt the sample representations and generate soft attention to refine the representation such that the relevant features from the query and support samples can be extracted for a better few-shot classification. Such an adaptive attention model is also able to explain what the classification model is looking for as the evidence for classification to some extent. As demonstrated experimentally, the proposed model achieves state-of-the-art classification results on various benchmark few-shot classification and fine-grained recognition datasets.


Introduction
In recent years, few-shot learning problems [7,16,26,28,30] have attracted intensive attention, in which a model must be adapted to tackling unseen classes with only a few training examples. Humans are able to learn new concepts easily with only a few samples, but this could be highly difficult for a neural network model since it typically needs a lot of training data to extract meaningful features or otherwise it would suffer over-fitting.
To ensure a good generalization ability to novel classes, most few-shot learning methods train the model on a collection of tasks, each of which instantiates a few-shot learning problem and contains both query samples and a few support samples from different classes. Following the episodic learning paradigm [29], previous works train the model by optimizing the initial one to fast adapt to various tasks, e.g. MAML [5], or learning a good distance metric that can cluster samples of the same class together [26,30,28]. However, MAML-alike optimization based methods usually suffer high optimization complexity. On the other hand, though metric-based methods are simpler and generally perform better, they lack flexibility and cannot offer strong adaptation to new tasks. This is mainly because, the metric learning procedure does not exploit the relations between support and query samples directly, and the resulted model embeds the query images into the latent space without concerning the support samples.
When conducting few-shot classification, humans usually take several glimpses at the support and query samples, pay attention to some critical parts and make the decision [4,19]. Such a procedure is natural and can be leveraged in meta learning. Properly modeling such a procedure of attention can help fast localize critical parts and enhance performance of few-shot classification. Despite several attention mechanisms that have been shown to be effective in the fully-supervised setting [31,34], in few-shot classification, existing methods are still unable to obtain an adaptive attention map w.r.t. the few support samples accurately.
In this work, we develop an efficient meta-reweighting strategy to adapt the representations of query samples by incorporating the representations of support samples. Channel-level reweighting coefficients are first generated from the support features through a small neural network and then applied to query features by a channel-wise multiplication to emphasize some important feature maps. The resulted representation could therefore contain the information from both the query and support samples. Based on the adapted representations after reweighting, we further propose a new support-adaptive attention mechanism: an adaptive attention map is first generated over the query sample from the merged representation by an attention module, which indicates the spatial location of the object within the query belonging to the class of corresponding support samples. Then the attention map is used to refine the representation of the query by filtering out irrelevant information w.r.t. the support samples before classifying the query. With such an adaptive attention mechanism, a boosted few-shot learning ability can be achieved. Meanwhile, the whole model is light-weight and easy to optimize.
To summarize, our contributions are two-fold: (i) We develop a feature meta-reweighting strategy to extract and exploit the information of support examples, which is different from the traditional metric learning or optimization based meta-learning methods.
(ii) We propose an attention mechanism based on the meta-reweighting strategy to localize the region of interest in query samples w.r.t. support samples, which helps refine the classification performance and meanwhile explains the behavior of the model to some extent.

Few-Shot Learning
Generally, few-shot learning methods can be divided to meta-learning based methods and metric learning based ones. In addition, some recent works also adopt transfer leaning [27] and data augmentation [20,25] to achieve high accuracy in few-shot classification tasks, but they are based on very deep network [9,35] which involves more intensive pretraining and computation.
Meta-learning based methods In [5], a model-agnostic meta-learning (MAML) approach was proposed to make the model adapt to novel classes within some steps of optimization during testing with only a few samples. Instead of directly optimizing the model w.r.t. the target task, MAML-alike methods [17,23] tried to find a good initialization for the parameters of a model, which can then generalize to the new task by a few steps of gradient decent w.r.t. the initial parameters. Recent works [1] and [23] made some improvements on MAML to enhance model performance and stability. The work [11] introduced the idea of MAML to unsupervised meta-learning and also achieved impressive results. Another straightforward way for quickly acquiring new knowledge is reviewing the learned knowledge. In [8], a stable external memory was used for a neural network to help achieve boosted performance. The RNN memory-based methods like [24] adopted an LSTM to interact with the external memory. The work [22] trained an LSTM-based meta-model to serve as an optimizer to another learner model. Their meta model also generated a task-common initialization of parameters for the learner model in order to quickly adapt to the test environment.
Metric learning One simple metric learning based approach is embedding images into a feature space and constraining the feature vector of the same class to be close in Euclidean distance or cosine distance. The works [30,26] adopted this idea and further extended it to zero-shot learning and other fields. The work [14] enhanced the Prototypical Network by adding a reweighting module based on the task. In [28], the metric was replaced with a neural network to measure two embedded features, and decide the relations between them. This practice is much better than directly using Euclidean or cosine distance since the embedding space may be very complicated. Deep metric learning is also widely used in representation learning [10], which demonstrates good performance. Our method can also be seen as learning a deep metric that measures similarity between query and support objects. Note, if we can obtain the location of the object in the image, we are actually comparing two objects instead of two images, which is easier and more effective.

Attention Models
Recent work [7] proposed an attention-based classification weight generator which achieved impressive results. Our method, however, is mainly based on the spatial attention concerning the precise location of the object in the image. Actually, in the past decades, the spatial attention mechanism was studied by the vision community with the goal of both classification and object localization. It is very challenging to learn to localize objects with only image-level labels. [37] proposed to replace some high-level layers of a classification network by a global average pooling (GAP) [18] followed by a fully connected layer to generate discriminative class activation maps (CAM). [36] improved it by using a fully convolutional network (FCN) and proved that the attention maps can be obtained by directly selecting the feature maps of the last convolution layer. However, none of these methods is applicable to few-shot setting. We extend this idea to few-shot classification in this work. Our adaptive attention module is somewhat similar to the way of getting object localization maps in [36], but their work was proposed for localization tasks.

Method
We consider a K-way n-shot classification problem. In each few-shot classification task T , we are given a set of support samples S = {(x 1 , y 1 ), · · · , (x N , y N )} and a batch of query samples The samples in the support set S are from K categories, each of which has n labeled samples, i.e., N = n × K. The few-shot classification model is required to acquire knowledge from the support set and classify the query samples accurately.

Model Workflow
Feature Extractor

Adaptive Attention Module
Classifier Support Features Query Feature Figure 1: Illustration of our framework. The images first go 1 through a feature extractor. Then the attention module integrates the support and query feature to get adaptive attention maps. Then point-wisely multiplying the attention maps with the query feature gives refined query features for the classifier which outputs the similarity scores between the support and the query.
An overview of our proposed adaptive attention network is illustrated in Fig. 1. It consists of three components: a feature extractor F (·; θ F ) similar to most classical few-shot models [6,7,16,28,26,30], a classifier C(·; θ C ) and an adaptive attention module A(·; θ A ). The classifier and attention modules are both light-weight as explained later.
We first explain the testing phase of the proposed model by taking the K-way 1-shot classification tasks as an example. Both the query and support samples will go through the feature extractor F at first to produce query and support feature maps respectively. Then the adaptive attention module A generates K attention maps for the query feature conditioned on the K support features. Specifically, for a query feature and one of the K support features, the attention module generates an attention map roughly localizing the object belonging to the support class in the query image. This is called an adaptive attention map for the query conditioned on one specific support. If the query image does not contain any object belonging to the class of the support image, the attention map will only  Figure 2: Illustration of our attention module. It consists of a meta-weight generator to generate class-specific weights and a spatial attention generator to precisely locate the object in query data points w.r.t. support data points.
highlight some background. These K attention maps are then applied on the query feature to generate mask-pooled query features. These attended features are fed into the classifier C to give a score indicating the confidence of the query belonging to the class of a specific support and the query is classified based on the largest confidence scores. With such an adaptive attention mechanism, our model can provide better sample representation to ease the downstream classification procedure. We now proceed to explain details of the attention module.

Attention Module
Taking as input a query feature and a support feature, the adaptive attention module aims to directly generate spatial attention indicating the location of the object belonging to the support class in the query image. The key is how to adaptively generate the attention map over the query based on the information from the specific support. In this work, we propose a meta-reweighting based attention generation approach. The support features from the feature extractor are first applied to modulate the query features through channel-wise multiplication. Then the modulated query features with support information are used to generate the attention maps. In this way, the support information can be efficiently integrated into the attention generation process and the generated attention map is able to help select important regions from the query features conditioned on the support. Such an approach is beneficial to few-shot learning scenarios with only scarce support samples for training an attention model through fully supervised learning.
As shown in Fig. 2, the attention module generates the support-adaptive attention through a metaweight generator A R (·; θ R ) and a spatial attention generator A S (·; θ S ).
Meta-weight generator Humans recognize an object mainly based on some discriminative feature of the support examples in their brain. For example, when trying to find a cat in a room, the focus may be something hairy or with a tail. Motivated by this, we adopt a meta-reweighting strategy that merges the extracted features with a class-dependent weight vector. In particular, for a support point x s with label y s , and a query point x q denote their corresponding extracted features as f s = F (x s ; θ F ) and f q = F (x q ; θ F ). Then we apply the meta-weight generator A R on f s to get a weight vector w s = A R (f s ; θ R ) for class y s . The channel number of feature maps in f s equals the number of weights in w s . Then we can obtain a class-specific feature where ⊗ denotes the channel-wise multiplication. Note that [28] also merged the feature maps of the query and support points to get relation scores by straightly concatenating the two feature maps. Differently, our channel-wise multiplication preserves the spatial information of the query feature while emphasizing some feature maps that are crucial for classifying the support class. If x q does not belong to the class y s , the emphasized feature maps do not contain useful information for class y s , in which case the classifier predicts a lower score.
Spatial attention generator Our spatial attention generator A S (·; θ S ) consists of an FCN of 2 convolutional layers with one-channel final output map. This architecture is inspired by [36] which proposed an efficient way to get the region of interest for the deep convolutional network in weakly supervised setting. The main goal of this generator is to get important spatial regions in the query image w.r.t. the support sample. Taking as input the class-specific feature f ys q of a query, the output map M ys q = A S (f ys q ; θ S ) then serves as an attention map for the corresponding support on the input query feature map and provides attention at the spatial level that helps refine it.
We experimentaly justify the advantage of using such a reweighting strategy instead of directly concatenating the feature maps like Relation Network [28] in Section 4.3. Through visualization, one will see the output of FCN in the spatial attention generator indeed serves as an accurate attention map. Given a support feature f s and a query feature f q , we can get a precise attention map which indicates the location of the object that belongs to the corresponding support class in the query.

Classifier
With the adaptive attention module, we can obtain the attention map for each query sample. We then point-wisely multiply the attention map with the query feature map. In particular, given an extracted query feature f q , we can get a class-dependent attention map M ys q w.r.t. the support feature f s . The feature fed into the final classifier can then be refined by the attention map to where denotes the point-wise multiplication. In this refined feature map, the region of interest is highlighted by the class-dependent attention map provided by the attention module. The final classifier will concentrate more on this region, thus get better performance.
More specifically, our classifier consists of a pooling layer followed by several linear layers, taking as input the class-dependent attention maps M ys q and the query feature f q and outputting a single score s q ys = C(f q M ys q ) for each pair of query and attention map. The detailed architecture will be given in the following section. The final output is a single score representing the confidence of the query point belonging to the corresponding support class. We rely on the largest score for the final decision.
Take as input a support and a query feature denoted as f s and f q . The final output score will then be is the adaptive attention map. Though we can use activation function like relu to force the output to be positive, the function is still asymmetric. So we add its symmetric form to get as the final output score. The whole framework can then be reinterpreted as deep semimetric learning. Note that the attention map in the original form is trying to find the location of the support object in the query image, and this symmetric form can also be interpreted as finding the location of the query object in the support image, which helps improve performance when the support image contains not only objects from the support class but also other distractor classes.

Training
Following the episodic training scheme [30], we randomly select K classes from the training dataset with n samples each to form the support set N = n × K examples for training. A fraction of the rest data in those K classes serve as the query set. The model is then trained on these classification tasks.
We first explain the loss function for training the attention module to produce adaptive attention maps.
Given the support set {x 1,1 , · · · , x 1,n }, · · · , {x K,1 , · · · , x K,n } with label y 1 , · · · , y K for each class and x q be a query sample with label y q . As described previously, the meta-weight generator outputs a weight vector w i,j for each support sample x i,j . By averaging the weights of each class, we can get K class-specific weights {w 1 , · · · , w k }. By Eqn. (1), our model generates K class-specific features {f y1 q , · · · f y K q } using the class-specific weights. By feeding them to the spatial attention generator, we can get K attention maps M yi q , i = 1, · · · , K. Then by applying a global average pooling layer, we can get K scores a q i = M yi q indicating the confidence that query x q belongs to class i. Here M denotes the average of the attention map M . The cross-entropy loss for the attention module is We then explain the loss function for training the classifier and the whole model. Using Eqn. (4) Our model outputs K scores {s q i |s q i = 1 n n m=1 d(F (x i,m ), F (x q ))}, i = 1, · · · , K, each of which indicates the confidence of the query samples for being classified into the same category as x i . The cross entropy loss is The final loss for end-to-end training the whole model is then L total = L CE + L Att .

Setting
We conduct experiments to evaluate the effectiveness of our proposed model on five datasets, which include: Omniglot [15], miniImageNet [30], CUB-200 [33], Stanford Dogs [12], Stanford Cars [13]. Details of the datasets and splits are provided in the supplementary material. Among the above five datasets, CUB-200, Stanford Dogs and Stanford Cars are originally proposed for fine-grained recognition in the fully supervised setting, and recently applied to the more challenging few shot setting [16,32,2], namely fine-grained few shot classification. Unlike miniImageNet, the variances in these datasets are small, and each class contains only around 100 images or less. Therefore they are more challenging than the generic datasets Omniglot and miniImageNet since the model is forced to learn to find more accurate evidence to make a decision.
All experiments are conducted in 5-way 1-shot or 5-way 5-shot scenario. In testing, we randomly run our model for 600 episodes on each dataset except Omniglot on which we run 1,000 episodes for a fair comparison. In each episode, we randomly batch 15 query images per class to form a query set of 75 images. The classification accuracy is then calculated by averaging the accuracies of the 600 (1,000 for Omniglot) episodes.
We adopt the commonly used 4 layers convolutional network (Conv-64F) [26,30] which has 64 channels in each layer as our feature extractor. We also evaluate our method with the ResNet-256F backbone used in [7,16,21], which is deeper. In order to get a more precise attention map, we remove the last max pooling layer. As for the attention module, we use a spatial pyramid pooling (SPP) layer followed by three linear layers with 200, 200 and 64 channels respectively for the meta-weight generator and two convolutional layers with 64 and 1 channels followed by a global average pooling layer for the spatial attention generator. The amount of additional parameters for the attention module is rather small, less than 1/3 of that for the Conv-64F feature extractor. The classifier consists of an SPP layer followed by three linear layers with 200, 200 and 1 channels respectively.

Comparison with State-of-the-arts
Results on generic datasets Table 1 reports few-shot classification performance on miniImageNet.
For each task, we also use the labeled data to perform task-specific fine-tuning for one iteration and report the results referred as "our fine-tune" model. We can see that our model outperforms all the state-of-the-art models on miniImageNet for both the Conv-64F and ResNet-256F backbone. Noticeably, our model can offer the spatial location that it is looking at, as shown in Figure 3, which largely lifts the interpretability of the model on decision making. Our model also achieves comparable performance with state-of-the-arts on Omniglot. Due to space limit, we defer the detailed results to the supplementary material.
Results on fine-grained datasets We also apply our method on fine-grained classification datasets: CUB-200, Stanford Dogs and Stanford Cars, which are more challenging. A classical DNN tends to suffer severe overfitting on such small datasets. We conduct similar experiments as on miniImageNet for both 5-way 1-shot and 5-way 5-shot scenarios. As observed from Table 2, for 1-shot learning, our method outperforms the state-of-the-arts by a large margin. Such results clearly demonstrate the strong learning ability from very few shots of our proposed model and the benefits of the adaptive attention. In the 5-shot setting, our method performs similarly to the latest method [16].  Table 1: Few-shot classification accuracy (%) with 95% confidence intervals on miniImageNet, compared with SOTAs. For the ResNet-256F architecture we refer to [21].   Table 3.

Ablation Study
Reweighting vs. concatenating for attention map generation As shown in Table 3, replacing the meta-reweighting strategy with concatenation leads to no improvement. Note this is actually a variant of Relation Network [28]. The attention module can act as a classifier which achieves 51.31% accuracy in one-shot setting and adding an additional classifier does not improve the model. The generated map cannot indicate location of the object in the query image, as shown in Figure 4, confusing the classifier.  Table 2: Few-shot classification accuracy (%) with 95% confidence intervals on fine-grained classification datasets, compared with SOTAs. The backbones are all Conv-64F. Here * denotes results from our implementation.
attention map is straight-forward: it destroys the spatial information of the query image after concatenation. Instead, our meta-reweighting strategy preserves the spatial information and the generated attention map precisely locates the dog as shown in the second row of Figure 4.

Effect of test data augmentation
A key observation is that the weights generated by the meta weight generator are sometimes not so reliable since there exist distracting classes in the support images. We then apply a test data augmentation to enhance the weight generator by randomly cropping and flipping the input images and compute the mean of these vectors obtained from these augmented support data. As shown in Table 3, this stabilizes the weight generation stage and thus helps the classification.  Table 3: Ablation studies on miniImageNet. The backbones are Conv-64F. TA: test data augmentation; AC: the classifier component.
Necessity of the classifier component As aforementioned, the attention module can also perform the classification. To investigate its performance, we also remove the classifier and only use the attention module as a classifier. In this way, our attention module has to generate the spatial attention while performing recognition. From the results, one can observe the attention map can also be used for classification with satisfactory performance, demonstrating the adaptive attention maps indeed incorporate discriminative information from the support. But it does not perform very well for classification compared with the additional classifier which specially focuses on classification with the help of the attention module. This demonstrates the necessity of the additional classifier.

Conclusion
In this paper, we present an efficient framework for few shot classification. It uses a meta-reweighting strategy together with an attention module to find the location of the query item w.r.t. the support samples and uses this attention map to adapatively refine query representation. Experimental results demonstrate the power of our proposed method especially in the one-shot setting. It outperforms all state-of-the-art models by a large margin across all real-world image datasets. Also, our method is far simpler than the recently proposed meta-learning methods which need a lot more computation and careful training. The visualization results also show potential for help understand and improve few-shot classification models.

Results on Omniglot
Following [26], we train the model on the 60-way 1-shot and 60-way 5-shot setting, and provide result for 5-way 1-shot and 5-way 5-shot scenarios in Table 4.  Table 4: 5-way 1-shot and 5-way 5-shot classification accuracy(%) results with 95% confidence intervals on Omniglot compare with other state or the art method, the backbone used are Conv-64F.