Foundations to Frontier
Tests
F. NLP and Large Language Models › F13 Multimodal Models

Vision-language models: CLIP and LLaVA

Vision-language models connect images and text. CLIP trains an image encoder and a text encoder so that matching image-caption pairs have similar embeddings, using a contrastive loss over large batches of web pairs. The result supports zero-shot classification by comparing an image with captions such as 'a photo of a dog', and image-text retrieval. LLaVA-style models go further: a projector maps the image encoder's patch features into a language model's token space, so the model can answer questions about images. This page covers contrastive pre-training, zero-shot use, the LLaVA recipe (projector alignment, then visual instruction tuning), resolution and token costs, and failure modes. In the exercises you write the symmetric InfoNCE loss, zero-shot classification and recall@k. You then train linear image and text encoders contrastively on synthetic pairs from 200 concepts, with 20000 pairs at each batch size. Zero-shot accuracy reached 0.86 with batches of 4 and 0.94 with 32 or 256. Image-to-caption retrieval among 1000 rose from 0.56 to 0.71. A temperature of 1 instead of 0.07 lowered zero-shot accuracy to 0.89.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Teaching pictures and words to meet

Show a child many pictures with captions and they learn which words go with which pictures. CLIP learns the same way from hundreds of millions of image-caption pairs: it turns images and texts into points in one shared space and is rewarded when each picture sits closest to its own caption rather than the other captions in the batch. Afterwards it can label pictures it was never trained to classify, just by comparing them with written descriptions. The demo shows the matching scores between four images and four captions.

Multimodal lab
image 11.000.000.000.00
image 20.001.000.000.00
image 30.000.010.990.00
image 40.000.000.000.99
a red dronea cat on a sofaa runway at nighta robot arm
Each row is a softmax over captions at temperature 0.07. Training pushes the diagonal (matching pairs) up and the rest down; a low temperature makes the model confident in its best match.
Why engineers care

Vision-language models let robots and drones follow instructions about what they see, and let engineers search image archives with plain-language queries.

📝 Quick check (Level 0)
0/3 answered
  1. 1. CLIP is trained on…
  2. 2. Zero-shot classification compares an image with…
  3. 3. LLaVA connects an image encoder to…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • CLIP (2021): ViT or ResNet image encoder + transformer text encoder; normalised embeddings; symmetric InfoNCE with learned temperature; 400M web pairs; batch 32768. SigLIP replaces softmax with a pairwise sigmoid loss.
  • Zero-shot: embed prompts ‘a photo of a [class]’ for each class (prompt ensembling helps), pick the most similar; also image-text retrieval and a frozen backbone for other models.
  • LLaVA (2023): CLIP ViT-L/14 at 336 px gives 576 patch features; a linear or MLP projector maps them into the LLM embedding space; stage 1 trains only the projector on captions; stage 2 instruction-tunes projector and LLM on visual conversations.
  • Variants: Flamingo (cross-attention into a frozen LLM), BLIP-2 (Q-Former compresses to 32 queries), Qwen-VL and InternVL (dynamic resolution tiles), PaliGemma.
  • Costs: visual tokens scale with resolution squared; high-resolution tiling multiplies them.
  • Failure modes: counting, spatial relations, small text, hallucinated objects (POPE benchmark), bias from web data.
📝 Quick check (Level 1)
0/3 answered
  1. 1. LLaVA-1.5 feeds the LLM about…
  2. 2. SigLIP replaces the softmax with…
  3. 3. A known VLM weakness:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Using vision-language models

Exercise 1.

  • The symmetric InfoNCE loss.
  • Zero-shot classification by nearest text embedding.
  • Retrieval recall@k.

Practice.

  • open_clip and Hugging Face transformers for CLIP and SigLIP; LLaVA, Qwen2.5-VL, InternVL, PaliGemma for visual question answering.
  • Use prompt templates matching your domain (‘an aerial photo of …’) and ensemble several.
  • For domain data, fine-tune with a contrastive loss or train a linear probe on frozen embeddings.
  • Choose resolution by task: small text and fine details need more visual tokens.
  • Evaluate hallucination explicitly (POPE-style yes/no probes).
import torch, open_clip
model, _, preprocess = open_clip.create_model_and_transforms("ViT-B-32", pretrained="laion2b_s34b_b79k")
tok = open_clip.get_tokenizer("ViT-B-32")
img = model.encode_image(preprocess(image).unsqueeze(0)); txt = model.encode_text(tok([f"an aerial photo of a {c}" for c in classes]))
img, txt = img / img.norm(dim=-1, keepdim=True), txt / txt.norm(dim=-1, keepdim=True)
probs = (100 * img @ txt.T).softmax(dim=-1)
🐍 Exercise 1: contrastive loss and zero-shot use
Write the symmetric InfoNCE loss, zero-shot classification and recall@k.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A library for CLIP models:
  2. 2. For domain images, adapt prompts such as…
  3. 3. Small text in images needs…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

InfoNCE. For a batch of B pairs with normalised embeddings, logits s_ij = u_i·v_j / τ; the loss is the average cross-entropy of picking the matching caption for each image and the matching image for each caption. It is a lower bound on mutual information that tightens with more negatives: each positive competes with B − 1 others in each direction.

Temperature. Cosine similarities lie in [−1, 1]; dividing by a small τ spreads logits so the softmax can be confident. A large τ compresses differences and weakens gradients from hard negatives; CLIP learns τ and clamps 1/τ at 100.

Batch size. More negatives per step give a better estimate of the contrast, especially for hard negatives, but at a fixed number of pairs seen a larger batch means fewer optimisation steps; gains flatten once negatives are plentiful.

From alignment to generation. A frozen aligned image encoder gives features already related to language; LLaVA’s projector only needs to map them into the LLM’s input space, which a small amount of caption data can do before instruction tuning.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Each positive competes with…
  2. 2. A large temperature…
  3. 3. At fixed data, larger batches mean…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do batch size and temperature affect contrastive image-text training, measured by zero-shot classification and retrieval?

History.

  • A. Radford et al., “Learning transferable visual models from natural language supervision” (CLIP, 2021).
  • X. Zhai et al., “Sigmoid loss for language image pre-training” (SigLIP, 2023).
  • H. Liu et al., “Visual instruction tuning” (LLaVA, 2023); “Improved baselines with visual instruction tuning” (LLaVA-1.5, 2024).

Exercise 2. Synthetic pairs: 200 concepts; each pair = concept mean + instance detail; ‘images’ (64 dimensions) and ‘captions’ (48 dimensions) are different noisy linear views; captions describe the detail only partly.

  • Training: linear encoders to 32 dimensions, normalised; symmetric InfoNCE; Adam; 20000 pairs seen at each batch size; τ = 0.07 unless stated.
  • Zero-shot: classify 1000 test images among 200 classes using one class-mean caption per class. Retrieval: image-to-caption R@1 among 1000 test pairs.
encoders zero-shot accuracy retrieval R@1
untrained 0.002 0.001
batch 4 (5000 steps) 0.864 0.562
batch 32 (625 steps) 0.942 0.662
batch 256 (78 steps) 0.942 0.706
batch 256, τ = 1 0.886 0.686
  • Contrastive alignment made zero-shot classification work from nothing.
  • Batch 4 gave too few negatives: retrieval, which must separate instances of the same concept, suffered most.
  • Batch 32 and 256 tied on zero-shot at the same number of pairs seen; 256 was better at retrieval on seeds 0 and 1 (0.706 vs 0.662, 0.719 vs 0.713) but worse on seed 2 (0.714 vs 0.731).
  • Temperature 1 weakened the contrast and lost 6 points of zero-shot accuracy.
  • Seeds 1 and 2 (zero-shot): batch 4 0.905 and 0.909, batch 32 0.939 and 0.943, batch 256 0.931 and 0.950, τ = 1 0.867 and 0.879.
  • Caveat: linear encoders on synthetic linear data; real CLIP benefits from huge batches partly because real negatives are diverse and hard.
🐍 Exercise 2 (research): how do batch size and temperature affect contrastive image-text training, measured by zero-shot classification and retrieval?
Train linear image and text encoders with InfoNCE at different batch sizes and temperatures; measure zero-shot accuracy and retrieval.

Vision-language models in AI, robotics and aerospace

  • Robotics: CLIP features ground language in perception for open-vocabulary detection and instruction following (CLIPort, RT-2).
  • Earth observation: remote-sensing CLIP variants (RemoteCLIP, GeoRSCLIP) enable text search of satellite archives.
  • Inspection: VLMs describe defects in inspection imagery, with human verification because of hallucination risk.

References

  1. A. Radford et al. (2021). “Learning transferable visual models from natural language supervision.” ICML.
  2. X. Zhai, B. Mustafa, A. Kolesnikov and L. Beyer (2023). “Sigmoid loss for language image pre-training.” ICCV.
  3. H. Liu, C. Li, Q. Wu and Y. J. Lee (2023). “Visual instruction tuning.” NeurIPS.
  4. Y. Li et al. (2023). “Evaluating object hallucination in large vision-language models.” EMNLP (POPE).