← Blog/blog/vit-data-hunger

The vision Transformer that needed 300 million images

A convolutional neural network, or CNN, is built to notice the same local pattern wherever it appears. The original Vision Transformer (ViT) removed that image-specific machinery. It cut an image into squares, called each square a token, and handed the resulting sequence to an almost ordinary language Transformer.

That simplicity is the paper's enduring idea. But the title's “at scale” is doing real work: the strongest model was pretrained on JFT, an internal dataset of 303 million images. On ImageNet-scale data, the authors found comparable CNNs ahead and larger ViTs sometimes worse.

01

Turn squares into words

For an image with height H, width W, and square patch width P, ViT creates HW/P² patches. Each patch is flattened and passed through the same learned linear projection. A learned classification token is prepended, and a position vector is added to every token so the model can recover where each square came from.

def patchify(image, patch_size):
    patches = []
    for top in range(0, len(image), patch_size):
        for left in range(0, len(image[0]), patch_size):
            patch = []
            for row in range(top, top + patch_size):
                for col in range(left, left + patch_size):
                    patch.append(image[row][col])
            patches.append(patch)
    return patches

The site runs the tested TypeScript implementation; the Python and C++ versions are line-for-line translations of its row-major patch extraction. The core also implements the affine patch projection, classification token, and position addition with hand-verified values.

02

Patch size is a compute dial

A 224×224 image with 16×16 patches becomes 196 image tokens, plus one classification token. Smaller patches preserve finer detail, but attention compares every token with every token. Halving patch width makes four times as many image tokens and nearly sixteen times as many attention pairs.

Analytical attention-pair counts from the tested core for one 224×224 image. The classification token is included. Smaller patches buy detail by lengthening the sequence.

Fine-tuning at higher resolution has the same effect. The paper keeps the patch size fixed, interpolates the learned position embeddings onto a larger grid, and accepts a longer sequence.

attention pairs
Analytical scaling for 16×16 patches. Doubling resolution from 112 to 224 quadruples patch count and increases attention pairs from 2,500 to 38,809—about 15.5× after including the class token.
03

Data replaces a baked-in image prior

An inductive bias is a structural assumption that helps a model generalize before it has seen every possibility. CNNs bake in locality, two-dimensional neighborhoods, and translation equivariance—the idea that moving an object should move its features rather than change what the feature means. ViT mostly does not. Beyond cutting patches and interpolating positions, it must learn spatial relationships from examples.

04

Read the leaderboard with its pretraining column

ModelPretraining dataImageNet top-1Pretraining compute
ViT-H/14JFT-300M (303M)88.55 ± 0.04%2.5k TPUv3-core-days
ViT-L/16JFT-300M (303M)87.76 ± 0.03%0.68k TPUv3-core-days
ViT-L/16ImageNet-21k (14M)85.30 ± 0.02%0.23k TPUv3-core-days
BiT-L ResNet152x4JFT-300M (303M)87.54 ± 0.02%9.9k TPUv3-core-days
Values reported in Table 2 of the paper, averaged over three fine-tuning runs. JFT-300M is an internal dataset; ImageNet-21k is the smaller public pretraining set used in the study.

The comparison is still meaningful: at matched JFT data, ViT-L/16 slightly exceeded BiT-L while using much less listed TPU compute. It is not a clean claim that Transformers always beat convolutions. Optimizer, regularization, resolution, model size, and access to pretraining data all move together.

05

What I would probe next

  • Hold compute fixed while varying patch size and dataset size.
  • Measure robustness to one-pixel shifts across patch boundaries.
  • Compare learned positions with explicit two-dimensional positions.

To inspect the attention mechanism that consumes these patch tokens, poke at the interactive transformer page. The previous FlashAttention walkthrough shows how the same token count becomes a systems bottleneck.

References

  1. Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, et al. (2021). An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. ICLR 2021
  2. Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, et al. (2017). Attention Is All You Need. NeurIPS 2017