TL;DR
LocalViT introduces a "locality mechanism" to Vision Transformers by injecting depth-wise convolutions into their feed-forward networks (FFN). This simple architectural tweak allows Transformers to capture local spatial correlations like CNNs do, leading to significant accuracy gains (up to 3.1% on ImageNet) across various Transformer families with negligible increases in model size.
What Problem Does This Paper Address?
Vision Transformers (ViTs) have taken the computer vision world by storm by treating images as sequences of tokens and using self-attention to model global dependencies. However, they lack the "inductive bias" of locality that makes Convolutional Neural Networks (CNNs) so effective. In a standard ViT, every token interacts with every other token globally, but there is no explicit mechanism for tokens to aggregate information from their immediate neighbors within the FFN.
The authors argue that this global-only connectivity contradicts the nature of visual data, where nearby pixels are highly correlated. The challenge is to efficiently combine the global reach of Transformers with the local processing power of CNNs without ballooning the model's computational cost or complexity.
Methods
The authors propose a "seemingly simple" solution inspired by the inverted residual blocks found in efficient CNNs like MobileNetV2.
In a standard Transformer, the FFN consists of two linear layers that expand and then contract the embedding dimension. The authors realized that if you rearrange the sequence of tokens back into a 2D lattice (an image-like grid), these linear layers are mathematically equivalent to $1 \times 1$ convolutions. To introduce locality, they simply insert a $3 \times 3$ depth-wise convolution between these two layers.
Key technical steps include:
- Seq2Img: Reshaping the 1D token sequence into a 2D feature map.
- Locality Injection: Applying a depth-wise convolution to aggregate local information.
- Class Token Handling: Since the "class token" used for classification doesn't fit into a 2D grid, it is split out before the local processing and concatenated back afterward.
- Activation Optimization: The authors experimented with various functions, finding that
h-swishand Squeeze-and-Excitation (SE) modules further boost performance.
Datasets
The primary benchmark used for training and evaluation is the ImageNet2012 dataset (ImageNet-1K), which contains 1.28 million training images and 50,000 validation images across 1,000 classes. The authors applied their LocalViT modifications to several baseline architectures, including DeiT, Swin Transformer, T2T-ViT, PVT, and TNT.
Key Insights and Ideas
- Locality is a Hierarchy Builder: The authors found that adding locality is most beneficial in the lower (earlier) layers of the network. This suggests that local information aggregation early on helps build a better hierarchy of features that the later global attention layers can then exploit.
- The "Inverted Residual" Connection: By viewing the FFN through a "lattice perspective," the authors bridged the gap between Transformer design and efficient CNN design. A Transformer FFN is essentially a MobileNetV2 block missing its spatial convolution.
- Generality: The mechanism isn't tied to one specific architecture. Whether a Transformer uses standard global attention (DeiT) or shifted windows (Swin), adding local convolutions to the FFN consistently helps.
- Visual Grounding: Qualitative analysis of feature maps showed that LocalViT models do a much better job of localizing and "focusing" on the actual objects in an image compared to baseline ViTs, which often produce noisier activations.
Key Results
The results demonstrate that locality is a powerful "free lunch" for Vision Transformers. As shown in the performance comparison, LocalViT variants consistently sit above the baseline curves for both parameter count and FLOPs.
- Accuracy Boosts: On ImageNet, LocalViT improved PVT-T by 3.1%, DeiT-T by 2.6%, and Swin-T by 1.0%.
- Efficiency: These gains were achieved with a "negligible" increase in parameters. For example, LocalViT-T adds only about 0.2M parameters to the 5.7M parameter DeiT-T baseline.
- Training Speed: LocalViT models converge faster, showing significantly higher accuracy in the early stages of training (up to 10% higher at epoch 25).
Limitations and Future Work
The main trade-off identified is inference throughput. While the parameter and FLOP increases are small, the overhead of reshaping tensors (Seq2Img/Img2Seq) and the fragmented nature of depth-wise convolutions can slow down actual wall-clock inference time. The authors noted a throughput decrease of about 2.7% for DeiT-based models, but up to 23.3% for Swin-based models.
Future work could involve optimizing these operations for specific hardware or exploring even more efficient ways to blend local and global processing, perhaps by integrating locality directly into the attention mechanism itself rather than the FFN.
Summary updated