CSC3104 · Module 7 of 9

ViTs + Multimodal

Use image–text & transformer-based vision

~57 min to study properly

A Vision Transformer treats an image like a sentence. It cuts a 224×224 image into 16×16 patches — a 14 × 14 grid, so 196 patches — and treats each patch as a word.

Checkpoint · the examiner asks

Why do Vision Transformers need significantly more training data than CNNs to reach peak performance? And how does CLIP perform zero-shot classification?

In one minute

A ViT cuts the image into patches (224/16 → 14×14 = 196 tokens), projects each into an embedding, adds [CLS] and position embeddings, then uses self-attention so any patch can attend to any other. Weak inductive bias means ViTs need lots of data. CLIP learns a shared image–text space with a contrastive loss, enabling zero-shot classification by cosine similarity with text prompts.

Traps examiners look for
  • Forgetting the +1 [CLS] token in the sequence length.
  • Saying ViTs are always better than CNNs — on small data CNN inductive bias usually wins.
  • Thinking CLIP zero-shot needs task-specific training — it only needs class prompts.
Read

Study notes

01An image as a sentence of patches

A Vision Transformer (ViT) reuses the transformer architecture from language models by turning an image into a sequence of tokens:

  1. Cut the image into P × P patches: a 224 × 224 image with 16 × 16 patches gives a 14 × 14 grid = 196 patches.
  2. Flatten each patch (16 × 16 × 3 = 768 values) and multiply by a learned matrix — the patch embedding — to get a D-dimensional token.
  3. Prepend a learnable [CLS] token whose final state is used for classification → sequence length 197.
  4. Add position embeddings so the model knows where each patch came from (otherwise the order of patches would be meaningless to it).

The sequence then passes through standard transformer blocks: multi-head self-attention, an MLP, layer norm and residual connections.

02Self-attention

Each token produces a query (what am I looking for?), a key (what do I contain?) and a value (what do I pass on?) by linear projections.

Attention(Q, K, V) = softmax(QKᵀ / √d) · V

  • QKᵀ scores every pair of tokens; dividing by √d keeps the scores in a range where softmax still has useful gradients.
  • softmax turns each row into weights that sum to 1.
  • Each token's output is a weighted mix of all values.

So a patch of a dog's ear can attend directly to the tail on the other side of the image in one layer — a CNN needs many layers for its receptive field to grow that large. The cost: the attention matrix is N × N, so compute and memory grow quadratically with the number of tokens. Halving the patch size gives 4× the tokens and ~16× the attention cost.

03Inductive bias and data hunger

CNNs have strong built-in assumptions — locality and translation equivariance — so they learn efficiently from modest data. ViTs have weak inductive bias: nothing tells them that neighbouring patches are related; they must learn it from data (position embeddings start random).

Consequences:

  • Trained from scratch on ImageNet-1k alone, early ViTs underperformed CNNs.
  • Pretrained on very large datasets (ImageNet-21k, JFT-300M) or with self-supervision, the flexibility wins: ViTs match or beat CNNs and scale better.
  • In practice you almost always fine-tune a pretrained ViT rather than train one; with little data and no pretraining, a CNN is the safer choice.

Hybrid designs (convolutional stems, Swin Transformer's local windows) put some locality back to get the best of both.

04CLIP and zero-shot classification

CLIP trains two encoders together — an image encoder (ViT or ResNet) and a text encoder — on ~400 million image–caption pairs from the web. The contrastive loss: in a batch of N pairs, maximise cosine similarity for the N matching pairs and minimise it for the N² − N mismatched ones. The result is a shared embedding space for images and text.

Zero-shot classification — no task-specific training:

  1. Write a prompt per class: "a photo of a dog", "a photo of a cat", …
  2. Embed the prompts and the image.
  3. Compute cosine similarities, multiply by a learned scale (≈100) and apply softmax → class probabilities.

Prompt wording matters ("a satellite photo of farmland"), and CLIP inherits biases and blind spots from web data — always evaluate on your own domain before relying on it.

Watch it done

Worked examples

Worked example 1

Counting tokens and attention cost

A ViT uses 384 × 384 images with 16 × 16 patches and a [CLS] token. How many tokens, and how many entries in one attention matrix? Compare with 224 × 224 images.

    Worked example 2

    CLIP zero-shot by hand

    Cosine similarities: dog 0.30, cat 0.25, car 0.10. Logit scale 100. Compute the softmax probabilities.

      Vocabulary

      Key terms

      Vision-language-action models

      Google DeepMind's RT-2 (2023) treated robot actions as text tokens, so a vision-language model could output motor commands. Gemini Robotics (2025) pushed this further: robots following open-ended instructions in scenes they had never seen.

      Practise

      Quick check

      0 of 8 correct so far. Wrong answers come with an explanation — and the buddy can unpack them.

      Q1ViT with 224×224 input and 16×16 patches: sequence length including [CLS]?

      Q2Self-attention cost grows with the number of tokens N as…

      Q3How many patches does a 224×224 image give with 32×32 patches?

      Q4What are position embeddings for in a ViT?

      Q5Why divide QKᵀ by √d?

      Q6Why do ViTs need more data than CNNs?

      Q7CLIP's training objective is…

      Q8Attention cost grows with the number of tokens N as…

      Exam-style

      Written practice

      Short answer5 marks

      Explain how a ViT turns an image into tokens, including the [CLS] token and position embeddings.

      Short answer6 marks

      Why do ViTs need more training data than CNNs, and how does CLIP perform zero-shot classification?

      Long answer12 marks

      A conservation group wants to identify 40 animal species from camera-trap photos but has only 30 labelled images per species. Compare (a) a CNN trained from scratch, (b) fine-tuning a pretrained ViT, and (c) CLIP zero-shot or linear probing, and recommend an approach with an evaluation plan.

      Watch

      Videos

      Fresh picks are chosen by AI every day.
      Hands-on

      ViT tokens & CLIP zero-shot

      4 guided tasks: predict, experiment, explain — then challenge yourself. Plus 2 coding exercises you run right in the browser.

      Start the lab
      SegmentationVideo + Tracking