Introduction
Deep learning typically relies on large, balanced datasets. However, real-world data often suffers from scarcity issues, such as long-tailed distributions, domain shifts, and the need for zero-shot recognition. Standard neural networks process images in a mini-batch independently, missing the opportunity to leverage relationships between samples.
BatchFormer is a simple, plug-and-play Transformer module designed to address these challenges by operating on the batch dimension during training. By enabling images within a mini-batch to "collaborate," it facilitates knowledge transfer from common classes to rare ones and improves feature robustness.
The BatchFormer Framework
The core innovation is applying a standard Transformer encoder to the batch dimension rather than the spatial dimension.
Batch-wise Interaction
While a typical Transformer attends to pixels or patches within a single image, BatchFormer looks at all $N$ images in a batch simultaneously. This attention mechanism identifies which features from one image can help refine the representation of another.
Architecture and Training
As shown in the architecture below, BatchFormer is inserted between the backbone and the classifier. A critical component is the shared classifier trick: during training, the model calculates losses for features both before and after the BatchFormer module. This dual-path training forces the backbone to learn robust, invariant features that remain effective even when the BatchFormer module is removed.
Zero-Inference Overhead
Because of the shared classifier, BatchFormer can be completely unplugged after training. At inference time, the model processes single images through the backbone and classifier as usual, benefiting from the superior features learned during the collaborative training phase without any additional computational cost.
Key Insights: Collaborative Learning
BatchFormer shifts the paradigm of a batch from a mere computational unit to a structured source of information. It enables:
- Knowledge Transfer: Head-class samples (e.g., a common "dog") share common features like fur or ears to help the network better represent tail-class samples (e.g., a rare "wolf").
- Focus on Objects: Grad-CAM visualizations indicate that BatchFormer helps the model ignore background noise (spurious correlations) and focus more intensely on the actual object of interest.
- Implicit Augmentation: By linking samples, the model effectively creates "virtual samples," acting as a form of implicit data augmentation that is particularly beneficial for rare categories.
Experimental Results
The authors evaluated BatchFormer across more than ten datasets, covering long-tailed recognition, zero-shot learning, domain generalization, and self-supervised learning.
- Long-Tailed Recognition: On ImageNet-LT, it improved "Few-shot" class accuracy by 6.9% using a ResNet-10 backbone.
- Zero-Shot Learning: Accuracy for "unseen" categories increased by nearly 5% on MIT-States and C-GQA benchmarks.
- Domain Generalization: It consistently outperformed specialized algorithms like SWAD across various domains (Art, Sketches, etc.).
- Contrastive Learning: Integrated with MoCo-v3, it improved final accuracy and significantly accelerated convergence, reaching baseline performance in 38 epochs instead of 100.
Conclusion
BatchFormer demonstrates that exploring sample relationships within a batch is a powerful strategy for robust representation learning. While its benefits are most pronounced in data-scarce or imbalanced scenarios, its zero-inference overhead makes it a highly practical addition to any deep learning pipeline. Future work may explore deeper integration of batch-wise attention throughout the network architecture.
Summary updated