DL0054 Deformable Attention

What is Deformable Attention and how does it reduce computational complexity for object detection tasks?

Answer

Deformable Attention is a sparse attention mechanism that learns dynamic sampling locations instead of attending to all spatial positions uniformly. It uses learned 2D offsets from reference points to sample only the most relevant features, reducing complexity from O(N^2) to O(NK) where K is a small constant (typically 4). This makes it ideal for high-resolution feature maps in object detection where full attention is computationally prohibitive — for a 1024×1024 feature map, standard attention requires ~1M operations per head while deformable attention needs only ~4K.

Sparse Sampling Locations Diagram

Figure 1: Deformable attention learns K=4 sampling offsets per query point instead of dense N×N attention

(1) Sparse Sampling: Instead of computing attention over all N \times N positions, deformable attention samples only K reference points per query, typically K=4 or 8, reducing the key-value set from N to K.
(2) Learned Offsets: The model predicts \Delta p_{mk} offsets from each reference point p_k using a lightweight linear layer on query features, requiring only O(NC) additional computation where C is channel dimension.
(3) Bilinear Interpolation: When offsets point to non-integer locations, bilinear interpolation computes feature values from the 4 nearest pixels, enabling sub-pixel precision sampling without modifying the feature map.

Complexity Comparison Chart

Figure 2: Complexity comparison shows O(NK) grows linearly while O(N²) becomes prohibitive for large feature maps

Mathematical Formulation:
y(p) = \sum_{m=1}^{M} W_m \left[ \sum_{k=1}^{K} A_{mk} \cdot W_m' x(p + p_k + \Delta p_{mk}) \right]

Where:

  •  p is the reference position (query location on the feature map)
  •  M is the number of attention heads
  •  K is the number of sampled keys per head (typically 4)
  •  p_k are fixed reference offsets (uniformly initialized)
  •  \Delta p_{mk} are learned deformable offsets (2D, predicted per head per key)
  •  A_{mk} is the attention weight (normalized, not from softmax over all positions)
  •  W_m, W_m' are projection matrices for each head

The offsets \Delta p_{mk} are predicted by a linear projection from query features: \Delta p_{mk} = W_\text{offset} \cdot q_m(p), where W_\text{offset} \in \mathbb{R}^{C \times 2K}. The attention weights A_{mk} are computed via a separate softmax over only K elements, not the full N positions. In Deformable DETR, multi-scale deformable attention extends this to sample across multiple feature map resolutions simultaneously, enabling the model to capture both small and large objects efficiently.


Login to view more content

Did you solve the problem?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *