Vision Transformer with Deformable Attention

Zhuofan Xia, Xuran Pan, Shiji Song, Li Erran Li, Gao Huang

TL;DR

The Deformable Attention Transformer (DAT) introduces a data-dependent sparse attention mechanism for Vision Transformers. By learning a shared set of deformed sampling points across queries, DAT dynamically shifts its focus to important image regions without incurring the prohibitive computational costs of traditional deformable mechanisms. This simple yet effective approach yields consistent performance boosts across image classification, object detection, and semantic segmentation tasks.

What Problem Does This Paper Address?

Vision Transformers (ViTs) have revolutionized computer vision, but they suffer from a fundamental tension between computational efficiency and receptive field flexibility.

Standard ViTs use dense global attention, which scales quadratically ($O(N^2)$) with the number of image patches. This is computationally expensive and introduces noise from irrelevant background regions. To fix this, hierarchical models like Swin Transformer and Pyramid Vision Transformer (PVT) introduce hand-crafted, sparse attention patterns (e.g., local windows or fixed downsampling). However, these approaches are data-agnostic; they apply the exact same attention boundaries regardless of whether the image contains a tiny bird or a massive skyscraper, potentially dropping critical long-range dependencies.

While Deformable Convolutional Networks (DCN) successfully solved this in the CNN world by learning data-dependent receptive fields, directly applying DCN to ViTs is non-trivial. Learning unique offsets for every single query patch results in a biquadratic complexity ($O(N_q N_k C)$), making it far too heavy to serve as a foundational backbone network.

Figure 1. Comparison of DAT with other Vision Transformer mod- els and DCN in CNN model. The red star and the blue star de- note the different queries, and masks with solid line boundaries denote the regions to which the queries attend. In a data-agnostic way: (a) ViT [12] adopts full attention for all queries. (b) Swin Transformer [26] uses partitioned window attention. In a data- dependent way: (c) DCN [9] learns different deformed points for each query. (d) DAT learns shared deformed points for all queries.

Methods

DAT strikes an elegant compromise: shared, data-dependent sparse attention. Based on the observation that global attention maps across different queries are often highly redundant, the authors propose learning a set of global offsets that are shared across all queries.

Here is how the Deformable Attention module works:

  1. Reference Grid: A uniform grid of reference points is defined across the feature map.
  2. Offset Generation: The feature map is projected into query tokens and fed into a lightweight offset network (consisting of a $5\times5$ depthwise convolution, GELU activation, and a $1\times1$ convolution) to predict 2D spatial offsets for each reference point.
  3. Bilinear Sampling: The reference grid is deformed by these offsets. Features are sampled at these continuous, deformed locations using bilinear interpolation.
  4. Attention Calculation: These sampled features are projected to form the keys ($k$) and values ($v$). Standard multi-head attention is then performed between the original queries ($q$) and these newly deformed keys and values.

To preserve spatial awareness, the authors also introduce a deformable relative position bias, interpolating a position bias table based on the continuous displacements of the deformed keys.

Figure 2. An illustration of our deformable attention mechanism. (a) presents the information flow of deformable attention. In the left part, a group of reference points is placed uniformly on the feature map, whose offsets are learned from the queries by the offset network. Then the deformed keys and values are projected from the sampled features according to the deformed points, as shown in the right part. Relative position bias is also computed by the deformed points, enhancing the multi-head attention which outputs the transformed features. We show only 4 reference points for a clear presentation, there are many more points in real implementation de facto. (b) reveals the detailed structure of the offset generation network, marked with sizes of feature maps.

Macro Architecture

DAT adopts a standard 4-stage pyramid structure. To balance performance and computational overhead, the first two stages utilize standard local window attention (similar to Swin), as early-stage feature maps are too large for efficient bilinear sampling. Stages 3 and 4 alternate between local window attention blocks and the proposed Deformable Attention blocks to capture global, data-dependent context.

Figure 3. An illustration of DAT architecture. N1 to N4 are the numbers of stacked successive local attention and shift-window / deformable attention blocks. k and s denote the kernel size and stride of the convolution layer in patch embeddings.

Datasets

The authors evaluated DAT across three standard computer vision benchmarks:

  • ImageNet-1K: For image classification (1.28M training images, 50K validation images).
  • COCO: For object detection and instance segmentation (118K training images, 5K validation images).
  • ADE20K: For semantic segmentation (20K training images, 2K validation images).

Key Insights and Ideas

  • Query-Agnostic Offsets are Enough: The core "aha" moment is that you don't need unique deformed points for every single query. A shared set of deformed keys/values effectively captures the prominent foreground objects in an image for all queries simultaneously, reducing space complexity from biquadratic to linear.
  • Automatic Foreground Focusing: Visualizations reveal that the offset network naturally learns to shift the sampling points toward foreground objects, effectively acting as an implicit saliency or object-localization mechanism.
  • Geometric Robustness: By allowing keys to shift continuously, the model handles geometric transformations (like rotation, scale, and deformation) much better than rigid window-based transformers, which is particularly evident in its performance on large objects.

Key Results

DAT was scaled into three variants (DAT-T, DAT-S, and DAT-B) and compared against competitive baselines like Swin and PVT:

  • ImageNet-1K Classification: DAT-T achieved 82.0% top-1 accuracy (+0.7% over Swin-T), while DAT-S reached 83.7% (+0.7% over Swin-S) with comparable FLOPs and parameter counts.
  • COCO Object Detection: When plugged into a RetinaNet framework, DAT-T outperformed Swin-T by +1.1 AP (1x schedule) and +0.8 AP (3x schedule). Notably, performance on large objects ($AP_l$) saw a massive jump of +2.1 to +3.1, validating the benefit of flexible, long-range deformable attention.
  • ADE20K Semantic Segmentation: Integrated with UperNet, DAT-B achieved a 49.38% mIoU, outperforming Swin-B by +1.2%.

Limitations and Future Work

The primary limitation of DAT is that deformable attention is restricted to the later stages (Stages 3 and 4) of the network. In the first two stages, the spatial resolution of the feature maps is very high ($56\times56$ and $28\times28$). Performing bilinear interpolation and dot-product attention on such large grids remains computationally expensive, forcing the model to rely on standard shifted windows early on.

Future work could explore more efficient sampling strategies or multi-scale offset networks to safely bring data-dependent deformable attention to the very earliest layers of vision backbones.

Summary updated

BibTeX
@misc{xia2022vision,
  author = {Zhuofan Xia and Xuran Pan and Shiji Song and Li Erran Li and Gao Huang},
  title = {{Vision Transformer with Deformable Attention}},
  year = {2022},
  eprint = {2201.00520},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2201.00520},
}
Figure