Foundations to Frontier
Tests
E. Deep Learning › E9 Object Detection Architectures

Deformable DETR, RT-DETR

Deformable DETR (Zhu et al., 2021) fixed DETR's slow training and weak small-object detection. Each query attends to a few sampled points (4 per head and level) around a reference point on multi-scale feature maps, instead of to every token. It reached higher accuracy than DETR with 10 times fewer epochs. RT-DETR (Zhao et al., 2024) made the approach real-time with an efficient hybrid encoder and query selection, beating YOLO models of similar speed without NMS. This page covers deformable attention, multi-scale features, query selection and NMS-free latency. In the exercises you implement bilinear sampling and deformable attention, then measure costs: global attention grows from 15 to 683 billion multiply–adds with image size, against 0.6 to 4.2 for deformable, and NMS work grows 300-fold from 5 to 100 objects.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Look at a few points, not everywhere

DETR’s attention compares every position with every other, which is slow, learns slowly and cannot afford fine feature maps for small objects. Deformable DETR lets each query look at only a few learned points near where it expects its object, on maps of several resolutions. Training became about ten times faster and small objects improved. RT-DETR then streamlined the design to run in real time, still without anchors or NMS, so its running time does not depend on how crowded the scene is.

Detection lab
kept 7 of 14 boxes above the score threshold (3 objects). Too high an IoU threshold keeps duplicates; too low merges neighbouring objects.
Why engineers care

Real-time transformer detectors with predictable latency suit robots and vehicles, where a crowded scene must not slow the perception loop. Deformable attention is also used in driving perception models (BEVFormer).

📝 Quick check (Level 0)
0/3 answered
  1. 1. Deformable attention looks at…
  2. 2. Deformable DETR trained about … faster than DETR.
  3. 3. RT-DETR's latency does not depend on…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Deformable attention for query q with reference point p:
    • output = Σ_h W_h Σ_l Σ_k A_hlk · x_l(p + Δp_hlk), with H = 8 heads, L = 4 levels and K = 4 points: 128 samples per query;
    • offsets Δp and weights A (a softmax over the L·K points) are predicted from the query by linear layers;
    • samples use bilinear interpolation, e.g. the 4 × 4 map with entries 0…15 at (1.5, 2) gives 9.5.
  • Tokens: 4 levels at strides 8–64 for a 512 × 512 image give 64² + 32² + 16² + 8² = 5,440 tokens.
    • Global attention costs about 2n²d = 15.2 G multiply–adds.
    • Deformable attention is linear in n: about 0.62 G.
  • RT-DETR:
    • Hybrid encoder: attention only within the coarsest scale (AIFI) plus convolutional cross-scale fusion (CCFM).
    • IoU-aware query selection: the top 300 encoder features, scored to reflect localisation quality, initialise the decoder queries.
    • Flexible speed: fewer decoder layers at inference.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Samples per query (8 heads, 4 levels, 4 points):
  2. 2. Global attention cost grows with tokens as…
  3. 3. RT-DETR initialises queries from…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Deformable attention in code

Exercise 1.

  • Bilinear sampling interpolated between neighbouring cells, giving 9.5 at (1.5, 2).
  • Deformable attention averaged its samples with equal weights, or picked one sample when its weight dominated.
  • 128 samples per query.

Practice.

  • Deformable attention needs a custom CUDA kernel, MultiScaleDeformableAttention. Hugging Face has DeformableDetrForObjectDetection and RTDetrForObjectDetection.

  • Deformable DETR improvements:

    • iterative box refinement, where each decoder layer refines the previous boxes;
    • a “two-stage” variant, where encoder proposals become queries.

    With both it reached 46.2 AP.

  • RT-DETR:

    • ResNet-50 and ResNet-101 backbones reach 53.1 and 54.3 AP at 108 and 74 frames per second on a T4 GPU with TensorRT;
    • fine-tunes like YOLO models;
    • exports to ONNX and TensorRT.
from transformers import RTDetrForObjectDetection
model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd")    # NMS-free real-time detector
🐍 Exercise 1: deformable attention
Implement bilinear sampling and single-head, single-level deformable attention, and count sampled points.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Deformable DETR with refinement and two-stage reached…
  2. 2. RT-DETR-R50 speed on a T4:
  3. 3. Iterative box refinement means…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why attention converged slowly. DETR’s attention weights start nearly uniform over all tokens, and must learn to concentrate on a few relevant locations, which takes many epochs. Deformable attention starts from a small, local set of sampling points around a reference, a strong prior, and learns where to move them. Training reached higher accuracy in 50 epochs (43.8 AP) than DETR in 500 (42.0).

Multi-scale without quadratic cost. Global attention over n multi-scale tokens costs O(n²d); deformable attention costs O(n·HLK·d). Exercise 2 shows global attention growing 45-fold from 512 to 1,333 pixels while deformable grows 7-fold. This makes stride-8 features affordable, which raised small-object AP from 20.5 to 26.4.

Why real time without NMS matters.

  • NMS costs up to n(n − 1)/2 IoU computations for n candidates.
  • Its run time depends on how many boxes pass the score threshold, so it varies with the scene and with the threshold.
  • RT-DETR’s latency is fixed by the architecture.
  • The authors showed end-to-end detectors can beat similarly fast YOLO models once NMS time is counted.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Deformable attention's cost in tokens n:
  2. 2. Small-object AP rose from…
  3. 3. NMS run time depends on…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

What do sparse attention and NMS-free buy?

History.

  • J. Dai et al. (2017) introduced deformable convolution (E7-9), the idea behind deformable attention.
  • X. Zhu, W. Su, L. Lu, B. Li, X. Wang and J. Dai (2021) introduced Deformable DETR at ICLR.
  • H. Zhang et al. (2023) introduced DINO (DETR with denoising anchor boxes).
  • Y. Zhao et al. (2024) introduced RT-DETR (“DETRs beat YOLOs on real-time object detection”) at CVPR.

Exercise 2. This is a calculation, not a training run:

  1. Multiply–adds of global self-attention and deformable attention over 4-level multi-scale tokens, for several image sizes.
  2. IoU computations of greedy NMS on candidates clustered around objects (8 per object), for scenes with 5 to 100 objects.
image side tokens global attention (G) deformable (G) ratio
512 5,440 15.2 0.62 24
800 13,269 90.1 1.52 59
1,024 21,760 242.4 2.50 97
1,333 36,526 683.1 4.19 163
objects NMS IoU computations
5 127
20 1,725
50 10,867
100 40,376
  • Global attention over multi-scale features is prohibitive at detection resolutions. Deformable attention keeps it linear, 163 times cheaper at 1,333 pixels.
  • NMS work grows quadratically with crowding, about 300 times from 5 to 100 objects, so latency depends on the scene. An NMS-free detector avoids that variability.
  • These are operation counts. Real latency also depends on memory access; the custom deformable-attention kernel is irregular in memory.
🐍 Exercise 2 (research): what do sparse attention and NMS-free buy?
Compare global and deformable attention cost across image sizes, and NMS work across scene crowding.

Deformable and real-time DETR in AI, robotics and aerospace

  • Bird’s-eye-view perception: deformable attention lifts multi-camera images into a bird’s-eye view for autonomous driving (BEVFormer).
  • Predictable latency: NMS-free detectors give constant processing time, which simplifies real-time scheduling on robots and drones.
  • Small objects: multi-scale deformable attention helps with distant objects in aerial and driving scenes.

References

  1. X. Zhu et al. (2021). “Deformable DETR: deformable transformers for end-to-end object detection.” Proc. ICLR.
  2. Y. Zhao et al. (2024). “DETRs beat YOLOs on real-time object detection.” Proc. CVPR.
  3. H. Zhang et al. (2023). “DINO: DETR with improved denoising anchor boxes for end-to-end object detection.” Proc. ICLR.
  4. Z. Li et al. (2022). “BEVFormer: learning bird’s-eye-view representation from multi-camera images via spatiotemporal transformers.” Proc. ECCV.