DN-DETR: Accelerate DETR Training by Introducing Query DeNoising
- Publication
- CVPR 2022Conference
- First submitted to arXiv
- arXiv:2203.01305
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{objectdetectiondndetr2022,
title = {{DN-DETR: Accelerate DETR Training by Introducing Query DeNoising}},
author = {Feng Li and Hao Zhang and Shilong Liu and Jian Guo and Lionel M. Ni and Lei Zhang},
year = {2022},
eprint = {2203.01305},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2203.01305}
}
DETR recast object detection as a matching problem and did away with anchors and NMS, but it trained astonishingly slowly. To get good results, it usually has to train for 500 epochs on COCO, while the original Faster R-CNN trains for only 12.
By early 2022, quite a few papers had tried to make DETR learn faster, most of them by changing the architecture. Deformable DETR has attention look only at a few sampled points near a reference point, Conditional DETR splits each query into a content part and a position part, and DAB-DETR writes each query directly as a box that the decoder refines layer by layer.
This paper comes from a team at the International Digital Economy Academy (IDEA) in Shenzhen, the Hong Kong University of Science and Technology, and Tsinghua University, and they took aim instead at the Hungarian matching DETR uses to hand out answers. Early in training, the same query on the same image may be matched to the dog in one epoch and to the cat in the next, so the target the model is trying to learn keeps jumping around.
Alongside the original queries, they feed the decoder an extra batch of noised ground-truth boxes and ask it to restore the original boxes and classes. The answers to these problems are known in advance, so no matching is needed; at inference, these queries are removed entirely.
The conference version was an oral presentation at CVPR 2022. The extended version adds deformable attention to the decoder and applies the same trick to the original DETR, Anchor DETR, Faster R-CNN, and segmentation models; it was later published in IEEE TPAMI in 2024.
Problem Definition
Rematched Every Round
First, recall how DETR trains. The model has a fixed number of queries, and each query outputs one class and one box. Every time the loss is computed, the Hungarian algorithm makes a one-to-one matching between predictions and ground truth, picking the assignment with the lowest total cost; a query matched to an object learns that object's box and class, and an unmatched one learns “no object.”
This matching is recomputed every iteration from the current predictions. At the start of training, the predicted boxes are still a mess, and several queries have about the same cost for the same object; nudge the cost matrix slightly, and the whole matching can flip. Citing research on the stable marriage problem, the authors point out that the Hungarian algorithm only minimizes the total cost, and the matching it produces is not stable.
For a single query, the learning target therefore keeps changing. This round asks it to move its box toward the dog; the next round asks it to move toward the cat.
DN-DETR is built on DAB-DETR. DAB-DETR writes each query as a 4D anchor box , and each decoder layer outputs a set of corrections that pushes the box toward an object. The authors therefore split training into two tasks, learning good anchors and learning the relative offset from an anchor to its object. If the object an anchor corresponds to keeps changing, the offset can't be learned stably.
At the time, few studies suspected the matching. In the paper that introduced TSP, Sun et al. used a trained DETR as a teacher to give a student model fixed label assignments; this sped up convergence only early in training, and the final results were about the same, so they concluded that the Hungarian loss isn't the main cause of slow convergence. DN-DETR focuses on exactly that early stretch of training.
Counting the Handoffs
The authors designed a metric to measure this, called IS (instability). For an image, first record the matching result at epoch :
The number of entries in this vector that differ between two consecutive epochs is the image's IS at epoch :
Here is the number of predictions, and is 1 when the condition holds. For the whole dataset, IS is then averaged over all images.
Take five queries and two objects, a dog and a cat. If the dog is handed to a different query, the old query goes from 0 to −1 and the new one from −1 to 0, so IS rises by 2. Swapping the dog and the cat also changes two entries, one for each object. So each object that changes hands adds at most 2 to IS, and when every object changes hands, IS is twice the number of objects.
The authors measured on COCO val2017, where each image has 7.36 objects on average, so the maximum IS is 7.36 × 2 = 14.72.
Figure 2 of the paper trains DETR, DAB-DETR, and DN-DETR for 12 epochs under the same settings and measures IS once per epoch. The x-axis is the epoch, the y-axis is IS, and the solid red line is DN-DETR:

At the leftmost point, all three lines are around 13, not far from the maximum. Working backward, an IS of about 13 means that on average more than six objects per image changed the query responsible for them, which is nearly all of them.
DN-DETR falls fastest, reaching about 9 by epoch 2 while DAB-DETR is still around 11; at the last epoch, DN-DETR is at about 5.7, DAB-DETR about 6.9, and the original DETR about 7.8. At epoch 7, though, DAB-DETR briefly dips below DN-DETR, then jumps back above 10 for the next two epochs.
Unstable matching keeps changing the targets early in training, yet one-to-one matching can't be dropped, since it's what lets DETR skip NMS at inference. So could the matching stay, while the decoder also gets a separate assignment whose targets don't jump around?
Solution
Two Batches of Queries in One Decoder
DN-DETR's decoder takes in two batches of queries at once:
- Matching part: as in DAB-DETR, a set of learnable anchors whose outputs are scored with the Hungarian loss.
- Denoising part: ground-truth boxes and classes with noise added, used as queries; each query must restore the object it came from, with no matching involved.
Figure 6 of the paper draws the whole training flow in one place. On the left, the yellow and brown blocks are two denoising groups, each computing its own reconstruction loss; on the right, the green block is the matching part, which computes the Hungarian loss; the square grid on the far right is the attention mask, which we'll come back to later:

The three colored paths in the figure are possible leaks, and a red cross means the mask blocks that path. The yellow and orange paths connect the two denoising groups, and the green one runs from the denoising part to the matching part.
The paper writes this as , where is the denoising part, the matching part, the image features output by the encoder, and the attention mask. The total loss is the matching part's Hungarian loss plus the denoising part's reconstruction loss. At inference, the denoising part is removed entirely, and what remains is the same model as DAB-DETR.
Since the denoising queries have to carry both a box and a class, the authors changed just one thing in DAB-DETR. Besides its anchor box, a DAB-DETR query has a content vector (the decoder embedding); DN-DETR replaces it with a class embedding followed by an indicator bit, 1 for denoising and 0 for matching. The matching part has no class to give, so all of its queries use the embedding of a single “unknown” class.
Turning a Ground-Truth Box into a Practice Problem
Let's walk one dog through it. Its ground-truth box, in normalized coordinates, is .
There are two kinds of box noise:
- Center shift: add to the center, with and . Since is less than 1, the new center always stays inside the original box.
- Scaling: the width is drawn at random from , and the height likewise.
Class noise randomly swaps the label, at a rate controlled by . The paper's defaults are and . For this dog, the center can move at most 0.06 left or right and 0.08 up or down, the width falls between 0.18 and 0.42, and the height between 0.24 and 0.56.
Following the official code, make five noised boxes for this dog. Each copy's label has a probability of of being replaced by a random class; the official code draws the new label uniformly from all classes, so it occasionally draws the original class again.
Here are the five boxes, plotted:
All five centers land in the gray area, and the widths and heights vary, but every box still surrounds this dog. These five boxes then serve directly as the anchors of five queries, and their labels are looked up in the embedding table to become content vectors. To the decoder, each one is a good anchor with an object definitely nearby, and the task is to correct it back to the black box and say “dog.”
Group 3's label has been swapped to “cat,” so the model has to work out from the box's position and the image content that this is actually a dog. The authors say label swapping is there to force the model to learn the relationship between boxes and classes.
The reconstruction loss reuses DAB-DETR's combination, L1 and GIoU for boxes and focal loss for classes. The official code computes this loss at every decoder layer, with the same weights as the matching part.
Several Copies of Every Object
One set of noise gives just one version, so the authors make several copies of each object at once. Each copy contains all the annotated objects in the image and is called a denoising group. With groups of objects each, the denoising part has queries.
The default is 5 groups. With COCO val2017's average of 7.36 objects per image, the denoising part averages about 37 queries, against 300 in the matching part. Images in the same batch have different numbers of objects, so the official implementation pads every group to the image with the most objects in the batch. The authors explained this when the question came up on GitHub, and mentioned that the later DINO switched to a dynamic number of groups. DN-DETR #8, 2022-04-19
The extra compute is small. For DAB-DETR with ResNet-50, 5 groups raise the training GFLOPs from 94.4 to 94.6; inference has no extra computation at all.
No Peeking at the Answers
Put ground-truth boxes straight into the decoder, and self-attention lets every query see every other one; a matching query only has to glance at a nearby denoising query to know roughly where the object is.
The authors list two kinds of leakage:
- The matching part sees the denoising part, which amounts to being handed the ground-truth boxes.
- Different noised versions of the same object see each other, which amounts to peeking at other versions of the same problem.
The attention mask blocks both paths. , where the first positions are the denoising part and the remaining are the matching part; means query can't see query :
Take 2 groups, each with 2 objects (a dog and a cat), plus 3 matching queries. The dog and the cat in the same group can see each other but not the two versions in the other group, and the three matching queries can't see any denoising query. This is the same pattern as the grid on the far right of Figure 6, where the gray cells are the masked entries, set to 1.
In the other direction, the denoising queries can look at the matching queries. The authors say this doesn't affect the results, because the matching queries are learned vectors and contain no ground-truth information to begin with.
This mask is used only in the decoder's self-attention. Someone on GitHub asked whether cross-attention needs masking too, and the authors replied that, in their experience, cross-attention doesn't cause leakage. DN-DETR #35, 2022-07-26 In cross-attention, each query attends only to image features, with no channel between queries.
The ablation in Table 5 of the paper uses ResNet-50, 1 denoising group, and 50 epochs of training. With noise on both boxes and labels and the mask in place, it scores 43.4 AP; with the same noise but no mask, it drops to 24.0, which is 18.2 below the 42.2 with no denoising at all.
The paper explains that without the mask, the matching part can see the noised ground truth and easily predicts the annotated objects. Following that reasoning, once the denoising queries are removed at inference, the cues the matching queries leaned on during training vanish with them.
Discussion
Experimental Setup
All experiments are on COCO 2017, trained on train2017 and evaluated on val2017. The main model, DN-DETR, is DAB-DETR plus denoising training, with a 6-layer encoder, a 6-layer decoder, a hidden dimension of 256, and 300 matching queries. The noise uses and , with 5 groups by default. The optimizer is AdamW with a learning rate of , on 8 A100s with a batch size of 16. The 50-epoch schedule lowers the learning rate at epoch 40, and the 12-epoch schedule at epoch 11.
The authors also attached the same training to Deformable DETR. DN-Deformable-DETR uses deformable attention only in the encoder, while DN-Deformable-DETR++ uses it in both the encoder and the decoder. Each has a matching DAB-Deformable-DETR baseline that also writes queries as 4D boxes but skips denoising, so that the benefit of 4D queries isn't credited to denoising.
Gains Under the Same Settings
The cleanest comparison is in Table 1. It uses single-scale features and 50 epochs of training, and DAB-DETR and DN-DETR differ only in whether they use denoising training:
| Backbone | DAB-DETR | DN-DETR |
|---|---|---|
| ResNet-50 | 42.2 | 44.1 (+1.9) |
| ResNet-101 | 43.5 | 45.2 (+1.7) |
| ResNet-50-DC5 | 44.5 | 46.3 (+1.8) |
| ResNet-101-DC5 | 45.8 | 47.3 (+1.5) |
All four backbones gain 1.5 to 1.9 AP. Cut training to 12 epochs and the gap grows. In Table 2, DC5-R50 goes from 38.0 to 41.7 (+3.7), and DC5-R101 from 40.3 to 42.8 (+2.5). The larger gains on the short schedule fit the authors' claim that the problem is concentrated in “early training stages.”
How the Speedup Is Counted
The title promises a speedup, and the authors give two kinds of evidence.
One is the convergence curves. The caption of Figure 7(a) says that for DC5-R50, before the learning rate drops, DN-DETR reaches 40 AP in 20 epochs, while DAB-DETR needs 40 epochs.
The other is to halve the schedule and compare directly:
| Model | Baseline | With denoising, half the epochs |
|---|---|---|
| DAB-DETR-DC5-R50 (50 vs. 25 epochs) | 44.5 | 44.4 |
| DAB-Deformable-DETR-R50 (50 vs. 25 epochs) | 46.9 | 46.8 |
| DAB-Deformable-DETR-R50++ (50 vs. 25 epochs) | 48.7 | 48.4 |
| DETR-R50 (500 vs. 250 epochs) | 42.0 | 42.2 |
With half the epochs, AP is only 0.1 to 0.3 lower, and the original DETR run even comes out 0.2 higher. This table is what the abstract means by “comparable performance with 50% training epochs.”
Halving the epochs doesn't exactly halve the time, though. The authors measured actual ResNet-50 training time on 8 A100s, where DAB-DETR takes 51.1 minutes per epoch and 57.7 minutes with denoising, about 13% more. GFLOPs barely change, yet the wall-clock time goes up by more than a tenth; when estimating training cost, you still have to look at measured time.
In total, DAB-DETR needs 2,555 minutes for 50 epochs, while the version with denoising needs 1,443 minutes for 25 epochs, saving about 44%. This timing table measures ResNet-50, whereas the 25-epoch AP comparison above uses DC5-R50, so the two come from different models.
The abstract's 46.0 AP at 12 epochs and 49.5 AP at 50 epochs come from DN-Deformable-DETR++, which uses multi-scale features and deformable attention in both the encoder and the decoder; the “+1.9AP” is the single-scale comparison on DAB-DETR. Against a baseline with the same ++ architecture, the gap at 50 epochs is 49.5 versus 48.7, or 0.8 AP.
Mask, Labels, and Number of Groups
The remaining two rows of Table 5 separate the contributions of box noise and label noise. Box noise alone gives 43.0, 0.8 above the 42.2 without denoising; adding label swapping brings it to 43.4.
For the number of groups, see Table 6:
| Backbone | No denoising | 1 group | 5 groups |
|---|---|---|---|
| ResNet-50 | 42.2 | 43.4 | 44.1 |
| ResNet-50-DC5 | 44.5 | 45.6 | 46.3 |
| ResNet-101 | 43.5 | 45.0 | 45.2 |
| ResNet-101-DC5 | 45.8 | 46.5 | 47.3 |
The gain from none to 1 group is mostly larger than from 1 to 5 groups; only ResNet-101-DC5 goes the other way. On this basis, the authors say that more groups are better but with diminishing returns, and they default to 5.
For the noise scale, see Figure 8. This experiment uses ResNet-50, a batch size of 64, and 20 epochs of training without a learning-rate drop, fixing one kind of noise at 0.4 and varying the other. Both curves are high in the middle and low at both ends. With one kind of noise set to 0, AP is between 33.5 and 34; it is highest at 0.2 to 0.4, where the figure marks peaks of 35.5 and 35.7; pushed to 0.8, it drops back to around 34. The paper concludes that both kinds of noise help, but too much noise drags performance down.
Beyond DAB-DETR
The extended version attaches denoising training to other models (Table 3, all ResNet-50):
- Anchor DETR: its queries have only 2D anchor points, so only the center shift is used; DC5, 12 epochs, 38.2 to 39.4 (+1.2).
- Original DETR: its queries have no explicit box, so a linear layer embeds the noised box into the query; 300 epochs, 40.6 to 42.6 (+2.0).
- Faster R-CNN: there's no content vector, so only box noise is added, and the noised boxes go into the detection head together with the RPN proposals; FPN, 12 epochs, 37.9 to 38.4 (+0.5).
A few rows in the table carry asterisks; their numbers come from the ablations in later papers that adopted denoising training, such as DINO and Group DETR. For example, DINO scores 46.0 without denoising and 47.4 with it.
Faster R-CNN gains the least. It assigns labels with an IoU threshold instead of Hungarian matching; this is consistent with the direction of the authors' diagnosis, but the two kinds of detectors differ in too many ways for this one row to pin down the cause.
Who Did the Practice Actually Help?
The matching queries can't see the denoising queries, and the two sides' losses are computed separately, so at what point does denoising actually help the matching? Someone asked exactly this on GitHub in October 2024, and the question has no reply in the repo. DN-DETR #72, 2024-10-14
Both batches of queries share the same backbone, the same encoder, and the same decoder layers and prediction heads, and the denoising gradients flow all the way back into these parameters. The paper supports its reading with two observations. One is the IS curve above. The other is Figure 3, which measures the average L1 distance between the matching queries' initial anchors and the ground-truth boxes they are matched to at the last decoder layer; over 12 epochs, DAB-DETR goes from about 0.146 to about 0.096 and DN-DETR from about 0.110 to about 0.082, staying closer throughout. The authors' view is that denoising teaches queries to look for objects nearby, so the matching jumps around less.
Later studies read “where it helps” differently:
- Group DETR (ICCV 2023) points out that in a denoising group each object has only one query, and no query corresponds to “no object”; self-attention within a group sees only other objects, never duplicate boxes. It therefore argues that denoising mainly adds positive supervision and gives no direct help with suppressing duplicate predictions. In the DAB-DETR, 12-epoch setting, adding denoising on top of Group DETR still gives another 1.5 AP, which its authors take as a sign that the two are complementary.
- H-DETR splits the parameters directly in a supplementary experiment. On DINO, which uses contrastive denoising, giving the denoising branch its own decoder and prediction heads moves AP from 49.1 to 49.0; separating the encoder as well drops it to 48.5. By these numbers, most of the benefit of denoising reaches the matching part through the training of the encoder.
- DINO, a paper the same team put on arXiv five days later, points straight at a gap in DN-DETR. The model learned to predict from anchors that have a ground-truth box nearby, but never learned to answer “no object” for anchors with no object nearby. DINO therefore added negative samples to the practice problems.
These accounts can all hold at once. The paper's evidence shows the drop in IS and the faster convergence appearing together; the paper itself doesn't separate whether denoising first steadies the decoder's targets or feeds the encoder more gradient from positives.
Conclusion
DN-DETR traces part of DETR's slow training to Hungarian matching being recomputed every round, and uses IS to turn “dog today, cat tomorrow” into a curve. During training, it adds a set of practice problems with known answers, giving the decoder noised ground-truth boxes and classes to restore, and uses an attention mask to keep the matching queries from peeking.
Under the authors' settings, it gains 1.5 to 1.9 AP with single-scale features at 50 epochs, and 2.5 to 3.7 AP at 12 epochs; it catches up with the baseline in half the epochs, at the cost of about 13% more time per epoch.
If you want to add denoising training to your own DETR-style model, get the mask right first; in the paper's ablation, AP fell from 43.4 to 24.0 without it. Images in a batch have different numbers of objects, so the groups need padding. The queries are best off carrying an explicit box, as in DAB-DETR; if they don't, you'll have to embed the box separately, as with the original DETR. Inference doesn't need to change at all.
These practice problems were later picked up by many DETR-style models. DINO added negative samples to them, and RT-DETR also carries 200 denoising queries during training.