← Back to blogs

Vision Transformers: ViT, Self-Attention, and the CNN Comparison

The ViT architecture and its trade-offs against CNNs

Vision Transformers (ViT) replace the convolutional backbone with a pure attention architecture. The foundational ViT paper (Dosovitskiy et al. 2021) showed that a standard Transformer encoder, applied directly to sequences of image patches, achieves strong image classification when pretrained on sufficient data (ImageNet-21K (Deng et al. 2009) or JFT-300M). ViT-Huge achieved 88.55% top-1 accuracy on ImageNet, matching the best reported CNNs of the time (BiT-L, Noisy Student) at lower pretraining compute. The paper's reading is that convolutional inductive biases are not strictly necessary, because a transformer can learn them from data.

Data efficiency became the focus of subsequent work. DeiT (Touvron et al. 2021) showed ViT can be trained on ImageNet-1K alone using strong regularization, achieving 81.8% top-1 accuracy without external data (83.4% with distillation from a RegNetY-16GF teacher). DeiT introduced knowledge distillation via a distillation token, showing that ViT benefits from CNN-style inductive biases transferred through distillation. DeiT III (Touvron et al. 2022) pushed this further with improved training recipes, achieving 85.2% with ViT-H/14.

Hierarchical designs addressed the computational limitations of global attention. Swin Transformer (Liu et al. 2021) introduced shifted window attention, computing attention within local windows while enabling cross-window connections through shifting. This achieves linear complexity and provides multi-scale feature maps compatible with dense prediction. Swin achieved 87.3% on ImageNet and, at publication, the best reported COCO detection (58.7 box AP) and ADE20K segmentation (53.5 mIoU) figures. Swin Transformer V2 (Ze Liu et al. 2022) scaled to 3B parameters with techniques for training stability (residual post-normalization, scaled cosine attention).

Alternative hierarchical approaches emerged. Pyramid Vision Transformer (PVT) (Wang et al. 2021) uses spatial reduction attention to handle high-resolution feature maps. Twins (Chu et al. 2021) combines locally-grouped attention with global sub-sampled attention. Focal Transformer (Yang et al. 2021) introduces focal attention attending both locally and globally with varying granularity. Nested Hierarchical Transformer (NesT) (Zhang et al. 2021) aggregates tokens in a nested manner to create hierarchical representations.

Hybrid architectures combine CNN and Transformer strengths. Convolutional vision Transformer (CvT) (Wu et al. 2021) introduces convolutions into attention projection. CoAtNet (Dai et al. 2021) systematically combines depthwise convolution and attention, achieving 90.88% ImageNet accuracy with JFT-3B pretraining. MaxViT (Tu et al. 2022) interleaves block and grid attention patterns with convolutions for efficient multi-scale attention. Mobile-Former (Y. Chen et al. 2022) uses parallel mobile and former branches with bidirectional connections. MobileViT (Mehta and Rastegari 2022) unfolds features for transformer processing while maintaining mobile efficiency.

Self-supervised learning proved particularly effective for ViT. DINO (Caron et al. 2021) showed self-supervised ViT features contain semantic segmentation information not present in supervised models, achieving 78.3% k-NN accuracy on ImageNet. A follow-up diagnostic, Vision Transformers Need Registers (Darcet et al. 2024), identified a structural artifact in large pre-trained ViTs: a small fraction of patch tokens develop anomalously high feature norms and attract disproportionate attention weight, producing irregular blobs in attention maps. These high-norm tokens appear preferentially at low-information background patches, not at semantically salient locations. The interpretation is that the model repurposes uninformative patch tokens as global scratch space to store and transmit information that does not fit naturally into the attention pattern of any single semantic patch.

The fix is minimal: append r=4r = 4 additional learnable [REG] tokens to the input sequence (alongside the [CLS] token) and include them throughout all layers. These tokens have no spatial correspondence and no loss target; they serve purely as internal workspace. With registers present, the high-norm artifact disappears from patch tokens, attention maps become spatially smooth, and dense prediction improves. The intervention requires no change to the training recipe, the loss function, or the backbone architecture. The paper reports the best figures at publication for self-supervised models on dense visual prediction tasks and improvements in object discovery quality with larger models. The caveat is that register count r=4r = 4 is a hyperparameter tuned on DINOv2-scale models; smaller ViTs trained on less data show less severe artifact behaviour and smaller gains from registers. MAE (He et al. 2021) showed that masking 75% of patches and reconstructing provides strong pretraining, with ViT-Huge achieving 87.8% accuracy using only ImageNet-1K. BEiT (Bao et al. 2022) predicted discrete visual tokens from masked patches. SimMIM (Xie et al. 2022) showed simple random masking with direct pixel prediction works well.

Efficiency improvements targeted inference cost. DynamicViT (Rao et al. 2021) dynamically prunes uninformative tokens based on learned importance scores, reducing FLOPs by 31-37% with <0.5% accuracy drop. EfficientFormer (Li et al. 2022) designed latency-driven architectures achieving mobile speed. Token-to-Token ViT (T2T-ViT) (Yuan et al. 2021) progressively tokenizes images to capture local structure.

Understanding ViT behavior revealed important properties. Empirical studies (X. Chen et al. 2021) showed ViT optimizes differently from CNNs, with sensitivity to optimizer, weight decay, and learning rate schedules. "How to train your ViT" (Steiner et al. 2022) provided a systematic study of data, augmentation and regularization for ViT training. Sharpness-aware minimization (SAM) (Foret et al. 2021) improved ViT generalization by optimizing for flatter minima. Adversarial robustness analysis (Naseer et al. 2021) (Shao et al. 2022) found ViT more robust than CNNs due to attention's global context. The data-scale crossover between the two families is measured in the original ViT paper (Dosovitskiy et al. 2021); the narrower claim of (X. Chen et al. 2022) is that a sharpness-aware optimizer lets ViTs beat ResNets on ImageNet-1K from scratch, with neither large-scale pretraining nor strong augmentation.

Cross-attention between scales improved multi-scale reasoning. CrossViT (C.-F. Chen et al. 2021) uses dual-branch architecture with different patch sizes, fusing via cross-attention. This achieves efficient multi-scale processing while maintaining reasonable computational cost.

One axis organises the whole list above. ViT made a single design decision, it deleted the convolutional prior, and every paper that follows pays some of that prior back. DeiT pays it back through the teacher and the augmentation stack. Swin, PVT, Twins and Focal re-impose locality inside the attention pattern itself. CvT, CoAtNet, MaxViT and MobileViT put the convolution back into the block. MAE, BEiT, SimMIM and DINO move the prior into the pretext task, so unlabeled data supplies it instead of the architecture. ConvNeXt runs the argument backwards, keeping the convolution and importing only the recipe. Sorted by where the bias is stored rather than by accuracy, the families read as follows.

Where each family of post-ViT work puts the convolutional prior back. The rows are sorted by the site of the prior rather than by accuracy, so the table is a classification of mechanisms and not a ranking; the per-row citations name the papers in each family and not the source of any single number. The last column records what the mechanism costs, which is the axis the accuracy tables in this chapter do not show.
Family Where the convolutional prior is reinstated Mechanism Price paid
ViT (Dosovitskiy et al. 2021) nowhere, pay in data instead patch embed, then a plain Transformer encoder needs ≥\geq14M pretraining images to reach parity
DeiT (Touvron et al. 2021) training recipe CNN teacher through a distillation token, heavy augmentation 300 epochs plus a teacher forward pass per step
Swin (Liu et al. 2021), PVT (Wang et al. 2021), Twins (Chu et al. 2021), Focal (Yang et al. 2021) attention pattern windows, spatial reduction, grouping, pooled multi-granularity keys no single layer sees the whole image any more
CvT (Wu et al. 2021), CoAtNet (Dai et al. 2021), MaxViT (Tu et al. 2022), MobileViT (Mehta and Rastegari 2022) inside the block convolutional token embed, depthwise conv on Q/K/V, MBConv stem convolution kernels back in the parameter count
MAE (He et al. 2021), BEiT (Bao et al. 2022), SimMIM (Xie et al. 2022), DINO (Caron et al. 2021) pretext task masked reconstruction or self-distillation learns the prior from unlabeled data long pretraining, and MAE features need fine-tuning before use
MLP-Mixer (Tolstikhin et al. 2021), PoolFormer (Yu et al. 2022) nowhere, and attention is dropped too fixed or parameter-free token mixer, the recipe carries the load token-mixing weights are tied to one resolution
ConvNeXt (Zhuang Liu et al. 2022) never removed it keep the convolution, import the ViT recipe and macro design none, which is what makes it the control experiment

The field converged on: (1) hierarchical designs for dense prediction (Swin), (2) strong regularization for data-efficient training (DeiT), (3) self-supervised pretraining for label efficiency (MAE, DINO), (4) hybrid CNN-Transformer architectures for mobile deployment, and (5) training recipes matched to the architectural choice, which move accuracy as much as the choice itself does.

Related-work leaderboard (representative numbers, ImageNet-1K top-1 unless noted):

References

Bao, Hangbo, Li Dong, Songhao Piao, and Furu Wei. 2022. BEiT: BERT Pre-Training of Image Transformers. arXiv. https://doi.org/10.48550/arXiv.2106.08254.
Caron, Mathilde, Hugo Touvron, Ishan Misra, et al. 2021. (DINO) Emerging Properties in Self-Supervised Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2104.14294.
Chen, Chun-Fu, Quanfu Fan, and Rameswar Panda. 2021. CrossViT: Cross-Attention Multi-Scale Vision Transformer for Image Classification. arXiv. https://doi.org/10.48550/arXiv.2103.14899.
Chen, Xiangning, Cho-Jui Hsieh, and Boqing Gong. 2022. When Vision Transformers Outperform ResNets Without Pre-Training or Strong Data Augmentations. arXiv. https://doi.org/10.48550/arXiv.2106.01548.
Chen, Xinlei, Saining Xie, and Kaiming He. 2021. An Empirical Study of Training Self-Supervised Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2104.02057.
Chen, Yinpeng, Xiyang Dai, Dongdong Chen, et al. 2022. Mobile-Former: Bridging MobileNet and Transformer. arXiv. https://doi.org/10.48550/arXiv.2108.05895.
Chu, Xiangxiang, Zhi Tian, Yuqing Wang, et al. 2021. Twins: Revisiting the Design of Spatial Attention in Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2104.13840.
Dai, Zihang, Hanxiao Liu, Quoc V. Le, and Mingxing Tan. 2021. CoAtNet: Marrying Convolution and Attention for All Data Sizes. arXiv. https://doi.org/10.48550/arXiv.2106.04803.
Darcet, Timothée, Maxime Oquab, Julien Mairal, and Piotr Bojanowski. 2024. "Vision Transformers Need Registers." International Conference on Learning Representations (ICLR). https://arxiv.org/abs/2309.16588.
Deng, Jia, Wei Dong, Richard Socher, Li-Jia Li, Kai Li, and Li Fei-Fei. 2009. "ImageNet: A Large-Scale Hierarchical Image Database." IEEE Conference on Computer Vision and Pattern Recognition (CVPR). https://doi.org/10.1109/CVPR.2009.5206848.
Dosovitskiy, Alexey, Lucas Beyer, Alexander Kolesnikov, et al. 2021. An Image Is Worth 16x16 Words: Transformers for Image Recognition at Scale. arXiv. https://doi.org/10.48550/arXiv.2010.11929.
Foret, Pierre, Ariel Kleiner, Hossein Mobahi, and Behnam Neyshabur. 2021. Sharpness-Aware Minimization for Efficiently Improving Generalization. arXiv. https://doi.org/10.48550/arXiv.2010.01412.
He, Kaiming, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollár, and Ross Girshick. 2021. Masked Autoencoders Are Scalable Vision Learners. arXiv. https://doi.org/10.48550/arXiv.2111.06377.
Li, Yanyu, Geng Yuan, Yang Wen, et al. 2022. EfficientFormer: Vision Transformers at MobileNet Speed. arXiv. https://doi.org/10.48550/arXiv.2206.01191.
Liu, Ze, Han Hu, Yutong Lin, et al. 2022. Swin Transformer V2: Scaling Up Capacity and Resolution. arXiv. https://doi.org/10.48550/arXiv.2111.09883.
Liu, Ze, Yutong Lin, Yue Cao, et al. 2021. "Swin Transformer: Hierarchical Vision Transformer Using Shifted Windows." IEEE/CVF International Conference on Computer Vision (ICCV), August. https://doi.org/10.48550/arXiv.2103.14030.
Liu, Zhuang, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, and Saining Xie. 2022. A ConvNet for the 2020s. arXiv. https://doi.org/10.48550/arXiv.2201.03545.
Mehta, Sachin, and Mohammad Rastegari. 2022. MobileViT: Light-Weight, General-Purpose, and Mobile-Friendly Vision Transformer. arXiv. https://doi.org/10.48550/arXiv.2110.02178.
Naseer, Muzammal, Kanchana Ranasinghe, Salman Khan, Munawar Hayat, Fahad Shahbaz Khan, and Ming-Hsuan Yang. 2021. Intriguing Properties of Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2105.10497.
Rao, Yongming, Wenliang Zhao, Benlin Liu, Jiwen Lu, Jie Zhou, and Cho-Jui Hsieh. 2021. DynamicViT: Efficient Vision Transformers with Dynamic Token Sparsification. arXiv. https://doi.org/10.48550/arXiv.2106.02034.
Shao, Rulin, Zhouxing Shi, Jinfeng Yi, Pin-Yu Chen, and Cho-Jui Hsieh. 2022. On the Adversarial Robustness of Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2103.15670.
Steiner, Andreas, Alexander Kolesnikov, Xiaohua Zhai, Ross Wightman, Jakob Uszkoreit, and Lucas Beyer. 2022. How to Train Your ViT? Data, Augmentation, and Regularization in Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2106.10270.
Tolstikhin, Ilya, Neil Houlsby, Alexander Kolesnikov, et al. 2021. "MLP-Mixer: An All-MLP Architecture for Vision." NeurIPS. https://arxiv.org/abs/2105.01601.
Touvron, Hugo, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, and Hervé Jégou. 2021. Training Data-Efficient Image Transformers & Distillation Through Attention. arXiv. https://doi.org/10.48550/arXiv.2012.12877.
Touvron, Hugo, Matthieu Cord, and Hervé Jégou. 2022. DeiT III: Revenge of the ViT. arXiv. https://doi.org/10.48550/arXiv.2204.07118.
Tu, Zhengzhong, Hossein Talebi, Han Zhang, et al. 2022. MaxViT: Multi-Axis Vision Transformer. arXiv. https://doi.org/10.48550/arXiv.2204.01697.
Wang, Wenhai, Enze Xie, Xiang Li, et al. 2021. Pyramid Vision Transformer: A Versatile Backbone for Dense Prediction Without Convolutions. arXiv. https://doi.org/10.48550/arXiv.2102.12122.
Wu, Haiping, Bin Xiao, Noel Codella, et al. 2021. CvT: Introducing Convolutions to Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2103.15808.
Xie, Zhenda, Zheng Zhang, Yue Cao, et al. 2022. SimMIM: A Simple Framework for Masked Image Modeling. arXiv. https://doi.org/10.48550/arXiv.2111.09886.
Yang, Jianwei, Chunyuan Li, Pengchuan Zhang, et al. 2021. Focal Self-Attention for Local-Global Interactions in Vision Transformers. arXiv. https://doi.org/10.48550/arXiv.2107.00641.
Yu, Weihao, Mi Luo, Pan Zhou, et al. 2022. "MetaFormer Is Actually What You Need for Vision." CVPR. https://arxiv.org/abs/2111.11418.
Yuan, Li, Yunpeng Chen, Tao Wang, et al. 2021. Tokens-to-Token ViT: Training Vision Transformers from Scratch on ImageNet. arXiv. https://doi.org/10.48550/arXiv.2101.11986.
Zhang, Zizhao, Han Zhang, Long Zhao, Ting Chen, Sercan O. Arik, and Tomas Pfister. 2021. Nested Hierarchical Transformer: Towards Accurate, Data-Efficient and Interpretable Visual Understanding. arXiv. https://doi.org/10.48550/arXiv.2105.12723.