Saral Shiksha Yojna
Courses/Computer Vision

Computer Vision

CSE471
Prof. Makarand Tapaswi + Prof. Charu SharmaSpring 2025-264 credits

ViT Pipeline, Scaling, and Swin

NotesStory
Unit 7 — Vision Transformers (ViT)

Eyes That Read Pixels Like Words

For 9 years — from AlexNet in 2012 to 2021 — every state-of-the-art vision model was a convolutional neural network. The local-receptive-field, weight-sharing, translation-equivariant inductive bias of convolutions was treated as a *truth* about how vision must work. There were occasional attempts to swap in self-attention, but they always lost to ResNets.

Then in late 2020, a paper from Google appeared with a startling claim: the convolutional inductive bias is not necessary. A plain Transformer — the same architecture used for translating English to French — could match or beat the best CNNs on ImageNet, *provided you fed it enough data*.

The paper was titled "An Image is Worth 16×16 Words" — and that's literally the recipe. Cut the image into patches. Treat each patch as a token. Feed the sequence to a standard Transformer encoder. That's it.

The model is called ViT — Vision Transformer (Dosovitskiy et al., ICLR 2021). And from 2021 onward, ViT replaced CNNs as the default vision backbone for nearly everything that operates at scale.

This unit covers two things: the ViT architecture itself, and Swin Transformer — a hierarchical variant that fixes ViT's main weakness.

Why "pixels as a sequence" doesn't work

The very first question the lecture asks: can we just unroll an image into a sequence of pixels and feed it to a Transformer?

A RGB image has pixels. A Transformer's self-attention is in sequence length. That's *billion* attention scores per layer. Computationally impossible.

So you need to compress the sequence. The simplest possible compression: chunk the image into non-overlapping patches and treat each patch as one token.

The ViT architecture

Patch tokenisation

Given input , patch size (typically ):

Each patch is flattened into a -dim vector and linearly projected to dimensions:

For : . So is — the patch projection happens to be roughly square (ViT-B was sized to make these match).

The [CLS] token

Following BERT, ViT prepends a special learnable token at position 0:

Input sequence:
Total length:

After the Transformer, the final hidden state of is used for classification — pass it through a single linear layer to get class logits.

The lecture asks the natural question: *why not just average all patch tokens?* The answer is yes, you can — a global-average-pooled version of ViT works similarly. But the original paper used because BERT did, and the convention stuck.

Position embedding choices

Your lecture explicitly asks: 1D, 2D, or none?

  • None — drop position info entirely. Result: ViT becomes a bag of patches. Accuracy drops ~3% on ImageNet (Transformers can do a lot without explicit position info).
  • 1D learned — assign each patch a 1D position index (raster scan), learn an embedding for each. This is what ViT actually uses. Simple, works well.
  • 2D learned — separate row and column embeddings. Marginal gain over 1D in the original paper.

Memorise: ViT uses 1D learned position embeddings, despite images being 2D. The Transformer figures out the 2D structure on its own.

The Transformer block

Same as the 2017 original, with one tweak — Pre-Norm:

The "Simple notation!" slide flags this — you should be able to write the block in 2 lines from memory.

The MLP is . The 4× expansion ratio is standard ( for ViT-B).

The ViT family

The "Herd of Vision Transformers" slide names the variants. Memorise these:

| Model | | | MLP | | Params | | --- | --- | --- | --- | --- | --- | | ViT-Tiny | 12 | 192 | 768 | 3 | ~5M | | ViT-Small | 12 | 384 | 1536 | 6 | ~22M | | ViT-Base | 12 | 768 | 3072 | 12 | ~86M | | ViT-Large | 24 | 1024 | 4096 | 16 | ~307M | | ViT-Huge | 32 | 1280 | 5120 | 16 | ~632M |

Consistent pattern: MLP size = , and is the per-head dimension. *ViT-B/16* means ViT-Base with patches.

Parameter calculation — exam favourite

For one Transformer block ():

  • Attention: each .
  • MLP: up () + down () → .
  • **Total per block: .**

For all layers:

Patch embedding (one-time):

**Total: M** — matches the slide's .

The Quiz — this WILL be on your exam

The lecture has an explicit quiz slide. What happens to parameters and sequence length when …

Q1 — Resolution $224 \to 336$ (patch fixed at 16)

  • Old . New .
  • **Sequence length: increases ().**
  • Parameters: ~unchanged. Transformer weights don't depend on . *Subtlety:* learned PEs are , sized to the old length. Fix: bilinear interpolation of the PEs to the new grid — no new parameters.

Q2 — Patch size $32 \to 16$ (resolution fixed)

  • Old . New . **Sequence length: .**
  • Parameters: slightly change. Patch embedding shrinks from to (factor of 4). The rest is unchanged. Dominant effect: more attention compute, more attention memory.

Q3 — Layers $8 \to 12$

  • Sequence length: unchanged.
  • **Parameters: increase linearly in .** Each block adds ; going adds M.

Q4 — Heads $1 \to 8$ (D constant) — the trickiest

In MHA, is partitioned across heads: each has . Projections are regardless of .

  • Sequence length: unchanged (heads don't affect tokens).
  • Parameters: unchanged. The surprising answer most students miss.

The exam line: *the number of heads doesn't change the parameter count of MSA — it only changes how the -dim space is partitioned for parallel attention computation.*

The data hunger — ViT's defining property

ViT's central finding: at small data scales (ImageNet-only), ResNets win. At large data scales (JFT-300M, 300 million images), ViT wins by a wide margin.

The reason: CNNs come with built-in inductive biases — *locality* (a feature depends mainly on its neighbours) and *translation equivariance* (moving an object doesn't change what it is). These priors help when data is small. When data is large, the priors *limit* the model — it can't learn task-specific structure that violates them.

ViT has no spatial inductive bias beyond the patch grid. Every patch can attend to every other patch from layer 1. With small data, this looks like overfitting. With huge data, it allows richer relationships than CNNs.

"ViTs transfer better than ResNets" comes from this scaling: pretrain on JFT-300M, transfer to ImageNet (+18 other tasks), ViTs win across the board.

Do ViTs see like CNNs?

Raghu et al. (NeurIPS 2021) asked: do ViTs act like CNNs from scratch? Or do they develop novel representations?

The analysis uses CKA — Centered Kernel Alignment — a similarity metric for representations. Two findings:

  • CNN representations evolve gradually. Early layers are texture-y, middle layers are part-y, late layers are object-y — a clear hierarchy.
  • ViT representations are remarkably uniform across layers. Even early layers attend globally; the representation doesn't have a CNN-like feature pyramid.

The attention distance slide makes this concrete. In CNNs, early-layer receptive fields are tiny (); in ViTs, even layer 1 attention spans the full image in many heads.

This is both a strength (ViT captures global context immediately) and a weakness (ViT lacks a multi-scale feature pyramid, making vanilla ViT worse for dense prediction tasks like segmentation and detection).

What the [CLS] token learns

Through training, the token learns to attend most strongly to semantically meaningful regions — the object, the salient parts. This emergent "object-localising" behaviour is what DINO amplified into actual segmentation maps without any segmentation labels.

Position embeddings are self-similar

A striking finding: take ViT's learned 1D position embeddings and compute cosine similarity between the embedding at position and all other positions. You get a 2D pattern centred at with high similarity decreasing radially. The position embeddings were 1D-learned with no 2D structure imposed — yet ViT figured out the 2D layout on its own from training.

Swin Transformer — fixing ViT's weaknesses

ViT has two acknowledged weaknesses:

1. Quadratic complexity in sequence length. At high resolution, explodes and self-attention becomes prohibitive. 2. No hierarchical features. Detection and segmentation work best with multi-scale features (FPN). ViT just has one resolution throughout.

Swin Transformer (Liu et al., ICCV 2021 Best Paper) fixes both. The name is Shifted Window Transformer.

Idea 1 — Windowed self-attention

Instead of letting every patch attend to every other, divide patches into non-overlapping windows (typically ) and compute self-attention only within each window.

  • Standard ViT: over the whole image.
  • Window attention: — **linear in ** because is constant.

For a patch grid: full attention costs M scores per layer; windowed at costs k. **About cheaper.**

Idea 2 — Shifted windows

Pure window attention has an obvious flaw: tokens in different windows never talk. The fix is elegant. In *alternate* Transformer blocks, **shift the window grid by pixels**.

  • Block : W-MSA — windows aligned to .
  • Block : SW-MSA — windows shifted by .
  • Block : W-MSA — aligned again.

After the shift, patches that were in different windows in the previous block now share a window. Information propagates across original window boundaries. The shift produces some edge windows of irregular size; Swin handles this with cyclic shift + masked self-attention — patches are cyclically wrapped to maintain regular window sizes, with attention masks blocking the wrap-around computations.

Idea 3 — Hierarchical patch merging

Standard ViT keeps the same number of tokens throughout. Swin progressively downsamples like a CNN feature pyramid:

Stage 1: patches at channels
Stage 2: patches at channels
Stage 3: patches at channels
Stage 4: patches at channels

Patch merging at the start of each new stage: take a block of patches, concatenate their features, linearly project . Halves spatial dims and doubles channels — *exactly like a strided convolution in a CNN*.

Result: Swin produces a multi-scale hierarchical feature pyramid like ResNet/FPN, making it a drop-in replacement for CNNs in detection (Mask R-CNN with Swin) and segmentation pipelines.

Swin vs ViT — one line

| | ViT | Swin | | --- | --- | --- | | Attention scope | Global (all patches) | Local windows + shifted alternation | | Complexity | | | | Feature hierarchy | Single resolution throughout | 4-stage pyramid (like ResNet) | | Best for | Classification, contrastive pretraining | Dense prediction (detection, segmentation) | | When to use | Large-scale pretraining target | Backbone for downstream perception tasks |

What you carry into the exam

The "16×16 words" insight and the patch-token compression. The ViT pipeline in 3 lines: linear-project flattened patches → -dim tokens → prepend , add 1D learned PEs → stack Pre-Norm Transformer blocks. The MLP's shape. ViT-B specs: , MLP = 3072, 12 heads, , ~86M params. The per-block formula and the worked total of 85.5M. The four quiz answers — *especially Q4's counter-intuitive parameters-unchanged*. The data-scaling story (ImageNet small → ResNet wins; JFT-300M big → ViT wins). The Raghu CKA findings — ViT representations are uniform across layers; CNN's are hierarchical. The attention-distance plot. Why 's attention localises objects. Why 1D PEs end up self-similar in 2D. Swin's three innovations — window self-attention (linear in ), shifted windows (cross-window communication), hierarchical patch merging (4-stage pyramid). The trade-off table.

You now know exactly what was inside SigLIP, what DINO was pretrained on, and what the family of "Vision Transformer Advances" was modifying.