LocalViT: Analyzing Locality in Vision Transformers

Yawei Li, Kai Zhang, Jiezhang Cao, Radu Timofte, Michele Magno, Luca Benini, Luc Van Gool · DOI

TL;DR

This paper introduces "LocalViT," a modification to Vision Transformers (ViTs) that injects local receptive field processing into the feed-forward network. By systematically analyzing the impact of this locality mechanism, the authors demonstrate significant performance improvements across various ViT architectures with minimal parameter and computational overhead, suggesting locality is a crucial component for ViTs.

What Problem Does This Paper Address?

Vision Transformers (ViTs) have achieved remarkable success in computer vision tasks by leveraging self-attention to model long-range dependencies across image tokens. However, this global connectivity comes at the cost of inherent locality, a fundamental inductive bias present in Convolutional Neural Networks (CNNs) that allows them to efficiently learn hierarchical features from local image regions. While ViTs can learn local patterns through stacking layers, they lack a dedicated mechanism for efficient local information aggregation within their feed-forward networks. This paper aims to bridge this gap by investigating how to effectively incorporate locality into ViTs to combine the strengths of both architectures, leading to improved performance without a substantial increase in computational cost.

Methods

The core innovation is the introduction of a locality mechanism into the feed-forward network (FFN) of ViTs. Inspired by the inverted residual blocks in MobileNets, which use depth-wise convolutions to efficiently process local information, the authors propose replacing the standard FFN with a module that includes depth-wise convolutions.

Here's a breakdown of the approach:

  • Sequence to Lattice Conversion: To apply depth-wise convolutions, which operate on spatial grids, the input sequence of tokens is first rearranged into a 2D feature map (a "lattice"). This allows for local operations.
  • Local Module: The FFN then incorporates a sequence of operations: two 1x1 convolutions (for channel mixing and expansion/squeeze) sandwiching a depth-wise convolution (for local feature aggregation).
  • Lattice to Sequence Conversion: After the local processing, the feature map is converted back into a sequence of tokens to be fed into the next self-attention layer.
  • Class Token Handling: A specific strategy is devised to handle the class token (used for classification) during the sequence-to-lattice conversion, ensuring it bypasses the local processing while still being integrated correctly.
  • Systematic Analysis: The paper conducts a thorough ablation study to analyze the impact of various design choices:
  • Activation Functions: Investigating different non-linearities (ReLU6, h-swish, SE, ECA) after the depth-wise convolution.
  • Layer Placement: Determining whether locality is more beneficial in lower, middle, or higher transformer layers.
  • Expansion Ratio (γ): Analyzing how the hidden dimension expansion in the FFN affects performance.

Datasets

The primary dataset used for training and evaluation is ImageNet2012. This dataset contains 1.28 million training images and 50,000 validation images across 1000 classes.

Key Insights and Ideas

  • Locality is Crucial for ViTs: The study strongly suggests that incorporating a dedicated locality mechanism significantly boosts ViT performance, even for architectures that already attempt some form of local processing (like Swin Transformers).
  • FFN is the Right Place: Modifying the feed-forward network, rather than just the attention mechanism, proves to be an effective strategy for injecting locality.
  • Lower Layers Benefit Most: Locality mechanisms are found to be most impactful when applied to the earlier layers of the transformer, likely because they help establish robust local feature representations that can be progressively refined by higher layers.
  • Activation Functions Matter: The choice of non-linear activation function after the depth-wise convolution can lead to substantial performance gains, highlighting the interplay between different architectural components.
  • Generality of the Approach: The proposed locality mechanism is shown to be effective across a diverse set of ViT architectures (DeiT, Swin, T2T-ViT, PVT, TNT), demonstrating its broad applicability.

Key Results

  • Performance Gains: LocalViT variants consistently outperform their baseline counterparts on ImageNet2012 classification. For example, LocalViT-T improves upon DeiT-T by 1.5% (with ReLU6) and up to 3.0% (with a different expansion ratio). LocalViT-PVT achieves a gain of 3.1% over PVT-T.
  • Minimal Overhead: The performance improvements come with only a negligible increase in parameters and computational cost. For instance, LocalViT-T adds only 0.1M parameters and a marginal increase in FLOPs compared to DeiT-T.
  • Improved Training Dynamics: Figure 4 shows that LocalViT models converge faster and achieve higher Top-1 and Top-5 accuracies during training compared to their baselines.
  • Competitive with CNNs: Some LocalViT variants achieve performance comparable to or better than established CNN architectures like MobileNetV2 and ResNet-50.
  • Feature Map Analysis: Figure 5 visually demonstrates that LocalViT produces feature maps that are better at localizing objects within images compared to standard ViTs.

Limitations and Future Work

  • Throughput Decrease: While the parameter and FLOPs increase is minimal, the authors acknowledge a slight decrease in inference throughput for some LocalViT variants (e.g., PVT and Swin) compared to their baselines. This decrease is generally within acceptable limits (up to 24%), but it is a trade-off for improved accuracy.
  • Future Directions: The paper suggests that combining the locality mechanism with other design choices for more efficient networks could be a fruitful area for future research. The authors also emphasize that their work focuses on understanding the importance of locality rather than solely achieving state-of-the-art performance, implying that further optimization is possible.

Summary updated

BibTeX
@inproceedings{li2023localvit,
  author = {Yawei Li and Kai Zhang and Jiezhang Cao and Radu Timofte and Michele Magno and Luca Benini and Luc Van Gool},
  title = {{LocalViT: Analyzing Locality in Vision Transformers}},
  booktitle = {IEEE/RSJ International Conference on Intelligent Robots and Systems},
  year = {2023},
  doi = {10.1109/iros55552.2023.10342025},
}
Figure