Back to paper library
arXiv 2023ICML 2023Model adaptationPaper notes

BLIP-2

Q-Former debuts

Original paper

BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models

Paper authors

Junnan Li · Dongxu Li · Silvio Savarese · Steven Hoi

Publication
ICML 2023Conference
First submitted to arXiv
arXiv:2301.12597
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{modeltuningblip22023,
  title = {{BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models}},
  author = {Junnan Li and Dongxu Li and Silvio Savarese and Steven Hoi},
  year = {2023},
  eprint = {2301.12597},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2301.12597}
}
On this page

This is the second-generation architecture named BLIP.

The first BLIP tackled a range of multimodal tasks. This second generation focuses on using a relatively small trainable bridge to steer large pretrained models in the direction we want.

Defining the Problem

We continue to focus on the field of Vision-Language Models (VLM).

Current pre-training architectures often rely on large-scale training, which not only requires significant resources but also does not transfer well to every downstream task due to their diverse nature.

If we could directly leverage pre-trained unimodal LLMs and align multimodal features under frozen parameters, wouldn’t it save us the effort of retraining a multimodal model?

With this idea in mind, the authors designed a lightweight query transformer, called Q-Former, as a bridge to extract key features from image encoders and convert them into outputs that the language model can understand.

Is this really possible? Let’s dive in and learn!

Solving the Problem

Model Architecture

This architecture diagram might look a bit complex, so let’s break it down step by step:

Starting from the far left, an image is input and processed by a pre-trained image encoder. The parameters of this encoder are frozen, meaning it only extracts features without participating in training.

Next, that large block on the right is the "Q-Former" module. Its image and text Transformers share self-attention layers, while the image side has a cross-attention layer in every other block. It takes two inputs:

  1. Learned Query Embeddings: These parameters are randomly initialized, with a length set to 32 tokens as described in the paper.
  2. Text Input Describing the Image: This is similar to common language model inputs, consisting of natural language descriptions.

Those shared self-attention layers are initialized from BERT-base; the added cross-attention layers are randomly initialized. Each of the 32 learned queries has 768 dimensions and extracts visual information through cross-attention. Whether queries and text can attend to each other depends on the task-specific mask below.

Phase 1 Pre-Training

In the diagram above, Q represents the Query token, and T represents the Text token. Masked sections are indicated with darker shading.

During Phase 1, Q-Former is combined with a frozen image encoder for image-text pair pre-training, optimizing the following three objective functions:

  1. Image-Text Matching (ITM)

    This objective teaches the queries and text to make fine-grained image-text matches. Gradients update the shared Q-Former, not just an isolated Q-only parameter group.

    The attention mask design, as shown above, allows queries and text tokens to interact. A two-class linear classifier scores each query output, and the matching score is the average of its logits across all queries. A hard negative mining strategy is applied during training to generate more challenging negative samples.

  2. Image-Grounded Text Generation (ITG)

    The generation loss is computed at text outputs, but its gradients also pass through queries and shared modules, teaching the queries to extract useful visual information.

    In ITG, queries can attend to one another but not to text. Each text token can attend to every query and previous text, preventing leakage from future target words.

  1. Image-Text Contrastive Learning (ITC)

    Alongside matching and image-grounded generation, we also want the query features explicitly aligned with the text features. All three objectives share Q-Former parameters; they differ in masks and prediction targets.

    Compute each query output’s similarity to the text [CLS] representation and use the maximum as the image–text similarity. Other pairs in the batch supply negatives.

    To prevent information leakage, a unimodal self-attention mask is used, ensuring that queries and text tokens cannot directly observe each other.

Phase 2 Pre-Training

As shown in the diagram, Q-Former is connected to a frozen large language model (LLM) to leverage its language generation capabilities. During this stage, the following two architectures can be employed for generation:

  • Decoder-Only Architecture: Pre-trained with a language modeling loss, requiring the LLM to generate text conditioned on visual features.
  • Encoder-Decoder Architecture: Pre-trained with a prefix language modeling loss. The input text is split into a prefix and a suffix. The prefix text, along with visual features, is sent to the LLM encoder, while the suffix text serves as the generation target for the LLM decoder.

In this design, the query embeddings ZZ output by Q-Former are linearly projected to the same dimension as the LLM’s text embeddings. These projected query embeddings are prepended to the input text embeddings, serving as soft visual prompts.

Since Q-Former has been pre-trained to extract visual features relevant to language, it acts as an information bottleneck, effectively filtering out irrelevant visual details and reducing the burden on the LLM for vision-language alignment.

Pre-Training Datasets

  • The pre-training mixture follows BLIP and contains 129 million images from COCO, Visual Genome, CC3M, CC12M, SBU, and 115 million selected from LAION400M.
  • A CapFilt-style pipeline produces captions for web images:
    • BLIP-large generates ten captions per image.
    • CLIP ViT-L/14 ranks these synthetic captions together with the original web caption by image-text similarity. The top two are kept, and one is sampled at each pre-training step.

Pre-Training Setup

  • Pre-Training Steps:

    • Feature Learning Stage: 250,000 steps.
    • Generative Learning Stage: 80,000 steps.
  • Batch Sizes:

    • Feature Learning: 2320 (ViT-L) or 1680 (ViT-g).
    • Generative Learning: 1920 (OPT) or 1520 (FlanT5).
  • Compute Efficiency:

    • Training on a single 16 × A100 (40G) machine:
      • Feature Learning Stage (Largest Model): Less than 6 days.
      • Generative Learning Stage (Largest Model): Less than 3 days.
  • Optimizer and Learning Rate:

    • Optimizer: AdamW (β1=0.9\beta_1 = 0.9, β2=0.98\beta_2 = 0.98, weight decay 0.050.05).
    • Learning Rate Schedule: Cosine decay with a peak learning rate of 1×10−41 \times 10^{-4} and linear warmup for 2000 steps; the minimum learning rate in the second stage is 5×10−55 \times 10^{-5}.
  • Image Processing:

    • Image Size: 224×224224 \times 224.
    • Augmentation: Random cropping and horizontal flipping.

Discussion

Without saying much, let’s first look at the results.

Compared to previous state-of-the-art models, BLIP-2 achieves higher performance while significantly reducing the number of trainable parameters required during VLM pre-training.

Zero-shot VQA

The authors first tested BLIP-2’s zero-shot capability. The table above shows BLIP-2’s performance on various VQA tasks, including VQAv2, GQA, and OK-VQA.

Evaluation Setup:

  • The prompt for the OPT model was "Question: {} Answer:".
  • The prompt for the FlanT5 model was "Question: {} Short answer:".
  • Beam search was used to generate answers, with a beam width of 5, and a length penalty of −1-1 to encourage concise answers.

On VQAv2 test-dev, the best BLIP-2 configuration scores 65.0 versus Flamingo80B’s 56.3, an 8.7-percentage-point gain. The 54× comparison refers to 10.2B versus 188M trainable parameters in the paper’s overview, not total model size; the largest BLIP-2 configuration still has about 12.1B total parameters. Flamingo’s GQA entry is blank, leaving GQA without a direct comparison.

Flamingo80B scores higher on OK-VQA. The authors hypothesize that its larger Chinchilla language model provides more relevant knowledge.

First-stage representation learning is important. Omitting it substantially worsens zero-shot VQA, and the OPT setup deteriorates during training. The paper calls this catastrophic forgetting, but the LLM weights remain frozen: the directly observed result is degraded task performance with the learned visual prompts, not forgetting caused by updating LLM weights.

Finetuned VQA

The authors fine-tuned BLIP-2 on annotated VQA data, freezing the LLM’s parameters and fine-tuning only the Q-Former and image encoder.

The question tokens also interact with Q-Former’s queries so that it can extract image features relevant to the question. In Table 4, BLIP-2 ViT-g OPT6.7B has the highest VQAv2 result among the open-ended generation models shown; the closed-ended BEIT-3 classifier still scores higher.

Image Captioning

The authors evaluated BLIP-2's performance on the image captioning task and compared it with other methods.

For the image captioning task, the initial prompt was "a photo of," and the model was trained using language modeling loss. During fine-tuning, the LLM's parameters were frozen, and only the Q-Former and image encoder parameters were updated.

On the COCO Karpathy test set, BLIP-2's best CIDEr score is 145.8, above the other methods in Table 3; OFA has the highest BLEU@4 at 43.9. After COCO fine-tuning, BLIP-2 also has the table's highest overall CIDEr and SPICE on zero-shot transfer to the NoCaps validation set.

Image-Text Retrieval

Retrieval does not require the LLM. The first-stage image encoder and Q-Former are fine-tuned on COCO, evaluated there and transferred zero-shot to Flickr30K. ITC similarity selects 128 candidates, followed by ITM reranking.

BLIP-2 leads the listed methods on most R@1 measures for Flickr30K zero-shot retrieval and COCO fine-tuned retrieval. Other methods lead or tie on some R@5 and R@10 measures.

The authors then examined the contribution of each loss in the table below. ITC and ITM directly learn image-text similarity. For the COCO fine-tuned retrieval objective in Table 6, adding ITG raises image-to-text R@1 from 84.5 to 85.4 and text-to-image R@1 from 67.2 to 68.3. This supports the idea that ITG helps queries extract text-relevant visual features.

Image-to-Text Generation Showcase

The authors showcased examples of various zero-shot image-to-text capabilities, including visual knowledge reasoning, visual commonsense reasoning, visual conversation, and personalized image-to-text generation.

Failure Cases

The three examples show inaccurate knowledge, an incorrect reasoning path, and outdated information. The model attributes someone else's quote to Einstein, overlooks Canadian winter weather, and mistakes an iPhone 14 for an iPhone 11. These are problems for future work.

Conclusion

The idea behind Q-Former is exciting: a small set of query tokens can guide a large language model toward multimodal tasks through a trainable bridge. The 32 queries are only its visual bottleneck; training updates the Q-Former module, which has about 188M parameters, rather than just those vectors. “Lightweight” is relative to the large frozen backbones.

If we want to address problems in specific domains, this approach allows us to transfer the capabilities of large pre-trained models toward our desired objectives.