Start studying
Module 3 · Computer Vision Module contents
Module 3  ·  Card 21 / 22advanced

Vision Transformers

A Vision Transformer splits the image into fixed-size patches.

Each patch is flattened and embedded like a token in a sentence.

Self-attention then lets every patch look at every other patch.

This captures long-range relationships that convolution reaches only slowly.

ViTs need more data than CNNs but scale better once you have it.

Learn in depth ↓
Module 03 · Computer Vision · 22 concepts
In depth  /  15 lines

Vision Transformers, in more detail

A vision transformer applies the language architecture to images with almost no changes.

The image is cut into fixed patches, typically 16 by 16 pixels.

A 224 by 224 image becomes 196 patches.

Each patch is flattened and projected into a vector, exactly like a word embedding.

Position information is added, since attention has no idea about arrangement.

Self-attention then lets every patch attend to every other patch.

A patch in the top corner relates directly to one in the bottom corner, in a single step.

A CNN needs many layers to connect those two places.

The catch is that a transformer assumes far less about images than a CNN does.

It does not know that nearby pixels are related or that patterns repeat across positions.

Everything a CNN builds in must instead be learned from data.

So ViTs underperform CNNs on small datasets and overtake them on very large ones.

Pretraining on hundreds of millions of images is what unlocked them.

Hybrid designs and hierarchical variants such as Swin narrowed the data requirement.

Treating images as sequences also made multimodal models much easier to build.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Vision Transformers

An image as a sequence

A transformer takes a sequence of vectors. An image is a grid of pixels, so the only real question is how to turn one into the other.

N=HWP2N = \frac{H W}{P^2}
  • PP: the patch size, usually 16.
  • NN: the resulting sequence length.

A 224 by 224 image cut into 16 by 16 patches gives 196 patches, and each patch is flattened into a vector of 16×16×3=76816 \times 16 \times 3 = 768 numbers.

z0=[ xcls  ;  x1E  ;  …  ;  xNE ]+Epos\mathbf{z}_0 = [\, \mathbf{x}_{\mathrm{cls}} \; ; \; \mathbf{x}_1 E \; ; \; \dots \; ; \; \mathbf{x}_N E \,] + E_{\mathrm{pos}}
  • EE: one linear projection, shared by every patch.
  • EposE_{\mathrm{pos}}: a learned position embedding, one per position.

That projection is equivalent to a single convolution with kernel size PP and stride PP, which is exactly how it is implemented.

After that line, nothing about the model is specific to images. It is the same encoder used for text.

Why the position embedding is not optional

Self-attention is permutation invariant, so without positions the model sees a bag of patches and cannot tell a face from its parts rearranged.

The embedding is learned rather than derived, and interpolating it is what allows a model trained at 224 pixels to be fine-tuned at 384.

xcls\mathbf{x}_{\mathrm{cls}}

The extra token belongs to no patch. It attends to all of them and its final state is the image representation the classifier reads, which avoids having to pool the patches by hand.

The cost, which is the real constraint

O(N2d)O(N^2 d)

Attention is quadratic in the sequence length, and the sequence length is quadratic in the image side.

P=16→N=196,P=8→N=784P = 16 \to N = 196, \qquad P = 8 \to N = 784

Halving the patch size quadruples the tokens and multiplies the attention cost by sixteen.

That is why 16 is the usual compromise, and why high resolution vision transformers use windowed attention instead.

One pass, in order

  1. Cut the image into non-overlapping patches.
  2. Flatten and project each patch to the model width.
  3. Prepend the class token and add the position embeddings.
  4. Run the transformer encoder blocks, each attention followed by an MLP.
  5. Take the final class token and pass it through one linear layer to the classes.

What it gave up, and what it bought

A convolution assumes neighbouring pixels are related and that a pattern means the same thing anywhere. A vision transformer assumes neither and has to learn both.

That is why it loses to a convolutional network on ImageNet alone and wins decisively once pretrained on a hundred million images.

The gain is reach. A convolutional network builds up its receptive field layer by layer, while every patch here can attend to every other patch in the first block.

Hybrids now dominate in practice: a convolutional stem for the cheap local structure, attention above it for the long-range relationships.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
How does a Vision Transformer turn a picture into tokens?
Computer Vision

22 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close