Conditional DETR

Conditional DETR is an object detector that speeds up DETR training by changing only how the decoder cross-attention query is formed.

  • Paper: Meng, Chen, Fan, Zeng, Li, Yuan, Sun and Wang (USTC / Peking University / Microsoft Research Asia), Conditional DETR for Fast Training Convergence, ICCV 2021, arXiv:2108.06152.
  • Problem: the same as Deformable DETR — DETR’s 500-epoch training schedule — but with a much smaller intervention.
  • Core idea: keeps global dense attention everywhere and changes only how the decoder cross-attention query is formed. Each query is given a conditional spatial query, predicted from the previous decoder layer’s embedding and a reference point, which lets every cross-attention head attend to a narrow spatial band around one object extremity.
  • Headline result: training converges faster for R50/R101 backbones and faster for the high-resolution DC5 variants.

Spatial attention maps: conditional DETR (top) vs DETR at 50 and 500 epochs

Motivation: the content query is overloaded

In DETR’s decoder cross-attention, the key is (content embedding from the encoder plus positional embedding of its 2D location) and the query is (decoder self-attention output plus object query ). The attention logit expands into four terms:

The paper’s empirical starting point: removing the positional terms from the second decoder layer onward costs only ~1.4 AP (34.9 → 34.0 at 50 epochs). So the spatial query contributes little and the whole burden of localization falls on the content query , which must simultaneously

  1. match the content key (find the object), and
  2. match the spatial key (find where in the object — the four box extremities needed for regression).

The object query gives only a generic, image-independent attention pattern; it cannot shrink the spatial search range for a specific instance. A content embedding strong enough to serve both roles is exactly what takes hundreds of epochs to learn. Figure 1 shows the consequence: DETR at 50 epochs (second row) fails to highlight the left and right edges at all, and only at 500 epochs (third row) do all four extremity bands appear.

Conditional cross-attention

The fix is to stop adding the spatial and content parts and instead concatenate them, so that the dot product splits cleanly into two independent halves:

The query is and the key is . The content dot product handles what (appearance matching for classification), the spatial dot product handles where (localization). Neither role contaminates the other, and no longer needs to encode positional information.

The whole novelty is then in constructing — a query that is conditional on the image, unlike DETR’s fixed learned .

One decoder layer in conditional DETR

Where the spatial information comes from

DETR predicts a box from the decoder embedding relative to a reference point :

with and the unnormalized 2D reference point (fixed to in the original DETR). Because the box is expressed as a displacement from , the embedding already contains the displacements of the four extremities with respect to in the unnormalized space. Conditional DETR simply reads that information out and reuses it as a spatial query — no new supervision, no hand-crafted attention prior.

Constructing the conditional spatial query

Two ingredients, :

  1. Map the reference point into the same positional space the keys live in, by normalizing and applying the same sinusoidal embedding used for :

  1. Map the displacement/scale information in the decoder embedding to a linear transformation in that space, (learnable linear projection + ReLU + learnable linear projection), and apply it:

is restricted to a diagonal matrix, so it is stored as a 256-dimensional vector and applied by element-wise multiplication — cheap, and empirically on par with a full matrix (see ablations). Because is produced in the same space as , the spatial dot product is meaningful from the start of training.

Multi-head attention is unchanged ( heads): spatial and content queries/keys are projected to each head by separate linear projections, while the value projections stay exactly as in DETR (content only). Because each head projects differently, the heads spread out over different distinct regions.

In the first decoder layer there is no previous decoder embedding, so the implementation falls back to DETR’s behavior with a concatenation instead of an addition: the positional embedding predicted from the object query is concatenated into the query (and key).

What the transformation learns

Spatial, content, and combined attention weight maps per head

Visualizing the softmax-normalized (spatial), (content) and their sum reveals what does. Five of the eight heads carry distinct roles — four extremities plus one region inside the box — and the remaining three duplicate them. The spatial maps localize a band overlapping the corresponding box edge; the content maps highlight scattered similar regions (including other instances of the same class); their combination keeps only the extremity highlights. So the transformation has two effects:

  • translate the highlight to each of the four extremities and to an interior position, and
  • scale the spatial spread — wide bands for large objects, narrow for small ones.

Which means encodes not only displacement but also object scale. The interior-region head depends mostly on the spatial map, suggesting a region inside the box already carries enough information for classification.

Reference points

Two options are studied for : learn it as a per-prediction model parameter, or predict it from the object query, (as in Deformable DETR). Both beat DETR’s shared : 40.7 and 40.9 AP respectively, vs 36.8.

Relation to other fast-converging DETRs

  • Deformable DETR replaces global dense attention with sparse deformable attention over sampled points, and additionally uses a multi-scale, high-resolution encoder. Conditional DETR keeps global dense attention and single-scale features, changing only the query formulation — it is a strictly smaller change and does not address the encoder’s quadratic cost.
  • SMCA (spatially modulated co-attention) is the closest concurrent work: it modulates the global cross-attention with hand-crafted Gaussian maps around learned centers. Conditional DETR instead learns the spatial queries from the decoder embeddings, so the attenuation form is not hand-crafted and the resulting maps pick out the four extremities and distinct interior regions.
  • The construction is also related to conditional/dynamic convolution (CondConv, dynamic filter networks): there the convolutional kernel weights are predicted from the input; here a linear projection is predicted from the decoder embedding to represent displacement and scaling.

Hyperparameters

Architecture is otherwise identical to DETR; the following is what the paper reports.

SettingValue
DatasetCOCO 2017 (118K train / 5K val)
BackbonesResNet-50, ResNet-101, DC5-R50, DC5-R101 (ImageNet-pretrained, batchnorm frozen)
Transformer initXavier
Object queries (DETR uses 100)
Attention heads; decoder repeated 6 times
Prediction FFNsOne after each of the 6 decoder layers, parameters shared across the 6
OptimizerAdamW, weight decay
Learning rate backbone, transformer
Dropout0.1
Schedule50 / 75 / 108 epochs, LR after 40 / 60 / 80 epochs
Classification lossFocal loss (as in Deformable DETR, not DETR’s cross-entropy)
Box lossL1 + GIoU, same weights as DETR
MatchingHungarian bipartite matching, same cost as Deformable DETR
AugmentationResize short side , long side ; random crop to a rectangular patch with probability 0.5

Results

COCO 2017 val (Table 1). Conditional DETR at 50 epochs already matches or beats DETR at 500 for the DC5 backbones:

ModelEpochsGFLOPsParamsAPAPAPAP
DETR-R505008641M42.020.545.861.1
DETR-R50508641M34.914.437.254.5
Conditional DETR-R50509044M40.920.844.659.2
Conditional DETR-R501089044M43.022.746.761.5
DETR-DC5-R5050018741M43.322.547.361.1
Conditional DETR-DC5-R505019544M43.824.047.660.7
Conditional DETR-DC5-R5010819544M45.125.349.062.2
DETR-DC5-R10150025360M44.923.749.562.3
Conditional DETR-DC5-R1015026263M45.026.148.962.8
Conditional DETR-DC5-R10110826263M45.927.250.363.3

Convergence curves vs DETR-DC5-R50

Against other single-scale DETR variants at comparable budgets, conditional DETR-R50 (40.9) beats Deformable DETR-R50-SS (39.4) and UP-DETR-R50 at 150 epochs (40.5); conditional DETR-DC5-R50 (43.8) beats deformable DETR-DC5-R50-SS (41.5). Against the multi-scale, -resolution variants (deformable DETR, TSP-FCOS, TSP-RCNN) the single-scale DC5 models come out roughly on par — 43.8 vs deformable DETR-R50’s 43.8, 45.1 at 108 epochs vs TSP-RCNN-R50’s 45.0 at 96 epochs.

Ablations

Reference points (ResNet-50): as in DETR → 36.8 AP; learned per-prediction parameter → 40.7; predicted from the object query → 40.9.

How the spatial query is formed (Table 3, ResNet-50). CSQ-P uses only (37.8), CSQ-T only the transformation (37.6), CSQ-C the decoder content embedding (37.1), CSQ-I the element-wise product of a transformation predicted from the self-attention output with (40.2). The proposed gives 40.9 — both ingredients are needed, and they must come from the decoder embedding and the reference point.

Isolating the contribution (Table 4, ResNet-50). Starting from DETR’s 34.9: offset regression w.r.t. a learned reference point alone → 35.0; focal loss alone → 35.3; both → 36.9; adding conditional cross-attention → 40.9. So the borrowed Deformable DETR ingredients account for ~2 AP and the conditional cross-attention mechanism for a further +4.0 AP.

Form of (Figure 5). Identity (i.e. no learned transformation) 37.8; single learned scalar 39.0; block-diagonal ( per head) 40.8; full matrix 40.7; diagonal 40.9. A single scalar already helps — narrowing the spatial range to the object’s scale is most of the benefit — and the diagonal form gets the rest at negligible cost.

Limitations

  • The encoder is untouched, so the cost of global self-attention over the feature map remains. High resolution is still bought via the expensive DC5 backbone rather than a multi-scale encoder, and the paper explicitly leaves combining conditional cross-attention with a multi-scale encoder to future work.
  • Convergence is – faster, not the plus small-object gain that multi-scale approaches deliver; single-scale features still cap AP (20.8 for R50 at 50 epochs).
  • Three of the eight attention heads are duplicates of the other five, so the head budget is not fully used.
  • The conditional spatial query only sharpens the decoder cross-attention; the slow, unstable Hungarian matching assignment early in training is not addressed (that is the target of the later DN-DETR / DINO line).

See also