Can an object detector identify every object in an image without generating thousands of proposals and then removing duplicate boxes with non-maximum suppression? DETR demonstrated that it can, provided detection is reformulated as direct set prediction.
Introduced by Nicolas Carion and colleagues in End-to-End Object Detection with Transformers, DETR combines a convolutional backbone, a Transformer encoder-decoder, and a loss built around bipartite matching. Instead of optimizing a collection of hand-designed detection stages independently, the architecture trains the complete prediction problem as one system.
This article develops that formulation from first principles. The companion notebook uses eight object queries, three target objects, and the Hungarian algorithm to make the assignment process observable. It is a controlled mathematical experiment rather than inference from a pretrained detector, so each reported value can be traced to the executed code.
Open the executed notebook in Google Colab
How does DETR reformulate object detection?
Most detectors developed before DETR divide the task into several decisions. A network generates region proposals or dense predictions, each candidate receives a class and a box, and a later procedure removes redundant overlaps. Faster R-CNN learned the proposal stage; one-stage detectors made the prediction grid increasingly efficient. Yet both families still depend on choices about assignment, sampling, anchors, and post-processing.
DETR begins with a different representation. The desired output is an unordered set of class-and-box pairs. If an image contains three objects, three positions in the output should represent them exactly once. Every unused position receives the special no object class.
The absence of order matters. Ground-truth annotations do not say that a bicycle belongs to output position 4 or that a person belongs to position 17. During training, the model must therefore discover which prediction should be compared with which annotation. DETR solves this ambiguity globally rather than evaluating every candidate independently.
That is the purpose of bipartite matching: select a one-to-one correspondence between predicted slots and target objects before computing the principal localization losses. Duplicate hypotheses are not removed after inference. They are discouraged through the structure of the training objective.
Which components make up the DETR architecture?
The original pipeline can be organized into four functional blocks:
- Convolutional backbone: a CNN such as ResNet-50 converts the image into a compact spatial feature map.
- Transformer encoder: the feature map is projected into a common dimension, combined with positional information, and flattened into a sequence of contextualized visual tokens.
- Transformer decoder: a fixed collection of learned object queries attends to the encoded image representation.
- Prediction heads: every decoder output produces class probabilities and a normalized bounding box.

DETR treats detection as direct set prediction. Bipartite matching enters during training, when each target object is assigned to one output slot.
In the reference implementation, the Transformer uses six encoder layers, six decoder layers, a hidden dimension of 256, and eight attention heads. The COCO configuration provides 100 queries. That number defines the maximum number of prediction slots; it does not imply that an image must contain 100 objects.
Positional encoding remains necessary because self-attention does not preserve spatial coordinates by itself. In the notebook, a feature map with shape 10 × 15 × 64 receives a two-dimensional sinusoidal encoding and is reshaped into a sequence of 150 × 64. Separate sinusoidal channels represent horizontal and vertical position, allowing attention to relate visual content to location.
This use of spatial tokens resembles the mechanism employed in image classifiers based on attention, although the output serves a different purpose. The Vision Transformer implementation in Python provides a useful complement if you want to inspect image tokenization in a classification setting.
What are object queries?
Object queries are learned vectors that serve as detection slots. Each query passes through the decoder, attends to the encoded image features, and produces one hypothesis composed of a class distribution and a bounding box.
It is tempting to interpret a query as a permanent semantic specialist: one query for cars, another for large objects, and another for the upper-left corner. That interpretation is too rigid. A query’s output depends on the image and on the learned state of the model. It is better understood as a trainable slot that participates in constructing the final set.
Most images contain fewer objects than the number of queries. Consequently, many decoder outputs must predict no object. The notebook uses eight queries for a scene containing three target rectangles. After assignment, three queries receive object targets and the remaining five receive the background target.
The queries alone do not eliminate non-maximum suppression. The behavior emerges from the complete system: a fixed prediction set, one-to-one assignment, and losses applied according to the selected pairs. Together, these components teach the model to reserve one prediction for each annotated object.
How does Hungarian matching work in DETR?
Before assignment, the training procedure does not know which query should be responsible for each target. DETR constructs a cost matrix containing every query-target combination, then searches for the set of pairs with the smallest total cost.
The matching cost combines three signals:
- Class cost: favors a query that assigns high probability to the target class.
- L1 distance: measures the absolute difference between the four box coordinates.
- Generalized IoU cost: measures geometric agreement, including cases in which boxes do not overlap.
The notebook follows the reference weights of 1 for class, 5 for L1 distance, and 2 for generalized IoU:
class_cost = -probabilities[:, target_classes]
l1_cost = np.abs(
predicted_boxes[:, None, :] - target_boxes[None, :, :]
).sum(axis=-1)
giou_cost = -pairwise_giou(predicted_boxes, target_boxes)
cost_matrix = class_cost + 5 * l1_cost + 2 * giou_cost
matched_queries, matched_objects = linear_sum_assignment(cost_matrix)
The call to linear_sum_assignment returns the global minimum-cost assignment. A sequence of local choices could assign two plausible queries to the same target while leaving another annotation without a suitable prediction. The bipartite solution imposes a one-to-one relationship across the entire set.

The green outlines identify the three pairs selected by the Hungarian algorithm. Every target object appears exactly once in the assignment.
In the executed experiment, query 0 is matched to the red object with cost -2.605, query 2 to the blue object with cost -2.768, and query 4 to the green object with cost -2.849. Several other queries predict plausible classes, but their combined classification and localization costs are higher.
What do the notebook outputs establish?
The synthetic image contains three rectangles. Their boxes are normalized as (center_x, center_y, width, height), while the eight simulated queries include strong matches, duplicate hypotheses, and predictions intended for the no-object class.
After matching, the loss components are:
Classification loss: 0.1934 L1 loss: 0.0163 GIoU loss: 0.0606 Total loss: 0.3961 Matched queries: 3 No-object queries: 5
The classification loss includes all eight queries. Following the reference DETR configuration, the no-object class receives a relative weight of 0.1. The L1 and GIoU losses are computed only for the three predictions matched to real objects.

One prediction supervises each target: query 0 for red, query 2 for blue, and query 4 for green.
These outputs validate the assignment and loss calculations in a controlled setting. They do not represent the accuracy of a trained network on a benchmark. The distinction is essential: the experiment explains the mathematics without presenting simulated predictions as pretrained-model inference.
Why was the original DETR difficult to train?
DETR’s conceptual simplicity came with two important limitations: slow convergence and weaker performance on small objects. In the comparison reported by the authors of Deformable DETR, DETR with ResNet-50 reached 42.0 AP after 500 epochs on COCO. Faster R-CNN with an FPN reached the same AP after 109 epochs. Small-object performance was 20.5 AP for DETR and 26.6 AP for Faster R-CNN in that comparison.
Global attention over a single-scale feature map is expensive and does not focus naturally on the limited spatial support of small objects. Deformable DETR addressed both issues by attending to a small set of sampling points around learned reference locations and by incorporating multiscale features. Under the same reported comparison, the variant reached 43.8 AP after 50 epochs.
Later systems preserved direct set prediction while changing query initialization, box refinement, denoising, and the training schedule. DINO, for example, reported 49.4 AP after 12 epochs and 51.3 AP after 24 epochs with a ResNet-50 backbone and multiscale features.
These figures describe the conditions reported in the respective papers; they are not interchangeable measurements from one unified experiment. Their significance is historical and architectural: DETR established the formulation that later detectors made substantially more practical.
When is the DETR formulation especially useful?
DETR is a foundational reference when you need to understand or develop end-to-end detectors, reason about global image context, or model a task whose output is naturally a set. Its architecture also provides a direct foundation for instance segmentation and for multimodal systems that associate regions with language.
That does not make the original DETR the automatic choice for every application. Latency, target hardware, object scale, annotation volume, and training budget remain decisive. An embedded system may benefit more from a compact convolutional detector, while an offline perception pipeline may justify a larger Transformer-based model.
The transferable idea is more important than one checkpoint. DETR demonstrated that object detection can be optimized as structured set prediction, with global assignment embedded in training and without NMS in the original inference pipeline.
Takeaways
- Detection becomes set prediction: DETR produces classes and boxes in parallel from a fixed number of queries.
- Object queries are trainable slots: they construct image-dependent hypotheses rather than representing permanently fixed categories or locations.
- Matching is global and one-to-one: the Hungarian algorithm minimizes the total assignment cost across queries and targets.
- The loss combines semantics and geometry: classification, L1 distance, and generalized IoU supervise the selected outputs.
- Unused queries learn the no-object class: the notebook matches three queries and assigns five to the background target.
- The formulation outlived the original limitations: Deformable DETR and DINO retained set prediction while improving convergence and multiscale detection.












