ElemedexA pocket encyclopedia of AI architectures
Vision Transformer (ViT)
No. 035

Vision Transformer (ViT)

ViT

Jigsaw

It cuts every photo into 16 × 16-pixel squares and reads them in a row, as if they were words. Nobody told it that neighboring pixels tend to look alike: it had to see hundreds of millions of images to find out on its own. Once it did, it beat the networks that were born knowing it.

Height
12 (Base) · 24 (Large) · 32 (Huge)
Weight
≈ 340 MB · 1.2 GB · 2.5 GB (fp32)
Parameters
86 M · 307 M · 632 M
Context
196 + 1 tokens (224 × 224 px, 16 × 16)
Training data
ImageNet (1.3 M) · ImageNet-21k (14 M) · JFT-300M (303 M images)
Year
2020
Generation
5 · Large language models
Creators
Dosovitskiy, Beyer, Kolesnikov, Weissenborn, Zhai, Houlsby et al. (Google Research, Brain Team)
Kind
Architecture

Base stats

Total
256

Approximate, ordinal scales meant for comparing species with each other; the hard numbers are in the data sheet.

Weaknesses

  • It is data-hungry: trained on ImageNet alone it falls below convolutional networks of its size.
  • Its best results depend on JFT-300M, an in-house Google dataset nobody else can use.
  • The cost of attention grows with the square of the number of patches: smaller patches make the model much more expensive.

Habitat

It was trained on ImageNet-21k (14 million images) and on JFT-300M, a private Google dataset of 303 million photos. Today it mostly lives inside other systems: it is the "eye" of CLIP and of multimodal models that connect a frozen ViT to a language model.

Moves

Abilities

Patches as tokens
The image is cut into 16 × 16 patches that are flattened and linearly projected; from then on, the transformer treats them just like words.
[class] token
Like BERT's [CLS], a learned vector prepended to the patches whose final state represents the whole image.
Global view
Thanks to self-attention, some heads integrate information from the whole image already in the first layers, which a convolution cannot do.

Anatomy

ViT-Base: the image is cut into 196 patches which, linearly projected and preceded by a [class] token, enter an almost unchanged transformer encoder. Only the final state of the [class] token is used for classification. Apart from the patching, nothing in the architecture knows the input is two-dimensional.

Evolution chain

035 Vision Transformer (ViT)
  • pair an image encoder with a text encoder, trained contrastively036 CLIP

The Vision Transformer (ViT) is a transformer encoder applied to images with as few changes as possible. The trick is to treat the image like a text: it is cut into 16 × 16-pixel patches and each patch plays the role of a word (Dosovitskiy et al., 2021, sec. 1). As the paper's title announces, an image is worth 16 × 16 words.

How it works

A 224 × 224-pixel image yields 196 patches. Each is flattened into a vector and passed through a learned linear projection; as in BERT, a special [class] token is prepended and learned position encodings are added. The sequence enters a standard stack of multi-head attention and MLP blocks, with normalization and residual connections, and the final state of the [class] token feeds the classifier (Dosovitskiy et al., 2021, sec. 3.1). Alammar & Grootendorst (2024) point out the detail that forces a projection instead of a vocabulary: unlike words, a patch almost never recurs exactly in another image, so it cannot be given a dictionary ID (2024, pp. 261–263).

The Base, Large and Huge variants copy BERT's configurations and add a bigger one: from 86 to 632 million parameters (Dosovitskiy et al., 2021, sec. 4.1).

What changed

Convolutional networks come with three built-in assumptions about images: locality, two-dimensional neighborhood structure and translation equivariance, repeated at every layer. ViT has almost none of them. The only moments at which the architecture "knows" its input is two-dimensional are the patching and the adjustment of positions when the resolution changes; everything else must be learned (Dosovitskiy et al., 2021, sec. 3.1).

The outcome was a lesson about inductive bias. Trained on ImageNet alone (1.3 million images), ViT falls a few points below comparable residual networks. With ImageNet-21k or JFT-300M the picture flips: the best model reaches 88.55% on ImageNet with about four times less pretraining compute than BiT-L, a large residual network trained on the same data (2021, secs. 1, 4.2). On subsets of JFT, ViT overfits more than a ResNet with 9 million images and overtakes it from 90 million on: the convolutional assumption helps with little data, but with a lot of it, learning the patterns directly is enough, even better (2021, sec. 4.3).

Looking inside is revealing. The learned positions reconstruct the grid of rows and columns, and some attention heads in the first layers become local, as if the network were reinventing convolution; others attend to the whole image from the start (2021, sec. 4.5).

Offspring

Since ViT, cutting into patches has become a standard way of turning images into tokens. It is the typical image encoder of CLIP (Millière & Buckner, 2024b, sec. 3.1.1), Gato uses it to mix images with text and actions (Reed et al., 2022, sec. 2.1), and multimodal models such as BLIP-2 connect a frozen ViT to a language model (Alammar & Grootendorst, 2024, pp. 274–275).

Limits

Data hunger is the price of flexibility. Moreover, the cost of attention grows with the square of the number of patches, so smaller patches make the model much more expensive (Dosovitskiy et al., 2021, sec. 4.1), and the best results depend on a private dataset.

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Buckner, C. J. (2024). From deep learning to rational machines: What the history of philosophy can teach us about the future of artificial intelligence. Oxford University Press. https://doi.org/10.1093/oso/9780197653302.001.0001 §
  • Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., Dehghani, M., Minderer, M., Heigold, G., Gelly, S., Uszkoreit, J., & Houlsby, N. (2021). An image is worth 16x16 words: Transformers for image recognition at scale. In 9th International Conference on Learning Representations (ICLR 2021). https://openreview.net/forum?id=YicbFdNTTy §
  • Millière, R., & Buckner, C. (2024b). A philosophical introduction to language models—Part II: The way forward (arXiv:2405.03207). arXiv. https://doi.org/10.48550/arXiv.2405.03207 §
  • Reed, S., Zolna, K., Parisotto, E., Gómez Colmenarejo, S., Novikov, A., Barth-Maron, G., Gimenez, M., Sulsky, Y., Kay, J., Springenberg, J. T., Eccles, T., Bruce, J., Razavi, A., Edwards, A., Heess, N., Chen, Y., Hadsell, R., Vinyals, O., Bordbar, M., & de Freitas, N. (2022). A generalist agent. Transactions on Machine Learning Research. https://openreview.net/forum?id=1ikK0kHjvj §