Papers · arXiv 2010.11929
An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
Published cs.CVcs.AIcs.LG arXivPDF
Summary in three lines
- 🎯 Convolutional neural networks have dominated computer vision due to strong inductive biases, whereas standard Transformers lacked direct, scalable application to 2D images.
- 🔧 Split an image into fixed-size flattened patches (e.g., 16x16), project them linearly to token embeddings, add position embeddings, and pass them through a standard Transformer encoder with a prepended [class] token.
- 📈 When pre-trained on JFT-300M, ViT-H/14 achieves 88.55% top-1 accuracy on ImageNet, 90.72% on ImageNet ReaL, and 77.63% on VTAB, using significantly less pre-training compute than comparable CNNs.
Abstract
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.
Reading guide
Problem and motivation
Convolutional architectures (e.g., ResNet) have long been the foundation of modern computer vision due to hardcoded inductive biases like translation equivariance and two-dimensional locality. While Transformers became the standard in natural language processing (NLP) due to computational efficiency and massive scalability, prior attempts to bring self-attention to vision typically coupled attention with CNN backbones or used specialized local/axial attention patterns that were difficult to scale efficiently on hardware accelerators. This paper investigates whether a pure Transformer architecture, applied directly to sequences of image patches with minimal modifications, can rival or outperform state-of-the-art CNNs.
Key ideas
- Images as Sequences of Words: An image is split into non-overlapping patches of size . Flattened patches are treated identically to NLP tokens.
- Minimal Inductive Bias: Unlike CNNs where spatial locality and translation equivariance are present in every layer, ViT uses global self-attention across patches. The 2D structure is only referenced when extracting patches and when interpolating position embeddings for higher-resolution fine-tuning.
- Scale Trumps Inductive Bias: Without strong inductive bias, ViT underperforms ResNets on small-to-medium datasets (like standard ImageNet without heavy regularization). However, when pre-trained on large datasets (ImageNet-21k with 14M images or JFT-300M with 303M images), ViT matches or exceeds CNN performance while consuming substantially fewer training FLOPs.
Method
- Patch Embedding and Input Construction: Given patch size , an image yields patches. Each flattened patch is linearly projected by matrix to dimension . A learnable classification token is prepended, and 1D learnable position embeddings are added:
- Transformer Encoder Layers: The sequence passes through identical blocks alternating Multihead Self-Attention (MSA) and MLP blocks, using pre-LayerNorm (LN) and residual connections:
- Representation and Head: The representation is derived from the first output token: A small MLP (with one hidden layer) serves as the classification head during pre-training, which is replaced by a single linear projection () at fine-tuning.
- Higher Resolution Fine-Tuning: When fine-tuning at higher image resolutions (e.g., or ), patch size remains fixed, increasing sequence length . The pre-trained 1D position embeddings are adjusted via 2D bicubic interpolation according to their original spatial grid locations.
Results
- Comparison to SOTA:
- ViT-H/14 pre-trained on JFT-300M achieved 88.55% on ImageNet, 90.72% on ImageNet ReaL, 94.55% on CIFAR-100, and 77.63% on the VTAB 19-task suite, taking 2.5k TPUv3-core-days.
- ViT-L/16 pre-trained on JFT-300M achieved 87.76% on ImageNet with only 0.68k TPUv3-core-days, outperforming BiT-L (ResNet152x4, 87.54%, 9.9k TPUv3-core-days) and approaching Noisy Student EfficientNet-L2 (88.4/88.5%, 12.3k TPUv3-core-days).
- Public dataset pre-training: ViT-L/16 pre-trained on ImageNet-21k reached 85.30% on ImageNet.
- Compute vs. Performance Trade-off: Across scaling experiments on JFT-300M, pure ViT dominated ResNets on the Pareto frontier, requiring to less compute to attain equivalent accuracy.
- Internal Representations: Lower layers contain attention heads that attend across the entire image alongside heads with small, localized attention distances. Attention distance generally increases monotonically with depth.
Limitations and open questions
- Data Hunger: ViT generalizes poorly on small datasets without either extensive pre-training data (ImageNet-21k or JFT-300M) or heavy regularization.
- Self-Supervised Gap: Initial experiments with masked patch prediction reached 79.9% on ImageNet (ViT-B/16), leaving a 4% performance gap compared to supervised pre-training.
- Application to Dense Prediction: The initial work focuses exclusively on classification; adapting pure patch transformers to dense spatial tasks such as object detection and semantic segmentation remains an ongoing research challenge.
Key terms
- Vision Transformer (ViT)
- A pure Transformer architecture applied directly to sequences of non-overlapping image patches for vision tasks without convolutional feature extraction.
- Patch Embeddings
- The -dimensional representations obtained by linearly projecting flattened pixel patches into the model's latent space.
- Classification Token ([class])
- A learnable token prepended to the patch sequence whose output state after the final Transformer layer serves as the overall image representation.
- Inductive Bias
- Built-in architectural assumptions; in CNNs, these include spatial locality and translation equivariance, which are largely absent in ViT.
- Position Embeddings
- 1D learnable vectors added to patch embeddings to retain spatial ordering information within the sequence.
- Attention Distance
- The average spatial distance in the image across which an attention head integrates information, conceptually analogous to receptive field size in CNNs.
- Hybrid Architecture
- A model where the patch embedding projection is applied to feature maps extracted from an early convolutional backbone rather than raw image pixels.
- Visual Task Adaptation Benchmark (VTAB)
- A benchmark suite comprising 19 diverse vision tasks categorized into Natural, Specialized, and Structured groups, evaluated using 1,000 examples per task.
Questions
Why does ViT perform worse than ResNet on smaller datasets like ImageNet-1k?
Transformers lack convolutional inductive biases like locality and translation equivariance. On smaller datasets, ViT tends to overfit because it must learn spatial relationships from scratch; ResNet's hardcoded inductive biases make it more sample-efficient on limited data.
How does ViT handle images of different or higher resolutions at fine-tuning?
ViT keeps the patch size fixed, which increases the total sequence length. The pre-trained 1D position embeddings are mapped back onto the original 2D grid and interpolated using 2D bicubic interpolation to match the new grid size.
Do 2D-aware position embeddings improve ViT performance over standard 1D embeddings?
No. The authors found that 2D coordinate embeddings or relative position embeddings gave virtually identical performance to standard learnable 1D position embeddings, because the model easily learns the 2D row/column topology on its own.
What is the difference between ViT and the Hybrid architecture?
Pure ViT cuts the raw input image directly into pixel patches and applies a linear projection. The hybrid model first runs the image through an initial ResNet backbone and extracts patch tokens from the resulting intermediate feature map.
The overview on this page was written by AI from the paper and may contain mistakes. Read the paper for the details.