- Introduced by
- Dosovitskiy, A. et al.
- Year
- 2021
- Type
- Transformer architecture for images (self-attention over patches)
What does it do?
The Vision Transformer (ViT), introduced by Dosovitskiy and colleagues in 2021, splits an image into fixed-size patches, treats the patches as a sequence, and applies Transformer self-attention to image classification.
When trained on large datasets it can outperform convolutional neural networks (CNNs), but that advantage depends on having enough training data.
The intuition behind the method
Convolutional networks read an image through small local filters that slide across it. ViT takes a different view: it chops the image into a grid of equal-sized patches, lines them up like words in a sentence, and lets a Transformer decide which patches should pay attention to which. Because attention can directly relate any patch to any other, the model learns global relationships across the whole image rather than only local neighbourhoods. Unlike a CNN, ViT builds in little assumption about locality or translation invariance, so it has to learn spatial structure from data. This is why its results depend so heavily on training set size and pre-training.
History
Dosovitskiy and colleagues introduced the Vision Transformer in 2021 in the paper An Image is Worth 16x16 Words. It applied the Transformer self-attention architecture, established in language tasks, directly to image patches and reported that it could surpass CNNs when trained at scale. Touvron and colleagues' Training Data-Efficient Image Transformers (2021) addressed ViT's need for large training sets, making the approach more practical with smaller training budgets.
When to use it
Choose ViT for image classification or prediction on pixel image data when you have a large labelled dataset and access to a GPU for training. It performs best with a large pre-training corpus or with transfer learning from a pre-trained checkpoint, so for a modest labelled dataset, fine-tuning a pre-trained ViT is usually more sensible than training from scratch. There is no fixed sample-size cut-off. As a rough guide, very small labelled sets favour a CNN or a classical method such as Random Forest or SVM. Whatever you choose, compare ViT against a CNN baseline on your own data. Where training at practical scale is not feasible, ViT is a poor fit.
Limitations and considerations
- Needs a large training set or large-scale pre-training; on small labelled datasets trained from scratch it generally underperforms CNNs.
- Training at practical scale is compute-intensive and generally requires a GPU; CPU use is realistic mainly for inference or very small experiments.
- On very small labelled datasets, patch-based attention may not learn reliably from scratch.
- Self-attention compares every pair of patches, so its cost grows quadratically with the number of patches. Halving the patch side length quadruples the number of patches and raises attention cost roughly sixteenfold, so patch size and input resolution must be chosen with compute in mind.
- Strong results usually depend on a large pre-training corpus or transfer learning rather than training from scratch.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Can outperform CNNs on large image datasets because self-attention models relationships between any pair of patches.
- Applies the Transformer architecture, developed for sequences, directly to image patches.
- Captures long-range dependencies across the whole image rather than only local neighbourhoods.
- Benefits from large-scale pre-training and from transfer learning with pre-trained checkpoints.
Applications
- Image classification on large labelled image datasets.
- Prediction tasks on pixel image data where relationships across the whole image matter.
- Transfer learning from large pre-trained image models to new vision tasks.
How it works
How it works
The image is divided into fixed-size patches, for example 16x16 pixels. Each patch is flattened and linearly projected into an embedding vector, and these embeddings form a sequence, the image counterpart of a sequence of word tokens.
The patch sequence passes through Transformer layers built on self-attention. The formula shown is a single attention head: query and key vectors are compared by scaled dot product, where d_k is the dimension of the key vectors, the scores are normalised with softmax, and the result weights the value vectors. Each patch can attend to every other patch, which lets the model capture global relationships across the image rather than only local neighbourhoods. The full ViT stacks several heads and layers, so the formula describes the building block, not the whole network.
Formula
\text{Attention}(Q,K,V) = \text{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d_k}}\right)VA classification head reads the representation that the Transformer layers produce for the sequence and outputs a predicted class. Because strong results depend heavily on large-scale pre-training, the usual practical route is transfer learning from a model pre-trained on a large image corpus.
Common pitfalls
- Training ViT from scratch on a small dataset instead of fine-tuning a pre-trained checkpoint.
- Expecting CNN-level accuracy from a dataset too small for ViT's data needs.
- Attempting practical-scale training without GPU resources.
- Ignoring data size: on a very small labelled dataset, a CNN or a classical method is often the better choice.
- Overlooking transfer learning when pre-trained image models exist for your task.
Frequently asked questions
How is ViT different from a CNN?
A CNN processes an image through local convolutional filters, while ViT splits the image into fixed-size patches, treats them as a sequence, and uses self-attention so any patch can relate to any other. This lets ViT model global relationships and, on large datasets, surpass CNNs.
How much data does ViT need?
ViT is data-hungry relative to CNNs. It works best with a large labelled dataset or a large pre-training corpus, and trained from scratch on small datasets it can underperform. There is no fixed cut-off, so compare it against a CNN baseline on your own data.
Do I need a GPU?
For training at practical scale, yes. ViT is compute-intensive and generally needs a GPU. CPU use is realistic mainly for inference or very small experiments. Using a pre-trained checkpoint with transfer learning reduces the training burden considerably.
What if my image dataset is small?
On a small dataset, ViT trained from scratch tends to underperform. A CNN or a classical method such as Random Forest or SVM is often the safer choice, or you can fine-tune a pre-trained ViT checkpoint.
Related methods
Sources
Dosovitskiy, A. et al. (2021). An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. ICLR.
Open publication referenceTouvron, H. et al. (2021). Training Data-Efficient Image Transformers. ICML.
Open publication reference