Abstract
We propose a conditional positional encoding (CPE) scheme for vision Transformers. Unlike previous fixed or learnable positional encodings, which are pre-defined and independent of input tokens, CPE is dynamically generated and conditioned on the local neighborhood of the input tokens. As a result, CPE can easily generalize to the input sequences that are longer than what the model has ever seen during training. Besides, CPE can keep the desired translation-invariance in the image classification task, resulting in improved performance. We implement CPE with a simple Position Encoding Generator (PEG) to get seamlessly incorporated into the current Transformer framework. Built on PEG, we present Conditional Position encoding Vision Transformer (CPVT). We demonstrate that CPVT has visually similar attention maps compared to those with learned positional encodings and delivers outperforming results.
Introduction
Standard Vision Transformers (ViTs) are inherently permutation-invariant, meaning they lack an internal sense of spatial structure. To address this, researchers typically add explicit Positional Encodings (PE)—either fixed sinusoidal functions or learnable vectors—to the input embeddings. However, these methods introduce significant limitations. Fixed-length PEs are rigid; they are tied to specific sequence lengths, causing performance to degrade when models are applied to higher-resolution images than those seen during training. Furthermore, absolute positional encodings break translation equivariance, a fundamental property in computer vision where a shift in the input should result in a corresponding shift in the representation.
Methodology: Conditional Positional Encoding
To overcome the rigidity of standard PEs, we introduce Conditional Positional Encoding (CPE). Unlike traditional methods that add static values to tokens, CPE is generated dynamically based on the local context of the tokens themselves. This is achieved through a Position Encoding Generator (PEG).
The PEG is implemented as a simple depth-wise convolution (typically $3 \times 3$) with zero padding. By reshaping the input tokens into a 2D grid, applying the convolution, and adding the result back to the original tokens, the model learns spatial relationships dynamically. Crucially, the use of zero padding allows tokens near the image borders to "sense" their absolute position relative to the edges, providing the benefits of absolute positioning without its inherent inflexibility.
Experimental Results
Our primary model, the Conditional Position encoding Vision Transformer (CPVT), was evaluated on ImageNet-1K. CPVT-Ti (Tiny) achieved 73.4% top-1 accuracy, surpassing the popular DeiT-tiny (72.2%) while utilizing a more efficient parameter count.
Generalization to Higher Resolutions
A key advantage of CPE is its ability to generalize to unseen image resolutions. Standard models like DeiT rely on interpolation when moving from a $224 \times 224$ training resolution to a $512 \times 512$ test resolution, which often leads to a sharp drop in accuracy. As shown in the table below, CPVT maintains significantly higher performance across varying resolutions.
Downstream Tasks
The benefits of PEG extend beyond classification. When integrated into "Pyramid" Transformers (such as PVT) for dense prediction tasks, we observed a 3.1% mIoU increase in semantic segmentation on ADE20K and a 2.0% mAP improvement in object detection on COCO. This suggests that the inductive bias provided by PEG is highly beneficial for tasks requiring precise spatial localization.
Conclusion
CPVT demonstrates that explicit, fixed positional encodings are not a necessity for Vision Transformers. By using a simple, convolutional PEG module, we can generate positional information that is both dynamic and translation-equivariant. This approach not only improves classification accuracy but also provides a robust solution for the long-standing challenge of resolution generalization in Transformer-based architectures.
Summary updated