Abstract
High-resolution images offer more information about scenes that can improve model accuracy. However, the dominant model architecture in computer vision, the vision transformer (ViT), cannot effectively leverage larger images without finetuning -- ViTs poorly extrapolate to more patches at test time, although transformers offer sequence length flexibility. We attribute this shortcoming to the current patch position encoding methods, which create a distribution shift when extrapolating. We propose a drop-in replacement for the position encoding of plain ViTs that restricts attention heads to fixed fields of view, pointed in different directions, using 2D attention masks. Our novel method, called LookHere, provides translation-equivariance, ensures attention head diversity, and limits the distribution shift that attention heads face when extrapolating. We demonstrate that LookHere improves performance on classification (avg. 1.6%), against adversarial attack (avg. 5.4%), and decreases calibration error (avg. 1.5%) -- on ImageNet without extrapolation. With extrapolation, LookHere outperforms the current SoTA position encoding method, 2D-RoPE, by 21.7% on ImageNet when trained at $224^2$ px and tested at $1024^2$ px. Additionally, we release a high-resolution test set to improve the evaluation of high-resolution image classifiers, called ImageNet-HR.
Introduction
In computer vision, increasing image resolution typically yields higher accuracy due to the preservation of fine-grained details. However, training models directly on high-resolution data is computationally prohibitive. A more efficient approach is to train on lower resolutions (e.g., $224^2$ px) and extrapolate to higher resolutions (e.g., $1024^2$ px) during inference.
While Vision Transformers (ViTs) are theoretically capable of handling varying sequence lengths, they struggle to maintain spatial reasoning when the number of patches increases. This failure is largely due to existing position encoding methods, such as learnable embeddings or sinusoidal encodings, which suffer from a significant distribution shift when encountering unseen patch positions. This research introduces LookHere, a novel architectural modification that replaces traditional position encodings with geometric spatial priors.
Methodology: Directed Attention Masks
LookHere moves away from absolute position embeddings by modifying the self-attention mechanism. It employs two primary components to maintain spatial grounding:
- 2D Directional Masks: Each attention head is assigned a fixed "field of view" (FOV) and a specific direction. For instance, one head may only attend to patches above and to the left of the query patch. This hard-coded diversity ensures translation equivariance and prevents the "attention collapse" often seen in deep ViT layers.
- Distance-Based Biases: Within the assigned FOV, attention scores are biased based on the Euclidean distance between patches, favoring local context.
Because these masks and biases are relative and geometric, they remain valid regardless of the total number of patches, allowing the model to scale seamlessly to higher resolutions without retraining.
Experimental Results and Extrapolation
The authors evaluated LookHere on ImageNet-1k and several robustness benchmarks, including a newly introduced high-resolution dataset, ImageNet-HR. This dataset contains 5,000 manually cropped $1024^2$ px images to provide a more accurate benchmark for high-resolution classification than standard upsampled sets.
Performance Gains
LookHere demonstrates remarkable stability as resolution increases. When trained at $224^2$ and tested at $1024^2$, LookHere outperformed the previous state-of-the-art method, 2D-RoPE, by 21.7% on ImageNet. Even at the original training resolution, the method improved top-1 accuracy by an average of 1.6% and boosted adversarial robustness by 5.4%.
Efficiency and Interpretability
LookHere is highly efficient, adding only 0.016% to the total FLOPs of a ViT-B/16 model. Furthermore, the directional constraints lead to cleaner, more interpretable attention maps, as each head is dedicated to a specific spatial orientation. The researchers also noted that extrapolation benefits are most pronounced for images containing small objects, which become more distinguishable at higher resolutions.
Conclusion
LookHere provides a simple, "zero-cost" drop-in replacement for position encodings that significantly enhances the extrapolation capabilities of Vision Transformers. By replacing learned spatial embeddings with fixed geometric priors, the model avoids the distribution shifts that typically plague high-resolution inference. Future work will explore scaling LookHere to larger architectures and applying these directional priors to other domains like video and 3D point cloud processing.
Summary updated