An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale (Vision Transformer)¶
Authors: Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, et al. (Google Research) Published: October 2020 (ICLR 2021) Paper: arxiv.org/abs/2010.11929
Why This Matters¶
Vision Transformer (ViT) revolutionized computer vision by proving that the pure Transformer architecture—without any convolutions—could match or exceed state-of-the-art CNNs on image classification. This breakthrough:
- Unified vision and language: The same architecture now works for both domains
- Enabled multimodal models: Paved the way for models like CLIP, Flamingo, and GPT-4 Vision
- Changed computer vision research: Shifted focus from CNN architectures to Transformers
- Improved scalability: Transformers scale better with data than CNNs
Real-world impact: Powers modern vision models in autonomous vehicles, medical imaging, content moderation, visual search, and all major multimodal AI systems.
The Problem¶
Before ViT: - Computer vision relied almost exclusively on Convolutional Neural Networks (CNNs) - CNNs have inductive biases (assumptions about locality and translation equivariance) baked into their architecture - While Transformers dominated NLP, applying them to vision seemed impractical due to: - Images have many pixels (high resolution = huge computational cost) - Self-attention scales quadratically with sequence length - No clear way to tokenize images
The question: Could pure Transformers work for vision if we had enough data and compute?
Core Innovation¶
Treating Images as Sequences of Patches¶
The key insight: Split an image into fixed-size patches and treat them like words in a sentence.
How it works:
- Patch Embedding
- Split image (e.g., 224×224) into 16×16 patches
- Results in 14×14 = 196 patches
- Flatten each patch into a vector
-
Linearly project to embedding dimension
-
Position Embeddings
- Add learnable position embeddings (like in NLP Transformers)
-
Tells the model where each patch is located
-
Class Token
- Prepend a special
[CLS]token (borrowed from BERT) -
Its final representation used for classification
-
Standard Transformer
- Feed patch embeddings through standard Transformer encoder
- Multi-head self-attention learns relationships between patches
- MLP layers process the representations
Mathematical representation:
Input image: x ∈ R^(H×W×C)
Patches: x_p ∈ R^(N×(P²·C))
where N = HW/P² (number of patches)
Patch embeddings: z_0 = [x_class; x_p¹E; x_p²E; ...; x_pᴺE] + E_pos
Transformer encoder: z_ℓ = Transformer(z_(ℓ-1))
Classification: y = LayerNorm(z_L⁰)
Architecture Details¶
Standard ViT Configuration¶
ViT-Base (ViT-B/16): - Patch size: 16×16 - Embedding dimension: 768 - Layers: 12 - Attention heads: 12 - MLP hidden size: 3072 - Parameters: ~86M
ViT-Large (ViT-L/16): - Embedding dimension: 1024 - Layers: 24 - Attention heads: 16 - Parameters: ~307M
ViT-Huge (ViT-H/14): - Patch size: 14×14 - Embedding dimension: 1280 - Layers: 32 - Attention heads: 16 - Parameters: ~632M
Why This Works¶
- Self-attention captures global context
- Unlike CNNs with limited receptive fields
-
Each patch can attend to all other patches from layer 1
-
Fewer inductive biases
- Model must learn spatial relationships from data
-
With enough data, this is more flexible than hard-coded CNN biases
-
Scalability
- Transformers scale better with data than CNNs
- Performance continues improving with larger datasets
Training Approach¶
Pre-training on Large Datasets¶
Critical finding: ViT requires more data than CNNs to reach comparable performance.
Datasets used: - ImageNet-21k: 14M images, 21k classes - JFT-300M: 300M images, 18k classes (Google internal)
Training details: - Optimizer: Adam with β₁=0.9, β₂=0.999 - Learning rate: Linear warmup + cosine decay - Regularization: Dropout, weight decay - Data augmentation: RandAugment, MixUp
Fine-tuning on Downstream Tasks¶
After pre-training: 1. Remove pre-training classification head 2. Add new head for target task 3. Fine-tune at higher resolution (e.g., 384×384) 4. Use fewer training steps than pre-training
Key trick: When fine-tuning at higher resolution, interpolate position embeddings to match new patch count.
Results and Impact¶
Performance Comparison¶
ImageNet (top-1 accuracy): - ViT-H/14 (JFT-300M pre-training): 88.55% - BiT-L (ResNet-152x4): 87.54% - Noisy Student (EfficientNet): 88.4%
Transfer learning (average over 19 tasks): - ViT-H/14: 77.91% - BiT-L: 76.08%
Computational Efficiency¶
Pre-training cost: - ViT-H/14: ~2.5k TPUv3-core-days - BiT-L: ~9.9k TPUv3-core-days
ViT is 4× more compute-efficient than ResNet-based models!
What Makes ViT Better?¶
- Scales better with data
- Performance gap widens with larger pre-training datasets
-
CNNs plateau faster
-
More efficient attention patterns
- Lower layers: Local attention (similar to convolution)
-
Higher layers: Global attention (captures long-range dependencies)
-
Transfer learning
- Pre-trained ViT transfers better across diverse tasks
Attention Visualizations¶
Key findings from attention analysis:
- Spatial awareness emerges naturally
- Model learns to attend to spatially nearby patches early
-
No convolution needed to learn locality
-
Global context from layer 1
- Some attention heads attend globally even in early layers
-
Captures long-range dependencies CNNs struggle with
-
Semantic grouping
- Attention heads learn to group semantically similar regions
- Example: All patches of "dog" attend to each other
Limitations¶
1. Data Hungry¶
- Requires large-scale pre-training (millions of images)
- On small datasets (e.g., ImageNet-1k only), CNNs perform better
- Why: Fewer inductive biases means more data needed to learn patterns
2. Computational Cost¶
- Quadratic complexity in number of patches
- High-resolution images expensive (e.g., 1024×1024 medical images)
- Solution: Hierarchical variants like Swin Transformer
3. Less Interpretable Position Embeddings¶
- Position embeddings are learned, not fixed
- Less clear how model understands spatial structure
- 2D structure not explicitly encoded
4. Fixed Input Size During Training¶
- Must interpolate position embeddings for different resolutions
- Not as naturally flexible as CNNs with varying input sizes
Practical Applications¶
Where ViT Excels¶
- Image Classification
- ImageNet, iNaturalist, CIFAR benchmarks
-
Medical image classification
-
Object Detection
- ViT backbone in Detection Transformers (DETR)
-
Faster R-CNN with ViT features
-
Semantic Segmentation
- Vision Transformer backbones in segmentation models
-
Medical imaging segmentation
-
Multimodal Models
- CLIP: Vision encoder using ViT
- Flamingo: Visual language models
-
GPT-4 Vision: Image understanding
-
Video Understanding
- Video Vision Transformer (ViViT)
- Temporal attention over frame patches
Variants and Improvements¶
Hierarchical Vision Transformers¶
Swin Transformer (2021): - Shifted window attention (local then global) - Hierarchical feature maps (like CNNs) - Better for dense prediction tasks
PVT (Pyramid Vision Transformer) (2021): - Multi-scale feature pyramid - Spatial-reduction attention for efficiency
Efficient Vision Transformers¶
DeiT (Data-efficient ViT) (2020): - Distillation from CNN teachers - Works well with ImageNet-1k only
Mobile-ViT (2021): - Lightweight for mobile devices - Combines convolutions and transformers
Hybrid Approaches¶
Early Convolutions + Transformers: - Use CNN stem to reduce patch count - Then apply Transformer - Combines CNN inductive biases with Transformer flexibility
Implementation Details¶
Pseudocode¶
class VisionTransformer:
def __init__(self, image_size=224, patch_size=16, num_classes=1000,
dim=768, depth=12, heads=12, mlp_dim=3072):
self.patch_embed = PatchEmbedding(patch_size, dim)
self.cls_token = Parameter(torch.randn(1, 1, dim))
self.pos_embed = Parameter(torch.randn(1, num_patches + 1, dim))
self.transformer = TransformerEncoder(dim, depth, heads, mlp_dim)
self.classifier = Linear(dim, num_classes)
def forward(self, img):
# Split into patches and embed
x = self.patch_embed(img) # (B, N, dim)
# Prepend class token
cls_tokens = self.cls_token.expand(x.shape[0], -1, -1)
x = torch.cat([cls_tokens, x], dim=1) # (B, N+1, dim)
# Add position embeddings
x = x + self.pos_embed
# Apply Transformer
x = self.transformer(x)
# Classify using class token
return self.classifier(x[:, 0])
Key Hyperparameters¶
Pre-training: - Batch size: 4096 - Learning rate: 0.001 (with linear warmup) - Weight decay: 0.1 - Optimizer: Adam - Augmentation: RandAugment(2, 15)
Fine-tuning: - Batch size: 512 - Learning rate: 0.003 (higher resolution: 0.01) - Fine-tuning steps: ~20k (much less than pre-training)
Key Takeaways¶
- Pure Transformers work for vision when trained on sufficient data
- Patches as tokens is the key insight for applying Transformers to images
- Scales better than CNNs with increased data and model size
- Unified architecture enables seamless multimodal learning
- Pre-training is critical - ViT needs large-scale pre-training to excel
Influence on Later Work¶
Direct Descendants¶
- CLIP (2021): ViT as image encoder
- DALL-E (2021): ViT-based image tokenization
- Swin Transformer (2021): Hierarchical ViT
- BEiT (2021): BERT-style pre-training for images
- MAE (2021): Masked autoencoder for images
Conceptual Impact¶
- Showed Transformers are universal architectures (not just for sequences)
- Inspired transformer applications in video, 3D, audio, biology, reinforcement learning
- Established that data > inductive biases at scale
Further Reading¶
Original Paper¶
- An Image is Worth 16x16 Words: https://arxiv.org/abs/2010.11929
Follow-up Papers¶
- DeiT: https://arxiv.org/abs/2012.12877 (Data-efficient training)
- Swin Transformer: https://arxiv.org/abs/2103.14030 (Hierarchical ViT)
- BEiT: https://arxiv.org/abs/2106.08254 (Self-supervised pre-training)
- MAE: https://arxiv.org/abs/2111.06377 (Masked image modeling)
Code Implementations¶
- Official JAX implementation: https://github.com/google-research/vision_transformer
- PyTorch (timm): https://github.com/huggingface/pytorch-image-models
- Hugging Face Transformers: https://huggingface.co/docs/transformers/model_doc/vit
Tutorials¶
- The Illustrated Vision Transformer: https://jalammar.github.io/illustrated-vit/
- ViT Paper Walkthrough (Yannic Kilcher): YouTube search
- Fine-tuning ViT on custom datasets: Hugging Face documentation
Published: October 2020 Impact Factor: 10,000+ citations (as of 2025) Legacy: Made Transformers the dominant architecture in computer vision, enabling the multimodal AI revolution.
Related in This Collection¶
- Attention Is All You Need
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
- Learning Transferable Visual Models From Natural Language Supervision (CLIP)
- GPT-4 Technical Report
- DALL-E 3: Improving Image Generation with Better Captions
- Deep Residual Learning for Image Recognition (ResNet)
- Masked Autoencoders Are Scalable Vision Learners (MAE)