Deformable DETR: Deformable Transformers for End-to-End Object Detection

DETR has been recently proposed to eliminate the need for many hand-designed components in object detection while demonstrating good performance . However , it suffers from slow convergence and limited feature spatial resolution , due to the limitation of Transformer attention modules in processing image feature maps . To mitigate these issues , we proposed Deformable DETR , whose attention modules only attend to a small set of key sampling points around a reference . Deformable DETR can achieve better performance than DETR ( especially on small objects ) with 10× less training epochs . Extensive experiments on the COCO benchmark demonstrate the effectiveness of our approach . Code is released at https : // github.com/fundamentalvision/Deformable-DETR . 1 INTRODUCTION . Modern object detectors employ many hand-crafted components ( Liu et al. , 2020 ) , e.g. , anchor generation , rule-based training target assignment , non-maximum suppression ( NMS ) post-processing . They are not fully end-to-end . Recently , Carion et al . ( 2020 ) proposed DETR to eliminate the need for such hand-crafted components , and built the first fully end-to-end object detector , achieving very competitive performance . DETR utilizes a simple architecture , by combining convolutional neural networks ( CNNs ) and Transformer ( Vaswani et al. , 2017 ) encoder-decoders . They exploit the versatile and powerful relation modeling capability of Transformers to replace the hand-crafted rules , under properly designed training signals . Despite its interesting design and good performance , DETR has its own issues : ( 1 ) It requires much longer training epochs to converge than the existing object detectors . For example , on the COCO ( Lin et al. , 2014 ) benchmark , DETR needs 500 epochs to converge , which is around 10 to 20 times slower than Faster R-CNN ( Ren et al. , 2015 ) . ( 2 ) DETR delivers relatively low performance at detecting small objects . Modern object detectors usually exploit multi-scale features , where small objects are detected from high-resolution feature maps . Meanwhile , high-resolution feature maps lead to unacceptable complexities for DETR . The above-mentioned issues can be mainly attributed to the deficit of Transformer components in processing image feature maps . At initialization , the attention modules cast nearly uniform attention weights to all the pixels in the feature maps . Long training epoches is necessary for the attention weights to be learned to focus on sparse meaningful locations . On the other hand , the attention weights computation in Transformer encoder is of quadratic computation w.r.t . pixel numbers . Thus , it is of very high computational and memory complexities to process high-resolution feature maps . In the image domain , deformable convolution ( Dai et al. , 2017 ) is of a powerful and efficient mechanism to attend to sparse spatial locations . It naturally avoids the above-mentioned issues . While it lacks the element relation modeling mechanism , which is the key for the success of DETR . ∗Equal contribution . †Corresponding author . ‡Work is done during an internship at SenseTime Research . In this paper , we propose Deformable DETR , which mitigates the slow convergence and high complexity issues of DETR . It combines the best of the sparse spatial sampling of deformable convolution , and the relation modeling capability of Transformers . We propose the deformable attention module , which attends to a small set of sampling locations as a pre-filter for prominent key elements out of all the feature map pixels . The module can be naturally extended to aggregating multi-scale features , without the help of FPN ( Lin et al. , 2017a ) . In Deformable DETR , we utilize ( multi-scale ) deformable attention modules to replace the Transformer attention modules processing feature maps , as shown in Fig . 1 . Deformable DETR opens up possibilities for us to exploit variants of end-to-end object detectors , thanks to its fast convergence , and computational and memory efficiency . We explore a simple and effective iterative bounding box refinement mechanism to improve the detection performance . We also try a two-stage Deformable DETR , where the region proposals are also generated by a vaiant of Deformable DETR , which are further fed into the decoder for iterative bounding box refinement . Extensive experiments on the COCO ( Lin et al. , 2014 ) benchmark demonstrate the effectiveness of our approach . Compared with DETR , Deformable DETR can achieve better performance ( especially on small objects ) with 10× less training epochs . The proposed variant of two-stage Deformable DETR can further improve the performance . Code is released at https : //github . com/fundamentalvision/Deformable-DETR . 2 RELATED WORK . Efficient Attention Mechanism . Transformers ( Vaswani et al. , 2017 ) involve both self-attention and cross-attention mechanisms . One of the most well-known concern of Transformers is the high time and memory complexity at vast key element numbers , which hinders model scalability in many cases . Recently , many efforts have been made to address this problem ( Tay et al. , 2020b ) , which can be roughly divided into three categories in practice . The first category is to use pre-defined sparse attention patterns on keys . The most straightforward paradigm is restricting the attention pattern to be fixed local windows . Most works ( Liu et al. , 2018a ; Parmar et al. , 2018 ; Child et al. , 2019 ; Huang et al. , 2019 ; Ho et al. , 2019 ; Wang et al. , 2020a ; Hu et al. , 2019 ; Ramachandran et al. , 2019 ; Qiu et al. , 2019 ; Beltagy et al. , 2020 ; Ainslie et al. , 2020 ; Zaheer et al. , 2020 ) follow this paradigm . Although restricting the attention pattern to a local neighborhood can decrease the complexity , it loses global information . To compensate , Child et al . ( 2019 ) ; Huang et al . ( 2019 ) ; Ho et al . ( 2019 ) ; Wang et al . ( 2020a ) attend key elements at fixed intervals to significantly increase the receptive field on keys . Beltagy et al . ( 2020 ) ; Ainslie et al . ( 2020 ) ; Zaheer et al . ( 2020 ) allow a small number of special tokens having access to all key elements . Zaheer et al . ( 2020 ) ; Qiu et al . ( 2019 ) also add some pre-fixed sparse attention patterns to attend distant key elements directly . The second category is to learn data-dependent sparse attention . Kitaev et al . ( 2020 ) proposes a locality sensitive hashing ( LSH ) based attention , which hashes both the query and key elements to different bins . A similar idea is proposed by Roy et al . ( 2020 ) , where k-means finds out the most related keys . Tay et al . ( 2020a ) learns block permutation for block-wise sparse attention . The third category is to explore the low-rank property in self-attention . Wang et al . ( 2020b ) reduces the number of key elements through a linear projection on the size dimension instead of the channel dimension . Katharopoulos et al . ( 2020 ) ; Choromanski et al . ( 2020 ) rewrite the calculation of selfattention through kernelization approximation . In the image domain , the designs of efficient attention mechanism ( e.g. , Parmar et al . ( 2018 ) ; Child et al . ( 2019 ) ; Huang et al . ( 2019 ) ; Ho et al . ( 2019 ) ; Wang et al . ( 2020a ) ; Hu et al . ( 2019 ) ; Ramachandran et al . ( 2019 ) ) are still limited to the first category . Despite the theoretically reduced complexity , Ramachandran et al . ( 2019 ) ; Hu et al . ( 2019 ) admit such approaches are much slower in implementation than traditional convolution with the same FLOPs ( at least 3× slower ) , due to the intrinsic limitation in memory access patterns . On the other hand , as discussed in Zhu et al . ( 2019a ) , there are variants of convolution , such as deformable convolution ( Dai et al. , 2017 ; Zhu et al. , 2019b ) and dynamic convolution ( Wu et al. , 2019 ) , that also can be viewed as self-attention mechanisms . Especially , deformable convolution operates much more effectively and efficiently on image recognition than Transformer self-attention . Meanwhile , it lacks the element relation modeling mechanism . Our proposed deformable attention module is inspired by deformable convolution , and belongs to the second category . It only focuses on a small fixed set of sampling points predicted from the feature of query elements . Different from Ramachandran et al . ( 2019 ) ; Hu et al . ( 2019 ) , deformable attention is just slightly slower than the traditional convolution under the same FLOPs . Multi-scale Feature Representation for Object Detection . One of the main difficulties in object detection is to effectively represent objects at vastly different scales . Modern object detectors usually exploit multi-scale features to accommodate this . As one of the pioneering works , FPN ( Lin et al. , 2017a ) proposes a top-down path to combine multi-scale features . PANet ( Liu et al. , 2018b ) further adds an bottom-up path on the top of FPN . Kong et al . ( 2018 ) combines features from all scales by a global attention operation . Zhao et al . ( 2019 ) proposes a U-shape module to fuse multi-scale features . Recently , NAS-FPN ( Ghiasi et al. , 2019 ) and Auto-FPN ( Xu et al. , 2019 ) are proposed to automatically design cross-scale connections via neural architecture search . Tan et al . ( 2020 ) proposes the BiFPN , which is a repeated simplified version of PANet . Our proposed multi-scale deformable attention module can naturally aggregate multi-scale feature maps via attention mechanism , without the help of these feature pyramid networks . 3 REVISITING TRANSFORMERS AND DETR . Multi-Head Attention in Transformers . Transformers ( Vaswani et al. , 2017 ) are of a network architecture based on attention mechanisms for machine translation . Given a query element ( e.g. , a target word in the output sentence ) and a set of key elements ( e.g. , source words in the input sentence ) , the multi-head attention module adaptively aggregates the key contents according to the attention weights that measure the compatibility of query-key pairs . To allow the model focusing on contents from different representation subspaces and different positions , the outputs of different attention heads are linearly aggregated with learnable weights . Let q ∈ Ωq indexes a query element with representation feature zq ∈ RC , and k ∈ Ωk indexes a key element with representation feature xk ∈ RC , where C is the feature dimension , Ωq and Ωk specify the set of query and key elements , respectively . Then the multi-head attention feature is calculated by MultiHeadAttn ( zq , x ) = M∑ m=1 Wm [ ∑ k∈Ωk Amqk ·W ′mxk ] , ( 1 ) where m indexes the attention head , W ′m ∈ RCv×C and Wm ∈ RC×Cv are of learnable weights ( Cv = C/M by default ) . The attention weights Amqk ∝ exp { zTq U T m Vmxk√ Cv } are normalized as∑ k∈Ωk Amqk = 1 , in which Um , Vm ∈ R Cv×C are also learnable weights . To disambiguate different spatial positions , the representation features zq and xk are usually of the concatenation/summation of element contents and positional embeddings . There are two known issues with Transformers . One is Transformers need long training schedules before convergence . Suppose the number of query and key elements are of Nq and Nk , respectively . Typically , with proper parameter initialization , Umzq and Vmxk follow distribution with mean of 0 and variance of 1 , which makes attention weights Amqk ≈ 1Nk , when Nk is large . It will lead to ambiguous gradients for input features . Thus , long training schedules are required so that the attention weights can focus on specific keys . In the image domain , where the key elements are usually of image pixels , Nk can be very large and the convergence is tedious . On the other hand , the computational and memory complexity for multi-head attention can be very high with numerous query and key elements . The computational complexity of Eq . 1 is of O ( NqC 2 +NkC 2 +NqNkC ) . In the image domain , where the query and key elements are both of pixels , Nq = Nk C , the complexity is dominated by the third term , as O ( NqNkC ) . Thus , the multi-head attention module suffers from a quadratic complexity growth with the feature map size . DETR . DETR ( Carion et al. , 2020 ) is built upon the Transformer encoder-decoder architecture , combined with a set-based Hungarian loss that forces unique predictions for each ground-truth bounding box via bipartite matching . We briefly review the network architecture as follows . Given the input feature maps x ∈ RC×H×W extracted by a CNN backbone ( e.g. , ResNet ( He et al. , 2016 ) ) , DETR exploits a standard Transformer encoder-decoder architecture to transform the input feature maps to be features of a set of object queries . A 3-layer feed-forward neural network ( FFN ) and a linear projection are added on top of the object query features ( produced by the decoder ) as the detection head . The FFN acts as the regression branch to predict the bounding box coordinates b ∈ [ 0 , 1 ] 4 , where b = { bx , by , bw , bh } encodes the normalized box center coordinates , box height and width ( relative to the image size ) . The linear projection acts as the classification branch to produce the classification results . For the Transformer encoder in DETR , both query and key elements are of pixels in the feature maps . The inputs are of ResNet feature maps ( with encoded positional embeddings ) . Let H and W denote the feature map height and width , respectively . The computational complexity of self-attention is of O ( H2W 2C ) , which grows quadratically with the spatial size . For the Transformer decoder in DETR , the input includes both feature maps from the encoder , and N object queries represented by learnable positional embeddings ( e.g. , N = 100 ) . There are two types of attention modules in the decoder , namely , cross-attention and self-attention modules . In the cross-attention modules , object queries extract features from the feature maps . The query elements are of the object queries , and key elements are of the output feature maps from the encoder . In it , Nq = N , Nk = H ×W and the complexity of the cross-attention is of O ( HWC2 + NHWC ) . The complexity grows linearly with the spatial size of feature maps . In the self-attention modules , object queries interact with each other , so as to capture their relations . The query and key elements are both of the object queries . In it , Nq = Nk = N , and the complexity of the self-attention module is of O ( 2NC2 +N2C ) . The complexity is acceptable with moderate number of object queries . DETR is an attractive design for object detection , which removes the need for many hand-designed components . However , it also has its own issues . These issues can be mainly attributed to the deficits of Transformer attention in handling image feature maps as key elements : ( 1 ) DETR has relatively low performance in detecting small objects . Modern object detectors use high-resolution feature maps to better detect small objects . However , high-resolution feature maps would lead to an unacceptable complexity for the self-attention module in the Transformer encoder of DETR , which has a quadratic complexity with the spatial size of input feature maps . ( 2 ) Compared with modern object detectors , DETR requires many more training epochs to converge . This is mainly because the attention modules processing image features are difficult to train . For example , at initialization , the cross-attention modules are almost of average attention on the whole feature maps . While , at the end of the training , the attention maps are learned to be very sparse , focusing only on the object extremities . It seems that DETR requires a long training schedule to learn such significant changes in the attention maps .

Summary: As a new framework for object detection, DETR is very important. However,  it suffers from slow convergence and limited feature spatial resolution. This paper proposes deformable attention, which attends to a small set of sampling locations rather than all the locations in the original DETR. Besides, the paper applies multi-scale deformable attention for better results.