Search

Search papers and lessons

Visual Instruction Tuning

Level 4 · 10 min read

Visual instruction tuning trains a language model on image-grounded questions and answers, so it responds to what the user asks about a picture instead of only describing it.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Neural network basics
  • Linear algebra
  • Post-training
  • Architectures
  • Sign in to see your progress

TL;DR

Visual instruction tuning teaches a model that already understands language to also look at images and respond to what a user asks about them. The training data is made of image, question and answer triples, so the model learns to answer the question rather than just caption the scene. The trick that made it cheap is that a text-only language model wrote most of those answers from a caption and a list of object boxes.

Where you'll see it: in chat assistants that accept a photo and answer questions about it, such as the LLaVA model introduced by the paper this lesson is based on.

Intuition

A caption is not an answer

Picture two assistants shown the same photo of a kitchen. You ask, "Is the milk past its date?" The first one has learned from image-caption pairs. It replies, "A kitchen with a fridge and a bowl of fruit." It has described the picture, which is what it was trained to do. The second one has been taught with examples of questions and answers about pictures. It reads the question, looks for the milk carton and the date, and answers the question.

Vision-language models trained on web image-text pairs are the first kind. They are good at matching a picture to words. What they lack is the habit of treating the text in front of them as an instruction. Instruction Tuning solved that problem for text: a Language Model that only continues text becomes an assistant once it is trained on instructions and good responses. Visual instruction tuning applies the same move to images.

Why it is hard

For text, instruction data is easy to find because people have been writing questions and answers for decades. For images, the data is scarce. Someone has to look at each picture, invent a question, and write a good answer. Doing that for hundreds of thousands of pictures is slow and expensive.

The key observation in the LLaVA paper is that you do not need a model that can see to write good visual instructions. A text-only model can write them if it is given a faithful description of the picture. The description is a set of captions plus a list of objects with their bounding boxes. From that symbolic text, a strong language model can invent questions, detailed descriptions and reasoning chains about the image. The image itself never has to pass through the language model. The visual model is only needed later, when the trained assistant sees real pixels.

flowchart LR
  A[Image] --> B[Captions and object boxes]
  B --> C[Text-only GPT-4 writes questions and answers]
  C --> D[Instruction data: image, question, answer]
  A --> E[Vision encoder]
  E --> F[Projection into word space]
  F --> G[Language model]
  D --> G
  G --> H[Assistant that answers about the image]

The diagram shows the two halves of the method. The left side creates training data without a vision model in the loop. The right side is the trained system, where a vision encoder feeds a language model through a small adapter.

The math

Visual instruction tuning is not a new loss. It is the standard next-token objective of a language model, applied to a sequence that starts with image tokens.

Turning an image into tokens

Let Xv\mathbf{X}_v be an image. A frozen vision encoder gg maps it to a grid of features Zv=g(Xv)\mathbf{Z}_v = g(\mathbf{X}_v). In the LLaVA design, gg is the ViT-L/14 encoder from CLIP, and it stays frozen. A single trainable matrix W\mathbf{W} projects each grid feature into the word-embedding space of the language model:

Hv=W⋅Zv.\mathbf{H}_v = \mathbf{W} \cdot \mathbf{Z}_v.

Each row of Hv\mathbf{H}_v now has the same width as a word embedding, so the image becomes a short sequence of "visual words" that the language model can attend to like any other tokens. Here W\mathbf{W} is a plain linear layer. It is cheap to train, and the authors chose it on purpose to iterate quickly on data.

Sequences with several turns

A conversation about one image has several question and answer turns, (Xq1,Xa1,…,XqT,XaT)(\mathbf{X}_q^1, \mathbf{X}_a^1, \dots, \mathbf{X}_q^T, \mathbf{X}_a^T). The image is placed in the first user turn, either before or after the text, chosen at random. Later turns contain only text. The answers from all turns are treated as the assistant's replies. The model is then trained on all the answer tokens at once.

The probability of the answer given the image and the instruction is a product over answer tokens:

p(Xa∣Xv,Xinstruct)=∏i=1Lpθ ⁣(xi∣Xv,Xinstruct,<i,Xa,<i).p(\mathbf{X}_a \mid \mathbf{X}_v, \mathbf{X}_{\text{instruct}}) = \prod_{i=1}^{L} p_{\boldsymbol{\theta}}\!\left(x_i \mid \mathbf{X}_v, \mathbf{X}_{\text{instruct},\lt i}, \mathbf{X}_{a,\lt i}\right).

Here LL is the length of the answer, xix_i is the ii-th answer token, and the subscript <i\lt i means "all tokens before position ii". The training loss is the negative log of this probability, averaged over the data. Only answer tokens contribute to the loss. The image and the questions are context, not targets.

Two stages

Training runs in two stages with different trainable parameters.

  1. Feature alignment. The vision encoder and the language model are both frozen. Only W\mathbf{W} is trained, on about 595 thousand filtered image-caption pairs. The question is a fixed instruction to describe the image briefly, and the target is the original caption. The projection learns to turn image features into something the frozen language model reads as words.
  2. End-to-end instruction tuning. The vision encoder stays frozen. W\mathbf{W} and the language model weights are both updated on the instruction data.

The point of stage 1 is that the language model is not disturbed while the new adapter is still poor. The point of stage 2 is to teach instruction following with images.

Worked examples

Example
A tiny hand-computed loss

Suppose the answer to a question is two tokens, "red" then "cube", and the model assigns these probabilities:

  1. p(red∣image,question)=0.5p(\text{red} \mid \text{image}, \text{question}) = 0.5
  2. p(cube∣image,question,red)=0.8p(\text{cube} \mid \text{image}, \text{question}, \text{red}) = 0.8

Step 1. The sequence probability is 0.5×0.8=0.40.5 \times 0.8 = 0.4.

Step 2. The negative log likelihood is −ln⁡0.4≈0.916-\ln 0.4 \approx 0.916.

Step 3. Suppose the model had been trained only on captions and assigned p(red)=0.1p(\text{red}) = 0.1 for the first token. The loss on the same answer would be far higher, because the caption model is not used to answering the question that was asked.

Interpretation: the image is in the conditioning context of every answer token, so the gradient pushes the model to ground each word in what it sees. Nothing in the loss is special to images. The grounding comes from the data.

Example
The data mix in the LLaVA paper

The paper collects 158 thousand instruction samples from COCO images. They are split by type.

  1. Conversation samples: 58 thousand.
  2. Detailed description samples: 23 thousand.
  3. Complex reasoning samples: 77 thousand.
  4. Total: 58+23+77=15858 + 23 + 77 = 158 thousand.

Each type answers a different need. Conversation teaches short back-and-forth about what is visible. Detailed description teaches the model to write a long, complete account of a scene. Complex reasoning teaches it to answer "why" and "what would happen" questions by working through steps.

Interpretation: the ablation in the paper shows that the mix matters. On the COCO-based LLaVA-Bench, the full mix scores 85.1 relative to a text-only GPT-4 reference. Removing the detailed and reasoning samples drops it to 73.8 when only conversation data is used, and to 21.5 with no instruction tuning at all.

Tradeoffs and failure modes

SituationProblemBetter choice
Few images but a strong text modelHand-written visual instructions are too slow to collectGenerate instructions from captions and boxes with a text-only model
Need fine detail, such as small text or a brand on a labelA low-resolution encoder or a short visual token budget loses the detailA higher-resolution encoder, or more visual tokens per image
Generated answers copy the teacher's mistakesThe student inherits the teacher's errors and biasesFilter answers, mix in human-written data, and evaluate on held-out human questions
Model says "yes" to things that are not thereThe model treats the image as a "bag of patches" and misses how objects combineMore reasoning-style data, and tests with negative questions
Simple adapter feels too weakA single linear projection may limit what the model can extractA small MLP or a cross-attention resampler

The main failure mode is hallucination. A model trained to answer questions learns that answering is rewarded, so it may answer about objects that are not in the picture. A second failure is over-reliance on the language prior. If the answer is common in text ("the sky is blue"), the model may give it without looking.

The alternative to visual instruction tuning is a system that chains a captioner, a detector and a text model together. That approach needs no end-to-end training, but the parts cannot correct each other, and the interface between them is a lossy text summary.

History

Instruction tuning for text came first. Wei and colleagues showed in 2021 that fine-tuning a large model on many tasks described in natural language improved zero-shot performance on tasks it had not seen. Ouyang and colleagues at OpenAI followed in 2022 with InstructGPT, which used human demonstrations and preference rankings to make a model follow user requests. Open instruction-tuned models that used machine-written data appeared in 2023.

Multimodal models were also developing in parallel. Flamingo and BLIP-2 showed that an image encoder and a language model could be joined with a learned bridge. The LLaVA paper observes that models such as BLIP-2 and OpenFlamingo tend to describe the image rather than follow the user's instruction.

In April 2023, Liu, Li, Wu and Lee published the LLaVA paper and named the approach visual instruction tuning. Its contribution was the data recipe: use a text-only GPT-4 to write instruction data from captions and boxes, then train a small projection plus the language model on it. The paper reports that, trained on about 80 thousand unique images, the model gave reasoning results similar to multimodal GPT-4 on several out-of-domain example images from the GPT-4 report.

Check yourself

QuestionRecall: what is the difference between image-caption training and visual instruction tuning?Show answer

Image-caption training teaches a model to produce a description of an image. Visual instruction tuning teaches it to respond to a user's question or instruction about the image. The second needs data with an instruction, a grounded answer and the image in context.

QuestionComputation: a three-token answer has token probabilities 0.4, 0.5 and 0.5 given the image and the context. What is the negative log likelihood of the answer, to two decimal places?Show answer

The sequence probability is 0.4×0.5×0.5=0.10.4 \times 0.5 \times 0.5 = 0.1. The negative log likelihood is −ln⁡0.1≈2.30-\ln 0.1 \approx 2.30. The loss sums the per-token negative logs, which are −ln⁡0.4≈0.92-\ln 0.4 \approx 0.92, −ln⁡0.5≈0.69-\ln 0.5 \approx 0.69 and −ln⁡0.5≈0.69-\ln 0.5 \approx 0.69, and these add to 2.302.30. Both views give the same number.

QuestionWhy: the LLaVA recipe freezes the vision encoder during both training stages. Why is that a reasonable choice?Show answer

The encoder was already trained to produce useful image features on large data, and the alignment stage needs a stable target for the projection to learn. Freezing it also cuts the number of trainable parameters and the memory cost, which makes training with the limited data and compute of the time practical. The paper trains only the projection in stage 1 and both the projection and the language model in stage 2, and it never updates the encoder.

QuestionApplied: you want an assistant that reads the labels on grocery packaging from a phone photo. Which part of the method would you expect to limit it, and what would you change?Show answer

The limit is visual resolution and detail, not the instruction following. A small encoder input and a short visual token budget can blur small text. You would use a higher-resolution encoder, give the model more visual tokens per image, and add training data with close-up images of text and labelled answers. Checking the output on real label photos would show whether the change worked.

QuestionApplied: a model answers yes when asked whether a bowl contains strawberries, but the bowl holds only yogurt. What does this suggest about the model, and how would you test for it?Show answer

It suggests the model is matching the question to likely objects in the scene instead of checking each object. This is the bag-of-patches failure. You test it with questions that have a no answer, such as asking about an object that is near the target but absent, and you measure how often the model says yes. Adding reasoning-style training data and negative questions is the usual fix.

Sources

  • Liu, Li, Wu, Lee, "Visual Instruction Tuning," 2023. arXiv:2304.08485
  • Ouyang et al., "Training language models to follow instructions with human feedback," 2022. arXiv:2203.02155
  • Wei et al., "Finetuned Language Models Are Zero-Shot Learners," 2021. arXiv:2109.01652