TL;DR
This paper introduces "LocalViT," a method to inject local feature processing into Vision Transformers (ViTs) by adding depth-wise convolutions to their feed-forward networks. This simple yet effective modification significantly boosts performance across various ViT architectures, demonstrating that locality is crucial for image understanding, even in models designed for global context.
What Problem Does This Paper Address?
Vision Transformers (ViTs) have achieved remarkable success in computer vision by leveraging self-attention to capture long-range dependencies across image tokens. However, this global connectivity comes at the cost of local information processing, a fundamental aspect of how Convolutional Neural Networks (CNNs) excel at image tasks. CNNs inherently process local regions through their convolutional filters, building up hierarchical features. ViTs, on the other hand, lack this explicit local inductive bias. This paper aims to bridge this gap by systematically investigating how to effectively incorporate locality mechanisms into ViTs without sacrificing their global reasoning capabilities or incurring excessive computational overhead.
Methods
The core innovation of LocalViT lies in modifying the feed-forward network (FFN) within the standard ViT encoder. Inspired by the inverted residual blocks in MobileNets, which use depth-wise convolutions to efficiently aggregate local information, the authors propose replacing or augmenting the FFN's fully-connected layers with depth-wise convolutions.
Here's a breakdown of the key methodological steps:
- Input Rearrangement: To apply 2D depth-wise convolutions, the sequence of image tokens is first reshaped into a 2D feature map (lattice perspective). This allows spatial proximity between tokens to be considered.
- Locality Injection: Depth-wise convolutions are then applied to this 2D feature map. This operation aggregates information within local spatial neighborhoods.
- Reconstruction: After the convolutional operations, the feature map is reshaped back into a sequence of tokens to be processed by the subsequent self-attention layer.
- Class Token Handling: A special "class token" is used for classification. When introducing depth-wise convolutions, which require a 2D structure, the class token is temporarily split from the image tokens and then re-concatenated after the FFN processing.
- Design Choices: The paper systematically analyzes various design choices for incorporating locality, including:
- Activation Functions: Investigating ReLU6, h-swish, and combinations with attention modules like SE and ECA.
- Layer Placement: Determining whether locality is more beneficial in lower, middle, or higher layers of the transformer.
- Expansion Ratio (γ): Analyzing how the expansion of the hidden dimension 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 experiments strongly suggest that even for models designed for global context, explicit local feature aggregation significantly enhances performance.
- Simple is Effective: Injecting depth-wise convolutions into the FFN is a surprisingly simple yet powerful modification that yields substantial gains.
- Generalizability: The proposed locality mechanism is not tied to a specific ViT architecture; it demonstrates broad applicability across various popular ViTs (DeiT, Swin, T2T-ViT, PVT, TNT).
- Lower Layers Benefit Most: Locality appears to be more impactful in the earlier layers of the transformer, where it can establish foundational local features that are then propagated and refined by subsequent layers.
- Activation Functions Matter: The choice of non-linear activation function after the depth-wise convolution plays a significant role in performance, with h-swish and attention modules showing promise.
Key Results
- Performance Gains: LocalViT consistently outperforms its baseline counterparts on ImageNet2012 classification. For example, LocalViT-T improves upon DeiT-T by 2.6%, and LocalViT-PVT improves upon PVT-T by 3.1%.
- Negligible Parameter/Compute Increase: The added locality mechanism results in only a marginal increase in parameters and computational cost, making it an efficient enhancement.
- 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.
- Comparable to CNNs: Some LocalViT variants achieve performance comparable to or even exceeding established CNN architectures like MobileNetV2 and ResNet-50.
- Feature Map Visualization: Figure 5 visually demonstrates that LocalViT models produce feature maps that better localize objects within the input images.
Limitations and Future Work
- Inference Throughput: The paper acknowledges that introducing locality mechanisms can lead to a decrease in inference throughput. While the decrease is reported as marginal for some architectures (e.g., <10% for DeiT-T and TNT-T), it is more significant for others like PVT-T and Swin-T (around 22-23%). The authors deem this trade-off acceptable given the performance gains.
- Future Work: The authors suggest that combining this locality mechanism with other design choices for more efficient networks could be a fruitful direction for future research. They also emphasize that their work focuses on understanding the importance of locality rather than solely achieving state-of-the-art performance, paving the way for further exploration.
Summary updated