BatchFormerV2: Exploring Sample Relationships for Dense Representation Learning

Zhi Hou, Baosheng Yu, Chaoyue Wang, Yibing Zhan, Dacheng TaoarXiv 2022 · DOI

Introduction: Beyond Spatial Attention

In modern computer vision, Vision Transformers (ViTs) have revolutionized how we process images by leveraging spatial self-attention. By relating different patches within a single image, these models capture complex global dependencies. However, standard training paradigms often overlook a critical source of information: the relationships between different images within a training mini-batch.

Comparison of attention mechanisms: Channel Attention (CNN), Spatial Attention (ViT), and Batch Attention (BatchFormer).

BatchFormerV2 addresses this gap by introducing a plug-and-play module that explores sample relationships. While its predecessor, BatchFormer, was limited to simple image classification, BatchFormerV2 is designed for "dense" tasks like object detection and panoptic segmentation. It bridges the training-testing inconsistency—where batch information is available during training but absent during inference—ensuring that the learned benefits persist even when processing a single image at test time.

The BatchFormerV2 Architecture

The core innovation of BatchFormerV2 is treating the batch dimension as a sequence for attention.

The BatchFormerV2 architecture applies self-attention across the batch dimension for each spatial position.

Key Components:

  1. Batch-Dimension Attention: The module transposes input features to apply a Transformer block across the mini-batch dimension for every spatial position. This allows the model to learn collaborative features, such as identifying an occluded object in one image by relating it to a clear example in another.
  2. Shared Transformer Blocks: To maintain efficiency and handle varying image sizes, the same Transformer block is shared across all spatial positions ($1 \le i \le N$).
  3. Two-Stream Training: To eliminate the need for a batch at inference, the authors employ a "siamese" training pipeline. One stream includes the BatchFormerV2 module, while the other (sharing the same weights) does not. This forces the backbone to learn batch-invariant representations.
  4. Zero Inference Cost: Because the two streams share weights, the BatchFormerV2 module can be simply removed after training. The resulting model retains the performance gains while maintaining the original architecture's speed and complexity.

Experimental Results

The authors evaluated BatchFormerV2 across diverse benchmarks, demonstrating its universal applicability:

  • Object Detection & Segmentation: On the COCO 2017 dataset, adding the module to DETR-based models (like Deformable-DETR) yielded improvements of 1.3% to 2.1% AP in detection and 1.7% PQ in panoptic segmentation.
  • Data-Scarce Scenarios: The module acts as a powerful regularizer. On CIFAR-100, a DeiT-B model saw a massive 14.4% boost in Top-1 accuracy when trained for 100 epochs.
  • Long-Tailed Learning: Performance on ImageNet-LT showed that batch-level relationships help the model better handle imbalanced data distributions.
  • 3D Reconstruction: The module also proved effective for 3D hand mesh reconstruction on the FreiHAND dataset.

Discussion and Future Directions

BatchFormerV2 effectively suppresses background noise by learning that common features like "sky" or "grass" across a batch are less informative than unique objects. However, the authors noted that integrating the module into very early layers can cause training instability (NaN losses) due to volatile gradients before basic spatial features are established.

Future research will explore more stable integration methods for early layers, different weight-sharing strategies between training streams, and the impact of varying batch sizes on the quality of learned sample relationships.

Summary updated

BibTeX
@misc{hou2022batchformerv,
  author = {Zhi Hou and Baosheng Yu and Chaoyue Wang and Yibing Zhan and Dacheng Tao},
  title = {{BatchFormerV2: Exploring Sample Relationships for Dense Representation Learning}},
  year = {2022},
  eprint = {2204.01254},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2204.01254},
}
Figure