Introduction
Vision Transformers (ViTs) have revolutionized computer vision by applying the transformer architecture, originally designed for natural language processing, to image data. However, a major hurdle for their widespread adoption is their "data hunger." Unlike Convolutional Neural Networks (CNNs), which possess an inherent "locality inductive bias" through their sliding filters, standard ViTs treat an image as a sequence of non-overlapping patches. This lack of spatial awareness means ViTs must see millions of examples to learn that neighboring pixels are related. This paper addresses this limitation by introducing two plug-and-play modules: Shifted Patch Tokenization (SPT) and Locality Self-Attention (LSA).
The Locality Inductive Bias Problem
The researchers identify two specific architectural weaknesses that hinder ViT performance on small-to-mid-sized datasets:
- Receptive Field Gap: Standard patch partitioning results in tokens with a very small receptive field—roughly 30 times smaller than the final feature map of a ResNet-50. This prevents the model from capturing crucial spatial relationships between neighboring patches early in the network.
- Attention Distribution: In small datasets, ViTs often fail to learn what to focus on, resulting in "smooth" or "flat" attention maps. Tokens frequently attend to themselves or background noise rather than the primary object of interest.
Proposed Methods: SPT and LSA
To bake locality bias directly into the transformer architecture, the authors propose two generic modules that can be integrated into almost any ViT variant.
Shifted Patch Tokenization (SPT)
SPT improves the initial tokenization process. Instead of simply slicing the input image, SPT creates several spatially shifted versions of the image (e.g., shifted diagonally by half a patch size). These shifted versions are concatenated with the original image to form a "stack." When this stack is tokenized, each visual token effectively contains information from a much wider receptive field, capturing the spatial relationships that standard ViTs typically ignore.
Locality Self-Attention (LSA)
LSA sharpens the model's attention mechanism through two key innovations:
- Diagonal Masking: This technique forces the model to ignore "self-token" relations by masking the diagonal of the similarity matrix. By doing so, each token is compelled to find relevant information in other tokens, mitigating the tendency of ViTs to focus redundantly on the token itself.
- Learnable Temperature Scaling: While standard ViTs use a fixed scaling factor for attention, LSA introduces a learnable parameter. This allows the model to dynamically "sharpen" or "smooth" its attention distribution during training, acting as a "focus knob" that helps the model learn more discriminative features.
Experimental Results
The researchers evaluated their methods on several benchmark datasets, including CIFAR-10, CIFAR-100, Tiny-ImageNet, and SVHN. The results demonstrate that these simple architectural changes lead to significant performance gains.
Key findings include:
- Significant Accuracy Boosts: Applying SPT and LSA (denoted as "SL-" models) to architectures like Swin, PiT, and CaiT improved performance on Tiny-ImageNet by an average of 2.96%. The Swin-T model saw a particularly impressive 4.08% increase.
- Surpassing CNN Benchmarks: On Tiny-ImageNet, the "SL-CaiT" model achieved 67.18% accuracy, outperforming established CNNs such as ResNet-110 (62.96%) and EfficientNet-B0 (66.79%).
- Generalization to Larger Datasets: The benefits are not limited to small datasets. On ImageNet-1K, the proposed methods provided a consistent boost of 1.06% to 1.60% across different architectures, suggesting that inducing locality bias is beneficial even at larger scales.
Conclusion
The introduction of Shifted Patch Tokenization and Locality Self-Attention provides a robust solution to the data hunger problem in Vision Transformers. By incorporating architectural priors that induce locality bias, these modules allow ViTs to outperform state-of-the-art CNNs on small-scale datasets without requiring massive pre-training or significant computational overhead. Future research may explore optimizing hyperparameters like shift ratios or developing more complex, learnable shifting strategies to further enhance these capabilities.
Summary updated