ViT Transformer: What a Vision Transformer Is and How It Reads an Image

What is a ViT transformer?
A ViT transformer, or vision transformer, is a neural network that reads an image the way a transformer, a network that lets words weigh each other, reads text: it cuts the image into small squares, turns each into a list of numbers, and lets the squares share information before naming the image.
It labels images and turns them into numbers for larger models: given a photo of a dog, a photo-sorting app gets back “dog”.
The vision transformer (ViT) is a way a neural network reads an image; the older one is the convolutional neural network (CNN), which slides small filters over the image.
A dog photo, read as patches
An app sorts photos by content. Our example is a dog photo of 224 x 224 pixels (the dots of an image), the default input size of vit_b_16 and of google/vit-base-patch16-224.
Google researchers (Dosovitskiy and colleagues) applied a standard transformer architecture, which reads text as tokens (lists of numbers for pieces of words), to images. They treated image patches “the same way as tokens (words)”, hence the paper’s title “An Image is Worth 16x16 Words”.
One pass, from pixels to a label
Cutting the photo into patches of 16 x 16 pixels gives 196 patches, the items the network processes; that count holds only for this photo size and patch size.
Each patch’s pixels are lined up into one list and multiplied by a learned matrix, a linear step; the result is a token, a list of numbers the transformer works with, which the paper calls the patch embedding. Its weights, the numbers learned in training, are shared by all patches.
The ViT adds a position embedding, learned numbers that say where the patch was, to each token, number by number. It also puts one extra learned token, the class token, at the front; its final numbers stand for the whole image because, through attention, it gathers information from every patch token in every block.
The transformer encoder, a stack of blocks, mixes them. In each block, self-attention lets each token score every other token for relevance and take in a mix weighted by those scores, so the ear patch can use the snout patch; then a two-layer network recomputes each token alone. The attention mechanism topic shows how the scores are computed.
Last, a classifier head turns the class token’s numbers into a label: “dog”.
Why a ViT needs so many images first
A CNN has an inductive bias, an assumption built into its design: nearby pixels belong together. A ViT has much less of it and must learn from data where each patch sits.
Trained only on ImageNet, about 1.3 million labelled photos, without strong regularization (extra guards against memorising), the paper’s ViTs labelled a few percentage points fewer test photos correctly than ResNets of comparable size (a common CNN design).
Pre-trained, that is trained first on a large general set, of 14 million images or more, they did better: “large scale training trumps inductive bias”.
So you start from pre-trained weights and fine-tune them, training further on your own smaller set. Max's guide fine-tunes a small ViT on pet photos.
The parts, and ViT against CNN
The parts of a ViT and their sizes covers the class token, position numbers and encoder block in depth. ViT vs CNN weighs whether the photo app needs a ViT or a CNN, and convolutional neural networks shows how a CNN works.
Where ViTs read images today
Published models use a ViT as an image encoder, the part that turns an image into numbers. CLIP (OpenAI) pre-trains its ViT to match images with their text; DINOv2 (Meta) is trained on a curated set of 142 million images.
If you only wanted to know what a ViT transformer is, you can stop here.
Model names and patch size
The paper was first posted in October 2020 and published at the ICLR 2021 conference. A name like ViT-L/16 means the Large model with 16 x 16 patches; the architecture article linked above lists the Base, Large and Huge sizes.
Its best model, ViT-H/14, uses 14 x 14 patches: a 224 x 224 photo then gives 256 patches, not 196. Smaller patches make the token list longer and, the paper notes, the model more expensive to run.
Other model designs, such as state-space models and mixture of experts, have their own topic pages.
Published measurements and their conditions
Each row’s authors or library measured these; we did not. Accuracy here is ImageNet top-1: the share of test images whose highest-ranked label is right.
| Measurement | Conditions | What it tells you |
|---|---|---|
| ViT-H/14 reached 88.55 %; Noisy Student, a CNN, 88.4 to 88.5 % | Dosovitskiy et al., 2020; pre-trained on JFT-300M, Google’s internal set of about 300 million images; fine-tuned at 518 pixels a side, larger than 224, which gives more patches per image; mean of 3 runs | Matching the best CNN holds with private data at this scale and resolution |
| ViT-L/16 87.76 % against 87.54 % for BiT-L, a large ResNet; pre-training took 0.68k against 9.9k TPUv3-core-days (Google TPU chip cores times days), about 15 times less by our division | Same paper, same JFT-300M data; the authors warn that compute also depends on other training settings | Similar accuracy for less pre-training compute, in Google’s setup |
| ViT-B/16: 77.91, 83.97 and 84.15 %; ViT-L/16: 76.53, 85.15 and 87.12 %, pre-trained on ImageNet, ImageNet-21k (14 million images) and JFT-300M | Same paper; fine-tuned at 384 pixels; one value per cell | The larger model loses to the smaller one on the smallest set and wins only with more data |
torchvision’s vit_b_16 weights IMAGENET1K_V1: 81.072 % | torchvision’s own training recipe; 224 x 224 input; documentation read on 1 December 2026 | What the ready weights you download score, not a number from the paper |
A 2021 study, “How to train your ViT?”, found that ViTs pre-trained on the public ImageNet-21k, with more compute plus augmentation and regularization, match or beat their counterparts pre-trained on the private JFT-300M.
What a ready model expects as input
In torchvision, the PyTorch library of vision models, vit_b_16 checks the image size and stops with “Wrong image height!” when an image is not exactly image_size, 224 by default.
The weights IMAGENET1K_V1 (the DEFAULT weights; vit_b_16() loads no weights unless you pass them) expect what their documentation lists: resize to 256 pixels, crop the central 224 x 224, scale values to 0 to 1, then subtract ImageNet’s mean and divide by its standard deviation.
Hugging Face’s google/vit-base-patch16-224, pre-trained on ImageNet-21k and fine-tuned on ImageNet, uses 0.5 for both the mean and the standard deviation instead. Prepare images for one model with the other’s recipe and every input value shifts, so use the preparation each set of weights documents.
When it breaks: bigger images or smaller patches lengthen the token list, which torchvision’s vit_b_16 already starts at 197 tokens (196 patches plus the class token); since attention compares every pair of tokens, its work grows with the square of the list’s length.
Cheat sheet
The article on one page: open it full size, save it or share it.

AI-assisted content, human-reviewed. Images AI-generated. Editorial Standards · Our Editors