JA EN
LearnVLMs & Multimodal
·★ MEMBER·PAPER·7 min read

Paper Deep Dive — ViT: Treating an Image Like a Sentence

A reading of the ViT paper (Dosovitskiy et al., 2020/2021) grounded strictly in its own text: the move of treating 16x16 patches as words, what the position-embedding ablation actually showed, the price of dropping the convolutional inductive bias, and how conditional the claim 'beats CNNs at scale' really is.

An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale

Primary source — what this article is built on

undefined2020-10-22undefined2026-08-065y 9mo later

An Image is Worth 16x16 Words: Transformers for Image Recognition at ScaleAlexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov et al. · 2020-10-22 · v2arXiv:2010.11929Paper page·PDF
undefined

While the Transformer architecture has become the de-facto standard for natural language processing tasks, its applications to computer vision remain limited. In vision, attention is either applied in conjunction with convolutional networks, or used to replace certain components of convolutional networks while keeping their overall structure in place. We show that this reliance on CNNs is not necessary and a pure transformer applied directly to sequences of image patches can perform very well on image classification tasks. When pre-trained on large amounts of data and transferred to multiple mid-sized or small image recognition benchmarks (ImageNet, CIFAR-100, VTAB, etc.), Vision Transformer (ViT) attains excellent results compared to state-of-the-art convolutional networks while requiring substantially fewer computational resources to train.


Laying out pixels one by one does not work

Self-attention compares every element of a sequence against every other. So why not turn an image into a sequence of pixels? The paper rules this out in the opening of its related work: applied naively, every pixel would attend to every other pixel, and the quadratic cost in the number of pixels does not scale to realistic input sizes. That is why the research of the day designed specialised attention patterns — local neighbourhoods, sparse approximations, one axis at a time — and those, the paper notes, "require complex engineering to be implemented efficiently on hardware accelerators" (§2). That is the starting point.

FIG 1All-pairs cost grows with the square of the element count. A 224x224 image flattened to pixels is 50,000 elements — a different order of magnitude

The move: cut the image into patches

What ViT does is simple. Reshape an image xRH×W×C\mathbf{x}\in\mathbb{R}^{H\times W\times C} into fixed-size P×PP\times P patches, flatten them, and map each to DD dimensions with a trainable linear projection. The patch count is N=HW/P2N=HW/P^{2}, and that number is the effective sequence length the Transformer sees (§3.1).

z0=[xclass;xp1E;xp2E;;xpNE]+Epos\mathbf{z}_{0}=[\mathbf{x}_{\text{class}};\,\mathbf{x}^{1}_{p}\mathbf{E};\,\mathbf{x}^{2}_{p}\mathbf{E};\cdots;\,\mathbf{x}^{N}_{p}\mathbf{E}]+\mathbf{E}_{pos}
(1)

Here xpiE\mathbf{x}^{i}_{p}\mathbf{E} is the embedding of patch ii, xclass\mathbf{x}_{\text{class}} is a learnable vector prepended to the sequence in the style of BERT's [class] token, and Epos\mathbf{E}_{pos} is the position embedding. From there a standard Transformer encoder runs unchanged, and the state of the leading token at the last layer becomes the image representation (§3.1).

Stated in words, the line says: lay the patches out in a row, put one extra slot at the front that belongs to no patch and exists only to accumulate a verdict about the whole image, and add to every slot a vector recording where in the picture that slot came from. z0\mathbf{z}_{0} is that row, and it is what the encoder receives as its input.

One 16×16 block of pixels equals one word: that reading is what the title announces. The intent of staying as close as possible to the original Transformer is stated explicitly, and the payoff is that scalable NLP implementations transfer almost out of the box (§3). The notation ViT-L/16 means the Large variant with patch size 16, and since sequence length is inversely proportional to the square of the patch size, smaller patches cost more compute (§4.1).

The position embedding barely mattered

ViT uses standard learnable 1D position embeddings, on the grounds that more elaborate 2D-aware variants gave no significant gain (§3.1). The ablation in Appendix D.4 is blunt about it. On ImageNet 5-shot linear: no positional information 0.61382, 1-D 0.64206, 2-D 0.64001, relative 0.64032. The gap between having and not having them is large; the gap between encoding schemes is close to nothing (Table 8). The paper's guess is that because the encoder works on patches rather than pixels, the spatial grid is only about 14×1414\times14, and learning spatial relations at that resolution is equally easy for any of these schemes.

The visualisations in §4.5 close the loop: after training, nearby patches have similar position embeddings and a row-column structure appears. The model learned 2D topology on its own — which, as the paper puts it, explains why hand-crafted 2D-aware variants yield no improvement. For the general question of how position is injected, see positional encoding in Transformers.

In a CNN, locality, two-dimensional neighbourhood structure and translation equivariance are baked into every layer. In ViT only the MLP layers are local and translation-equivariant; the self-attention layers are global. Two-dimensional structure enters at exactly two points — cutting the image into patches at the star

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn et al.. (2020-10-22) An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. arXiv:2010.11929Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment