MSViT: Dynamic Mixed-scale Tokenization for Vision Transformers

Jakob Drachmann Havtorn, Amélie Royer, Tijmen Blankevoort, Babak Ehteshami Bejnordi · DOI

Introduction

Vision Transformers (ViTs) have revolutionized computer vision by applying the transformer architecture to image data. However, a fundamental inefficiency persists: standard ViTs partition images into a grid of uniform, fixed-size patches. This approach treats every region of an image—from a featureless sky to a highly detailed object—with the same level of granularity. MSViT (Mixed-Scale Vision Transformer) addresses this by introducing a dynamic gating mechanism that allows the model to adaptively choose patch sizes based on local image complexity.

The Challenge of Uniform Tokenization

In traditional ViT architectures, the computational cost is directly tied to the number of tokens, which is determined by the patch size. Slicing a 224x224 image into 16x16 patches results in 196 tokens. While this provides high resolution, it is computationally expensive. Previous attempts to mitigate this, such as token pruning or merging, often occur in the middle layers of the network. This means the initial layers still process the full token set, and the "lossy" nature of pruning can discard spatial information necessary for dense tasks like semantic segmentation.

MSViT: Architecture and Methodology

MSViT introduces a "conditional gating" module at the input level to optimize tokenization from the start of the pipeline.

1. Dynamic Mixed-Scale Tokenization

The core of MSViT is a lightweight, four-layer Multi-Layer Perceptron (MLP) that acts as a gate. For each region of the image, the gate decides whether to represent it as a single coarse token (e.g., 32x32 pixels) or multiple fine tokens (e.g., four 16x16 pixels). This allows the model to allocate more parameters and compute to complex foreground objects while using fewer resources for simple backgrounds.

2. Generalized Batch-Shaping (GBaS) Loss

Training discrete gates is notoriously difficult due to the risk of "mode collapse," where the model defaults to a single scale (usually the fine scale) to maximize accuracy. The authors introduce the GBaS loss, which utilizes a hyperprior to encourage diversity in gating decisions across both the spatial dimensions of an image and the entire training batch. This loss is specifically designed to be robust against distribution shifts caused by standard data augmentation techniques like Inception-style cropping.

3. Adaptive Trimming (AT)

To translate theoretical computational savings into actual training speedups, MSViT employs Adaptive Trimming. This strategy reorders tokens and trims inactive ones, ensuring the hardware processes a reduced sequence length. This results in a 1.16x to 1.35x speedup in training time per epoch, depending on the target sparsity.

4. Architecture Agnostic Design

Because the gating occurs at the input stage, MSViT is highly flexible. It can be integrated into various transformer backbones, including standard ViTs, DeiT, and hierarchical models like Swin Transformers.

Experimental Results

The authors evaluated MSViT on ImageNet-1k for classification and ADE20k for semantic segmentation, often initializing weights from ImageNet-21k pretraining.

Image Classification

On ImageNet-1k, MSViT-S/16,32 (utilizing 16px and 32px scales) achieved a top-1 accuracy of 81.47% using only 3.23 GMACs. This outperforms the standard ViT-S baseline, which achieved 80.75% accuracy with higher compute (3.32 GMACs). This demonstrates that MSViT can improve accuracy while simultaneously reducing computational overhead.

Semantic Segmentation

In dense prediction tasks on the ADE20k dataset, MSViT demonstrated its "lossless" advantage. Unlike pruning methods that discard data, MSViT retains all spatial information through its multi-scale approach. It successfully reduced the encoder's MACs by approximately 40% with a negligible decrease in mIoU (0.4 points).

Key Insights

  • Local Semantic Awareness: Despite only seeing local patches, the gating module effectively learns to distinguish between foreground and background. It identifies that blurry or uniform regions (like grass or sky) can be processed coarsely, while sharp edges require fine-grained tokens.
  • Complementarity: MSViT is not mutually exclusive with other efficiency methods. When combined with mid-network token pruning, it achieves an even more favorable accuracy-complexity trade-off by reducing tokens at the input and then pruning further in subsequent layers.

Conclusion and Future Directions

MSViT provides a robust framework for dynamic tokenization in vision transformers. By moving the efficiency bottleneck to the input layer and using a lossless multi-scale approach, it maintains high performance across diverse tasks. Future research may explore expanding the gating mechanism to support more than two scales (e.g., 8px to 64px) and developing specialized CUDA kernels to further optimize wall-clock latency on standard hardware.

Summary updated

BibTeX
@misc{havtorn2023msvit,
  author = {Jakob Drachmann Havtorn and Amélie Royer and Tijmen Blankevoort and Babak Ehteshami Bejnordi},
  title = {{MSViT: Dynamic Mixed-scale Tokenization for Vision Transformers}},
  year = {2023},
  eprint = {2307.02321},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2307.02321},
}
Figure