Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Ask 100 questions, get a set of answers
DETR replaces most of a detector’s hand-built machinery with a transformer. It has 100 learned “queries”, each asking in effect “is there an object for me to describe?” Each query answers with a class, possibly “no object”, and a box. During training, each true object is paired with exactly one query by solving a matching problem, so the network learns to give each object one answer and to say “no object” otherwise. Duplicates are discouraged by the training itself, so no clean-up step is needed.
DETR made detection end to end, with no anchors and no NMS, and the same set-prediction idea now drives segmentation (Mask2Former), tracking and 3-D detection for autonomous driving.
- 1. DETR predicts…
- 2. Each true object is matched to…
- 3. DETR needs NMS?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Architecture:
- ResNet backbone (stride 32), then a 1 × 1 conv to d = 256;
- flatten to H/32 · W/32 tokens with sinusoidal positional encodings, e.g. 25 × 38 = 950 tokens for 800 × 1216;
- a 6-layer encoder and a 6-layer decoder with N = 100 object queries;
- an FFN per query predicts a class (C + 1 with “no object”) and a normalised box.
- Matching cost for prediction i and object j: −p̂_i(c_j) + 5·‖b_i − b_j‖₁ − 2·GIoU(b_i, b_j). The Hungarian algorithm finds the one-to-one assignment of minimum total cost.
- Set loss:
- matched predictions get cross-entropy on their class plus L1 and GIoU box losses;
- unmatched predictions get cross-entropy on “no object”, weighted by 0.1;
- with 7 objects, 93 of the 100 queries are unmatched.
- Greedy versus Hungarian:
- cost matrix [[1, 2], [2, 10]]: greedy picks 1, then is forced to 10 (total 11), while the optimum is 2 + 2 = 4;
- for [[1, 2], [2, 1]] both give 2.
- 1. DETR's object queries:
- 2. Weight of the 'no object' class term:
- 3. Matching algorithm:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.DETR in code
Exercise 1.
- The Hungarian solver and a brute-force search over all 120 assignments gave the same optimal cost.
- The set loss down-weighted unmatched predictions by 0.1.
- The sinusoidal encoding of position 0 was (0, …, 0, 1, …, 1).
Practice.
- DETR is available in the original Facebook repository and in Hugging Face Transformers (
DetrForObjectDetection). - Fine-tuning needs long schedules: DETR trained for 500 epochs on COCO, against 109 for the comparable Faster R-CNN baseline in the paper.
- Auxiliary losses after every decoder layer help training.
- Matching costs: class 1, L1 5, GIoU 2.
- AdamW: learning rate 10⁻⁴ for the transformer, 10⁻⁵ for the backbone, with gradient clipping.
- Deformable DETR, DINO and RT-DETR (E9-13) fix the slow convergence and the small-object weakness.
from scipy.optimize import linear_sum_assignment
C = -probs[:, gt_cls] + 5 * l1(pred_boxes, gt_boxes) - 2 * giou(pred_boxes, gt_boxes) # (100, n_obj)
rows, cols = linear_sum_assignment(C) # one query per object- 1. DETR's COCO training length:
- 2. Auxiliary losses are applied…
- 3. DETR's optimiser:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Detection as set prediction.
- Anchor-based detectors assign many predictions to one object (one-to-many), then remove duplicates with NMS.
- DETR’s loss is invariant to the order of predictions and charges duplicates: once an object is matched, any second prediction of it is unmatched and trained towards “no object”.
- Decoder self-attention lets queries communicate, so they can avoid predicting the same object.
Hungarian matching. Finding the minimum-cost one-to-one assignment is a linear assignment problem, solved exactly in O(n³) by the Hungarian algorithm. A greedy matcher is fine when each object has one clearly best prediction. When predictions are noisy and objects crowd together, greedy can lock in a pairing that forces an expensive one later (exercise 2), which gives worse training targets.
Strengths and weaknesses.
- Global self-attention reasons over the whole image: AP on large objects was 61.1 against 53.4 for Faster R-CNN-FPN.
- Small objects were weaker (20.5 against 26.6), because DETR uses only a stride-32 map; multi-scale attention is quadratic in tokens.
- Training converged slowly, because attention starts nearly uniform and must learn to focus.
- 1. One-to-one matching discourages…
- 2. Hungarian algorithm complexity:
- 3. DETR's weakness:
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does matching need to be optimal?
History.
- H. W. Kuhn (1955) published the Hungarian method.
- R. Stewart, M. Andriluka and A. Ng (2016) used set prediction with bipartite matching for crowds.
- N. Carion, F. Massa, G. Synnaeve, N. Usunier, A. Kirillov and S. Zagoruyko (2020) introduced DETR at ECCV.
- Successors include Deformable DETR, Conditional DETR, DAB-DETR, DN-DETR, DINO and RT-DETR (E9-13).
Exercise 2. In synthetic scenes with 5 objects and 20 predictions, of which several are noisy copies near each object, we compute DETR’s matching cost and compare:
- the Hungarian optimum against a greedy matcher (repeatedly take the cheapest remaining pair);
- positives per object under one-to-one matching and under IoU ≥ 0.5 assignment.
This is done for four levels of prediction noise and crowding, 120 scenes each:
| prediction noise, crowding | greedy worse than Hungarian | mean excess cost | positives per object, one-to-one | IoU ≥ 0.5 |
|---|---|---|---|---|
| 0.03, spread | 0.000 | 0.0000 | 1.0 | 2.52 |
| 0.08, spread | 0.008 | 0.0010 | 1.0 | 0.46 |
| 0.08, crowded | 0.058 | 0.0136 | 1.0 | 0.55 |
| 0.15, crowded | 0.267 | 0.1235 | 1.0 | 0.13 |
- Greedy is fine for clean predictions but not for messy ones. Early in training, when predictions are noisy and objects crowd together, it gives suboptimal targets in up to a quarter of scenes, so DETR uses the exact Hungarian algorithm.
- One-to-one matching always gives exactly one positive per object.
- IoU-threshold assignment gives several positives per object for good predictions (2.5, duplicates that need NMS) and almost none for poor ones (0.13).
DETR in AI, robotics and aerospace
- End-to-end pipelines: no NMS or anchor tuning simplifies deployment and makes detectors easier to extend to tracking (MOTR) and 3-D (DETR3D, PETR) for driving.
- Set prediction: the matching loss applies to any task that outputs an unordered set, such as landmarks, keypoints or trajectories.
- Caution: small-object performance and training cost matter for aerial imagery; use deformable variants (E9-13).
References
- N. Carion et al. (2020). “End-to-end object detection with transformers.” Proc. ECCV.
- H. W. Kuhn (1955). “The Hungarian method for the assignment problem.” Naval Research Logistics Quarterly 2.
- R. Stewart, M. Andriluka, A. Y. Ng (2016). “End-to-end people detection in crowded scenes.” Proc. CVPR.