Abstract
Non-Local (NL) blocks have become a staple in computer vision for capturing long-range dependencies. However, their computational overhead and reliance on the Softmax operator present significant challenges for lightweight architectures. This paper investigates the limitations of Softmax in NL blocks, revealing that it forces models to prioritize feature magnitude over angular relationships, leading to suboptimal attention maps. We propose the Scaled Non-Local Block, which replaces Softmax with a simple scaling factor. This modification allows for the application of the associative law of matrix multiplication, reducing computational complexity from $O(N^2C)$ to $O(NC^2)$ and enabling "free" multi-head attention. Experimental results on CIFAR and Tiny-ImageNet demonstrate that the Scaled NL block improves accuracy, enhances robustness to initialization, and significantly reduces memory consumption.
Introduction
In deep learning for computer vision, capturing global context is essential for tasks requiring a holistic understanding of an image. Non-Local (NL) blocks, inspired by the self-attention mechanism in Transformers, allow each pixel to interact with every other pixel in a feature map. While effective, standard NL blocks are computationally expensive, with costs growing quadratically relative to the number of pixels ($N$).
Recent research suggests that the bottleneck is not just the computation itself, but the specific normalization used: the Softmax operation. This paper re-evaluates the necessity of Softmax in lightweight non-local networks and introduces a more efficient, robust alternative.
The Softmax Bottleneck
Standard NL blocks use Softmax to normalize attention maps, ensuring that the weights assigned to different spatial locations sum to one. However, the authors identify two primary issues with this approach:
- The "Vertical Line" Problem: Visualization of standard NL attention maps often reveals distinct vertical lines. This indicates that certain "key" pixels receive high attention values across almost all "query" pixels. Rather than learning specific spatial relationships, the model becomes fixated on the most prominent (highest magnitude) features, regardless of their relevance to the specific query.
- Magnitude vs. Direction: Importance analysis shows that standard NL blocks rely heavily on the magnitude of feature vectors. When magnitude information is removed and only the direction (angular relationship) is used, performance drops significantly. This suggests that Softmax hinders the model's ability to utilize cosine similarity effectively.
The Scaled Non-Local Block
To address these limitations, the authors propose the Scaled Non-Local Block. The core innovation is the removal of the Softmax operation, replaced by a scaling factor of $\frac{1}{\sqrt{HWC}}$ (where $H, W$ are spatial dimensions and $C$ is the number of channels) to prevent gradient divergence during training.
Computational Efficiency
The removal of Softmax enables a critical mathematical optimization. In a standard NL block, the operation is typically computed as $(Query \times Key^T) \times Value$, resulting in an intermediate $N \times N$ attention matrix. Without Softmax, the associative law of matrix multiplication allows the operation to be reordered as $Query \times (Key^T \times Value)$.
- Standard NL Complexity: $O(N^2C)$
- Scaled NL Complexity: $O(NC^2)$
In most vision tasks, the number of pixels ($N$) is significantly larger than the number of channels ($C$), making the Scaled NL block substantially more efficient in terms of both memory and time.
Experimental Results
The proposed method was evaluated using PreResNet and Wide-ResNet architectures on CIFAR-10, CIFAR-100, and Tiny-ImageNet.
Accuracy and Robustness
On CIFAR-100, a PreResNet56 equipped with Scaled NL blocks achieved a 24.53% error rate, a notable improvement over the 25.12% error rate of the standard NL block. Furthermore, the Scaled NL block proved more robust to weight initialization. While standard NL blocks often require carefully tuned initialization (e.g., a standard deviation of 0.01) to converge, the Scaled NL block functions effectively with standard He initialization.
Multi-Head Attention and Scalability
One of the most significant advantages of the Scaled NL block is the implementation of multi-head attention at virtually no extra cost. Because of the $O(NC^2)$ complexity, increasing the number of heads does not scale the memory or time requirements linearly as it does in standard NL blocks.
| Metric | Standard NL (4-head) | Scaled NL (4-head) |
|---|---|---|
| Memory Usage | 5229 MB | 3417 MB |
| Training Speed | 100 ms/batch | 76 ms/batch |
Conclusion
The Scaled Non-Local Block offers a compelling alternative to standard NL blocks for lightweight neural networks. By rethinking the role of Softmax, the authors have developed a module that is not only more computationally efficient but also more capable of learning meaningful spatial relationships. While future work is needed to validate these findings on larger datasets like ImageNet-1k and in tasks like object detection, the Scaled NL block represents a significant step toward making global context modeling accessible for resource-constrained applications.
Summary updated