End-to-End Object Detection with Transformers
Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko
Read the Paper on arXivDETR (DEtection TRansformer) reformulates object detection as a direct set prediction problem, eliminating the need for hand-designed components like non-maximum suppression (NMS), anchor generation, or region proposal networks that have dominated detection architectures for a decade.
The architecture is elegantly simple: a ResNet-50 CNN backbone extracts a feature map of shape (e.g., for a standard COCO image), which is projected to 256 channels, flattened to a sequence of tokens, and processed by a standard transformer encoder-decoder. The encoder (6 layers, 8-head self-attention, ) performs global reasoning over all spatial positions. The decoder (6 layers) takes 100 learned object queries and transforms them into 100 output embeddings via cross-attention to the encoded image features. Two feed-forward network (FFN) heads predict a class label (92 COCO classes + "no object") and a normalized bounding box for each query.
The key insight is that detection can be viewed as predicting a set of objects -- and the Hungarian algorithm provides a differentiable-compatible way to compute the optimal one-to-one matching between the predictions and ground-truth objects (where typically ). Unmatched predictions are supervised to output the "no object" () class. This set-based loss is permutation-invariant -- the model never needs to learn an ordering over detections.
Why this matters architecturally:
COCO benchmark results (ResNet-50 backbone, 500 epochs):
DETR's strength is on large objects (where global attention shines) but weakness on small objects (where the downsampled feature map loses detail). Deformable DETR, introduced later, fixes this with multi-scale features and converges 10x faster.
Click any topic to jump in
Transformer backbone replacing region proposals and anchor boxes.
Learned slot embeddings, one per potential object in the image.
2D sinusoidal encodings injecting spatial structure into attention.
Queries gather visual evidence from encoder features for localization.
Optimal one-to-one assignment of predictions to ground truth.
Permutation-invariant loss removing the need for NMS.
Sparse attention variant solving slow convergence and high resolution cost.
DETR replaces the entire proposal/anchor machinery of traditional detectors with a fixed set of N=100 learnable embeddings called object queries. Each query is a 256-dimensional vector that learns to specialize in detecting certain types of objects in certain spatial regions. Through 6 decoder layers of self-attention (among queries) and cross-attention (to image features), each query independently produces at most one detection or outputs 'no object'.
The proposal/anchor paradigm and its accumulated complexity:
Every major object detector before DETR generates a massive number of candidate detections, then filters them down:
Faster R-CNN's proposal pipeline:
Single-stage detectors (YOLO, SSD, RetinaNet):
The fundamental problems:
Object queries: N=100 learnable embeddings that directly produce the detection set:
DETR replaces the entire anchor/proposal/NMS pipeline with 100 learned vectors, each of dimension :
These are randomly initialized and learned via backpropagation, just like any other network parameter. They are the only input to the decoder (besides the encoded image features).
How object queries work through the decoder:
Each of the 6 decoder layers performs three operations:
1. Self-attention among queries (queries reason about each other):
This is the key to NMS-free detection: queries can "see" what other queries are predicting and learn to avoid duplicates. If query 7 is already detecting a dog, query 23 learns (through self-attention) to detect a different object or output .
2. Cross-attention to image features (queries look at the image):
where is the encoder output (850 tokens for a typical COCO image) and is the 2D positional encoding. Each query attends to all spatial positions and learns to focus on regions relevant to its predicted object.
3. FFN refinement (per-query feature refinement):
where the FFN has hidden dimension 2048 (8x expansion from ).
After 6 decoder layers, each query produces a 256-dim output embedding that is fed to two parallel prediction heads:
Why N=100?
COCO images contain at most ~63 annotated objects (the maximum in the dataset). Setting N=100 provides comfortable headroom. The authors tested:
Query specialization (emergent behavior):
Despite having no explicit spatial initialization, trained queries develop clear specialization:
No NMS needed because:
N=100 learned 256-dim embeddings replace ~200K anchors, RPN, NMS, and all associated hyperparameters -- the entire proposal machinery reduced to a single parameter matrix
Self-attention among queries enables duplicate suppression without NMS: each query 'sees' what others are predicting and learns to output distinct objects or empty-set
Cross-attention to encoder output (HW x 256 tokens) allows each query to attend to any spatial position -- attention maps become sparse and object-focused after training
N=100 is optimal for COCO (max 63 objects per image): N=50 loses 1.2 AP from capacity limits, N=200 loses 0.2 AP from training difficulty with many empty-set targets
Query specialization emerges naturally: specific queries learn to detect objects in particular spatial regions, at particular scales, or of particular categories -- without any explicit assignment
Each query produces a 92-class prediction (91 COCO + empty-set) and a normalized box (x_c, y_c, w, h) via separate FFN heads after 6 decoder layers
Each decoder layer transforms query q_i through three sub-layers: (1) self-attention with all other queries (for duplicate suppression), (2) cross-attention to the HW encoded image features (for localization), (3) FFN with 2048 hidden units (for refinement). After 6 layers, the final q_i is decoded into class logits and box coordinates.
Bounding boxes are predicted as normalized center coordinates and dimensions relative to the image. The sigmoid activation constrains all values to [0,1]. The 3-layer MLP has hidden dimension 256 with ReLU activations. This direct regression avoids the anchor-relative parameterization of Faster R-CNN.
Object queries are learned embeddings that act as 'slots' the decoder fills with object hypotheses. Each query attends to the encoder output via , gathering features from spatial locations relevant to its slot. After training, each query specializes to a particular size/location prior — like learned anchor boxes but in feature space.