Skip to content
AI360Xpert
Beta

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.

One hundred learned queries decode into boxes, Hungarian matching pairs each truth once, and deformable heads sample only 4 points per scale instead of the whole map.
One hundred learned queries decode into boxes, Hungarian matching pairs each truth once, and deformable heads sample only 4 points per scale instead of the whole map.

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 N=100N = 100 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 K=4K = 4 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 N=100N = 100 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 xx into a feature map ff with channel width d=256d = 256. 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 NN 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 p^\hat{p} over the classes plus a no-object class, and a box b^=(cx,cy,w,h)\hat{b} = (cx, cy, w, h) with normalized center, width, and height. Here NN is the fixed set size (usually 100), p^\hat{p} is the predicted class probability, and b^\hat{b} is the predicted box.

The matching loss

Let the truths be y={yi}y = \lbrace y_i \rbrace padded with no-object tokens to size NN, where each truth is a class plus box yi=(ci,bi)y_i = (c_i, b_i). For truth ii and prediction jj the match cost is Cij=−p^j(ci)+λL1∥bi−b^j∥1+λgiouLgiou(bi,b^j)C_{ij} = -\hat{p}_j(c_i) + \lambda_{L1} \lVert b_i - \hat{b}_j \rVert_1 + \lambda_{giou} \mathcal{L}_{giou}(b_i, \hat{b}_j), where λL1\lambda_{L1} and λgiou\lambda_{giou} weight the two box terms and Lgiou\mathcal{L}_{giou} is the generalized overlap penalty. The Hungarian algorithm finds the permutation σ\sigma of predictions with the lowest total cost in O(N3)O(N^3) 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 32×3232 \times 32 map compares 10241024 queries against 10241024 keys, about 1,048,5761{,}048{,}576 pairs. Deformable attention replaces the full comparison with sampling. For query embedding zqz_q with reference point pqp_q on level ll, head mm samples KK offsets Δp\Delta p with weights AA that sum to one across levels and points, and reads the feature with bilinear interpolation at pq+Δpp_q + \Delta p. With L=4L = 4 levels and K=4K = 4 points per head, each query reads 1616 locations instead of 10241024, so 10241024 queries need 16,38416{,}384 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 N=3N = 3 queries and two truths: a cat at b1=[0.2,0.2,0.4,0.4]b_1 = [0.2, 0.2, 0.4, 0.4] and a dog at b2=[0.7,0.7,0.3,0.3]b_2 = [0.7, 0.7, 0.3, 0.3]. Suppose the three predictions produce this cost matrix, where each entry already sums its class and box terms:

C=[−0.791.830.501.83−0.752.20000]C = \begin{bmatrix} -0.79 & 1.83 & 0.50 \\ 1.83 & -0.75 & 2.20 \\ 0 & 0 & 0 \end{bmatrix}

Here −0.79=−0.85+0.06-0.79 = -0.85 + 0.06 (query 1 is confident on the cat and close on the box: ∣0.2−0.22∣+∣0.2−0.19∣+∣0.4−0.38∣+∣0.4−0.41∣=0.06|0.2 - 0.22| + |0.2 - 0.19| + |0.4 - 0.38| + |0.4 - 0.41| = 0.06) and −0.75=−0.80+0.05-0.75 = -0.80 + 0.05 (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 (1,1),(2,2)(1, 1), (2, 2) total −0.79+−0.75+0=−1.54-0.79 + -0.75 + 0 = -1.54, while (1,3),(2,2)(1, 3), (2, 2) totals 0.50+−0.75+0=−0.250.50 + -0.75 + 0 = -0.25 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 3!=63! = 6 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 −1.54-1.54. The last line shows 1,048,5761{,}048{,}576 dense pairs against 16,38416{,}384 deformable lookups. Real training uses the Hungarian algorithm instead of brute force because N=100N = 100 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 N=100N = 100 boxes in one shot with no anchors proposed anywhere.
  • The Hungarian algorithm pairs each truth with exactly one prediction in O(N3)O(N^3) time.
  • Unmatched queries are supervised as no-object, so duplicates never exist and NMS has nothing to prune.
  • Deformable heads sample K=4K = 4 points per scale, cutting 1,048,5761{,}048{,}576 dense pairs to 16,38416{,}384 lookups on a 32×3232 \times 32 map.
  • The worked matrix peaks at total cost −1.54-1.54 with query 1 on the cat, query 2 on the dog, and query 3 on nothing.