Can Vision Transformers Perform Convolution?

Shanda Li, Xiangning Chen, Di He, Cho-Jui Hsieh

Introduction

Vision Transformers (ViTs) have emerged as powerful alternatives to Convolutional Neural Networks (CNNs), particularly when trained on massive datasets like JFT-300M. However, their performance often degrades significantly in "low-data regimes," where traditional CNNs maintain a competitive edge. This discrepancy is frequently attributed to the "inductive bias" of CNNs—specifically locality and translation invariance—which ViTs supposedly lack. This research investigates whether this limitation is a matter of expressive power or an optimization challenge, ultimately proving that ViTs are a mathematical superset of CNNs.

Theoretical Framework: Expressing Convolution via Attention

The core theoretical contribution of this work is a constructive proof demonstrating that a Multi-Head Self-Attention (MHSA) layer can be configured to act exactly as a convolution. This mapping requires two critical components:

  1. Multi-head Attention: Different heads are utilized to attend to various spatial offsets.
  2. Relative Positional Encoding: This allows the model to identify specific neighbors (e.g., "top-left") in a context-agnostic manner, which is not possible with absolute positional encoding.

A significant finding regarding "patch-input" ViTs (the standard architecture using $16 \times 16$ patches) is that they only require 9 heads to mimic a standard convolution. This is because any pixel's receptive field for a typical kernel (like $3 \times 3$) spans at most the current patch and its eight immediate neighbors. Thus, 9 heads are sufficient to gather all necessary information across patch boundaries.

Methodology: Two-Phase Training

To leverage this theoretical insight, the authors propose a novel two-phase training pipeline designed to inject "convolutional bias" into the ViT architecture.

The two-phase training pipeline: transitioning from a Convolution Phase to a Self-Attention Phase via weight initialization.
  • Phase 1 (Convolution Phase): A variant of the ViT is trained where the attention layers are replaced by standard convolutions.
  • Phase 2 (Self-Attention Phase): The learned convolution weights are mapped into MHSA weights using the established theoretical construction. Training then continues with the standard ViT architecture.

Key Results and Performance

The proposed approach, termed CMHSA (Convolutional Multi-Head Self-Attention), was evaluated on the CIFAR-100 dataset. The results demonstrate that initializing ViTs with a convolutional bias dramatically improves performance and optimization stability.

Performance comparison of CMHSA against standard ViT and DeiT models on CIFAR-100.

As shown in the table, the CMHSA-5 model achieved a Top-1 accuracy of 78.74%, significantly outperforming the original ViT-base (60.90%) and the improved DeiT-base (69.98%). Furthermore, this initialization strategy allowed for the removal of the "warm-up" phase typically required for Transformer stability, as the model starts from a much more effective functional state.

Conclusion

This research confirms that the poor performance of ViTs on small datasets is not due to a lack of expressive power, but rather a difficulty in navigating a vast search space during optimization. By proving that ViTs can perform convolution and providing a method to initialize them with this capability, the authors bridge the gap between the flexibility of Transformers and the efficient inductive biases of CNNs. Future work may explore how to approximate this mapping for models with fewer than 9 heads and its impact on large-scale foundation models.

Summary updated

BibTeX
@misc{li2021can,
  author = {Shanda Li and Xiangning Chen and Di He and Cho-Jui Hsieh},
  title = {{Can Vision Transformers Perform Convolution?}},
  year = {2021},
  eprint = {2111.01353},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2111.01353},
}
Figure