Transformer-based Detectors
DETR predicts a fixed set of boxes and matches each truth exactly once, so anchors and NMS disappear; deformable attention makes it train fast by sampling a few points per scale.
Why Does This Exist?
Classic detectors decide by hand where to look. Engineers tile the image with thousands of anchor boxes, label each one with overlap thresholds, then prune the surviving pile with non-maximum suppression, which cannot pass gradients and needs its own threshold. Crowded scenes break the heuristics first: two overlapping people produce a thicket of near-duplicate boxes that no single NMS threshold separates cleanly. The non-maximum suppression visual shows the cleanup step this family removes; the general setup with precision numbers lives in object detection.
DETR (Carion and coauthors, 2020) deletes the whole pipeline by framing detection as set prediction: a CNN backbone plus a transformer predicts a fixed set of boxes in one shot, and a one-to-one matching pairs each truth with exactly one prediction, so duplicates are impossible by construction. The price is time. The matching must settle before the boxes sharpen, which takes about 500 epochs, and single-scale attention reads one low-resolution map where small objects are a smudge. Deformable DETR (Zhu and coauthors, 2020) pays that bill with sparse multi-scale attention: each query samples only points per scale, so high-resolution maps become affordable and training drops to about 50 epochs.
This page covers the set framing, the matching loss, and the deformable speedup. The matching worked by hand on a tiny matrix lives in DETR as the prerequisite; multi-scale pyramids live in feature pyramids.
Think of It Like This
An auction with numbered paddles
A private auction has exactly numbered paddles and one arbiter. The room displays the whole collection at once, every paddle bids at the same time, and the arbiter awards each item to at most one paddle. Paddles that win nothing are recorded as holding no item, so nothing is ever sold twice and no cleanup crew removes duplicate sales.
Paddles are the object queries, the room display is the encoder memory, the arbiter is the Hungarian algorithm, and empty wins are the no-object class. Deformable attention is the rule change that lets each bidder glance at only 4 spots per shelf instead of scanning every shelf fully. Where it stops: a sale with 120 real items overflows 100 paddles, just as a fixed query set caps how many objects one image can report.
How It Actually Works
From pixels to a set of boxes
A CNN backbone turns the image into a feature map with channel width . The transformer encoder flattens the spatial map into a sequence, adds fixed sine positional encodings, and mixes global context with self-attention. The decoder starts from learned object queries, which are slots with no image content yet whose embeddings are trained. Each query attends to the encoder memory and outputs a class distribution over the classes plus a no-object class, and a box with normalized center, width, and height. Here is the fixed set size (usually 100), is the predicted class probability, and is the predicted box.
The matching loss
Let the truths be padded with no-object tokens to size , where each truth is a class plus box . For truth and prediction the match cost is , where and weight the two box terms and is the generalized overlap penalty. The Hungarian algorithm finds the permutation of predictions with the lowest total cost in time, and the training loss is computed only on those matched pairs while every unmatched query is supervised as no-object. Because each truth owns exactly one prediction, the network learns to never emit two boxes for one object.
Deformable attention in one equation
Dense attention on a map compares queries against keys, about pairs. Deformable attention replaces the full comparison with sampling. For query embedding with reference point on level , head samples offsets with weights that sum to one across levels and points, and reads the feature with bilinear interpolation at . With levels and points per head, each query reads locations instead of , so queries need lookups, about 64 times fewer. Small objects recover because the saved budget buys high-resolution levels the dense form could never afford.
Worked matching numbers
Take queries and two truths: a cat at and a dog at . Suppose the three predictions produce this cost matrix, where each entry already sums its class and box terms:
Here (query 1 is confident on the cat and close on the box: ) and (query 2 on the dog). The third row is all zeros: an unmatched query costs nothing because it answers no-object. Trying the assignments: pairs total , while totals and every other pairing is positive. The winner is query 1 takes the cat, query 2 takes the dog, query 3 takes nothing, so each truth is paired once and no duplicates survive.
Code
The point of this snippet is the matching, not the network: brute-forcing all assignments on the worked example matrix confirms the pairing derived by hand, and the second print confirms the dense-to-sparse arithmetic.
import itertools
cost = [ [-0.79, 1.83, 0.50], [1.83, -0.75, 2.20], [0.0, 0.0, 0.0],]
best = min( itertools.permutations(range(3)), key=lambda a: sum(cost[i][a[i]] for i in range(3)),)print(best)# -> (0, 1, 2)print(round(sum(cost[i][best[i]] for i in range(3)), 2))# -> -1.54
print(1024 * 1024, 1024 * 16)# -> (1048576, 16384)Query 0 takes truth 0, query 1 takes truth 1, query 2 takes nothing at total cost . The last line shows dense pairs against deformable lookups. Real training uses the Hungarian algorithm instead of brute force because has far too many permutations to enumerate.
Watch Out For
Judging DETR at 50 epochs
Original DETR needs about 500 epochs before its matching settles and the boxes sharpen. The symptom is low average precision at 50 epochs blamed on the code or the data, with the loss still swapping which query owns which object between epochs. Fix it by budgeting the long schedule or by moving to deformable variants that converge in about 50 epochs before judging the run.
Single-scale maps that erase small objects
A single low-resolution encoder map turns a 12-pixel object into a smudge, so overall precision looks fine while small-object precision collapses. The symptom is respectable mean score with a near-zero small-object split. Fix it with multi-scale deformable sampling across several feature pyramid levels rather than raising the input resolution and paying full self-attention cost on it.
The Quick Version
- DETR predicts a fixed set of boxes in one shot with no anchors proposed anywhere.
- The Hungarian algorithm pairs each truth with exactly one prediction in time.
- Unmatched queries are supervised as no-object, so duplicates never exist and NMS has nothing to prune.
- Deformable heads sample points per scale, cutting dense pairs to lookups on a map.
- The worked matrix peaks at total cost with query 1 on the cat, query 2 on the dog, and query 3 on nothing.