Detection Transformer (DETR) is an object detection algorithm that applies transformers to identify and locate objects in images. It was introduced in a seminal 2020 paper by Carion et al. and has since become the baseline for a line of influential algorithms, including DINO DETR (not to be confused with DINO) and CoDETR.
Overview The DETR paper refers to R-CNN as a baseline. A number of innovations were introduced:
Transformer-based design. DETR replaces the R-CNNs final CNN layers with transformer, attention-based components. Single-step. DETR replaces the R-CNN's signature two-step process (region proposal and region classification) with a single, unified learned step that predicts both simultaneously. Namely, the network begins with object queries (learned parameter vectors) and trains to predict a bounding box and classification for each. Set prediction. Rather than independently predict each query in isolation (as in R-CNN’s region classification), a combined component jointly predicts a set. This enables coordination, which reduces the massive duplication that plagues R-CNN. This in turn removes the necessity for NMS post-processing. These innovations largely carry over to a comparison with single-step algorithms like YOLO. While YOLO eliminated the R-CNN's region proposal step, it retained the basic structure which applies a learned network to given fixed anchors (paralleling region proposals). DETR's architecture eliminates fixed, unlearned anchors.
Model design The main components are illustrated in the figure below. DETR's core is an encoder-decoder transformer. The encoder processes the image and extracts meaningful representations of its content; the decoder attends to these representations to produce one output embedding per object query.
Object queries Object queries are the DETR equivalents of R-CNN's region proposals and YOLO's anchors. Each corresponds to a candidate prediction, or "slot". The queries are implicitly interpreted as distinct search specifications. As the encoder features and network weights are shared across all slots, it is the queries that diversify the predictions across different image regions and box sizes. Their role is thus analogous to "experts", each specializing in a distinct region or object shape. Importantly, a special "no-object" category is included, paralleling R-CNN's background category. The original architecture used 100 queries, well beyond the true number of objects in many images. Most object predictions, in such cases, are classified "no-object". As noted above, a major DETR design target is to make no-object predictions as accurate as possible, to eliminate duplication.
Components Backbone. The input image is first processed by the backbone, a convolutional neural network (CNN) which captures local texture and semantic structure. Its output is a low-dimensional feature map (the penultimate layer of a pre-trained ResNet network) that is forwarded to the encoder. Its dimensionality reduction (by a factor 32 in the paper) helps manage the encoder's complexity, which is quadratic in the size of its input. Encoder. The feature map is transferred to a standard transformer encoder. The backbone's feature map is flattened, projected to the encode embedding dimension, and position-encoded (as with vision transformers). Position encoding includes both spatial coordinates (x and y) of each map component, each encoded separately and concatenated. Decoder. The decoder is distinguished from standard transformer equivalent in that its output is produced in parallel, rather than an autoregressive sequential process. The inputs are the encoder outputs, as well as object queries. The outputs are embedding vectors, one for each of the object queries. Feed forward network (FFN). A simple multilayer perceptron (MLP) that is applied to each of the object embeddings to produce a candidate object prediction, consisting of bounding box coordinates and a classification score.
Training loss DETR's training loss is one of its significant innovations. The loss consists of two steps. The first is bipartite matching, which involves matching each candidate prediction (slot) from the model with a supervising ground truth object (or "no-object" if none is found). In the second step, a differentiable ("Hungarian") loss is computed between the objects in each pair, and used to backpropagate a gradient. The two steps have parallels with traditional CNN-based algorithms. With R-CNN and YOLO, object prediction slots coincide with region proposals or anchors, which are fixed or computed independently from the object predictor. Matching slots to ground truth, with those algorithms, uses a simple IoU criterion on the corresponding bounding boxes. Matched pairs are used to compute a differentiable loss equivalent to DETR's. Note that the matching itself does not participate in the differentiable loss. Once computed, at each training step, the matching is treated as fixed, frozen, and external to the optimization, again paralleling R-CNN and YOLO.
Bipartite matching Bipartite matching is an important ingredient of the DETR design.
Paralleling anchors or region proposals of the above-discussed baselines, DETR uses the bounding box predicted at each slot (bounding box and classification) as the basis for matching. A cost is computed between every slot and ground truth object (elaborated below). Bipartite matching is achieved by solving the assignment problem, namely minimizing a sum of costs over all matched pairs. In the typical case, the ground truth objects are augmented by no-object nodes, to match the number of prediction slots. The Hungarian algorithm is used to compute the optimal matching. The cost wij between a ground truth object number i and slot j is given by:
… excerpt ends here. Continue reading the full article.

