Fast Convergence of DETR with Spatially Modulated Co-Attention
- Publication
- ICCV 2021Conference
- First submitted to arXiv
- arXiv:2101.07448
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{objectdetectionsmcadetr2021,
title = {{Fast Convergence of DETR with Spatially Modulated Co-Attention}},
author = {Peng Gao and Minghang Zheng and Xiaogang Wang and Jifeng Dai and Hongsheng Li},
year = {2021},
eprint = {2101.07448},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2101.07448}
}
Add Some Gaussian!
Fast Convergence of DETR with Spatially Modulated Co-Attention.
Continuing our discussion on DETR, we already know how DETR works, so let’s jump straight to the problem at hand.
Defining the Problem
Similar to Deformable DETR, the authors of this paper also observed a major issue:
- DETR's convergence speed is too slow.
The root of the problem lies in the fact that DETR's cross-attention mechanism does not consider predicted bounding boxes. This results in the need for multiple iterations to generate appropriate attention maps for each object query.
Thus, the solution to faster convergence might lie in improving the cross-attention mechanism.
Solving the Problem
Spatially Modulated Co-Attention
The core idea behind the Spatially Modulated Co-Attention (SMCA) mechanism is:
Combining learnable cross-attention maps with hand-crafted query spatial priors.
SMCA dynamically predicts the initial center and separate width and height scales of the box corresponding to each object query, generating 2D spatial Gaussian-like weight maps. These maps modulate the cross-attention weights over image features so that each query focuses more on locations near its predicted box.
Initial Predictions
Each object query dynamically predicts the center and scale of the object it is responsible for. The predicted center and scale of the object query can be expressed as:
Where the object query is projected via a two-layer MLP and sigmoid function to predict normalized centers within the range of . The normalized centers are then inversely transformed to obtain the real center coordinates in the image.
Additionally, object queries predict separate width and height scales, generating a 2D Gaussian-like weight map that reweights cross-attention near the predicted object location.
Gaussian Weight Map
Once the object center and scale are predicted, SMCA generates a 2D Gaussian weight map , which can be expressed as:
Where are spatial indices, and is a hyperparameter that adjusts the width of the Gaussian distribution.
assigns higher weights to locations near the center and lower weights to positions farther from the center.
Using the spatial prior , SMCA modulates the cross-attention map . The modulation is performed as follows:
Here, SMCA adds the logarithm of the spatial weight map to the cross-attention scores and applies softmax normalization over all spatial locations, increasing the focus on the predicted bounding box region and reducing the search space of cross-attention, thus speeding up the model's convergence.
Multi-Head SMCA
In the multi-head version of SMCA, different cross-attention heads are modulated based on their respective spatial weight maps.
Each attention head starts from the shared center and predicts a head-specific offset as well as a head-specific scale .
A Gaussian spatial weight map is generated for each head, centered at with the predicted scales.
The multi-head cross-attention feature map is expressed as:
To improve object detection performance, SMCA integrates multi-scale features. A CNN extracts multi-scale visual features with downsampling rates of 16, 32, and 64, respectively. These features are directly extracted from the CNN backbone without using a Feature Pyramid Network (FPN).
Each object query dynamically selects the most relevant scale by generating attention weights for different scales:
Cross-attention at different scales is computed as:
This mechanism allows each object query to dynamically select the most relevant scale while suppressing irrelevant scale features.
After cross-attention computation, the updated object query features are used to predict the bounding box and classification scores:
Bounding box predictions also use the initial spatial prior: the initial center's pre-sigmoid logits are added to the box-center logits first, and sigmoid is then applied to the combined logits. In the original equation, denotes the center value before sigmoid:
This ensures that the predicted bounding boxes are closely related to the highlighted cross-attention areas in SMCA.
Training Strategy
-
Dataset:
- The experiments are conducted on the COCO 2017 dataset. The training set contains 118,000 images, while the validation set contains 5,000 images. Performance is evaluated using mAP (Mean Average Precision), consistent with previous research.
-
Implementation details:
- The experimental setup follows the original DETR. ResNet-50 is used for feature extraction (denoted as SMCA-R50).
- Unlike DETR, SMCA uses 300 object queries (instead of 100) and replaces the original cross-entropy classification loss with focal loss to address foreground/background class imbalance. The initial focal loss probability is set to 0.01 to stabilize the training process.
-
Training setup:
- The model is trained for 50 epochs, with the learning rate reduced by a factor of 10 at epoch 40.
- The initial learning rate for the Transformer encoder is set to , while the learning rate for the pre-trained ResNet backbone is set to , using the AdamW optimizer.
- The default downsampling rates for multi-scale feature encoding are 16, 32, and 64.
-
Loss function and matching:
- Classification loss, L1 distance loss, and GIoU loss coefficients are set to 2, 5, and 2, respectively, during bipartite matching.
- After bipartite matching, the model minimizes classification loss, bounding box L1 loss, and GIoU loss with the same coefficients.
-
Additional experimental details:
- The Transformer layers use post-norm settings, consistent with previous studies.
- Random cropping is used during data augmentation, with the maximum width or height after cropping set to 1333.
- All models are trained on 8 V100 GPUs, each processing one image.
Discussion
Comparison with DETR
Single-scale SMCA mainly replaces DETR's decoder cross-attention and adds a small network that predicts spatial priors; the full multi-scale model also changes the encoder. In Table 1, single-scale SMCA takes 0.043 seconds and 86 GFLOPs per image versus DETR's 0.038 seconds and 86 GFLOPs. Full SMCA takes 0.100 seconds and 152 GFLOPs versus DETR-DC5's 0.079 seconds and 187 GFLOPs. Inference time and computation therefore need separate comparison.
With single-scale features, SMCA uses a self-attention dimension of 256 and an intermediate dimension of 2048 for the feed-forward network (FFN). For multi-scale features, the intermediate dimension of the FFN is set to 1024, and the encoder uses 5 layers of intra-scale and multi-scale self-attention, ensuring a similar parameter count to DETR for fair comparison.
As shown in the table above, "SMCA w/o multi-scale" achieves an mAP of 41.0 using single-scale features in 50 epochs, while the mAP improves to 43.7 with multi-scale features. With extended training, SMCA’s single-scale feature mAP increases from 41.0 to 42.7, and the multi-scale feature mAP improves from 43.7 to 45.6.
For reaching roughly 43 mAP, full SMCA reaches 43.7 at 50 epochs while DETR-DC5 reaches 43.3 at 500. That is a tenfold comparison in epoch count, with total GPU time requiring a separate comparison; full SMCA also takes 0.100 seconds per image versus DETR-DC5's 0.079 at inference.
Ablation Studies
A series of ablation experiments were conducted to verify the importance of each component in SMCA.
Head-shared SMCA
The authors added a head-shared spatial modulation mechanism to a 50-epoch DETR baseline while keeping the learning rate, training schedule, and self-attention settings fixed. This 34.8-mAP baseline already uses 300 queries and focal loss; it is not the original 100-query, cross-entropy DETR configuration.
The results show that head-shared modulation improves mAP from 34.8 to 40.2 after 50 epochs. Batch sizes 8, 16, and 32 give 40.2, 40.2, and 39.9 mAP, respectively; those three settings differ little.
Using the multi-head mechanism for spatial modulation modulates each Transformer head with a different spatial weight map. Each head starts from the same object center and predicts offsets relative to the shared center and head-specific scales. The results show that multi-head spatial modulation achieves an mAP of 41.0, a further improvement over head-shared modulation (40.2 to 41.0).
The authors also tested spatial weight maps with fixed scales, shared or independent predictions for width and height. When fixing the Gaussian distribution scale (set to 1), mAP is 38.5 (an improvement of 3.7 over the baseline), demonstrating the importance of predicting object centers in spatial modulation.
Multi-head SMCA
Introducing multi-scale feature encoding in the encoder, the design labeled as "SMCA (2Intra-Multi-2Intra)" uses 2 layers of intra-scale self-attention, 1 layer of multi-scale self-attention, followed by 2 more layers of intra-scale self-attention. This design improves performance from 41.0 to 43.7. Removing SSA results in a drop to 42.6.
Replacing 2Intra-Multi-2Intra with five intra-scale self-attention layers reduces mAP from 43.7 to 43.3, supporting the role of cross-scale exchange in this comparison. Five intra-scale layers score 43.3 versus 42.9 for three; three multi-scale layers also score 43.3, tying five intra-scale layers while using a different layer count and compute budget.
The authors ultimately chose 2Intra-Multi-2Intra, at 43.7 mAP with 39.5M parameters, compared with 41.0 mAP and 41.0M for single-scale SMCA. The weight-sharing rows are a separate ablation: sharing only FFN gives 43.0/42.2M, sharing only self-attention 42.8/44.7M, and no sharing 42.3/47.3M. These rows do not directly measure cross-dataset generalization.
Comparison with Other SOTA Architectures
The authors compared SMCA with other object detection frameworks on the COCO 2017 validation set.
-
Comparison with DETR:
- DETR uses an end-to-end Transformer for object detection. DETR-R50 and DETR-DC5-R50 refer to the ResNet-50-based and dilated ResNet-50-based versions of DETR. Compared to DETR, SMCA achieves faster convergence and better performance on small, medium, and large object detection tasks.
-
Comparison with Faster RCNN:
- Faster RCNN with FPN (Feature Pyramid Network) is a two-stage object detection method. SMCA-R50 at 108 epochs reaches 45.6 mAP versus Faster RCNN-FPN-R50++ at 108 epochs with 42.0.
- The latter has higher small-object AP (26.6 versus SMCA's 25.9), while SMCA has higher large-object AP (62.6 versus 53.4). This compares complete detectors and does not isolate the cause to multi-scale self-attention.
-
Comparison with Deformable DETR:
- Deformable DETR replaces the original DETR encoder's self-attention and the decoder's cross-attention with local deformable attention, while retaining the decoder's self-attention. This speeds up convergence compared with DETR, though the reported large-object AP is lower.
- In the listed R50 settings, DETR-DC5 has 61.1 large-object AP, Deformable DETR at 50 epochs 58.0, and SMCA at 50 epochs 60.4. Backbone and training schedules differ, so the difference cannot be attributed to spatial modulation alone.
- SMCA achieves an mAP of 43.7 at 50 epochs, comparable to Deformable DETR (43.8), but SMCA excels at large object detection, while Deformable DETR is better at small object detection.
-
Comparison with UP-DETR:
- UP-DETR explores unsupervised pre-training. In the table, SMCA reaches 45.6 mAP at 108 epochs while UP-DETR-R50+ reaches 42.8 at 300; the training procedures differ, so these are reported system results.
-
Comparison with TSP-FCOS and TSP-RCNN:
- TSP-FCOS and TSP-RCNN combine DETR’s Hungarian matching method with FCOS and RCNN detectors, resulting in faster convergence and better performance than DETR.
- Similar to Deformable DETR and Faster RCNN-FPN, TSP-FCOS and TSP-RCNN excel at small object detection but fall behind in large object detection.
- In shorter training, SMCA-R50 at 50 epochs reaches 43.7 mAP, close to TSP-RCNN-R50 at 36 epochs with 43.8 and above TSP-FCOS-R50 at 36 epochs with 43.1. Longer training gives SMCA 45.6 at 108 epochs versus TSP-RCNN+-R50, a distinct row, at 96 epochs with 45.0; this is not a single-module ablation.
-
Comparison using ResNet-101 Backbone:
- When replacing ResNet-50 with ResNet-101, the performance improvement trend is similar to that of the ResNet-50 backbone.
Conclusion
In this paper, the authors successfully integrated a Spatially Modulated Co-Attention (SMCA) mechanism into the DETR framework, achieving significant performance improvements.
In the reported R50 comparison, full SMCA reaches 45.6 mAP at 108 epochs, while DETR-DC5 reaches 43.3 at 500 epochs. The result supports faster convergence in epochs and higher AP under those settings, though full SMCA's per-image inference time is 0.100 seconds versus DETR-DC5's 0.079. By exploring global features with spatial priors, SMCA points to a practical way to improve the training path of DETR.
Several papers continue to improve DETR, so let’s keep exploring them.