Computer Science
Computer Science-2021.gz:143148
Computer Science
4b3b964d697bc87fe6feca35a61f6cddb570b70e
Trainable Class Prototypes for Few-Shot Learning

Metric learning is a widely used method for few shot learning in which the quality of prototypes plays a key role in the algorithm. In this paper we propose the trainable prototypes for distance measure instead of the artificial ones within the meta-training and task-training framework. Also to avoid the disadvantages that the episodic meta-training brought, we adopt non-episodic meta-training based on self-supervised learning. Overall we solve the few-shot tasks in two phases: meta-training a transferable feature extractor via self-supervised learning and training the prototypes for metric classification. In addition, the simple attention mechanism is used in both meta-training and task-training. Our method achieves state-of-the-art performance in a variety of established few-shot tasks on the standard few-shot visual classification dataset, with about 20% increase compared to the available unsupervised few-shot learning methods.

Abstract-Metric learning is a widely used method for few shot learning in which the quality of prototypes plays a key role in the algorithm. In this paper we propose the trainable prototypes for distance measure instead of the artificial ones within the meta-training and task-training framework. Also to avoid the disadvantages that the episodic meta-training brought, we adopt non-episodic meta-training based on self-supervised learning. Overall we solve the few-shot tasks in two phases: meta-training a transferable feature extractor via self-supervised learning and training the prototypes for metric classification. In addition, the simple attention mechanism is used in both meta-training and task-training. Our method achieves state-ofthe-art performance in a variety of established few-shot tasks on the standard few-shot visual classification dataset, with about 20% increase compared to the available unsupervised few-shot learning methods.

I. INTRODUCTION
In recent years deep learning has made major advances in computer vision areas such as image recognition, video object detection and tracking. A deep neural network needs a large amount of labeled data to fit its parameters whereas it is laborious to label so many examples by human annotators. Thus the problem of learning with few labeled samples called few-shot learning has been paid more and more attention. Fewshot learning is described as a classification task set in N -way and k -shot, which means to distinguish N categories, each of which has k (quite small) labeled samples. The model predict classes for new examples only depending on k labeled data. The annotated data is called the support set, and the new data belonging to the N categories is called query set.
People have proposed varieties of few-shot methods, all of which rely on meta-training assisted with base classes. The universal approach is to use the base classes to construct fake few-shot tasks for training the network first, with the purpose of enabling the network an ability to accomplish real fewshot tasks through simulating the process of carrying out the fake tasks. This is called the meta-training stage with tasks as samples. Next, use the trained network to complete real few-shot tasks of novel classes, and calculate the classification accuracy on the query set in the tasks to evaluate the algorithm, which is usually called the meta-testing. The whole procedure is shown in Fig.1.
Few-shot learning algorithms could be classified into three categories. The first [1]- [4] is based on metric learning, which consists of three steps of feature extraction, distance measure, Fig. 1. The universal method used in supervised few-shot learning, which consists of meta-training and meta-testing. In the meta-training, the training sample is actually a mimic few-shot task comprised of some labeled data chosen from base classes. And in the meta-testing the model will solve a real task with few labeled data and an unlabeled query set chosen from novel classes. We show a model trained for solving 3-way 1-shot tasks in this figure. and prediction, relying on effective metric design and reducing the cross entropy loss in meta training to improve classification accuracy. The second are the teacher-student network based methods including [5]- [8]. The teacher network guides the student network to solve the few-shot tasks in terms of parameter initialization, parameter update and other aspects. The algorithm enables the teacher network to obtain the ability to instruct the student network via meta-training. The third category such as [9] and [10] is based on the transduction, which propagates the label of the support data to the queries through a specific graph, thereby obtaining the predicted class of the query set. The algorithm optimizes the accuracy of propagation label through meta-training.
The meta-training determines the model's performance in the few-shot learning algorithm. However it brings two obvious drawbacks. First, the meta-training requires a large number of labeled auxiliary examples (base classes). Those algorithms can not work without adequate labeled samples. Second, the meta-training phase uses tasks as training samples. Therefore, a task type decided by the values of N and k needs to be certain before meta-training to ensure that the number of images contained by each mimic few shot task (i.e. a metatraining sample) is consistent during training. The meta-trained network can only be used to solve few-shot tasks with the same type as the meta-training samples, and it performs worse in other types of tasks. However, in reality we need to solve various types of few-shot tasks, and it is unreasonable to metatrain the network from scratch in order to solve a few shot task with new type.
In order to solve these two problems, we solve the few shot classification based on self-supervised learning according to [11]. Specifically, our method called TrainPro-FSL abandons the episodic meta-training phase, which takes the fake fewshot tasks as samples, and uses instead two new phases: the non-episodic meta-training via self-supervised learning directly using a single image as a training sample, and the task-training to solve a real few shot task. In the first phase, a discriminative self-supervised learning method is used to obtain a feature extractor with good generalization ability using unlabeled images. In the second phase, we use metric learning to solve real few-shot tasks. The meta-trained feature extractor is used to extract features from all the images in the current task, and SGC is carried out, based on a specific graph defined by the current task as the feature aggregation method described in [11]. We use the aggregated support set features to obtain the class prototypes which are trained via a simple neural network. Finally we use the class prototypes to implement distance measure and further classify the query images.
Furthermore, we demonstrate that the attention mechanism are helpful for both meta-training and task-training. We add attention structure to the neural network in the meta-training phase. In the task-training phase, an attention mask that does not increase network parameters is introduced to correct the query image features. Both of these methods can improve the classification accuracy of our algorithm.
Our key contributions can be summarized as follows: • We propose TrainPro-FSL, a unsupervised few-shot learning algorithm with trainable prototypes. By adopting the methodology of self-supervised learning, the two problems intrinsic in the existing episodic meta-training paradigm are solved simultaneously. Thus our method does not require a large number of labeled samples for training. In addition, the meta-trained model can carry out different types of real few-shot tasks. • We propose to use trainable prototypes to implement metric classification, which is better than the manual design in [12]. • We propose to use a simple attentive mechanism in the metric learning to further correct the feature of examples in the query set. • Adequate experiments demonstrate that our method reaches state-of-the-art accuracy on miniImageNet , a standardized benchmark in few-shot learning.
The paper is organized as follows. In II, we introduce the related works. Our methodology is described in III. In IV, experimental results on the standard vision dataset are shown in comparison with the proposed works. Finally, a conclusion is drawn in V.

II. RELATED WORKS
In this section we aim to show the metric learning used in few-shot learning algorithms proposed in previous years. In addition, we introduce some unsupervised few-shot learning methods presented recently and popular self-supervised algorithms.

A. Metric Learning
The core of metric learning is to extract features from the support set and query set, then obtain the class prototypes using the support set, and predict classes of queries via the nearest neighbor algorithm and attention mechanism. Therefore the quality of class prototypes obtained in algorithm are vital for the performance of the metric learning. Through episodic meta-training, a metric based method obtains a feature extractor that facilitates completing the classification task based on distance measurement. Matching Networks [1] used LSTM to extract full context embeddings from images and applied attention mechanism to classify. Prototypical Networks [2] proposed to use Euclidean distance to better measure the similarity between features, and use prototypes of each class to classify queries. Relation Network [3] used a neural network to replace the traditional distance metric, and directly output the queries' categories via an end-to-end network. DC-IMP [13] introduced dense classification and leverage implanting to bring metric learning the task dependency.

B. Unsupervised Few Shot Learning
The base classes in unsupervised methods has no labels. Some existing methods use unsupervised learning or data enhancement methods to leverage these unlabeled base classes to artificially construct fake support set and query set for meta training. They are able to combine with the few-shot learning methods as mentioned above (such as MAML [5] and Prototypical Net [2]) to fulfil few-shot tasks. UFLST [14] and CACTU [15] use clustering to make pseudo-labels for unlabeled examples, then use the pseudo-labeled data as ordinary labeled data to construct fake few-shot tasks to complete meta-training. AAL [16] and UMTRA [17] took each instance as one class and randomly sample multiple examples to construct a fake support set, then generate a corresponding query set according to the support set by data augmentation techniques. ULDA [18] developed a new simple data augmentation method to enhance the difference between the support set distribution and query set distribution when constructing the fake few-shot tasks for meta-training.

C. Self-Supervised Learning
The popular self-supervised algorithms can be divided into three categories, namely contrastive self-supervised learning, generative self-supervised learning and discriminative selfsupervised learning. The core of contrastive self-supervised learning is contrastive learning. The main idea of contrastive learning is to make the features of positive sample pairs close to each other in the feature space, and keep the features of negative sample pairs away from each other, such as BYOL [19], SimCLR [20], and their performance can even be comparable to supervised algorithms. Generative self-supervised learning algorithms such as image colorization [21] and VQ-VAE [22], usually take the original image as the groundtruth, train the network and make the network output close to the groundtruth. In generative self-supervised learning, images are generated at the pixel level, so the features extracted by the network are partial to image details, rather than semantic features.
Discriminant self-supervised learning can train the classification algorithm without the category label. The category here is set by the algorithm itself, and the label of the training sample is automatically generated according to the algorithm setting. In the discriminative self-supervised algorithm, the design of pretext tasks is diverse. Commonly used algorithms include jigsaw puzzle [23], rotation prediction [24], and deep clustering [25].

III. METHODOLOGY
The notations and problem formulation of self-supervised few-shot learning are introduced in III-A, and our paradigm is presented in III-B. Finally, the loss design for trainable prototypes and attention mechanism are described in III-C and III-D respectively.

A. Problem Formulation
Given two datasets, namely D base and D task with disjoint classes. D base consists of a large number of unlabeled examples from the base classes. D task has a small number of labeled examples called the support set D s , along with some unlabeled ones called the query set D q , all from the new classes. They stand for the total data in a few-shot learning task. The number of classes in the novel dataset D task , the number of support samples and the number of query inputs for each of these classes are denoted N , k and q respectively. So there are totally N × (k + q) examples in a few-shot learning task. Our aim is to predict the classes of the query set of D task . Different from the previous works like [8], our D base does not have any labels. So we train the classification network with only a few labeled examples namely D s in a real sense.

B. Proposed Paradigm of Solution
We first train a backbone deep neural network able to extract useful and compact features from inputs, which will be used as a generic feature extractor. In this non-episodic meta-training phase, we train the network with D base = [26], a kind of discrimitive self-supervised learning method, which promises a transferable feature extractor. Thus we obtain the extractor f ϕ : R w×h×3 → R e .
We then use f ϕ to obtain the features of the total data in D task (both D s and D q ) namely f ϕ (D task ) = {f ϕ (x)|x ∈ D task }. Then we step into the second phase namely the task-training phase. First we build a nearest neighbor graph using the cosine similarity according to [27] : The base graph denoted G task (V, E) uses f ϕ (D task ) to construct vertices. In details, its vertices matrix V ∈ R [N ×(k+q)]×e is the stacked representations of support set and query set i.e. each vertex represents an image's feature. We make the values of graph edges represent the similarity between vertices-that is, similar vertices have larger adjacency values. To get the adjacency matrix E ∈ R [N ×(k+q)]×[N ×(k+q)] , we first define a similarity matrix S with the same dimension computed as follows: where V i,: denotes the i -th row in V . Then we just save the m largest values on each row and on the corresponding column in S to obtain a more sparse matrix helpful to reduce the interference. Finally, we normalize the resulting matrix to get the adjacency matrix: where D is the degree diagonal matrix computed by D i,i = j S i,j . We can consider E as the Laplacian matrix in GCN [28] used to aggregate information among vertices.
We aggregate features for each vertex via the graph structure to get V new according to [11]. Then we use the metric learning to classify the query images based on V new . In metric learning, the quality of class prototypes has a great influence on classification accuracy. Therefore, we abandon the traditional method of artificially constructing class prototypes and use neural networks to obtain good prototypes via training to implement the metric classification. In order to improve the performance, we adopt the attention mechanism in training phase in the meanwhile.
Our paradigm is illustrated in Fig.2. In general TrainPro-FSL has two phases: (1) Meta-training phase: training a generic feature extractor via ODC algorithm. (2) Task-training phase: training the prototypes using the support set data after the feature aggregation through graph. Once the latter is finished, the performance of this model is evaluated by measuring the distance between features of D q and prototypes. The process of task-training phase is formalized in Algorithm 1.
The details of the two phases are provided in the following, first the meta-training phase then the task-training phase.
Meta-training phase: We follow the methodology called ODC, an effective discrimitive self-supervised learning method, proposed in [29]. The ODC is an online update deep clustering algorithm, which improves the DeepCluster [25] in the way of pseudo-label update. It discards the clustering after each epoch of training, and chooses to update the image features and pseudo-labels at the same time during the training process. ODC adds sample memory and centroids memory to DeepCluster to achieve the online update of pseudo-labels.
In a training process of ODC, there are three main steps: update network parameters, update sample memory and update centroids memory. During training we adopt the simple cross Algorithm 1: The process of task-training phase with prototypes training.
Input: A N -way k -shot task with the dataset D task = {D s , D q }; The meta-trained feature extractor f ϕ Output: Trained prototypes 1 Obtain features of all inputs including labeled and unlabeled ones, f ϕ (D task ); 2 Build the graph G task (V, E) based on f ϕ (D task ); 3 Aggregate vertices features V of the graph to get V new ; 4 randomly initialize θ; Classifier training; 5 Use manifold augmentation to extend labeled data in semantic level according to [11], and get the augmented feature set V new aug ; 6 Train Cls θ using V new aug and cross entropy loss to obtain Cls θ * ; Prototypes training; 7 Initialization the prototypes proto 0 randomly; 8 Use Cls θ * to calculate the loss of the prototypes classification and the metric classification of the support set according to Eq. (8); 9 Update proto 0 based on the loss function and finally obtain proto * ; 10 Return proto * ; entropy loss as the loss function. Through the self-supervised learning we obtain a feature embedding, which can get features with semantic information.
Task-training phase: In this phase we aim to obtain the well trained prototypes for the distance measure. Therefore we need to train a classifier firstly as the base neural network to train the prototypes. Specifically we fix the meta-trained parameters in the backbone and train a task-dependent classifier Cls θ on the transferred representations of the fewshot task's dataset namely D task . Before training a linear classifier with D s having few labeled examples, a method similar to simplified graph convolution [12], namely the graph aggregation is used according to [11]. We construct the graph G task (V, E) for the current few-shot task through the steps introduced before.
Then we propagate feature ( [12]) to obtain new features for each vertex: where I is the identity matrix and γ is a hyperparameter denoting the number of times to aggregate feature. At the same time, α is also a key value to balance between the neighbors representations and the self-ones. After aggregation, we use the labeled part of the vertices to train the task-dependent classifier Cls θ , a simple fully connected network. Then we use the trained network Cls θ with loss function specially designed for prototypes training to do the backpropagation and further obtain the trained prototypes. The design for loss function is introduced in details in III-C. After finishing the task-training, we have the helpful class prototypes to do distance measure. As done in the method of usual metric learning algorithms, we classify the query image into the class corresponding to the prototype with the closest distance to the feature of this query example. To improve the classification accuracy we design a simple attention mask add to the query examples' features before the calculation of distance which is described in III-D.

C. Loss Design for Trainable Prototypes
In the metric learning algorithm, the quality of the prototypes determine the classification accuracy of the distance metric, but the method of directly averaging the support examples features as in the [2] cannot achieve the best results.
We proposes trainable prototypes and consider it as a network parameter to participate in the training, so that you can get the prototypes that cannot design artificially. During the prototypes training, the design of loss function is very important. There are two requirements for the good prototypes: one is that it can be correctly classified by a trained classification head, and the other is that it can effectively improve the accuracy of metric classification. According to these two aspects, the corresponding loss function can be designed respectively.
The first term of the total loss is the cross-entropy loss of prototypes classification. In the calculation loss, the groundtruth of the class prototype is the one-hot form of its corresponding category which is a vector of length N, each category corresponds to a dimension in the vector, the value is 1 in the dimension representing the category to which it belongs, and the value is 0 in other dimensions. The classification loss can be described as follows: whereŷ i,c is the value of the prediction vector of the i-th class prototype in the c-th dimension, y i,c is the value on the c-th dimension of the one-hot label of the i-th class prototype. In order to make the training process converge, it is not enough to use only the cross entropy loss, and an entropy loss of the prototypes classification result must be designed. Because the last layer of the classification head is the softmax layer, so that the classification result is a probability distribution. The value of each dimension represents the probability that the prototypes belongs to the corresponding class. We believe that the higher credibility of the classification result of the prototype is better. Therefore, the value of the classification result in the corresponding dimension of the correct category should be close to 1, and the value of other positions should be close to 0. In order to strengthen this trend, Entropy loss can be designed as follows: Entropy loss hopes that the smaller the entropy of the prediction result, the better, which ensures that the prediction result tends to the one-hot vector and strengthens the training tendency. In addition to the above two losses, cross-entropy loss needs to be calculated for the metric classification results of the support examples, which is called metric loss. This loss is to ensure that the class prototypes have a good distance metric classification ability. Since the query examples has no label, the support examples are used instead in calculating the metric loss during training. We write the metric loss as: where the proto c represents the prototype of the c-th category. The total loss is the weighted summation of these three items expressed as follows: L total = λL entropy + δL class + L metric (8)

D. Attention Mechanism
We aim to pay more attention to important features while ignoring other content that are not vital for distance measure by adding attention mechanism to metric learning. Therefore, we introduce the attention mask used to correct the query examples features. Because the number of labeled support examples at task-training phase is small, the network parameters involved in this process cannot be increased, otherwise it will cause serious overfitting. The purpose of adding an attention mechanism in the metric classification is to highlight the features channel related to a certain category in both the query example feature and the corresponding prototype, so as to obtain the corrected query feature that is easier to classify. For this purpose, it is found that the class prototype trained in task-training is a natural attention mask.
The channel corresponding to a larger value in a prototype represents an important feature that distinguishes this class from other classes. Multiplying the prototype as the attention mask with the query example feature can highlight the channel related to this class in the query feature. If the query image belongs to this category, multiply the mask and then do the distance measure to get a larger similarity value, which can be better distinguished from other categories; if the query image does not belong to this category, then the similarity value will be smaller, and a better classification accuracy can also be achieved.
Therefore the attention mask are proposed as follows: atten mask n = Sof tmax (µ * abs (proto n )) where atten mask n represents the attention mask corresponding to the n-th category and µ is a scale factor. Then we use the attention mask to correct the query example features: query n = ε * query * atten mask n + query where query n is the query example features after correcting by the attention mask of the n-th category. query is the aggregated query features. ε is a weighting factor set as 10000 in our experiments. In addition to adding the attention mask in the task-training phase, we also add the attention module SENet [30] to the network in the meta-training phase, and verify its effectiveness through experiments.

IV. EXPERIMENTS
We conduct experiments on the widely used few-shot image classification benchmark: miniImageNet [1], which is a derivative of ImageNet.

A. Models and implementation details
Architecture. In the meta-training phase we use ResNet50 [31] as the structure of feature extractors f ϕ . This backbone has 50 convolutional layers grouped into 16 blocks. And in every block we add a SE block. We set the input size as 224× 224 and flatten the outputs as inputs to the graph aggregation, so that e = 2048 in V ∈ R [N ×(k+q)]×e .
In consideration of the extreme few labeled examples we take only one fully connected layer and a following softmax layer as the structure of the classifier Cls to avoid overfitting. And in the metric classification we measure the feature distance by calculating the cosine between two vectors.
Optimization and hyper-parameters setup. For the metatraining phase, we train the backbone in a total of 400 epochs from scratch using the SGD optimizer [32] and the crossentropy loss. In order to get a better model we also adopt early stopping. We set the total clusters number as 1680 while the unlabled samples used in ODC composed of 168 categories. For the task-training phase, we train the classifier and the prototypes in 11 epochs 1000 epochs respectively, using the Adam optimizer [33] and the loss function shown in the previous section.

B. Results on miniImageNet
The miniImageNet dataset consists of 100 classes randomly sampled from the ImageNet and each class contains 600 images of size 84×84. It is usually divided into three parts [8]: training set with 64 base classes, validation set with 16 classes, and testing set with 20 novel classes. In the meta-training phase we use 168 classes randomly chosen from ImageNet according to [11] to get a well feature extractor, having no labels. In the task-training phase we sample novel classes to design few-shot tasks as inputs. We ensure that the novel classes have never been seen in the meta-training phase.
We evaluate our method on 1000 randomly sampled tasks and report their mean accuracy in TABLE I. We compare our method in both 5-way 1-shot and 5-way 5-shot setting with some classical supervised few-shot learning methods and novel unsupervised methods proposed recently. It can be found that our method is much better than previous unsupervised fewshot learning methods( [14] etc.), improving them by more than 18%. The CSSL-FSL [11] also use non-episodic metatraining to train a feature extractor via a contrastive selfsupervised algorithm. Our method improves it by more than 4%. Even compared with supervised methods( [5] and [2]), our method still has improvement by 2-11% both on 5-way 1-shot and 5-way 5-shot tasks.

C. Ablation experiments
In this section, we conduct ablation experiments to analyze how trainable prototypes and attention mechanism affects the few-shot image classification performance. TABLE II shows the results of the ablation studies on miniImageNet in 5-way 1shot, 5-way 5-shot, 5-way 10-shot and 5-way 30-shot setting. In the table, without TrainProto means that the mean value of support features is directly calculated as the prototype to participate in the distance measure and without AttenMask means don't use attention mask to correct the query features. From the table we can see the original model performs best. Attention mask before the distance measure improves accuracy by 0.1%. When the number of shots is 1 or 5, the improvement in accuracy brought by the AttenMask is more obvious, indicating that the attention mask can achieve greater advantages when there are few label samples.
The SENet used in feature extractor can provide about 1% extra gain. And without TrainProto, the result will decrease by 2-6%. The reason is that TrainProto is actually equivalent to obtaining the class prototypes through a very complicated function, which cannot be designed manually. Therefore, the prototypes obtained by training must be better than those obtained by directly averaging.

D. Other Experiments
In this section, we compare the effects of two different attention modules on meta-training. We added Non-Local(NL) [34] and SENet modules(SE) to the ODC backbone network respectively, and compared their effects on the accuracy of few shot classification after training. The results are shown in TABLE III. We also compares the algorithm without attention module(w/o Atten) in meta-training with the two improved algorithms mentioned above.
Generally speaking, the addition of SENet and Non-local modules can improve the performance of the algorithm, and the improvement of SENet is more than that of Non-local. On the few-sample classification tasks with shot=1 and 5, the two attention modules brought the higher improvement in classification accuracy, reaching a maximum of 1.2%. But when shot=10, the SENet module brings a 0.06% decrease in accuracy, and when shot=30, the Non-local module brings a 0.38% decrease in accuracy. In general, these two attentions have played a positive role in the training of the feature extractor, and the improvement of SEnet is more obvious especially when shot is extreme small.

V. CONCLUSION
A novel method of obtaining prototypes for metric classification called TrainProto-FSL is proposed in this paper, which contains classifier training and prototypes training. And the whole framework designed as [11] helps our algorithm avoid the shortcomings related with the traditional episodic metatraining.
Experiments show a state-of-the-art performance on a standard vision dataset miniImageNet. It proves that the trained prototypes get from neural network are more efficacious than the hand-designed ones, and the attention mechanism is also useful for the performance improvement. In the following study we will explore the effective self-supervised learning algorithm specially for few shot learning.