Beyond Self-attention: External Attention using Two Linear Layers for Visual Tasks

Meng-Hao Guo, Zheng-Ning Liu, Tai-Jiang Mu, Shi-Min HuIEEE TPAMI 2021 · DOI

TL;DR

This paper introduces "external attention," a novel, computationally efficient attention mechanism for visual tasks. By leveraging small, shared memory units instead of pairwise comparisons within a single sample, external attention achieves linear complexity, implicitly captures dataset-wide correlations, and matches or surpasses self-attention performance with significantly reduced computational and memory costs.

What Problem Does This Paper Address?

Deep learning models, particularly in computer vision, have benefited immensely from attention mechanisms, especially self-attention. Self-attention excels at capturing long-range dependencies within an image by calculating affinities between all pairs of feature locations. However, this comes at a significant cost: quadratic complexity with respect to the number of features (e.g., pixels). This makes self-attention computationally prohibitive for high-resolution images. Furthermore, self-attention primarily focuses on intra-sample relationships, potentially missing valuable inter-sample correlations that could lead to more robust feature representations. This paper aims to address these limitations by proposing a more efficient and globally aware attention mechanism.

Methods

The core innovation is "external attention." Unlike self-attention, which computes affinities between query and key vectors derived from the same input feature map, external attention computes affinities between the input query vectors and external, learnable memory units. These memory units (two in total: one for keys, one for values) are small, shared across the entire dataset, and updated via back-propagation.

Here's a breakdown:

  • External Memory: Instead of an N x N attention matrix (where N is the number of features), external attention uses a smaller S x d matrix (where S is the memory size, a hyperparameter, and d is the feature dimension). This drastically reduces computation.
  • Linear Complexity: The computational complexity becomes linear with respect to the number of input features (O(dSN)), making it scalable to large inputs.
  • Dataset-Level Correlations: The shared memory units learn representations that capture global context across the entire dataset, acting as a form of regularization and improving generalization.
  • Multi-Head External Attention: Similar to multi-head self-attention, this allows the model to attend to different aspects of the input simultaneously, enhancing its representational power.
  • EAMLP Architecture: The authors propose an all-MLP architecture (EAMLP) for image classification by integrating multi-head external attention, demonstrating its potential to replace traditional convolutional layers.
  • Normalization: The paper utilizes a "double-normalization" technique to stabilize attention map computations, which is shown to be beneficial for both external and self-attention.

Datasets

The paper evaluates external attention across a wide range of visual tasks and datasets:

  • Image Classification: ImageNet-1K
  • Object Detection & Instance Segmentation: MS COCO
  • Semantic Segmentation: PASCAL VOC, ADE20K, Cityscapes
  • Image Generation: CIFAR-10, Tiny-ImageNet
  • Point Cloud Classification & Segmentation: ModelNet40, ShapeNet part segmentation

Key Insights and Ideas

  • Efficiency is Key: The quadratic complexity of self-attention is a major bottleneck for high-resolution vision tasks. External attention offers a practical solution by achieving linear complexity.
  • Global Context Matters: By learning from shared memory units across the entire dataset, external attention implicitly captures inter-sample relationships, which can lead to more robust and generalizable features than self-attention's intra-sample focus.
  • Simplicity and Versatility: The external attention module is designed to be a drop-in replacement for self-attention in existing architectures, making it easy to integrate and experiment with.
  • Memory Size as a Hyperparameter: The size of the external memory (S) offers a trade-off between computational cost and representational capacity, allowing for fine-tuning.
  • Normalization Boosts Performance: The proposed double-normalization technique significantly improves the performance of attention mechanisms, including external attention.

Key Results

  • Performance: External attention achieves results comparable to or superior to self-attention and its variants across various tasks, including image classification, object detection, semantic segmentation, and point cloud analysis.
  • Efficiency: External attention is significantly more computationally efficient. For example, it requires only half the parameters of self-attention and is up to 32 times faster for a given input size.
  • Image Classification: The proposed EAMLP architecture achieves a top-1 accuracy of 79.4% on ImageNet-1K.
  • Object Detection/Segmentation: External attention brings about a 1% improvement in accuracy on MS COCO for both object detection and instance segmentation.
  • Image Generation: External attention-based GANs (EAGAN) show better results (lower FID, higher IS) than SAGAN on CIFAR-10 and Tiny-ImageNet.
  • Point Clouds: External attention-based methods achieve state-of-the-art results on ModelNet40 classification and ShapeNet part segmentation.

Limitations and Future Work

  • Memory Size Tuning: The paper notes that choosing an appropriate memory unit size (S) is important for optimal performance, suggesting this is a hyperparameter that requires careful tuning.
  • EAMLP Convergence: While the EAMLP architecture shows promise, the paper mentions that for larger models (EAMLP-14 and EAMLP-19), using batch normalization instead of layer normalization in the MLP blocks led to failed convergence cases.
  • Future Work: The authors express hope that external attention will inspire practical applications and research in other domains, such as Natural Language Processing (NLP). The inherent efficiency and global context capture of external attention make it a promising candidate for various sequence modeling tasks.

Summary updated

BibTeX
@article{guo2021beyond,
  author = {Meng-Hao Guo and Zheng-Ning Liu and Tai-Jiang Mu and Shi-Min Hu},
  title = {{Beyond Self-attention: External Attention using Two Linear Layers for Visual Tasks}},
  journal = {IEEE Trans. Pattern Anal. Mach. Intell.},
  year = {2021},
  doi = {10.1109/tpami.2022.3211006},
}
Figure