An Empirical Study of Spatial Attention Mechanisms in Deep Networks

Xizhou Zhu, Dazhi Cheng, Zheng Zhang, Stephen Lin, Jifeng Dai

Abstract

Spatial attention mechanisms, particularly the Transformer's self-attention, have become foundational in deep learning. This study presents a comprehensive empirical analysis by decomposing the standard attention module into four distinct mathematical terms. We find that the widely used "query-key content matching" is largely redundant for self-attention in computer vision, though essential for encoder-decoder tasks like translation. By integrating deformable convolutions with key-content saliency, we propose an optimized attention configuration that significantly improves the accuracy-efficiency tradeoff for vision tasks.

Introduction

The rapid adoption of Transformer-style attention across NLP and Computer Vision has often treated the underlying equations as a black box. It is generally assumed that the success of these models stems from comparing the content of a "query" element with "key" elements. However, the relative importance of content-based matching versus positional biases remained unclear. This paper addresses this gap by disentangling these factors to understand what truly drives performance in spatial attention across different modalities.

Generalized Attention Formulation

To analyze the components of attention, the authors decompose the standard Transformer attention weight calculation into four terms:

  1. $\mathcal{E}_1$ (Query & Key Content): Compatibility between query and key content.
  2. $\mathcal{E}_2$ (Query Content & Relative Position): Content-dependent spatial routing.
  3. $\mathcal{E}_3$ (Key Content Only): Pure saliency of the key element.
  4. $\mathcal{E}_4$ (Relative Position Only): Global positional bias.
Decomposition of the four attention terms: (a) query and key content, (b) query content and relative position, (c) key content only, and (d) relative position only.

By selectively enabling or disabling these terms using manual "switches," the study identifies which mechanisms are critical for specific tasks, such as object detection (COCO), semantic segmentation (Cityscapes), and neural machine translation (WMT 2014).

Key Findings

The Role of Query-Key Matching

A major insight is that the $\mathcal{E}_1$ term (query-key content matching) is virtually useless for self-attention in vision. Removing it maintains accuracy while significantly reducing computational overhead. In contrast, $\mathcal{E}_1$ is vital for encoder-decoder attention in NMT, where it facilitates alignment between different sequences (e.g., source and target words).

Efficiency in Vision via Deformable Convolutions

Deformable convolutions are found to be a superior, sparse alternative to the dense $\mathcal{E}_2$ term in vision tasks. Because image features change gradually, sparse sampling is more efficient and effective than the global dense computation of standard Transformers. Conversely, Dynamic Convolutions, while effective in NLP, failed to generalize well to vision tasks.

Results and the "Winning Formula"

The study demonstrates that for vision, the optimal configuration combines sparse spatial routing with key saliency. Specifically, replacing the heavy $\mathcal{E}_2$ term with a Deformable Convolution and retaining only the $\mathcal{E}_3$ (key content saliency) term yields the best results.

Performance vs. efficiency on COCO object detection. The "0010 + deformable" configuration (red circle) achieves superior mAP with lower GFLOPs compared to the full Transformer (1111).

On the COCO dataset, this "Deformable + $\mathcal{E}_3$" module achieved 40.8 mAP at 221.1 GFLOPs, significantly outperforming the full Transformer baseline of 38.8 mAP at 281.4 GFLOPs.

Conclusion

This empirical study reveals that standard Transformer attention is often suboptimal for computer vision. By understanding the mathematical contributions of different attention terms, researchers can design more efficient architectures tailored to specific data requirements. Future work may extend this analysis to channel-wise attention and further refine dynamic routing mechanisms for non-textual modalities.

Summary updated

BibTeX
@inproceedings{zhu2019empirical,
  author = {Xizhou Zhu and Dazhi Cheng and Zheng Zhang and Stephen Lin and Jifeng Dai},
  title = {{An Empirical Study of Spatial Attention Mechanisms in Deep Networks}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2019},
  doi = {10.1109/iccv.2019.00679},
}
Figure