DETR — Detection as Set Prediction, No Anchors and No NMS

  1. 1Attention Before Transformers — Score, Squash, Multiply
  2. 2Query, Key, Value — Dot-Product Attention from Scratch
  3. 3Attention Is All You Need — The Transformer, Block by Block
  4. 4Attention Meets Pixels — Windows, Axes and the First Fully Attentional Networks
  5. 5DETR — Detection as Set Prediction, No Anchors and No NMS
  6. 6An Image Is Worth 16×16 Words — ViT, Patch by Patch
  7. 7DeiT — Training a Vision Transformer on ImageNet Alone
  8. 8Swin Transformer — Windows, Shifts and a Pyramid for Dense Prediction
  9. 9Convolutions Strike Back — Was It the Attention or the Recipe?
  10. 10Masked Autoencoders — Hide 75 %, Encode the Rest
  11. 11CLIP — Learning Pictures From the Words Next to Them
  12. 12Vision Transformers Today — May 2022 to October 2026, Year by Year

Summary

How DETR turns detection into predicting a set: 100 learned object queries, a one-to-one matching found by the Hungarian algorithm, and a loss that needs no anchors and no NMS. After this post you can write the matching cost, the set loss and the box loss from memory, and say exactly why DETR trains slowly and how Deformable DETR fixed it.

Table of Contents

  1. 1. Detection as Set Prediction2
  2. 2. The Architecture, Tensor by Tensor2
  3. 3. Bipartite Matching3
  4. 4. The Set Loss4
  5. 5. What the Queries Learn5
  6. 6. Results5
  7. 7. Slow Convergence and Its Fix5
  8. 8. What Came Next5
  9. 9. References5
  10. 10. Connections5
TrivediInformational[Page 1]
S. TrivediOct 2026

Every detector in Object Detection — A Quick Read predicts a set of boxes, but none of them is trained as one. [CARI20] put the problem statement in a sentence: detectors "address this set prediction task in an indirect way, by defining surrogate regression and classification problems on a large set of proposals, anchors, or window centers", and then lean on "postprocessing steps to collapse near-duplicate predictions". DETR (May 2020) removes the surrogates. The pipeline is one line: CNN → transformer encoder → transformer decoder fed N learned queries → N (class, box) pairs, matched one-to-one with the ground truth.

image3×H₀×W₀ResNet-50stride 32f2048×H×W1×1convd = 256HW tokens+ 2-D sinepositionsTransformerencoder6 layersTransformerdecoder6 layersmemoryN = 100object querieslearned, R^(100×256)FFNlinear100 boxescx,cy,w,h100 classes91 + ∅a set:no anchors, no NMSparallel decoding: all 100 queries at once, self-attention among them, cross-attention to the memoryHungarian loss (Eq. 3)after every decoder layer
Fig. 1 DETR end to end. A ResNet-50 feature map is projected to d = 256 and flattened into HW tokens; six encoder layers read them; six decoder layers turn N = 100 learned object queries into 100 predictions, each a box and a class that may be "no object" (∅). Nothing is suppressed afterwards.

Brush up before you start ;)

1. Detection as Set Prediction

Picture 100 raised hands and 7 questions. Each question goes to the hand that already looks closest to the answer; everyone else puts their hand down (that is ∅). No two hands get the same question, so nobody needs shushing afterwards. That is DETR: the 100 hands are the object queries, the pairing is the bipartite matching, the shushing it does not need is NMS.

Two things make a prediction a set rather than a list:

  • A fixed size N, larger than any image needs. N = 100; COCO averages 7 instances per image and tops out at 63 in train (§4). The spare slots predict a special class ∅, "no object", which "plays a similar role to the 'background' class in the standard object detection approaches" (§3.2).
  • A loss that does not care about order. The matching of §3 pairs each ground truth with exactly one slot, so permuting the slots changes nothing. The paper's words: the matching "plays the same role as the heuristic assignment rules used to match proposals or anchors to ground truth objects in modern detectors. The main difference is that we need to find one-to-one matching for direct set prediction without duplicates" (§3.1).

2. The Architecture, Tensor by Tensor

Let's follow the shapes through Fig. 1.

  • Backbone. ximg∈ℝ3×H0×W0 → ResNet-50 with frozen BatchNorm → f∈ℝ2048×H×W, H=H0/32, W=W0/32. A 1×1 convolution brings 2048 down to d=256; the map is flattened into HW tokens of width 256.
  • Encoder. Six standard layers of Attention Is All You Need — The Transformer, Block by Block, 8 heads. One change: fixed 2-D sine positional encodings are added to the queries and keys at every attention layer, not once at the input (A.3).
  • Decoder. Six layers. The input is not a shifted target sequence but N = 100 object queries: learned embeddings in ℝ100×256 (Listing 1 of the paper initialises them as nn.Parameter(torch.rand(100, hidden_dim))). They are decoded in parallel, with self-attention among the 100 and cross-attention to the HW encoder tokens. No causal mask, no autoregressive loop (§3.2).
  • Heads. A 3-layer MLP (hidden 256, ReLU) gives (cx,cy,w,h)∈[0,1]4 through a sigmoid; a linear layer gives a softmax over 91 COCO classes plus ∅. Output: 100 × (92 logits + 4 coordinates). The same heads, with auxiliary matching losses, sit after every decoder layer.

At the largest input the paper allows, shortest side 800 and longest side 1333 (§4), the encoder sees 800/32×1333/32=25×41= 1,025 tokens of width 256. The DC5 variant doubles the map's resolution: 50 × 82 = 4,100 tokens, so the encoder's attention matrix is 16× larger, which the paper books as "an overall 2x increase in computational cost" (§4). That is Eq. 1 of Attention Meets Pixels — Windows, Axes and the First Fully Attentional Networks deciding a design. Parameters: 41.3 M = 23.5 M ResNet-50 + 17.8 M transformer (§4.1).

NOTE: DETR does not remove the backbone. ResNet-50 stays; what the transformer replaces is the region proposal network, the RoI heads and NMS.

TrivediInformational[Page 1]
S. TrivediOct 2026

3. Bipartite Matching

3.1 The Problem

Call the ground truth y={yi}, padded with ∅ up to size N, and the predictions y^={y^i}i=1N. We want the pairing with the smallest total cost (Eq. 1 of the paper):

σ^=argminσ∈𝔖N∑i=1Nℒmatch(yi, y^σ(i))(1)
  • σ: a permutation of N elements; 𝔖N is the set of all N! of them; σ(i) is the slot assigned to ground truth i.
  • The Hungarian algorithm finds σ^ in polynomial time. Nobody tries 100! permutations. My picture for it: a wedding seating plan, guests (truths), chairs (slots), a cost per pair, and you want the cheapest full seating, not the greedy one.

3.2 The Cost of One Pair

Each ground truth is yi=(ci,bi): a class ci∈{1..91}∪{∅} and a box bi∈[0,1]4 as centre, height and width relative to the image. The pair cost is (§3.1):

ℒmatch(yi,y^σ(i))=−1{ci≠∅} p^σ(i)(ci)+1{ci≠∅} ℒbox(bi,b^σ(i))(2)
  • p^σ(i)(ci): the slot's softmax probability for the true class; b^σ(i): its box; 1: an indicator that switches both terms off for padded ∅ entries.
  • A slot is cheap for an object if it already believes in that class and its box is close. The class term uses the probability, not its log, on purpose: the paper says it keeps the term commensurable with the box term.
  • Padded rows cost the same whatever the slot does, so they never influence which slot gets a real object. With M real objects the matcher effectively works on an M × 100 matrix; for a typical COCO image that is 7 × 100 = 700 costs.
ground truth y, padded to Npredictions ŷ, N = 100A, small boxB, large boxC, medium box∅ … ∅ (N − 3 of them)slot 1 p(C) = .45slot 2 p(A) = .55slot 4 p(B) = .50slot 6 p(B) = .70slot 7 p(A) = .25… slot 99thin: a cost L_match for every pairbold: the assignment σ̂ with the smallest totalmatched slots learn class + box; the other 97 learn ∅. No slot is used twice, so there is nothing to suppress.
Fig. 2 The matching as a bipartite graph. Every (truth, slot) pair has a cost from Eq. 2 (thin); the Hungarian algorithm keeps the one-to-one assignment with the smallest total (bold). Matched slots learn their object; every other slot learns ∅.

Now the same thing with numbers. The grid below is Eq. 2 for three objects and eight of the hundred slots; raise the class weight, or drag slot 4 across the image, and watch the outlined cells jump.

10.0
The cost matrix the matcher sees. Rows are the three objects, columns are 8 of the 100 slots; darker is cheaper, the outlined cells are the chosen assignment. Raise the class weight and the matcher starts trusting what a slot believes over where its box is; slide slot 4 across the image and watch it steal an object.
  • At the paper's weight (1) the matcher gives object A to slot 7, whose box fits; from a class weight of 3.25 on it switches to slot 2, which is surer of class A (0.55 against 0.25) even though its box is worse.
  • Slide slot 4 right: between a shift of 0.22 and 0.34 its box sits on object C and it steals it from slot 1, although slot 1 believes in C more (0.45 against 0.30); push further and slot 1 gets it back. The matcher weighs belief and geometry together, every time.
TrivediInformational[Page 2]
S. TrivediOct 2026

4. The Set Loss

4.1 The Hungarian Loss

With σ^ fixed, the loss is an ordinary classification + box loss over every slot (Eq. 2 of the paper):

ℒHungarian(y,y^)=∑i=1N[−logp^σ^(i)(ci)+1{ci≠∅} ℒbox(bi,b^σ^(i))](3)
  • Compare with Eq. 2: the probability became a log-probability, and the class term no longer has an indicator. Every one of the 100 slots gets a classification loss: the matched ones toward their object's class, the other ~93 toward ∅. Only matched slots get a box loss.
  • No gradient flows through the matcher. σ^ is a discrete assignment treated as a constant; only the loss terms are differentiated.
  • Imbalance: in a 7-object image, 93 % of the class terms say "nothing here". The ∅ log term is down-weighted by 10, which the paper calls "analogous to how Faster R-CNN training procedure balances positive/negative proposals by subsampling" (§3.1). This is DETR's own trick; the focal-loss answer to the same problem is in Understanding Focal Loss —A Quick Read, and Deformable DETR will use it.
  • Losses are normalised by the number of objects in the batch (A.2).

4.2 The Box Loss

ℓ1 on normalised coordinates has a scale problem: a 10-pixel miss is a disaster on a small box and a rounding error on a large one. So the box loss mixes ℓ1 with a scale-free term (Eq. 9–10 of the paper, weights from A.4):

ℒbox(b,b^)=λiou ℒiou(b,b^)+λL1 ‖b−b^‖1,λiou=2,  λL1=5ℒiou(b,b^)=1−(|b∩b^||b∪b^|−|B(b,b^)⧵(b∪b^)||B(b,b^)|)(4)
  • ‖·‖1: the sum of absolute differences of the four coordinates; |·|: area; B(b,b^): the smallest box enclosing both.
  • The bracket is the generalised IoU (the paper's ref. [38]): IoU minus the empty fraction of the enclosing box. When the boxes stop overlapping, IoU is 0 and stuck, but the second term keeps growing, so there is still a gradient. GIoU is not "IoU with a sign"; it is IoU with a penalty for the air between the boxes.
  • The same ℒbox, with the same weights, is used inside Eq. 2 (§4.2: "using the same weighting every time").

Let's slide a prediction off its box and watch the three terms. In the figure the prediction keeps the truth's shape and moves along x; push it off vertically with dy, or change its size, and the overlap dies sooner.

0.01.0
ℓ1 and GIoU as the box slides away. The prediction keeps the ground truth's shape and slides along x. The dotted line is where the overlap reaches zero: plain IoU loss goes flat there, GIoU keeps rising because the empty part of the enclosing box keeps growing. Push dy or the scale and the overlap vanishes sooner.
  • Past the dotted line the IoU loss is flat at 1 and would teach nothing; the GIoU loss keeps climbing toward 2. And the third tile is the scale problem: the same 0.4 shift gives the same ℓ1 on a 0.06 × 0.04 box, but a much larger GIoU loss, which is the whole reason for mixing the two.
TrivediInformational[Page 3]
S. TrivediOct 2026

5. What the Queries Learn

  • Slots are not anchors and not class slots. Fig. 7 of the paper shows each slot specialising in regions and box sizes, with several modes each; "almost all slots have a mode of predicting large image-wide boxes".
  • They generalise past the training counts. Fig. 5: all 24 giraffes in a synthetic image are found, although no training image has more than 13.
  • Self-attention among the queries is the duplicate remover. Fig. 4: adding NMS (IoU 0.7) helps after decoder layer 1, because "there is no communication in the first layer", and slightly hurts after layer 6. Decoder depth is worth +8.2 AP from layer 1 to layer 6.
  • Position matters, even for a set. Table 3: no spatial encoding at all costs 7.8 AP (32.8). The encoder tokens carry 2-D sines; the object queries are learned positional encodings.
  • The decoder "typically attends to object extremities, such as legs and heads" (Fig. 6). Removing the encoder entirely costs 3.9 AP (Table 2).

6. Results

COCO valGFLOPs / FPSParamsAPAP50AP75AP_SAP_MAP_L
Faster R-CNN-FPN180 / 2642 M40.261.043.824.243.552.0
Faster R-CNN-FPN+180 / 2642 M42.062.145.526.645.453.4
Faster R-CNN-R101-FPN+246 / 2060 M44.063.947.827.248.156.0
DETR86 / 2841 M42.062.444.220.545.861.1
DETR-DC5187 / 1241 M43.363.145.922.547.361.1
DETR-R101152 / 2060 M43.563.846.421.948.061.8
DETR-DC5-R101253 / 1060 M44.964.747.723.749.562.3

Table. 1 Table 1 of [CARI20], COCO val. FLOPS and FPS measured on torchscript models.

  • The headline is same AP, better large, worse small: 42.0 against 42.0, AP_L 61.1 vs 53.4, AP_S 20.5 vs 26.6.
  • Half the FLOPs (86 vs 180) and 28 FPS against 26. DETR is not slow at inference; it is slow to train: 500 epochs for Table 1, and 300 epochs take 3 days on 16 V100s (§4). Keep the two sentences apart.
  • Ablations: class + GIoU only, 39.9 (−0.7); class + ℓ1 only, 35.8 (−4.8); all three, 40.6 (Table 4). Overriding a ∅ prediction with its second-best class at test time is worth +2 AP (§4).
  • The conclusion names the open problems itself: "training, optimization and performances on small objects".

7. Slow Convergence and Its Fix

[ZHU20] (Deformable DETR, 2021 ICLR) start from the two problems above: "DETR needs 500 epochs to converge, which is around 10 to 20 times slower than Faster R-CNN" and has "relatively low performance at detecting small objects" (§1). Their diagnosis (§3.1) has two parts:

  • Encoder self-attention is quadratic in HW, so high-resolution maps, the ones small objects need, are unaffordable (the 16× of §2).
  • "At initialization, the cross-attention modules are almost of average attention on the whole feature maps", while at the end of training they are very sparse. Learning that sparsity from a uniform start is what takes 500 epochs.

The fix keeps the matching and the set loss and changes only what a query reads. Instead of a softmax over all HW keys, each query reads K sampled points per head (their Eq. 2):

DeformAttn(zq,pq,x)=∑m=1MWm[∑k=1KAmqk·Wm′x(pq+Δpmqk)]∑k=1KAmqk=1(5)
  • zq: the query feature; pq: its 2-D reference point; x∈ℝC×H×W; M = 8 heads; K = 4 samples per head; Δpmqk∈ℝ2: a learned offset; Amqk: a softmaxed weight; Wm,Wm′: the output and value projections. x(·) is read by bilinear interpolation, so the offsets can be fractional.
  • Offsets and weights both come from one linear layer on zq (3MK channels: 2MK offsets, MK weights). The multi-scale version samples over L = 4 feature levels.
  • The bill: deformable attention costs O(2NqC2+min(HWC2,NqKC2)) for Nq queries (§4.1, A.1). In the encoder Nq=HW, so DETR's O(H2W2C) self-attention becomes O(HWC2), linear in the map size, which is what lets the encoder read multi-scale maps at all.

NOTE: deformable attention is not a deformable convolution, although both sample at learned fractional offsets. A deformable conv applies a fixed kernel weight at each sample; here the weight A_mqk is predicted and softmax-normalised, and the query may be an object query rather than a pixel.

COCO valEpochsAPAP_SParamsGFLOPsFPSGPU-hours
Faster R-CNN-FPN+10942.026.642 M18026380
DETR50042.020.541 M86282000
DETR-DC550043.322.541 M187127000
DETR-DC55035.315.241 M18712700
DETR-DC5+ (focal loss, 300 queries)5036.216.341 M18712700
Deformable DETR5043.826.440 M17319325
+ iterative box refinement5045.426.840 M17319325
++ two-stage5046.228.840 M17319340

Table. 2 Training cost against accuracy, Table 1 of [ZHU20] (COCO val; the first three rows repeat Table. 1 above with the epochs and GPU-hours [ZHU20] add). Run time on a V100.

  • 10× fewer epochs (50 vs 500) and 6× fewer GPU-hours (325 vs 2000) for +1.8 AP, and AP_S goes from 20.5 to 26.4, level with Faster R-CNN. DETR-DC5 at the same 50 epochs sits at 35.3; focal loss and 300 queries alone only buy 36.2.

8. What Came Next

Five months after DETR, An Image Is Worth 16×16 Words — ViT, Patch by Patch (October 2020) asked a stranger question: what if there is no CNN at all, and no decoder either? Cut the image into 16×16 patches, feed the 196 tokens to the plain encoder of Attention Is All You Need — The Transformer, Block by Block, and read the class off one extra token. DETR and ViT are independent 2020 papers; the cost arithmetic of §2 is also why a transformer backbone at detection resolution had to wait for the windows of Swin Transformer — Windows, Shifts and a Pyramid for Dense Prediction. Stay tuned.

9. References

  1. [CARI20] Carion, Massa, Synnaeve, Usunier, Kirillov, Zagoruyko. End-to-End Object Detection with Transformers. 2020, arXiv 2005.12872.
  2. [ZHU20] Zhu, Su, Lu, Li, Wang, Dai. Deformable DETR: Deformable Transformers for End-to-End Object Detection. ICLR 2021, arXiv 2010.04159.
  3. [VASW17] Vaswani et al. Attention Is All You Need. 2017, arXiv 1706.03762.

10. Connections

What this page builds on, cites or links to, and what points back at it. A plain box is a “builds on”; “cited” and “linked in the text” are the lighter links. Hover a box for a preview.

POINTS AT THIS PAGEPAPERAn Image Is Worth 16×16 Words — ViT,Patch by PatchPAPERRT-DETR — DETRs Beat YOLOs, Encoder byEncoderREAD · CITEDVision Transformers Today — May 2022 toOctober 2026, Year by YearPAPER · CITEDRevolutionary Object Detection Algorithmfrom Facebook AIPAPER · CITEDRT-DETRv3 — More Positives WithoutPaying at InferencePAPER · CITEDRT-DETR-World — Naming Any Object inReal TimeREAD · LINKED IN THE TEXTAttention Meets Pixels — Windows, Axesand the First Fully Attentional NetworksREAD · LINKED IN THE TEXTConvolutions Strike Back — Was It theAttention or the Recipe?PAPER · LINKED IN THE TEXTAttention Is All You Need — TheTransformer, Block by BlockPAPER · LINKED IN THE TEXTRT-DETRv2 — A Bag of Freebies for theDecoderThis pageTHIS PAGE POINTS ATREADObject Detection — A Quick ReadREADAttention Meets Pixels — Windows, Axesand the First Fully Attentional NetworksPAPERUnderstanding Focal Loss —A Quick ReadPAPERAttention Is All You Need — TheTransformer, Block by BlockPAPER · CITEDRevolutionary Object Detection Algorithmfrom Facebook AIREFERENCE · CITEDDeformable DETR: Deformable Transformersfor End-to-End Object DetectionPAPER · LINKED IN THE TEXTAn Image Is Worth 16×16 Words — ViT,Patch by PatchPAPER · LINKED IN THE TEXTSwin Transformer — Windows, Shifts and aPyramid for Dense Prediction

Related

Go deeper

  1. Start with
    Read
  2. Paper
  3. Paper
  4. Paper
  5. Paper
  6. Read
  7. Read
  8. Paper
  9. Read
  10. Paper
  11. You are here
    DETR — Detection as Set Prediction, No Anchors and No NMS
    Paper
  12. Then
    Paper
  13. Paper
  14. Paper
  15. Read
  16. Paper
  17. Paper
  18. Read
  19. Paper
  20. Paper
  21. Paper
  22. Paper
  23. Paper

Go broader

<- Older [Page 5] Newer ->