# What Is a Vision Transformer?

> How a transformer reads a photo as small patches, why it needs so much data, and what it costs, explained simply for your AI interview.

**In short:** A vision transformer is a transformer that reads an image as a row of small square patches, much as a language model reads words. It was introduced in 2020, and it needs a lot of training data to do well against older image models.

AI and LLM interview question Q072, Transformer architecture. Intermediate, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-a-vision-transformer

## Say it in 30 seconds

A vision transformer is a transformer that reads a picture as a row of small square patches, the way a language model reads words. Each patch becomes one item, and every item can look at every other item. It needs a lot of training data, because it has fewer built-in image habits than older convolutional networks. Its cost also grows fast with the number of patches, so I would test a simple convolutional network first on a small dataset.

## Say it in 2 minutes

A vision transformer is a transformer that reads pictures. A transformer is the design that has become the standard for language tasks: it reads a row of pieces, and each piece looks at every other piece. For a picture, the pieces are small square patches, such as 16 by 16 pixels. Each patch becomes one item in the row, the way a word does in a sentence.

Here is how a photo becomes a label. The image is cut into patches. Each patch is turned into a list of numbers, and a position is added so the model knows where it sits. An extra slot goes at the front of the row. After the transformer layers, the final value of that slot goes to a small classifier, which picks the label. So a photo of a dented car door can come out labelled "dent".

The catch is data. Convolutional networks, the older image design, come with built-in habits about pictures, such as looking at neighbouring pixels. A vision transformer has fewer of those habits, so with a small set of labelled photos it often trails a convolutional network. The 2020 paper found the gap closes with large training data. The cost also grows fast: attention compares every patch with every other, so a 1024 by 1024 photo cut into 16 by 16 patches has 4,096 patches, and about 16.8 million pairs to compare in each attention head.

In an interview, say that it depends on the data, name the trade-off, and offer to test a convolutional network first.

## What is a vision transformer, in plain words?

A vision transformer, or ViT, is a transformer that works on pictures. A transformer is the design that has become the standard for language tasks. It reads a row of pieces, such as words, and lets each piece look at every other piece. A vision transformer does the same with an image, after cutting the image into pieces.

The pieces are small squares of the picture called patches. The paper that introduced the idea used 16 by 16 pixel patches as its running example. Each patch is handled the way a language model handles a word.

Think of a claims assessor at an insurer who gets a phone photo of a dented car door. She does not take in the whole photo at once. She cuts it into a grid, checks each square, and compares squares to see which parts belong to the same dent. A plausible case is an app that reads photos of car damage and tags the panel, so a person only checks the unclear ones.

- **Patch:** A small square of the picture, such as 16 by 16 pixels.
- **Token:** One item the transformer reads. For images, each patch becomes one token.
- **Self-attention:** The step where every token looks at every other token and decides which ones matter to it.
- **Class token:** An extra slot added at the front. Its final value is used to give the label.

A Google Research team posted the paper to arXiv on 22 October 2020. Its title says the idea in one line: "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale".

## How does a vision transformer turn a photo into a label?

It runs a few steps in order. The chart shows the path, and the steps below follow it.

*Diagram, From photo to label: A car door photo is cut into 196 tiles, each tile becomes a list of numbers, and a transformer compares them all. The label is read from one extra slot, the class token, after the layers finish.*
1. The photo is cut into square tiles of 16 by 16 pixels.
2. Each tile becomes a list of numbers, with its position added.
3. All the tiles go into the transformer, which compares them.
4. The label is read from the class token, the extra slot.

1. **Cut the picture into patches:** A 224 by 224 pixel photo cut into 16 by 16 squares gives 14 rows of 14, so 196 patches.
2. **Turn each patch into numbers:** A learned step turns every patch into a list of numbers, called its embedding.
3. **Add a position to each patch:** Without a position, the model would not know which tile sat at the top or the bottom of the door.
4. **Add the class token:** One extra learnable slot goes at the front, so the row has 197 items: 196 patches and this slot.
5. **Read the label:** The transformer layers run, and the final value of that slot goes to a small classifier that picks the label.

The middle part is the standard transformer stack, called an encoder: layers of self-attention, each followed by a small per-patch network, repeated. Each attention step lets every patch use information from every other patch.

## Why does it need so much data?

A convolutional neural network, or CNN, is the older image design. It has built-in habits about pictures. It looks at small neighbourhoods of pixels, and the same filter slides across the whole image. The paper calls these built-in assumptions inductive biases, the habits a model starts with before it sees any data.

- **CNN: Built-in habits.** Local looks and one filter reused everywhere, so it learns from fewer images.
- **ViT: Learns the habits.** Only the small per-patch networks inside each layer (called MLPs) are local. Attention looks across the whole picture, so layout must be learned from data.
- **Small data: The CNN often wins.** With few labelled images, the missing habits show, and a convolutional network usually does better.

> "We show that this reliance on CNNs is not necessary and a pure transformer applied directly to sequences of image patches can perform very well on image classification tasks." (Dosovitskiy et al., 2020, abstract)
>
> "We find that large scale training trumps inductive bias." (Dosovitskiy et al., 2020, Section 1)

The paper's answer is that more data closes the gap. It trained on larger sets, from 14M to 300M images, and found that large scale training beats the built-in habits. On mid-sized sets without strong regularisation (extra rules that stop the model memorising its training photos), a vision transformer trailed ResNets (a well-known family of convolutional networks) of comparable size by a few points.

A worked case: a mid-size insurer has 6,000 labelled damage photos. A plain vision transformer trained only on those photos is a risky bet. A safer route is a vision transformer first trained on a large general image set (pre-training), then trained further on the 6,000 photos (fine-tuning). Test a convolutional network, the older design, on the same photos before you choose.

> **Tip:** Asked whether a vision transformer beats a CNN? Say it depends on the data. Large pre-training data favours the transformer. Small data often favours the CNN.

## What does it cost, and why do patch sizes matter?

Attention compares every token with every other token. The paper makes the point for raw pixels, where each pixel would attend to every other pixel: "With quadratic cost in the number of pixels, this does not scale to realistic input sizes." Cutting the image into patches keeps the token count far below the pixel count. Doubling the number of tokens roughly quadruples the comparisons.

Take a 1024 by 1024 pixel photo cut into 16 by 16 patches. That gives 64 rows of 64, so 4,096 tokens. Each pair of tokens is compared, which is 4,096 times 4,096, about 16.8 million pairs per attention head, per layer.

Halve the patch to 8 by 8 and the tokens become 16,384. That is four times the tokens and sixteen times the pairs, about 268 million. Smaller patches see finer detail, but they cost much more to run.

> **Tip:** Interview tip: cost grows with the square of the token count. Shrinking the patches is a quality lever with a steep bill.

### How would you choose a model for a factory defect camera?

A plant makes steel brackets and has about 300 labelled defect photos a month. The lighting is fixed and the camera angle never changes. The team asks whether to use a vision transformer.

- **Try first: A simple CNN.** Small data and a fixed scene suit the built-in habits. It is the reference to beat.
- **Try next: A vision transformer.** First trained on a large image set, then trained further on the plant's photos.

Measure both on photos the models never saw in training, and check the speed on the hardware the factory really has. Pick the simpler model unless the transformer wins by a margin that matters to the line.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a transformer that reads an image as patches.
- [ ] How a photo becomes a label: patches, numbers, positions, class token.
- [ ] Why it needs data: fewer built-in image habits than a CNN.
- [ ] The cost: attention grows with the square of the number of patches.
- [ ] A test plan: a plain CNN, checked on photos it never trained on.
- [ ] Honest limits: it depends on the data and the hardware.

**Common traps**

- Trap: "A vision transformer is always better than a CNN." Better: It depends on the data size. With little data, a CNN often wins, as the paper found on mid-sized sets.
- Trap: "Smaller patches are free detail." Better: Smaller patches mean more tokens, and attention cost grows with the square of that count.
- Trap: "Each patch is just a pixel." Better: A patch is a block of pixels, turned into a list of numbers and treated as one token.

## Can you spot the mistake?

**Spot the mistake:** A junior engineer wants a vision transformer trained from scratch on 2,000 photos, and says it will beat the CNN. What is wrong?

A. Nothing. More layers always win.
B. A vision transformer trained from scratch on a small set has few built-in image habits, so a CNN is the safer first test.
C. A vision transformer cannot read photos at all.
D. Patches should be one pixel wide.

**Answer:** B. A vision transformer has less built-in knowledge of images, so it needs large data or pre-training. On a small set, test a CNN first.

**Quick check:** A vision transformer with 16 by 16 patches reads a 224 by 224 photo. How many patches come out, not counting the class token?

A. 14
B. 196
C. 256
D. 224

**Answer:** B. 224 divided by 16 is 14, and 14 times 14 is 196. The class token then makes 197 items in the row.

## Can you answer these from memory?

**Flash cards**

- **What is a vision transformer?** A transformer that reads an image as a row of patches, like words.
- **What is a patch?** A small square of the picture, handled as one token.
- **Why does ViT need lots of data?** It has fewer built-in image habits than a CNN, so it learns them from data.
- **What is the class token?** An extra slot whose final value gives the label.
- **Why are small patches costly?** More patches mean more tokens, and attention cost grows with the square of that.
- **Is a ViT always better than a CNN?** No. With small data a CNN often wins. Test both on your photos.

## Likely follow-up questions

- Q442 How do images become tokens? https://designervenkat.online/interview/ai/how-do-images-become-tokens
- Q073 Walk through one decoder block's forward pass. https://designervenkat.online/interview/ai/walk-through-one-decoder-block-forward-pass
- Q075 Code scaled dot-product attention from scratch. https://designervenkat.online/interview/ai/code-scaled-dot-product-attention-from-scratch
- Q071 State-space models (e.g. Mamba) vs transformers? https://designervenkat.online/interview/ai/state-space-models-e-g-mamba-vs-transformers
- Q074 What uses memory at inference time? https://designervenkat.online/interview/ai/what-uses-memory-at-inference-time
