Search

Search papers and lessons

Visual Instruction Tuning (LLaVA)

Haotian Liu, Chunyuan Li, Qingyang Wu et al. · 2023

LLaVA links a CLIP image encoder to the Vicuna language model with one linear layer, then trains it on GPT-4-written questions and answers about images so it follows visual instructions.

On this page

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

TL;DR

LLaVA (Large Language and Vision Assistant) is a model that can look at a picture and answer questions about it the way a chat assistant answers text questions. It takes a CLIP image encoder, connects it to the Vicuna language model through one trainable linear layer, and trains the combined system on image-grounded conversations. The conversations were written by a text-only GPT-4 from image captions and object boxes, so no human had to write them.

Where you'll see it: in the design of open vision-language assistants that accept an image in a chat window, and as the starting point for connecting a pretrained image encoder to a chat language model with a small connector.

The problem

Before this paper, two kinds of system existed. Large language models such as ChatGPT followed instructions well, but only in text. Vision models were built for one task at a time: a classifier classifies, a detector detects, and the task is fixed by the model's design rather than by a request. Multimodal models like Flamingo and BLIP-2 could take images, but the authors report that BLIP-2 and OpenFlamingo "focus on describing the image, instead of following the user instruction" (their Section 5.1 comparison).

The obstacle was data. Instruction tuning for language models works because there are many thousands of examples of instruction and good answer. For images, the public data is mostly image-caption pairs, such as CC3M (which the paper filters) and LAION. A caption says what is in a picture. It does not say how to answer "what is unusual about this image?" The paper calls the creation of multimodal instruction-following data "time-consuming and less well-defined" when humans do it.

Key idea

Use a text-only GPT-4 as the annotator. A text model cannot see a picture, so the authors describe the picture to it in a form it can read: a few captions, plus a list of object categories with their bounding boxes and locations. Given that text, GPT-4 writes a question and an answer that a viewer of the picture would recognise as correct. Three kinds of data come out of this loop: conversations about the image, detailed descriptions, and questions that need step-by-step reasoning.

Then train the model on those triples with the image attached as input. The architecture is deliberately plain. The image becomes a short sequence of vectors in the language model's embedding space, and the language model simply continues the text. Because the visual part is one linear layer, the authors could iterate on the data quickly, which the paper says was the point of the simple design.

The method

Data

The seed is COCO images. For each image, the pipeline gives GPT-4 the caption and boxes, plus a few hand-written in-context examples (the only human annotation). The paper collects 158K samples: 58K conversations, 23K detailed descriptions and 77K complex-reasoning items. The paper reports that GPT-4 gave higher-quality data than ChatGPT in its early experiments, especially on spatial reasoning.

Architecture

The model has three parts, shown in the diagram below.

flowchart LR
  I[Image X_v] --> E[CLIP ViT-L/14 encoder<br/>frozen]
  E -->|grid features Z_v| P[Linear projection W<br/>trainable]
  P -->|visual tokens H_v| S[Sequence]
  T[Instruction text X_instruct] --> S
  S --> L[Vicuna language model<br/>trainable in stage 2]
  L --> A[Answer X_a]

The image encoder is CLIP's ViT-L/14 (see Learning Transferable Visual Models From Natural Language Supervision (CLIP)). Its grid features are mapped by a single matrix, written W\mathbf{W}, into the language model's word-embedding size:

Hv=W⋅Zv,Zv=g(Xv)\mathbf{H}_v = \mathbf{W} \cdot \mathbf{Z}_v, \qquad \mathbf{Z}_v = g(\mathbf{X}_v)

Here gg is the frozen CLIP encoder, Zv\mathbf{Z}_v is the grid of image features, and Hv\mathbf{H}_v is the sequence of visual tokens that the language model reads as if they were words. The paper notes that more elaborate connectors exist (gated cross-attention in Flamingo, the Q-Former in BLIP-2) and leaves them to future work. The language model is Vicuna, which the authors chose because it had the best instruction following among the public checkpoints they considered.

Training objective

A multi-turn conversation is written as one sequence. The image is placed before or after the first question at random, which the paper says makes the model robust to order. Every answer is treated as an assistant response. The probability of the answer tokens given the image and the instruction is

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}}\big(x_i \mid \mathbf{X}_v, \mathbf{X}_{\text{instruct},<i}, \mathbf{X}_{a,<i}\big)

where xix_i is a token of the answer, θ\boldsymbol{\theta} holds the trainable parameters, and <i<i means "all tokens before position ii". The training loss is the usual next-token cross-entropy, applied only to the answer tokens. The image is never predicted, only conditioned on.

Training happens in two stages.

Stage 1, feature alignment. The authors filter CC3M to about 595K image-caption pairs, using noun-phrase frequency to keep rare concepts. Each caption becomes a single-turn example in which a short "describe the image briefly" question is paired with the original caption. Only W\mathbf{W} is trained; both the CLIP encoder and the language model are frozen. The paper describes this stage as training "a compatible visual tokenizer for the frozen LLM."

Stage 2, end-to-end fine-tuning. The CLIP encoder stays frozen. W\mathbf{W} and the language model are trained on the 158K instruction data, with the chatbot and ScienceQA variants fine-tuned separately.

Worked example: how many optimiser steps

The paper gives batch sizes and epoch counts, so you can count the steps. These are my arithmetic on its numbers, not figures the paper reports.

  1. Stage 1: 595,000 pairs at batch size 128 gives 595,000/128≈4,649595{,}000 / 128 \approx 4{,}649 steps for one epoch.
  2. Stage 2: 158,000 samples at batch size 32 gives 158,000/32=4,937.5158{,}000 / 32 = 4{,}937.5 steps per epoch, so about 4,938 per epoch.
  3. Three epochs of stage 2 gives 3×4,937.5≈14,8003 \times 4{,}937.5 \approx 14{,}800 steps.

Stage 1 therefore takes about a third as many steps as stage 2, but each step updates far fewer parameters.

Results

Multimodal chatbot. On LLaVA-Bench (COCO), the authors take 30 COCO-val images and write 90 questions (three types per image). A GPT-4 judge compares the candidate's answer with a text-only GPT-4 answer that sees the ground-truth captions and boxes, and scores both. The full-data model scores 85.1 relative points, against 21.5 for the same architecture with no instruction tuning. Training on conversation data alone scores 73.8 overall (−11.3 from the full mix), which shows that the detailed-description and reasoning data add to the score.

On the harder LLaVA-Bench (In-the-Wild), with 24 images and 60 questions, LLaVA scores 67.3 ± 2.0 overall and 81.7 on complex reasoning. BLIP-2 scores 38.1 and OpenFlamingo 19.1. The paper describes the gap to BLIP-2 as "+29%"; the difference is 29.2 points on this scale, so that is what the number means here.

ScienceQA. ScienceQA has about 21K multiple-choice questions, split 12,726 / 4,241 / 4,241 for train, validation and test. LLaVA alone reaches 90.92% accuracy, close to the previous best of 91.68% (MM-CoT Large). A text-only GPT-4 with two-shot prompting reaches 82.69%, which the authors compare against GPT-3.5's 75.17%. The best result, 92.53%, comes from using GPT-4 as a judge: when LLaVA and text-only GPT-4 disagree, GPT-4 sees both answers and picks a final one. That is 0.85 points above MM-CoT Large.

Ablations on ScienceQA.

ChangeAccuracyDifference from 90.92
Last-layer CLIP features89.96−0.96
7B model instead of 13B89.84−1.08
No pre-training stage, train on ScienceQA only85.81−5.11

The authors conclude that the pre-training stage matters most of these, and that reasoning-first versus answer-first changes convergence speed more than final accuracy.

Failures. The paper is candid about them. On the In-the-Wild set LLaVA says yes when asked whether strawberry yogurt is in a fridge that holds only yogurt and strawberries. The authors describe this as the model sometimes seeing the image as "a bag of patches".

Why it mattered

The paper's own claim is modest: it is "an initial step in visual instruction tuning". Its concrete contributions are the data pipeline that turns image-caption pairs into instruction data with a text-only model, the demonstration that a linear connector plus that data gives a model that follows visual instructions, and the release of the data, code, and a LLaVA-Bench evaluation. The authors released the data, code and demo, and their conclusion is that the work should "inspire future research on building more capable multimodal models". Nothing here goes beyond what the paper reports.

Reading guide

Read the paper in this order and use these lessons when a section leans on an idea you have not met:

  1. Introduction (Section 1): Training Language Models to Follow Instructions with Human Feedback for instruction tuning of language models, and Vision-Language Model for what a multimodal model is.
  2. Related work (Section 2): Visual Instruction Tuning for how the idea is borrowed from text, and LLaMA: Open and Efficient Foundation Language Models for the open language model the paper builds on.
  3. GPT-assisted data (Section 3): Visual Instruction Tuning for the instruction-following format the data is written in.
  4. Architecture (Section 4.1): Learning Transferable Visual Models From Natural Language Supervision (CLIP) for the image encoder, and Vision-Language Model for the shared-space idea behind the projection.
  5. Training (Section 4.2): Visual Instruction Tuning again for the loss, which is ordinary next-token prediction on answer tokens.
  6. Experiments (Section 5): no new prerequisites beyond the above. The scoring is a GPT-4 judge, and the ScienceQA numbers are accuracies on a fixed split.

Check yourself

QuestionWhy does the paper use a text-only GPT-4 to write training data for an image model, rather than writing the data from the image directly?Show answer
GPT-4 in this pipeline never sees the picture. The authors give it a caption and a list of object categories with bounding boxes, which is a text description of the image. Because GPT-4 is strong at writing questions and answers in text, it can produce varied and reasoned instructions from that description. The data are cheap to generate and do not need human annotators beyond a few seed examples.
QuestionStage 1 of training uses 595,000 image-caption pairs at batch size 128 for one epoch. About how many optimiser steps is that?Show answer
595,000 / 128 ≈ 4,648.4, so roughly 4,649 steps. Since the paper gives no step count, this is arithmetic from its settings, and the number assumes every batch is full.
QuestionIn stage 1, the paper trains only the projection matrix W and freezes both the CLIP encoder and the language model. Why?Show answer
Stage 1 has one job: learn a mapping from CLIP's image features into the word-embedding space of the language model, which the paper calls training "a compatible visual tokenizer for the frozen LLM". Freezing the language model keeps its pre-trained language ability intact while the projection learns to speak its embedding language. The paper does not run the alternative, so the reason is its stated design goal, not a measured result.
QuestionThe ablation says removing pre-training costs 5.11 points on ScienceQA, and the GPT-4 judge adds some points on top of LLaVA. Compare the two gains.Show answer
The GPT-4 judge adds 92.53 − 90.92 = 1.61 points over LLaVA alone, while removing pre-training costs 90.92 − 85.81 = 5.11 points. So in these numbers the pre-training stage matters more than the judge step. The two comparisons are separate experiments on the same benchmark, so the difference is a comparison of two ablations, not a general ranking of the method's parts.
QuestionThe GPT-4 judge is text-only. When would it be a poor choice to decide between LLaVA's answer and another answer?Show answer
When the question depends on visual details that the judge cannot check. The judge sees only the two answers and the question, so if both answers sound plausible it can only pick by reasoning. The paper's yogurt example shows the kind of visual detail LLaVA itself can get wrong, and a text-only judge has no way to catch that. The paper says the judge helped on ScienceQA because some questions with an image as context do not actually need it, so the judge can catch and correct some of LLaVA's errors; that would not carry over to tasks where the image is essential.

Sources

Read the full paper

Haotian Liu, Chunyuan Li, Qingyang Wu et al., 2023. arXiv:2304.08485