Conditional Positional Encodings for Vision Transformers

Xiangxiang Chu, Zhi Tian, Bo Zhang, Xinlong Wang, Xiaolin Wei · DOI

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. Our code is available at https://github.com/Meituan-AutoML/CPVT .

Introduction

Published at ICLR 2023, this research introduces an architectural advancement for Vision Transformers (ViTs) that addresses a fundamental challenge in spatial modeling: providing positional information without compromising the model's flexibility regarding input resolution.

Standard Transformers are permutation-invariant, treating input patches as an unordered set. To fix this, researchers typically employ Absolute Positional Encodings (APE)—fixed or learnable vectors added to patches at the network's input. However, APE has two major flaws. First is inflexibility: models trained on one resolution (e.g., $224 \times 224$) struggle with larger resolutions (e.g., $384 \times 384$) because fixed-length APE vectors must be interpolated, often degrading accuracy. Second is broken equivariance: APE assigns unique, static IDs to coordinates, which conflicts with "translation equivariance"—the ability of a model to recognize an object consistently regardless of its position in the frame.

Methodology: The Position Encoding Generator (PEG)

The authors propose Conditional Positional Encoding (CPE), a dynamic scheme that generates position information based on the local neighborhood of input tokens using a simple convolutional "plugin" called the Position Encoding Generator (PEG).

Comparison of ViT, CPVT, and CPVT-GAP architectures.

The PEG is typically inserted after the first encoder block and is implemented as a simple 2D depth-wise convolution with a $3 \times 3$ kernel. This approach offers several key advantages:

  • Conditionality: The positional signal is generated dynamically based on the local features of the image itself.
  • Implicit Absolute Positioning: By utilizing zero-padding, the convolution allows the model to implicitly calculate its absolute position relative to the image borders.
  • Seamless Integration: PEG is a "plug-and-play" module requiring no changes to the standard Transformer API.

The resulting architecture is termed the Conditional Position encoding Vision Transformer (CPVT). A variant, CPVT-GAP, replaces the traditional class token with Global Average Pooling to further enhance translation invariance.

Experimental Results

CPVT was rigorously tested on ImageNet-1K, ADE20K (semantic segmentation), and COCO (object detection). A standout result is the model's superior generalization to varying input resolutions without additional fine-tuning.

Performance comparison across varying input resolutions showing CPVT's superior generalization.

As shown in the experimental data, when upscaling from $224 \times 224$ to $512 \times 512$ resolution, the baseline DeiT-tiny accuracy drops from 72.2% to 65.9%. In contrast, CPVT-Ti maintains high performance, reaching 74.2% at $384 \times 384$ resolution. Overall, CPVT-Ti achieves 73.4% on ImageNet, outperforming DeiT-tiny (72.2%) while maintaining a similar parameter count.

Furthermore, adding PEG to pyramid-style Transformers (like PVT) improved semantic segmentation on ADE20K by 3.1% mIoU and object detection on COCO by 2.4% mAP. These gains come with negligible computational overhead, adding only about 0.34M FLOPs to a 2.1G FLOP model.

Discussion and Conclusion

This paper demonstrates that simple local operators like convolutions can effectively replace complex learnable embeddings for positional encoding in Transformers. By leveraging the "edges" of the image via zero-padding, the model can infer its global position while remaining flexible to input size.

While CPE provides a much stronger bias toward translation equivariance than APE, the use of zero-padding means the model is not mathematically 100% equivariant. Future work could explore more complex multi-position encodings or alternative local operators beyond $3 \times 3$ depth-wise convolutions to further optimize the PEG module.

Summary updated

BibTeX
@misc{chu2021conditional,
  author = {Xiangxiang Chu and Zhi Tian and Bo Zhang and Xinlong Wang and Xiaolin Wei},
  title = {{Conditional Positional Encodings for Vision Transformers}},
  year = {2021},
  eprint = {2102.10882},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2102.10882},
}
Figure