Few-Shot Classification with Contrastive Learning

Zhanyuan Yang, Jinghua Wang, Yingying Zhu

Abstract

A two-stage training paradigm consisting of sequential pre-training and meta-training stages has been widely used in current few-shot learning (FSL) research. Many of these methods use self-supervised learning and contrastive learning to achieve new state-of-the-art results. However, the potential of contrastive learning in both stages of the FSL training paradigm is still not fully exploited. In this paper, we propose a novel contrastive learning-based framework that seamlessly integrates contrastive learning into both stages to improve the performance of few-shot classification. In the pre-training stage, we propose a self-supervised contrastive loss in the forms of feature vector vs. feature map and feature map vs. feature map, which uses global and local information to learn good initial representations. In the meta-training stage, we propose a cross-view episodic training mechanism to perform the nearest centroid classification on two different views of the same episode and adopt a distance-scaled contrastive loss based on them. These two strategies force the model to overcome the bias between views and promote the transferability of representations. Extensive experiments on three benchmark datasets demonstrate that our method achieves competitive results.

Introduction

Deep learning models typically require vast amounts of labeled data to achieve high performance. Few-Shot Learning (FSL) seeks to overcome this limitation by enabling models to adapt to new concepts using only a handful of examples. The current state-of-the-art in FSL generally follows a two-stage pipeline: a pre-training phase on a large "base" dataset, followed by a meta-training phase that uses episodic tasks to simulate the few-shot environment.

A significant challenge in this pipeline is the reliance on standard Cross-Entropy (CE) loss during pre-training. While effective for classification on the base classes, CE loss often leads to representations that are overly specialized to those specific categories. This "feature overfitting" hinders the model's ability to generalize to unseen "novel" classes. This paper introduces a framework that integrates contrastive learning across both stages of training to learn more robust, transferable features.

Proposed Method

1. Enhanced Pre-training with Local Information

Standard contrastive learning often focuses on global feature vectors, which can discard important spatial nuances. The proposed framework introduces a self-supervised contrastive loss that operates at both global and local levels:

  • Global Contrastive Loss: Utilizes standard InfoNCE and supervised contrastive losses to align different views of the same image and group instances belonging to the same category.
  • Local Contrastive Loss (Map-Map and Vector-Map): To capture fine-grained features, the model calculates similarities between spatial feature maps (local patches) and between global vectors and local maps. By forcing the model to correlate local regions, it learns to identify objects based on textures and parts—features that are more likely to be shared between base and novel classes (e.g., the texture of a dog's fur being similar to a wolf's).

2. Meta-training with Cross-View Episodic Training (CVET)

In the meta-training stage, the authors introduce Cross-View Episodic Training (CVET) to improve the consistency of the learned representations. In a standard FSL episode, a model is given a "support set" of labeled examples and a "query set" to classify.

CVET creates two distinct augmented views of the same episode. The model is then required to perform nearest-centroid classification across these views—for instance, using the support set from View A to classify the query set from View B. To ensure the model is not overwhelmed by extreme data augmentations, a distance-scaled contrastive loss is applied. This loss uses a scaling factor based on the distance between features, focusing the model's attention on meaningful semantic relationships rather than the noise introduced by heavy augmentation.

Experimental Results

The framework was evaluated on three standard FSL benchmarks: miniImageNet, tieredImageNet, and CIFAR-FS. The results indicate that the proposed method consistently outperforms standard baselines and existing contrastive methods.

  • miniImageNet: The model achieved 70.19% accuracy on 5-way 1-shot tasks, a significant improvement over the FEAT baseline (66.78%).
  • tieredImageNet: The model reached 72.62% on 1-shot tasks.
  • CIFAR-FS: The model achieved 77.56% on 1-shot tasks.

Visualization using t-SNE plots confirms that this method produces tighter, more distinct clusters for novel classes compared to traditional training methods, suggesting a higher degree of class separability in the embedding space.

Discussion and Insights

Local vs. Global Features

The inclusion of local feature map contrastive losses is a key driver of performance. By focusing on local patches, the model avoids relying solely on global object orientation or lighting, which may vary significantly between the base and novel datasets.

Episodes as Context

CVET treats the entire few-shot task as a context. By maximizing mutual information between different views of the same task, the model learns "meta-knowledge"—the underlying logic of how to distinguish between classes—rather than just memorizing class-specific features.

Limitations

While effective, the framework introduces some computational overhead. The addition of multiple projection heads and the processing of dual views per episode increases the parameter count by approximately 1.9M during pre-training and extends training time. Additionally, the model's performance is sensitive to the choice of data augmentation; while SimCLR-style augmentations are beneficial, overly aggressive techniques like AutoAugment can introduce excessive bias.

Conclusion

By seamlessly integrating contrastive learning into both the pre-training and meta-training stages, this framework addresses the limitations of standard cross-entropy-based few-shot learning. The combination of local feature map correlations and cross-view episodic training significantly enhances the transferability of representations, setting a competitive new standard for few-shot classification tasks.

Summary updated

BibTeX
@inproceedings{yang2022few,
  author = {Zhanyuan Yang and Jinghua Wang and Yingying Zhu},
  title = {{Few-Shot Classification with Contrastive Learning}},
  booktitle = {European Conference on Computer Vision},
  year = {2022},
  doi = {10.1007/978-3-031-20044-1\_17},
}
Figure