Where are my Neighbors? Exploiting Patches Relations in Self-Supervised Vision Transformer

Guglielmo Camporese, Elena Izzo, Lamberto Ballan

Abstract

Vision Transformers (ViTs) typically require massive datasets to overcome their lack of inherent inductive bias. This paper introduces RelViT, a self-supervised learning strategy designed to improve ViT performance on small-to-medium datasets by explicitly teaching the model spatial relationships between image patches. By leveraging $N^2$ relational signals per image, RelViT provides a dense training signal that significantly boosts accuracy without additional data.

Introduction: Addressing the Data Hunger of ViTs

Vision Transformers have revolutionized computer vision, but they possess a significant "hunger" for data. Unlike Convolutional Neural Networks (CNNs), which have built-in assumptions about spatial locality (inductive bias), ViTs start with a blank slate. Consequently, while ViTs dominate on massive datasets like ImageNet-21k, they often underperform CNNs when data is scarce. The authors of RelViT address this gap by introducing self-supervised tasks that mimic the spatial awareness inherent in CNNs, forcing the model to learn how image patches relate to one another.

Methodology: The RelViT Framework

RelViT (Relational Vision Transformer) moves beyond training just the single classification token ([CLS]) and instead utilizes all output tokens generated by the transformer backbone.

Self-Supervised Learning Tasks

The core of RelViT involves adding specialized heads to the transformer to solve four self-supervised learning (SSL) tasks that provide a dense training signal.

RelViT architecture overview showing the Vision Transformer backbone and the four relational heads (Spatial, Distance, Angle, and Absolute Position).
  1. Spatial Relations (SpRel): The model predicts the relative orientation between two patches (e.g., "top-left").
  2. Distances (Dist): The model estimates the Euclidean distance between patch centers.
  3. Angles (Ang): The model predicts the relative angle between patches.
  4. Absolute Positions (AbsPos): The model identifies the 2D coordinates of a single patch within the original image grid.

Mega-Patches and the Shortcut Problem

To make these tasks more robust, the authors introduced Mega-Patches. Instead of a rigid grid, they randomly sample rectangular regions of varying sizes, resize them to the standard patch size, and feed them into the model. This prevents the model from simply memorizing a fixed grid and encourages it to understand context and scale.

Comparison between standard uniform grid patches (left) and the stochastic Mega-Patch approach (right) used to prevent grid memorization.

Furthermore, the authors identified a "shortcut problem" where standard Positional Embeddings (PEs) allowed the model to "cheat" by reading coordinates directly. To solve this, PEs are either removed or patches are shuffled during SSL pre-training.

Experimental Results

RelViT was evaluated on several small-to-medium benchmarks where ViTs typically struggle. The strategy consistently outperformed standard supervised ViTs:

  • Tiny-Imagenet: Accuracy improved from 43.19% to 51.98% (+8.79%).
  • ImageNet-100: Accuracy rose from 58.04% to 66.46% (+8.42%).
  • CIFAR-10: Accuracy increased from 86.09% to 90.23%.
  • Generalization: When applied to a Swin Transformer on the Flower-102 dataset, the "RelSwin" variant improved accuracy by 19.10% over the supervised baseline.

Conclusion

RelViT demonstrates that the lack of inductive bias in Vision Transformers can be compensated for through dense, relational self-supervised tasks. By exploiting the $N^2$ signal available in patch pairs, RelViT effectively teaches the model 2D geometry, making it a powerful tool for training high-performing transformers on limited datasets.

Summary updated

BibTeX
@inproceedings{camporese2022where,
  author = {Guglielmo Camporese and Elena Izzo and Lamberto Ballan},
  title = {{Where are my Neighbors? Exploiting Patches Relations in Self-Supervised Vision Transformer}},
  booktitle = {BMVC},
  year = {2022},
}
Figure