Vision Transformer,
explained.
For a decade, computer vision meant convolutions — the CNN was the only serious way to see. Transformers had conquered language, but everyone "knew" images were different: they needed the special machinery of convolution. This paper called the bluff. Chop an image into little squares, pretend each square is a word, and feed the lot to a plain Transformer. With enough data, it worked — and the CNN's monopoly ended.
What changes against AlexNet: the inductive bias is traded for data
Every strong vision model between rung 1 and this one was convolutional, and the reason was principled: a convolution assumes that nearby pixels are related and that a cat is a cat wherever it appears. Those assumptions are true, they are baked into the architecture rather than learned, and they are why a CNN can be trained on a hundred thousand images at all.
ViT's change is to remove them. Cut the image into fixed-size
patches, flatten each one, project it to a vector, and hand the sequence to
an unmodified transformer encoder. At 16×16 patches on a 224-pixel
image that is 196 tokens, plus one learned
[class] token whose final representation is classified. Position
is supplied by a learned embedding, which means the model is told
that patches have an order rather than being built to know it. Nothing in the
architecture knows that patch 15 is next to patch 16.
Stated in rung 1's units, the result is a genuine trade rather than a win. Trained on ImageNet-scale data alone, ViT is worse than a comparable ResNet — the paper reports this directly. Pre-trained on one to two orders of magnitude more images and then transferred, it is better, and it keeps improving as data grows where the CNN's curve flattens. So the delta is not "attention beats convolution". It is: the convolutional prior is worth a large amount of data, and once you have more data than that, it is a ceiling.
The CNN monopoly
Since AlexNet in 2012, convolutional neural networks owned computer vision. Convolutions bake in assumptions that fit images beautifully: locality (nearby pixels relate), translation equivariance (a cat is a cat wherever it appears), and a hierarchy from edges to shapes to objects. These "inductive biases" are why CNNs learn efficiently from modest data.
Meanwhile Transformers had taken over NLP by throwing out recurrence in favor of pure attention. The obvious question hung in the air — could attention replace convolution too? — but the consensus was no: images have millions of pixels, and letting every pixel attend to every other is computationally impossible, and Transformers lack the vision-friendly biases CNNs have baked in.
The big idea: an image is worth 16×16 words
The workaround is almost cheeky. You can't treat each pixel as a token — too many. So don't. Cut the image into a grid of fixed-size patches (say 16×16 pixels each). A 224×224 image becomes just 196 patches. Now treat each patch as a "word" and the image as a short sentence of a couple hundred tokens — a length Transformers handle with ease.
That's the entire trick. The paper's title says it outright: An Image is Worth 16×16 Words. Once an image is a sequence of patch-tokens, you can feed it to the same Transformer encoder that reads text, with essentially no vision-specific changes.
Turning patches into tokens
Each patch is a little block of pixels. Flatten those pixels into a vector and pass it through a single linear projection — a learned matrix that maps the raw pixels into a patch embedding, the visual equivalent of a word embedding. Because a Transformer has no built-in sense of order, you also add a positional embedding to each patch so the model knows where in the grid it came from.
The result is a sequence of embeddings — exactly the input a Transformer expects. From here, standard multi-head self-attention lets every patch look at every other patch, so the model can relate the top-left of an image to the bottom-right in a single layer, something a CNN only achieves after stacking many layers.
The counting is exact. Split an H×W image into P×P patches and you get a fixed grid, each patch a flattened pixel vector — for the canonical ViT-Base/16 on a 224×224 image:
But that grid size isn't free, because self-attention is O(N²) in the token count. Want finer detail from smaller patches? Every halving of the patch side quadruples the tokens and so 16×es the attention compute:
Patch size is the dial that trades spatial resolution against a quadratic compute bill — the single most important knob in a ViT.
The [class] token
How do you get a single label out of a bag of patch representations? ViT borrows BERT's trick: prepend one extra learnable token — the [class] token — to the sequence. It carries no pixels of its own; it's a blank slot whose job is to gather information.
As the Transformer's layers mix information through attention, this token accumulates a summary of the whole image. After the final layer, you read off its vector and attach a small classification head to predict the label. One token in, one summary out — clean and architecture-agnostic.
The catch: it's hungry for data
There's no free lunch. By discarding convolution, ViT also discards its helpful biases. It doesn't assume nearby patches are related or that objects can move — it has to learn all of that from examples. On a mid-sized dataset like ImageNet alone, ViT actually loses to a comparable CNN, precisely because the CNN's baked-in assumptions are a head start.
The magic appears at scale. Pre-train ViT on a truly enormous dataset — hundreds of millions of images (JFT-300M) — and it not only catches up but surpasses the best CNNs. With enough data, the model learns the right visual structure itself, and its freedom from rigid biases becomes an advantage rather than a handicap.
Inductive bias vs. data. CNNs encode strong priors, so they need less data but are constrained by those priors. ViT encodes almost none, so it needs a lot of data — but given that data, it can learn better representations than the priors would allow.
The results
Pre-trained on JFT-300M and fine-tuned on standard benchmarks, ViT matched or beat state-of-the-art CNNs (like the best ResNets) on ImageNet and friends — at lower pre-training compute. It proved, concretely, that convolution was not a requirement for top-tier vision; it was one option among others.
| Setup | ViT vs CNN |
|---|---|
| Small data (ImageNet only) | CNN wins (bias helps) |
| Medium data (ImageNet-21k) | roughly even |
| Large data (JFT-300M) | ViT wins, at less compute |
An intriguing bonus: analysis of ViT's attention showed that even in early layers, some heads attend globally across the whole image — learning a kind of long-range "receptive field" that CNNs only build up gradually. The model discovered its own way to see.
The limits
ViT's dependence on gigantic pre-training datasets was a real barrier — few teams have JFT-300M. The plain design also struggles with high-resolution images and dense tasks like detection and segmentation, where the fixed patch grid is coarse. And self-attention over many patches is quadratic in cost, which bites as images (and patch counts) grow.
Follow-up work rushed to fix these: DeiT showed ViT could be trained data-efficiently with distillation; hierarchical designs like the Swin Transformer reintroduced locality and multi-scale structure for detection and segmentation. Pure ViT was a proof of concept more than a final answer.
Why it still matters
ViT broke the assumption that vision needs its own special architecture, and in doing so it helped unify AI around one backbone: the Transformer now processes text, images, audio, and video with the same core machinery. That convergence is why multimodal models feel natural — everything is just a sequence of tokens.
Its fingerprints are everywhere downstream: CLIP's image encoder, the vision half of multimodal LLMs, and modern image and video generators all lean on Transformer-over-patches. When a single model can look at a picture and read a caption in the same representation space, it's because ViT showed a patch could be a token.
Text became tokens (Attention), images became patch-tokens (ViT) — once everything is a sequence of tokens, one architecture can fuse them. ViT is a key step toward the multimodal models we use now.
What the authors admit, and what happened next
The data appetite is stated as a finding rather than buried — the paper's own framing is that large-scale pre-training is what makes the approach work, and that below that threshold the missing inductive bias costs you. It also notes the practical consequence: attention over patches is quadratic in the number of patches, so higher resolution is expensive in a way convolution is not.
The data-hunger claim was then substantially weakened by better training, which is the most useful thing that happened to this paper. Touvron and colleagues' DeiT recipe made a ViT competitive on ImageNet-1k alone using heavy augmentation — mixup and CutMix among them — plus distillation from a convolutional teacher. Read together, the two results say the original ceiling was partly an artefact of the training recipe rather than a property of the architecture, which is a caution worth carrying to every other "needs enormous data" claim on this site.
How it held up: the patch-plus-transformer backbone is now the default for
anything that has to put images and text in the same representation — it is
the image tower in CLIP and the visual encoder
feeding LLaVA. Pure global attention did not win
unchallenged: hierarchical designs such as Swin reintroduced locality and
multi-scale windows and remain strong for dense prediction, which suggests
the convolutional prior was right about something even where it was not
required. And the [class] token itself is largely gone — pooling
over patch tokens works as well or better, and rung 5's features come from
the patch tokens directly.
Tokenize an image and price the attention
"An image is worth 16×16 words" becomes literal when you count the words. This turns image size and patch size into a token count — landing on ViT-Base/16's exact 196 patches, 197 tokens, 768-dim patch vectors — and then prices self-attention, which is quadratic in that count. Shrink the patch for finer detail and watch the attention bill explode 16×. Change the resolution or patch size and see which configs you could actually afford to train.
Write the body of attn_cost. Everything else in the file is
already written; this is the function the paper is about. Press ▶ Run to check it against
the 5 assertions below — they start red.
- A 224x224 image with 16x16 patches yields 196 patches
- The sequence adds one [class] token
- A 16x16x3 patch flattens to 768 pixel values
- Halving the patch side quadruples the tokens
- Attention is quadratic: 4x tokens is 16x cost
5 questions on this paper
Answer each one out loud before you open it. Recalling an answer is what makes it stick; recognising one you have just read is not. Your score is kept on this device.
What is a Vision Transformer (ViT)?
Show answer
A Vision Transformer is a 2020 model (Dosovitskiy et al.) that applies the standard Transformer — the architecture from NLP — to images. It splits an image into a grid of fixed-size patches (e.g. 16×16 pixels), turns each patch into a token via a linear projection, adds positional information, and processes the sequence with self-attention, with almost no vision-specific design.
How does ViT turn an image into tokens?
Show answer
ViT divides the image into a grid of non-overlapping patches, flattens each patch's pixels into a vector, and linearly projects it into an embedding — one token per patch. It prepends a learnable [class] token and adds positional embeddings so the model knows where each patch sits, then feeds the whole sequence to a Transformer encoder.
Is ViT better than a CNN?
Show answer
It depends on data. CNNs have built-in inductive biases (locality, translation equivariance) that help when data is limited, so on medium datasets they win. But when pre-trained on very large datasets, ViT matches or beats top CNNs, because with enough data it can learn those biases itself and isn't constrained by them.
Why does ViT need so much data?
Show answer
Unlike CNNs, ViT has few built-in assumptions about images — it doesn't assume nearby pixels are related. That freedom is powerful but data-hungry: it must learn spatial structure from scratch. On small datasets it underperforms CNNs; pre-trained on hundreds of millions of images, it excels.
What is the [class] token in ViT?
Show answer
Borrowed from BERT, the [class] token is an extra learnable token prepended to the patch sequence. After the Transformer processes everything, the final vector at this token position serves as the image's summary representation, and a small classification head is attached to it to predict the label.
Learning to see
From the network that started the deep-learning era to one you can segment anything with — and the shift from labels to self-supervision in between.
- 1AlexNet2012
- 2Vision Transformer2021
- 3SimCLR2020
- 4Masked Autoencoders2022
- 5DINOv22023
- 6Segment Anything2023
Explore the topic
See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.
More Paper Breakdowns
- Attention Is All You NeedThe paper that introduced the Transformer — the architecture underneath GPT, BERT, Claude, and Gemini. Self-attention, Query/Key/Value, multi-head attention, positional encoding, and the real BLEU results, explained in depth with analogies and animated visuals, not just equations.Read →
- Word2VecHow words became math. Dense word vectors learned from the company a word keeps, so king − man + woman lands near queen. Skip-gram, CBOW, the distributional hypothesis, and the origin of every embedding model powering search and RAG.Read →
- ResNetThe skip connection that made deep learning deep. Why stacking layers made networks worse, and how residual learning let them reach 150+ layers and win ImageNet — the identity shortcut every Transformer still uses inside every block.Read →
- Latent DiffusionThe paper behind Stable Diffusion. Compress an image into a small latent space with a VAE, run the diffusion denoising there instead of on pixels for a tens-of-times efficiency win, and steer it with text via cross-attention. The architecture that put text-to-image generation on consumer GPUs.Read →
- DDPMThe original denoising diffusion math — the paper that made diffusion work. A fixed forward process adds Gaussian noise over many steps; a U-Net learns the reverse. The masterstroke: train the network to simply predict the added noise with a plain MSE loss. The foundation under every modern image generator.Read →
- GANsThe counterfeiter and the detective. Two networks locked in a game: a generator forging fakes from noise and a discriminator trying to catch them. The minimax objective that taught machines to imagine, its notorious instabilities and mode collapse, and how diffusion later overtook it.Read →