TiC: Exploring Vision Transformer in Convolution

Song Zhang, Qingzhong Wang, Jiang Bian, Haoyi Xiong

Vision Transformers (ViTs) have taken the computer vision world by storm, but they come with a frustrating "resolution lock." Because standard ViTs rely on global self-attention and fixed positional encodings, you often can't just feed them a higher-resolution image without retraining or messy re-sampling.

A new paper from Baidu Inc., "TiC: Exploring Vision Transformer in Convolution," proposes a clever fix: Vision Transformer in Convolution (TiC). By reimagining self-attention as a convolutional sliding window, the authors have created a model that handles arbitrary image sizes with ease and efficiency.

TL;DR

TiC introduces Multi-Head Self-Attention Convolution (MSA-Conv), a mechanism that implements self-attention within a convolutional sliding window. This allows Transformers to process images of any resolution without retraining, while reducing computational complexity from quadratic to linear—making it significantly more efficient for high-resolution tasks than standard ViTs.

What Problem Does This Paper Address?

Standard Vision Transformers are computationally expensive. Their global attention mechanism has a complexity of $O(N^2)$, where $N$ is the number of image patches. As image resolution grows, the memory and compute requirements explode.

Furthermore, ViTs are often "brittle" regarding input size. Models like the Segment Anything Model (SAM) require images to be resized to exactly 1024x1024. This is because they use positional encodings that don't scale naturally. In contrast, traditional Convolutional Neural Networks (CNNs) like ResNet are resolution-agnostic; you can slide a convolution over an image of any size. The authors set out to bring this "any-resolution" flexibility to the Transformer world.

Methods

The core innovation is the Multi-Head Self-Attention Convolution (MSA-Conv). Instead of looking at every patch in the image at once, the model looks at a local window of patches, just like a convolution kernel.

Figure 2: (a) The architecture of TiC; (b) the architecture of MAS-Conv Transformer Block; (c) the illustration of MSA-Conv.

Key components include:

  • Sliding Window Attention: Self-attention is computed only within a local $K \times K$ window that slides across the image.
  • Dilated and Depthwise Variants: Much like CNNs, the authors implemented dilated MSA-Conv (to see further without more compute) and depthwise versions (different kernels for different attention heads).
  • Multi-Directional Cyclic Shifted Mechanism: To prevent the model from only seeing local information, they use a shifting strategy (in four diagonal directions) to establish long-range connections between distant tokens.
  • Inter-Pooling Mechanism: This aggregates neighboring tokens within a window, effectively enlarging the receptive field while keeping computational costs low.

To make this practical, the team developed custom C++/CUDA kernels. Standard deep learning library functions like nn.Unfold are notoriously slow for large feature maps; the custom implementation ensures TiC is actually fast in practice.

Datasets

The authors evaluated TiC primarily on ImageNet-1K, the gold standard for image classification. They also conducted exploratory "modular" experiments on CIFAR-100 to see how MSA-Conv performs when plugged into existing architectures like ResNet.

Key Insights and Ideas

  • The Receptive Field Gap: One of the most interesting findings is the visualization of the Effective Receptive Field (ERF). Vanilla ViTs tend to focus intensely on the exact center of their attention, while Swin Transformers focus on the edges of their windows. TiC produces an ERF that looks much more like a classic ResNet—smooth and centered, which the authors argue is more natural for vision tasks.
Figure 1: Effective receptive field (Luo et al. 2016b) of the mainstream backbones and TiC.
  • Linear Scaling: Because MSA-Conv uses a fixed window size, its complexity is $O(N)$. This means as you double the image size, the compute cost only doubles, rather than quadrupling as it would in a standard ViT.
  • Resolution Flexibility: Because it's a sliding window, the model doesn't need to "know" the image size beforehand. It can process a 224x224 image or a 1024x1024 image using the exact same weights and configuration.

Key Results

TiC proves that you don't need global attention to get state-of-the-art results:

  • Accuracy: When trained from scratch on ImageNet-1K, TiC-B achieved 79.0% Top-1 accuracy, significantly outperforming the standard ViT-B (76.45%) and rivaling the Swin-B (80.48%).
  • Fine-tuning: After pre-training and fine-tuning, TiC-B reached 82.71%, nearly matching Swin-B's 83.31%.
  • Efficiency: At high resolutions (1024x1024), TiC's memory usage is roughly 11.3 GB, compared to Swin-B's 12.2 GB. More importantly, its FLOPs (floating-point operations) are nearly half that of a standard ViT-B at that scale.

Limitations and Future Work

While TiC is a strong proof-of-concept, the authors note a few areas for improvement:

  • Implementation Overhead: The current CUDA implementation still has some computational overhead during the gradient propagation stages that could be further optimized.
  • Deformable Attention: The authors suggest that adding "deformable" capabilities to MSA-Conv—allowing the model to choose which patches to attend to within a region—could further boost performance.
  • Hybrid Models: There is significant potential in using MSA-Conv as a "plug-and-play" module within traditional CNN architectures to give them the representational power of Transformers without losing their structural benefits.

For those interested in the implementation, the code is available on GitHub.

Summary updated

BibTeX
@misc{zhang2023tic,
  author = {Song Zhang and Qingzhong Wang and Jiang Bian and Haoyi Xiong},
  title = {{TiC: Exploring Vision Transformer in Convolution}},
  year = {2023},
  eprint = {2310.04134},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.04134},
}
Figure