§11.5Vision-and-Text Models & Where to from Here

Part III NLPT pp. 94–100 · ~4 min read

  • contrastive learning
  • multimodal
  • zero-shot learning

Text plus audio was one pairing; vision plus text is the other natural one — and the current research frontier. We’ll look at four models.

VQA, LayoutLM, and DALL·E

  • VQA (visual question answering) extends the QA task to images: answer a question about a picture. Models like LXMERT and VisualBERT pull features from the image (with a ResNet) and fuse them with the question.
  • LayoutLM reads scanned documents (receipts, invoices) using three modalities — text, image, and layout — with a spatially-aware self-attention.
  • DALL·E goes the other direction: text → image. It reuses the GPT architecture autoregressively, treating words and pixels as one long sequence.

CLIP

CLIP is the standout. It has two encoders (one for images, one for text) and is trained by contrastive learning on 400 million image/caption pairs: for each batch, pull the matching image-caption embeddings together (high dot product) and push all the mismatched pairs apart.

text captions →images ↓dogcatcartreedogcatcartreehighlowlowlowlowhighlowlowlowlowhighlowlowlowlowhighcontrastive objective: maximize the diagonal, minimize the rest
CLIP’s contrastive training. Matching image-caption pairs (the diagonal) are pushed to high similarity; all mismatches to low.

That training makes CLIP a flexible zero-shot classifier: to classify an image, embed the candidate class names as text and pick the closest. No fixed label set, no retraining. Point it at a photo of Optimus Prime with classes defined on the fly:

from transformers import CLIPProcessor, CLIPModel
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
texts = ["a photo of a transformer", "a photo of a robot", "a photo of agi"]
inputs = processor(text=texts, images=image, return_tensors="pt", padding=True)
probs = model(**inputs).logits_per_image.softmax(dim=1)   # tensor([[0.9557, 0.0413, 0.0031]])
CLIP zero-shot classification of a photo of Optimus Prime — classes defined purely by text.
CLIP probability
"a photo of a transformer"0.9557
"a photo of a robot"0.0413
"a photo of agi"0.0031
Dotted-underlined cells have explanations — click one.

It (almost) got it right — a photo of AGI, of course. Here are the four vision-and-text models side by side:

Four ways to combine vision and text. Click a cell for detail.
ModalitiesWhat it does
VQAimage + textAnswers questions about an image
LayoutLMtext + image + layoutUnderstands scanned documents
DALL·Etext → imageGenerates images from text
CLIPimage + textZero-shot image classification
Dotted-underlined cells have explanations — click one.

Where to from here?

That’s the end of this excerpt — three chapters covering the transformer’s anatomy (Ch. 3), a full question-answering application (Ch. 7), and its frontiers (Ch. 11). To keep going:

Reinforce what you’ve learned

  • Join a community event — Hugging Face runs open sprints (adding datasets, fine-tuning ASR models, JAX/Flax projects).
  • Build your own project — reimplement a paper, or apply transformers to a domain you care about.
  • Contribute a model to the Transformers library.
  • Blog about it — teaching is the best test of your own understanding.

Thanks for joining the ride through the transformers landscape. You started at self-attention, built a QA system, and arrived at the frontier — scaling, efficiency, and multimodality.

Check yourself: vision + text

1.What does VQA (visual question answering) do?

2.What three modalities does LayoutLM combine, and for what?

3.How does DALL·E generate images from text?

4.How is CLIP trained?

5.How does CLIP perform zero-shot image classification?

6.In the contrastive similarity matrix (images × captions), which cells should be high after training?

6 questions