Thanks to Transformers’ computational efficiency and scalability, it has become possible to train models of unprecedented size. With the models and datasets growing, there is still no sign of saturating performance. Inspired by the Transformer scaling successes in NLP, we experiment with applying a standard Transformer directly to images, with the fewest possible modifications. To do so, we split an image into patches and provide the sequence of linear embeddings of these patches as an input to a Transformer. Image patches are treated the same way as tokens (words) in an NLP application. We train the model on image classification in supervised fashion.
We find that large scale training trumps inductive bias. Our Vision Transformer (ViT) attains excellent results when pre-trained at sufficient scale and transferred to tasks with fewer datapoints.

We note that Vision Transformer has much less image-specific inductive bias than CNNs. In CNNs, locality, two-dimensional neighborhood structure, and translation equivariance are baked into each layer throughout the whole model. In ViT, only MLP layers are local and translationally equivariant, while the self-attention layers are global. The two-dimensional neighborhood structure is used very sparingly: in the beginning of the model by cutting the image into patches and at fine-tuning time for adjusting the position embeddings for images of different resolution.
As an alternative to raw image patches, the input sequence can be formed from feature maps of a CNN. As a special case, the patches can have spatial size 1x1.
When feeding images of higher resolution, we keep the patch size the same, which results in a larger effective sequence length. The Vision Transformer can handle arbitrary sequence lengths (up to memory constraints), however, the pre-trained position embeddings may no longer be meaningful. We therefore perform 2D interpolation of the pre-trained position embeddings, according to their location in the original image.
We first compare our largest models to state-of-the-art CNNs from the literature. Our large models pretrained on large datasets outperforms to state-of-the-art or performs well, while requiring substantially less computational resource to train. Interestingly, this model still took substantially less compute to pre-train than prior state of the art.
This result reinforces the intuition that the convolutional inductive bias is useful for smaller datasets, but for larger ones, learning the relevant patterns directly from data is sufficient, even beneficial.
First, Vision Transformers dominate ResNets on the performance/compute trade-off. Second, hybrids slightly outperform ViT at small computational budgets, but the difference vanishes for larger models. Third, Vision Transformers appear not to saturate within the range tried, motivating future scaling efforts.
The first layer of the Vision Transformer linearly projects the flattened patches into the model-dimensional space. After the projection, a learned position embedding is added to the patch representations. i.e closer patches tend to have more similar position embeddings. Self-attention allows ViT to integrate information across the entire image even in the lowest layers. Specifically, we compute the average distance in image space across which information is integrated, based on the attention weights. We find that some heads attend to most of the image already in the lowest layers, showing that the ability to integrate information globally is indeed used by the model. Other attention heads have consistently small attention distances in the low layers. This highly localized attention is less pronounced in hybrid models, suggesting that it may serve a similar function as early convolutional layers in CNNs. Further, the attention distance increases with network depth. Globally, we find that the model attends to image regions that are semantically relevant for classification.