Sub topic 16
DETR: Object Queries and Set Prediction
Assume you know convolutional feature maps, gradient descent, classification, and bounding-box regression. Familiarity with Transformers helps, but you do not need to know their full implementation.
DETR, or Detection Transformer, treats object detection as set prediction. Instead of producing many candidate boxes and then removing duplicates, it predicts a fixed-size set of object slots. Each slot either describes one object or predicts “no object.”
From image features to object slots
A convolutional backbone converts the image into a feature map. The Transformer encoder processes this map so that each location can use information from other locations. This gives the model a global view of the image.
The Transformer decoder receives a fixed collection of learned vectors called object queries. An object query is a learned embedding that asks the decoder to find one possible object. The queries do not represent fixed categories such as “car query” or “person query.” During training, any query can learn to describe any object.
Each query produces two outputs:
- A class distribution, including a special “no object” class.
- A bounding box, commonly represented by normalized center coordinates, width, and height.
If the model uses 100 queries, it always returns 100 predictions. An image containing three objects should produce three meaningful predictions and 97 “no object” predictions.
One-to-one matching during training
The prediction order has no natural meaning. Prediction 1 is not required to describe the leftmost object, and prediction 2 is not required to describe the next object. Therefore, the training loss must be independent of prediction order.
DETR solves this with bipartite matching. The Hungarian algorithm chooses a one-to-one assignment between ground-truth objects and predictions. Its cost combines classification error and box error, often including box distance and IoU-based loss.
For example, suppose an image has two labeled objects and the model makes four predictions. Matching may assign prediction 3 to the first object and prediction 1 to the second object. Predictions 2 and 4 then receive the “no object” target.
After matching, DETR calculates:
- Classification loss for every query.
- Bounding-box losses only for matched object queries.
- A “no object” classification target for unmatched queries.
This assignment teaches different queries to specialize in different objects without manually defining anchors.
Why DETR does not need anchors or NMS
Traditional detectors often begin with anchor boxes and produce many overlapping candidates. They use non-maximum suppression, or NMS, to keep one candidate and discard nearby duplicates.
DETR removes both mechanisms:
- Object queries replace predefined anchor boxes.
- One-to-one matching discourages multiple queries from predicting the same ground-truth object.
- Set-based inference returns one prediction set directly, without a duplicate-removal stage.
This design makes the pipeline simpler and avoids hand-tuning anchor sizes and NMS thresholds. However, the original DETR can converge slowly and may struggle with small objects because global attention and coarse feature maps are expensive or insufficiently detailed.
> callout note > Object queries are slots, not object identities. A query may represent a different object in every image.
Interpreting the output
At inference time, discard predictions whose highest class is “no object.” The remaining class scores and boxes form the detected set. Since the output is a set, its order is irrelevant.
Exercise
An image contains five objects, and DETR uses 100 object queries. How many predictions should be trained toward the “no object” class?
Show answer
Ninety-five predictions. Five queries are matched one-to-one with the ground-truth objects, and the remaining 95 receive the “no object” target.