Deformable DETR

Deformable DETR is an end-to-end object detector that replaces dense Transformer attention in DETR with deformable attention.

  • Paper: Zhu, Su, Lu, Li, Wang and Dai (SenseTime Research / USTC / CUHK), Deformable DETR: Deformable Transformers for End-to-End Object Detection, ICLR 2021, arXiv:2010.04159.
  • Kept from DETR: the set-prediction formulation — Hungarian matching, no NMS, no anchors.
  • Core idea: replaces the dense Transformer attention over feature-map pixels with a (multi-scale) deformable attention module that attends to only a small fixed set of learned sampling points around a reference point.
  • Headline result: removes both of DETR’s practical defects at once: it converges in 50 epochs instead of 500 ( fewer) and it can consume high-resolution multi-scale feature maps, lifting small-object AP substantially.

Deformable DETR detector overview

Motivation: why DETR is slow and weak on small objects

Both problems trace back to the same source — Transformer attention treating every feature-map pixel as a key element.

  • Slow convergence. With standard initialization the attention logits are near-uniform, so at the start of training. In the image domain is huge, so the gradients w.r.t. individual keys are ambiguous and it takes a very long schedule for the attention maps to sharpen onto the few meaningful locations they end up using. DETR needs 500 epochs on COCO, roughly 10–20× slower than Faster R-CNN.
  • Limited spatial resolution. Encoder self-attention has complexity ; with the third term dominates, i.e. — quadratic in the spatial size. High-resolution or multi-scale feature maps are therefore unaffordable, and small objects suffer (DETR: vs Faster R-CNN’s ).

Deformable convolution (Dai et al., 2017) already attends to sparse spatial locations efficiently, but lacks the element-relation modelling that makes Transformers work. Deformable attention is the combination of the two: sparse, data-dependent sampling as a pre-filter for prominent key elements, with attention weights over the sampled points.

Deformable attention module

Deformable attention module

Given an input feature map , a query element with content feature and a 2-d reference point :

where:

  • — index of the attention head; each head samples its own points and has its own weights.
  • — index of the sampled point within a head. (in practice ), so a query looks at a handful of locations instead of all pixels.
  • — the query’s reference point, a 2-d location in the feature map: where this query starts looking.
  • — the sampling offset, an unconstrained real-valued 2-d shift predicted for point of head . It says “look this far from the reference point”.
  • — the feature read out at that shifted location. The location is fractional, so this is a bilinear interpolation of the four surrounding pixels (which also makes the offsets differentiable).
  • — the attention weight for that sampled point, with per head: how much this particular sample contributes.
  • — value projection of head , mapping the sampled feature into the head’s -dimensional subspace.
  • — output projection of head , mapping the head’s result back to channels before the heads are summed.

Read from the inside out, the formula is therefore: for each head, sample points at learned offsets around the reference point, take a weighted average of their (projected) features, project the result back, and sum over heads. It has exactly the shape of ordinary multi-head attention — the only change is that the inner sum runs over chosen locations rather than over all key elements.

The crucial structural difference from standard attention: both the offsets and the attention weights are predicted directly from the query feature , not from query–key dot products. A single linear projection maps to channels — the first are the offsets , the remaining go through a softmax to give . No key is ever touched to decide whether to attend to it, which is what removes the term.

Multi-scale deformable attention

The module extends to feature levels with essentially no extra machinery. With normalized reference-point coordinates (where and are the top-left and bottom-right image corners):

where the new pieces relative to the single-scale version are:

  • — index of the feature level; the inner sum now runs over levels and sampling points.
  • — the reference point in image-normalized coordinates, so one point addresses all levels regardless of their resolutions.
  • — rescales that normalized coordinate to the pixel grid of level (multiply by ).
  • , — offset and weight for point at level in head ; the weights are now normalized jointly across levels, , so a head can shift its budget toward whichever scale is informative.

Each query now samples points spread across all levels, so cross-scale information exchange happens inside the attention module itself — no FPN top-down path is needed (and the ablation confirms adding one does not help).

Two degenerate cases are worth noting: with , and fixed to identity, the module is deformable convolution; when the sampling points cover all locations, it is ordinary Transformer attention.

Complexity

Deformable attention costs (Appendix A.1; with , , the so those terms vanish into the constant).

  • In the encoder, , so complexity is — linear in spatial size, versus DETR’s .
  • In the decoder cross-attention, (number of object queries), so complexity is — completely independent of .

Architecture

Encoder

Multi-scale feature maps () are taken from ResNet stages – via convolutions, plus a lowest-resolution level obtained by a stride-2 convolution on . All levels have channels; strides are .

Constructing the multi-scale feature maps

All Transformer attention modules are replaced by multi-scale deformable attention. Query and key elements are both pixels of the multi-scale maps, and for each query pixel the reference point is the pixel itself. To let the model know which level a query came from, a learned scale-level embedding is added to the feature alongside the (fixed) positional encoding; the are randomly initialized and trained jointly.

Decoder

Only the cross-attention modules are replaced by multi-scale deformable attention — self-attention among object queries keeps standard Transformer attention, since its key elements are queries, not feature-map pixels.

For each object query, the 2-d normalized reference point is predicted from the query embedding by a linear projection + sigmoid. Because deformable cross-attention gathers its features from around , the detection head is made to predict the box relative to that same point rather than in absolute image coordinates (Appendix A.3):

Reading it piece by piece. The head outputs four raw numbers . The width and height terms are the simple case: just squashes a raw number into , since the box size is expressed as a fraction of the image and has nothing to offset against.

The center is where the reference point enters. We want “center = reference point, nudged a bit”, but already lives in the bounded interval, so adding a raw offset to it could leave that interval. The fix is to do the addition in logit space:

  1. maps the reference coordinate back to an unbounded real number ().
  2. Add the predicted offset there, where addition is unconstrained and well-behaved.
  3. maps the sum back into , guaranteeing — valid normalized coordinates by construction, no clipping needed.

So is an initial guess of the box center and the head only has to learn a correction to it. Two consequences: the regression problem is easier (predict a small delta, not an absolute position), and the learned decoder attention ends up strongly correlated with the predicted box, since the points it samples and the box it outputs are anchored to the same . Both accelerate convergence.

The same trick reappears in the two variants below, where the thing being corrected is the previous decoder layer’s box instead of the reference point.

Variants

Both variants are enabled by the cheap, fast-converging backbone and can be stacked.

Iterative bounding box refinement. Each decoder layer refines the box predicted by the previous layer (inspired by RAFT’s iterative refinement). At layer :

Prediction heads are not shared across decoder layers, and gradients are blocked at to stabilize training. Initial boxes: , , (results are robust to ). The cross-attention of layer also uses as its reference point and modulates the sampling offsets by box size, , so sampling locations scale with the current box.

Two-stage Deformable DETR. Object queries in plain DETR are image-independent. Here the first stage is an encoder-only Deformable DETR used as a region proposal network: every pixel of the multi-scale maps is an object query and directly predicts a box (3-layer FFN for regression, linear layer for foreground/background classification), trained with the same Hungarian loss. Top-scoring boxes are picked as proposals — no NMS. The decoder is removed in this stage to avoid the quadratic self-attention cost over queries. In the second stage, proposals become the initial boxes and their positional encodings become the object queries, then iterative refinement runs as above. Pixel at level with normalized coordinates predicts

with base object scale .

Hyperparameters

Reported training configuration (Section 5, Appendix A.4):

SettingValue
DatasetCOCO 2017 (train / val / test-dev)
BackboneImageNet-pretrained ResNet-50 (default)
Feature levels, extracted from – (+ stride-2 conv on ),
Attention heads
Sampling points per head per level
Object queries (DETR uses 100)
Classification lossFocal loss, loss weight 2
Epochs50, LR decayed at epoch 40
OptimizerAdam, base LR , , , weight decay
LR multiplier for the linear projections predicting reference points and sampling offsets
Decoder layers
HardwareNVIDIA Tesla V100

Initialization. random; weights of the linear projection predicting and set to zero; biases set so that at initialization and the heads sample in the 8 compass directions, , , , , , , , for . With iterative refinement, these offset biases are further scaled by so initial sampling points lie inside the previous layer’s box.

Results

COCO 2017 val (Table 1):

MethodEpochsAPAPAPAPParamsFLOPsGPU hoursFPS
Faster R-CNN + FPN10942.026.645.453.442M180G38026
DETR50042.020.545.861.141M86G200028
DETR-DC550043.322.547.361.141M187G700012
DETR-DC55036.216.339.253.941M187G70012
Deformable DETR5043.826.447.158.040M173G32519
+ iterative box refinement5045.426.848.361.740M173G32519
++ two-stage5046.228.849.261.740M173G34019

jumps from 20.5 to 26.4–28.8, i.e. the small-object gap to Faster R-CNN closes and then reverses. Training cost drops from 2000–7000 GPU hours to ~325.

Convergence curves vs DETR-DC5

Ablations on deformable attention (Table 2, COCO val): single-scale (≈ deformable convolution) gives 39.7 AP; single-scale 42.3; multi-scale inputs + but no multi-scale attention 42.3 → multi-scale deformable attention 43.8. So multi-scale inputs are worth AP ( AP), larger another , and cross-level attention . Adding FPN (43.8) or BiFPN (43.9) on top changes nothing — the module already exchanges information across levels.

State of the art (Table 3, COCO test-dev): ResNet-101 → 48.7 AP; ResNeXt-101 → 49.0 AP; ResNeXt-101 + DCN → 50.1 AP; with test-time augmentation (horizontal flip + multi-scale testing) → 52.3 AP.

What the model looks at (Appendix A.5/A.6): gradient-norm visualizations show Deformable DETR attends to the left/right object boundaries when predicting and , and to top/bottom for and — similar to DETR. Unlike DETR, it also looks at pixels inside the object, which is what the category score needs; decoder attention focuses on the whole foreground instance rather than only extreme points.

Limitations

  • Despite FLOPs on par with Faster R-CNN + FPN, inference is ~25% slower (19 vs 26 FPS) because deformable sampling implies unordered memory access. It is still faster than DETR-DC5, whose bottleneck is memory access in dense attention.
  • Sampling offsets and attention weights come from the query alone; the module never compares query and key content, so it is a pre-filter heuristic rather than full content-based retrieval.
  • Performance on large objects (AP 58.0 without refinement) is below DETR’s 61.1 — the global receptive field of dense attention still helps there, and only the refinement variants recover it.
  • Custom CUDA kernels are required for the multi-scale deformable attention op; it is not expressible efficiently with stock framework primitives.

See also