Ugg chop picture into rocks, feed to word-brain
For nine winters — since the AlexNet hunt of 2012 — every best eye-machine was a CNN (Convolutional Neural Network): a machine that slide a little stamp-rock over picture, seeing only close-neighbor pixels. Tribe swear eye MUST work this way. Then Google shaman show new trick: a plain Transformer — same word-brain that turn English grunt into French grunt — beat CNN at seeing! One catch: first feed it a MOUNTAIN of picture-food.
The magic scroll called "An Image is Worth 16×16 Words." That IS the recipe: chop picture into squares, call each one a word-token, feed to word-brain. Machine named ViT (Vision Transformer). After 2021, ViT push CNN off the throne.
Why Ugg no feed every pixel
Why not make every pixel a token? A picture have pixel. Word-brain self-attention — every token stare at every other — cost grow like : that is BILLION look-looks per layer. Fire run out! So Ugg squish the line by chopping into patch-rocks.
How Ugg make the rocks
Patch tokenisation — chop picture into non-overlap squares.
For with : rocks. Flatten each rock to numbers, then squish to numbers with learn-matrix :
Ugg WARN: patch-making is NOT "just flatten"! It a learned linear projection — same as one Conv2d, kernel , stride . For ViT-B, , so almost square.
[CLS] token — glue one special learn-rock at the front: . Line now long. Ugg WARN: never forget the ! At the end, push the final CLS-rock through one linear layer for the class-guess. Could instead global-average-pool all rocks — within 1%. ViT keep CLS because BERT did.
Position embedding — brain no know which rock sit where, so add place-marker. Choices: none (drop ~3%), 1D learned (number rocks in reading-order, learn one vector each — THIS is ViT), 2D learned (tiny gain). Remember: ViT use 1D markers even though picture is 2D. Brain figure out the 2D by itself.
The word-brain block
ViT use Pre-Norm: clean numbers with LayerNorm BEFORE each step, add old numbers back after.
MSA = multi-head self-attention (rocks look at rocks). MLP = think-step: . The fat-middle is standard.
ViT herd — burn the Base into memory: ViT-Base has , , MLP , , ~86M. ViT-Large ~307M, ViT-Huge ~632M. Always MLP , per-head . "ViT-B/16" = Base with rocks.
Count the stones (exam love this)
Per block: attention each → ; MLP up+down → . **Per block .**
Match slide's . LN, CLS, markers too small to count.
The four quiz stones
- **Q1: bigger picture ** (patch stay 16). Rocks , sequence UP ~2.25×. Params ~SAME. But markers now wrong size — bilinear-interpolate them, no new params.
- **Q2: smaller patch ** (picture stay). Rocks , sequence UP exactly 4×. Params shrink tiny ( smaller); attention compute ~4× more, memory ~16× more.
- **Q3: more layers .** Sequence SAME. Params UP linear — each block , so M.
- **Q4: more heads ** ( fixed). Ugg WARN, trickiest! Sequence SAME, params SAME. Heads only SPLIT the space (); projections stay . Heads no multiply stones! Most tribe grunt this wrong.
Why ViT so hungry
Inductive bias — a machine's built-in belief. CNN born with two: locality (nearby matter most) and translation equivariance (move object, still same object). Small food → belief HELP; big food → belief LIMIT. ViT born with almost none — every rock stare at every rock from layer 1. Small food (ImageNet): ViT overfit, ResNet win. HUGE food (JFT-300M, 300 million picture): ViT win by far, and transfer better everywhere.
Do ViT see like CNN?
Raghu shaman use CKA (Centered Kernel Alignment) — a sameness-ruler for machine-thoughts. CNN thoughts climb a slow ladder (texture → part → whole object); ViT thoughts stay UNIFORM — even layer 1 look across the whole picture. So attention distance is both small AND large in every ViT layer: global eyes right away, but no multi-scale ladder, making plain ViT weak at dense job (segment, detect). Bonus: the CLS-rock learn to stare at the true object (DINO grow this into free segment-maps), and 1D markers end up self-similar in 2D — sameness high near, fade radial.
Swin — patch the two ViT holes
ViT two weakness: (1) too costly at big picture; (2) no ladder of scales. Swin (Shifted Window) Transformer fix both.
Idea 1 — Windowed attention. Chop rocks into windows (), attention only INSIDE each window. Cost drop — **linear in **. On grid: 9.8M → 154k look-looks, ~64× cheaper. Ugg WARN: "linear" true only when window stay fixed.
Idea 2 — Shifted windows. Pure windows never talk across walls. Fix: every OTHER block, slide the grid by . Block = W-MSA (aligned), block = SW-MSA (shifted) — now rocks from old-separate windows share one, so news travel across walls. Ragged edge windows fixed by cyclic shift + masked attention (wrap rocks around, mask the wrap). The cyclic shift IS how the shift get done — not a separate thing.
Idea 3 — Patch merging. ViT keep same rock-count forever; Swin shrink like CNN. Take a block, glue features, project — half the space, double the channel. Four stages: at at at at — a real feature-pyramid, so Swin drop into detection and segment machines.
Ugg remember
- **ViT = chop picture into patch-tokens, add [CLS] + 1D learned markers, run Pre-Norm Transformer blocks, read the CLS-rock for the guess.**
- **Per block ** ( attention MLP). ViT-B: , MLP , , ~86M. Sequence .
- Quiz Q4: more heads → params UNCHANGED. Heads split , no multiply. The trap tribe always fall in.
- ViT hungry: small food ResNet win, JFT-300M big food ViT win — ViT own no locality bias to lean on.
- **Swin = window attention () + shifted windows (cross-talk) + patch merging (4-stage pyramid).** ViT to pretrain, Swin to detect and segment.