# Transformers in Vision: A Survey

Vision Transformers are surveyed across recognition, generation, video and low-level tasks

Salman Khan, Muzammal Naseer, Munawar Hayat, Syed Waqas Zamir, Fahad Shahbaz Khan, Mubarak Shah · arXiv · 2021-1-4

- **Problem** Self-attention compares every patch with every other patch, so its cost grows as O(n2) in the number of patches. That makes high-resolution tasks such as detection and segmentation expensive.
- **Method** The authors group vision Transformers by how they change self-attention and pre-training. They compare the advantages and limitations of popular models in architecture and experiments.
- **Results** The authors compare popular vision Transformers on architectural design and experimental value. They also list open research directions.

## Why plain Transformers struggle with images

Language Transformers reached very large scale, with BERT-large at 340 million parameters, GPT-3 at 175 billion and the Switch transformer at 1.6 trillion. Images carry spatial and temporal structure, so a generic design needs changes before it helps vision. The open question is therefore which parts of the Transformer to keep and which to redesign.

The first obstacle is cost. Self-attention compares every token with every other token, so its time and memory grow as O(n2) in the number of patches n. That makes high-resolution tasks such as detection, segmentation and super-resolution expensive.

The second obstacle is data. A Transformer has no built-in translation invariance or weight sharing, so it has to learn these properties from training examples. ViT needs hundreds of millions of image examples to reach reasonable ImageNet performance. ViT-L drops by an absolute 13% on ImageNet when trained only on the ImageNet train set, compared to pre-training on JFT with 300 million images.

## How self-attention replaces fixed filters

The key idea is that each token's output is a weighted mix of all tokens, with the weights computed from the input itself. A convolution applies the same learned filter everywhere, whereas self-attention recomputes its weights for every input. Because it doesn't rely on a grid, the same layer can process irregular inputs that convolution handles poorly.

Each row of the input matrix X holds one of n entities with dimension d, and three learnable matrices project X into queries, keys and values. Dot products of queries with keys are divided by the square root of d\_q, normalized with softmax, and then used to weight the values. The output is \\( Z=\\mathrm{softmax}\\left(QK^{T}/\\sqrt{d\_q}\\right)V \\), an n by d\_v matrix.

Multi-head attention runs h self-attention blocks in parallel, with h equal to 8 in the original Transformer. Their outputs are concatenated and projected back to the model dimension before the next layer. In a decoder that predicts the next entity, a mask zeroes attention to future positions through a Hadamard product.

The authors cite Cordonnier et al., whose empirical results suggest that multi-head self-attention with enough parameters can express convolution as a special case. Self-attention with positional encodings can also learn kernel weights and the receptive field, which is closer to deformable convolution than to a fixed filter. This is why the authors treat attention as a more general operator than convolution, not as a replacement that loses local structure.

## Pre-training and fine-tuning

Most of these models train in two stages: a pre-training run on a large dataset, then adaptation to a downstream task with smaller data. The pre-training can be supervised, as with ViT, or self-supervised with pretext tasks that need no manual labels. Pre-training avoids costly annotation, but it moves the cost into compute and dataset size.

Some recent ViTs train end-to-end on ImageNet-1K without a dedicated pre-training phase. Their performance generally stays below that of the pre-trained counterparts, so the two-stage recipe remains the stronger option.

## Hierarchical and hybrid ViT designs

In standard ViTs the number of tokens and the token width stay fixed across blocks. That limits the model's ability to capture fine spatial detail at different scales, so dense prediction methods often use a convolutional backend.

Multi-stage designs reduce the token count while widening the features, which suits dense prediction. Swin Transformer computes attention within local windows and shifts the window partition between layers, so that neighbouring windows interact. CrossViT processes smaller and larger patches in two branches and fuses them with a cross-attention module.

Hybrid designs keep convolution for tokenization and local features. LeViT places a four-layered CNN block with 3x3 convolutions in front of a hierarchical ViT, and the authors report it is 5 times faster than EfficientNet on CPU at inference. CCT trains from scratch on CIFAR10 with about 95% accuracy, which the authors say traditional ViTs can't match.

CoAtNets stack convolution and attention layers vertically and use a relative attention module. Trained only on ImageNet-1k without extra data, they reach 86% ImageNet top-1 accuracy.

## DETR and set prediction for detection

DETR treats detection as predicting a set of boxes in one pass, and matches predictions to ground-truth boxes with a bipartite set loss. The design removes hand-crafted parts such as the region proposal network and non-maximum suppression.

The encoder flattens the CNN feature map into a sequence of features. The decoder predicts all boxes in parallel from learned object queries, whereas an RNN would predict sequence elements one by one.

The authors report that DETR is comparable to the Faster R-CNN model, which they count as a notable feat given its simple design. So the evidence supports dropping the hand-built components without a clear accuracy loss, though not a clear accuracy gain.

## Self-supervised ViTs

Contrastive self-supervision that worked for CNNs was extended to ViTs, with MoCo v3 proposed for stabilized training. DINO showed that self-supervised ViTs segment background pixels without any pixel-level supervision, a behaviour not seen in CNNs or fully supervised ViTs.

EsViT merges neighbouring tokens across stages and adds patch-level self-supervision that promotes correspondence between similar patches. The comparison concerns off-the-shelf features, not fine-tuned accuracy.

## Costs, data and interpretability

Efficient attention variants such as pooling, local windows and low-rank projections trade cost against accuracy. The authors say almost all of them trade complexity against accuracy, need special hardware, or still don't scale to very large images.

The Set Transformer shows one route, since its Induced Self-Attention Block uses a low-rank projection to lower the cost from O(n2) to O(mn). That result concerns set inputs, so it doesn't by itself settle the cost for image patches.

Data efficiency is still open, although DeiT uses distillation and T2T-ViT combines spatially close tokens to train on ImageNet from scratch. CCT and NesT bring back CNN-like feature hierarchies to train from scratch on small datasets such as CIFAR-10.

On robustness, ViTs keep up to 60% top-1 accuracy on ImageNet when 80% of the image content is randomly occluded. Interpretation is harder, because attention from each layer mixes into later layers in a complex way. Attention roll-out and attention flow make simplistic assumptions, and the authors call visualizing and interpreting Transformers an unsolved problem.

Hardware-efficient vision Transformers are still missing for resource-constrained devices. The evolutionary searches used for hardware-aware design also carry a CO2 cost.

## Paper info

- **Authors**: Salman Khan, Muzammal Naseer, Munawar Hayat, Syed Waqas Zamir, Fahad Shahbaz Khan, Mubarak Shah
- **Venue**: arXiv · Preprint
- **First published**: 2021-1-4
- **Added**: 2026-11-12
- **Updated**: 2022-1-19
- **Version**: v5
- **arXiv**: 2101.01169
- **DOI**: 10.1145/3505244
- **Category**: cs.CV · cs.AI · cs.LG
- **Original paper terms**: [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2101.01169v5) · [PDF ↗](https://arxiv.org/pdf/2101.01169v5)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Salman Khan, Muzammal Naseer, Munawar Hayat, Syed Waqas Zamir, Fahad Shahbaz Khan, Mubarak Shah (2021). Transformers in Vision: A Survey. arXiv:2101.01169v5. https://doi.org/10.1145/3505244

**BibTeX**

```
@article{khan2021transformers,
  title = {{Transformers in Vision: A Survey}},
  author = {Salman Khan and Muzammal Naseer and Munawar Hayat and Syed Waqas Zamir and Fahad Shahbaz Khan and Mubarak Shah},
  year = {2021},
  eprint = {2101.01169},
  archivePrefix = {arXiv},
  doi = {10.1145/3505244},
  url = {https://arxiv.org/abs/2101.01169v5}
}
```

## Other paper commentary

- [Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour](https://refivu.com/en/papers/c430ecf3-8156-45a7-a3f7-eafc308c0d49-accurate-large-minibatch-sgd-training-imagenet-in-1.md)
- [Image Segmentation Using Deep Learning: A Survey](https://refivu.com/en/papers/c5b4071c-1e13-4bda-97cd-37556fea547e-image-segmentation-using-deep-learning-a-survey.md)
- [Residual Attention Network for Image Classification](https://refivu.com/en/papers/a2dac1b2-ffde-41af-9f9c-c9914f383632-residual-attention-network-for-image-classification.md)
- [DeCAF: A Deep Convolutional Activation Feature for Generic Visual Recognition](https://refivu.com/en/papers/b018056b-58d2-42cc-9184-ed3588d7abd4-decaf-a-deep-convolutional-activation-feature-for-generic.md)
- [Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers](https://refivu.com/en/papers/c0f3cbcf-d34f-4a38-a72f-084577c0ea46-rethinking-semantic-segmentation-from-a-sequence-to-sequence.md)
- [CenterNet: Keypoint Triplets for Object Detection](https://refivu.com/en/papers/f08ac102-1d96-4756-a172-5c1d6ab4ebfb-centernet-keypoint-triplets-for-object-detection.md)
- [A Broad-Coverage Challenge Corpus for Sentence Understanding through Inference](https://refivu.com/en/papers/6a9a87c0-1034-4d6f-8def-43a3279e4725-a-broad-coverage-challenge-corpus-for-sentence-understanding.md)
- [Supervised Contrastive Learning](https://refivu.com/en/papers/a5818cfe-00fa-4e85-a38e-cdce6aa6cad5-supervised-contrastive-learning.md)
- [The Future of Digital Health with Federated Learning](https://refivu.com/en/papers/1562dfe5-7a60-4996-ad5c-7fe34a7d94e7-the-future-of-digital-health-with-federated-learning.md)
- [Past, Present, and Future of Simultaneous Localization And Mapping: Towards the Robust-Perception Age](https://refivu.com/en/papers/52381e81-8cdd-4551-89ee-d6c7630fb938-past-present-and-future-of-simultaneous-localization-and.md)
- [Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation](https://refivu.com/en/papers/e938eef4-a963-441d-bfef-194cc6a9a9e3-estimating-or-propagating-gradients-through-stochastic.md)
- [On Calibration of Modern Neural Networks](https://refivu.com/en/papers/49c54409-e306-4419-a26c-4569bedfbd40-on-calibration-of-modern-neural-networks.md)

## Terms in this paper

- [ImageNet](https://refivu.com/en/terms/5eadad128667-imagenet)
- [Transformer](https://refivu.com/en/terms/48e8be2570bc-transformer)
- [Top-1 accuracy](https://refivu.com/en/terms/653c0b9eaa58-top-1-accuracy)
- [self-attention](https://refivu.com/en/terms/426589b81077-self-attention)
- [CLIP](https://refivu.com/en/terms/b5b5b48d5f4e-clip)
- [multi-head attention](https://refivu.com/en/terms/370ae5c971cd-multi-head-attention)
- [Vision Transformer](https://refivu.com/en/terms/143abf165d88-vision-transformer)
- [DeiT](https://refivu.com/en/terms/24b9f164881b-deit)
- [Swin Transformer](https://refivu.com/en/terms/957ac355698c-swin-transformer)
- [ModelNet40](https://refivu.com/en/terms/555f2682f629-modelnet40)
- [Visual Question Answering](https://refivu.com/en/terms/fc0a624b0e20-visual-question-answering)
- [positional encodings](https://refivu.com/en/terms/3424fe16f570-positional-encodings)
- [JFT dataset](https://refivu.com/en/terms/af04a67c4cb2-jft-dataset)
- [ViLBERT](https://refivu.com/en/terms/877c8baff675-vilbert)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/5380a1d5-fae3-41ad-ab72-e650e9db12fe-transformers-in-vision-a-survey)
