Search

Search papers and lessons

InstructBLIP: General-purpose Vision-Language Models via Instruction Tuning

Wenliang Dai, Junnan Li, Dongxu Li et al. · 2023

InstructBLIP tunes a pretrained BLIP-2 model on 26 vision-language datasets written as instructions, and lets the query transformer read the instruction, so the image features depend on the task.

On this page

  • Colour = topic mix
  • Neural network basics
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

InstructBLIP takes a pretrained BLIP-2 model, which pairs a frozen image encoder and a frozen language model through a small trainable bridge, and tunes only the bridge, using 13 of 26 public vision-language datasets rewritten as natural-language instructions. Its main change is that the bridge, the Q-Former, also reads the instruction, so the visual features it passes on depend on the task. Trained on 13 datasets, it reaches state-of-the-art zero-shot results on 13 held-out datasets, ahead of BLIP-2 and of the larger Flamingo models.

Builds on: BLIP-2: Bootstrapping Language-Image Pre-training, Instruction Tuning · Leads to: Vision-Language Model

Why it mattered

Instruction tuning had already made text-only language models follow new tasks described in plain words. Vision-language models were less flexible: most were built for one task family, and the instruction-tuning step had been studied little for images. The paper states this gap directly: vision-language instruction tuning "remains under-explored."

The work gave the field a concrete, open recipe. The authors released a family of InstructBLIP models through the LAVIS library, with two language-model backbones (FlanT5 and Vicuna), so other groups could run, test and extend the same method. Its evaluation style, which holds some datasets out of training and scores zero-shot, is a direct way to ask whether a vision-language model generalises.

The problem

Two problems came up before this paper.

The first is that putting many vision-language tasks into one training mix does not by itself give a general model. The paper's own experiment shows that multitask training without instructions does not generalise well to unseen datasets and tasks. The model learns the training formats, not the idea of following a request.

The second is that a visual encoder that ignores the request produces the same picture features for every question. BLIP-2 feeds the language model a fixed set of visual vectors, whatever the instruction asks. If the question is "what colour is the bag?" and the next one is "what does the sign say?", the model gets the same summary of the image both times. The paper calls this an instruction-agnostic approach and proposes to change it.

How it works

A trained InstructBLIP model answers a question about an image in the following steps. The training run uses the same pieces, plus a loss on the answer.

  1. Start from a BLIP-2 checkpoint. It has three parts: a frozen image encoder (the paper uses a ViT-g/14 model), a Q-Former that is a small Transformer with learnable query vectors, and a frozen language model. The paper uses four variants: FlanT5-XL (3B), FlanT5-XXL (11B), Vicuna-7B and Vicuna-13B.
  2. Turn each dataset into instructions. Every task gets 10 to 15 hand-written instruction templates. A caption dataset gets templates such as "Briefly describe the content of the image." A question-answering dataset gets templates that wrap the question. One example is then one image, one instruction and one target answer.
  3. Trace one example. Take a photo of a brown dog on a beach, the instruction "Briefly describe the content of the image.", and the target "A brown dog on a beach." The image goes through the frozen image encoder and produces a list of patch features.
  4. Let the Q-Former read the instruction. The Q-Former has KK learnable query vectors. The instruction tokens are fed into its self-attention layers together with the queries, so the queries can take on the task. The queries then cross-attend to the image features. The output is KK vectors that carry the image content the instruction asks about.
  5. Project into the language model. A linear layer maps the KK output vectors into the language model's embedding space. The language model reads them as a sequence of soft visual tokens, placed before the instruction text.
  6. Generate and score the answer. The language model is trained to produce the target tokens. The loss is the usual language-modelling loss on those tokens, so the model learns to predict "A brown dog on a beach."
  7. Balance the datasets. Training samples come from the 13 held-in datasets with probabilities that grow with the square root of dataset size, so small datasets are not drowned out by large ones.
  8. Update only the Q-Former. The image encoder and the language model are frozen throughout. Only the Q-Former is finetuned during instruction tuning.
flowchart LR
  I["Image"] --> E["Frozen image encoder"]
  T["Instruction text"] --> Q["Q-Former (trainable), queries + instruction"]
  E --> Q
  Q --> P["Linear projection"]
  P --> L["Frozen language model"]
  T --> L
  L --> A["Answer tokens"]

The math

Step 4 in symbols: instruction-aware queries

Let Q∈RK×d\mathbf{Q} \in \mathbb{R}^{K \times d} be the KK learnable query embeddings, let V\mathbf{V} be the frozen image-encoder features, and let T\mathbf{T} be the instruction tokens. The Q-Former returns

Z=QFormerϕ(Q,V,T),Z∈RK×d.\mathbf{Z} = \mathrm{QFormer}_\phi(\mathbf{Q}, \mathbf{V}, \mathbf{T}), \qquad \mathbf{Z} \in \mathbb{R}^{K \times d}.

The index ϕ\phi denotes the Q-Former's trainable weights. The word "instruction-aware" means the input T\mathbf{T} is part of the function, not only the image. The paper's own comparison, in the ablation below, shows that removing this input lowers scores on every dataset in the ablation table.

Step 5 in symbols: projection to the language model

A linear map W\mathbf{W} sends each output vector to the language model's embedding size:

H=W Z.\mathbf{H} = \mathbf{W}\,\mathbf{Z}.

The rows of H\mathbf{H} act as KK soft tokens prepended to the instruction. The language model is frozen, and the paper finetunes only the Q-Former, so only the weights ϕ\phi receive gradients.

Step 6 in symbols: the training loss

Let y=(y1,…,yN)\mathbf{y} = (y_1, \dots, y_N) be the target answer tokens. The standard language-modelling loss is the negative log-likelihood of the answer, given the visual tokens and the instruction:

L(ϕ,W)=−∑i=1Nlog⁡pLM(yi∣y<i, H, T).\mathcal{L}(\phi, \mathbf{W}) = -\sum_{i=1}^{N} \log p_{\text{LM}}\big(y_i \mid y_{\lt i},\, \mathbf{H},\, \mathbf{T}\big).

Here pLMp_{\text{LM}} is the frozen language model's next-token distribution, and y<iy_{\lt i} is the answer prefix before token ii.

Step 7 in symbols: balanced sampling

For DD datasets with sizes S1,…,SDS_1, \dots, S_D, the probability of drawing a sample from dataset dd is

pd=Sd∑i=1DSi.p_d = \frac{\sqrt{S_d}}{\sum_{i=1}^{D} \sqrt{S_i}}.

Taking the square root flattens the difference between large and small datasets. The paper then adjusts a few weights by hand, lowering the weight of the multiple-choice A-OKVQA and raising that of open-ended OKVQA.

Example
Balanced sampling with two datasets

Suppose one dataset has S1=100S_1 = 100 samples and another has S2=400S_2 = 400 (illustrative sizes, not from the paper).

  1. Square roots: 100=10\sqrt{100} = 10 and 400=20\sqrt{400} = 20.
  2. Sum: 10+20=3010 + 20 = 30.
  3. Probabilities: p1=10/30=1/3p_1 = 10 / 30 = 1/3 and p2=20/30=2/3p_2 = 20 / 30 = 2/3.
  4. Compare with uniform mixing over all samples, which would give 100/500=0.2100 / 500 = 0.2 and 400/500=0.8400 / 500 = 0.8.

Interpretation: the small dataset gets about 0.330.33 of the draws instead of 0.20.2, and the large dataset gets 0.670.67 instead of 0.80.8. The gap between them shrinks, which is the point of the scheme.

Inference by ranking candidates (paper section 2.5)

For tasks with a fixed set of answers, such as classification or multiple choice, the model does not generate freely. For each candidate answer cc in a candidate set C\mathcal{C}, the model computes its log-likelihood, and the prediction is the candidate with the highest value:

y^=arg⁡max⁡c∈Clog⁡pLM(c∣H,T).\hat{y} = \arg\max_{c \in \mathcal{C}} \log p_{\text{LM}}\big(c \mid \mathbf{H}, \mathbf{T}\big).

The paper uses this ranking for datasets including ScienceQA, IconQA, HatefulMemes and the multiple-choice part of A-OKVQA. It also widens the positive and negative labels for binary tasks into several verbalisers, such as "yes" and "true", to match how words occur in text.

Results

The main zero-shot comparison is on held-out datasets the model never trained on. The table gives the paper's reported numbers for four held-out datasets. A "-" marks a dataset where Flamingo-80B has no score in the paper.

ModelTextVQAVizWiziVQAScienceQA (image context)
Flamingo-80B35.031.640.7-
BLIP-2 (FlanT5-XL)43.129.840.454.9
InstructBLIP (FlanT5-XL)46.632.753.170.4

The paper reports that the FlanT5-XL InstructBLIP model, which has about 4B parameters, beats the 80B Flamingo on all six datasets they share, with an average relative gain of 24.8%. Against its own backbone, BLIP-2 FlanT5-XL, it reports an average relative gain of 15.0% over the held-out set. On MSRVTT-QA, the largest of its video gains, the paper reports up to 47.1% relative improvement over the previous best. In Table 1 that is 25.6 for the FlanT5-XXL variant against 17.4 for Flamingo-80B.

Ablations. Table 2 of the paper removes one component at a time and reports scores on the held-in and held-out sets.

VariantHeld-in averageScienceQA (image context)iVQA
FlanT5-XL, full94.170.453.1
FlanT5-XL, no instruction-aware features89.863.447.5
FlanT5-XL, no data balancing92.666.051.1
Vicuna-7B, full100.860.552.2
Vicuna-7B, no instruction-aware features98.955.236.8
Vicuna-7B, no data balancing98.859.450.3

Removing instruction-aware features costs the most, and the paper says the drop is largest on tasks needing spatial reasoning (ScienceQA) and temporal reasoning (iVQA). Removing data balancing causes uneven training, with different datasets peaking at very different steps.

Instruction tuning versus multitask training. With the same FlanT5-XL backbone and the same settings, the paper compares instruction tuning with plain multitask training. The two perform similarly on held-in datasets. On unseen held-out datasets, multitask training stays about where BLIP-2 was, while instruction tuning improves. The paper concludes that the instruction format, not the mere mixing of tasks, drives the gain in zero-shot generalisation.

Limits the paper states or shows. The image resolution stays at 224 by 224 during instruction tuning and the image encoder stays frozen, which keeps training cheap but limits fine detail. The authors say the model inherits hallucination and bias from its frozen language model, and they do not recommend deployment without a safety and fairness check for the specific use. Several held-out sets come from datasets close to held-in tasks, so the zero-shot gains are strongest on tasks the model has seen in some form. The balancing weights are partly set by hand.

Reading guide

The paper is short, and its sections map onto the lessons a reader needs.

Check yourself

QuestionWhich two components of BLIP-2 are frozen during InstructBLIP instruction tuning, and which part is trained?Show answer

The image encoder and the language model are frozen. The Q-Former is the part that is finetuned.

QuestionIn the balanced sampling scheme, a dataset of 900 samples and one of 100 samples are mixed. What is the sampling probability of the 900-sample dataset?Show answer

The square roots are 900=30\sqrt{900} = 30 and 100=10\sqrt{100} = 10, which sum to 4040. The probability for the large dataset is 30/40=0.7530 / 40 = 0.75. Uniform mixing would give 0.90.9, so the square-root rule lowers its share.

QuestionWhy does the paper make the Q-Former read the instruction, and what does the ablation show?Show answer

BLIP-2 gives the language model the same visual features whatever the task. Letting the Q-Former read the instruction lets it extract the image content the task needs. The ablation shows that removing this lowers the held-in average from 94.194.1 to 89.889.8 for FlanT5-XL, and lowers iVQA from 52.252.2 to 36.836.8 for Vicuna-7B.

QuestionA model is evaluated on datasets it trained on and on datasets it never saw. Why does the paper hold out datasets, and what is the risk of reading the held-in score as a measure of generalisation?Show answer

Held-out datasets test zero-shot generalisation, which is the claim of the paper. Held-in scores measure fit to the training mix, which can be high even when the model has only memorised the formats of those datasets. The risk is reading held-in results as generalisation; the held-out results are the evidence for that claim, and even they are strongest on tasks close to ones seen in training.

Sources

Read the full paper

Wenliang Dai, Junnan Li, Dongxu Li et al., 2023. arXiv:2305.06500