---
title: "Zero-shot image-to-text generation with BLIP-2"
description: "BLIP-2 connects a frozen pre-trained image encoder (ViT) to a frozen LLM (OPT or Flan-T5) through a lightweight Querying Transformer (Q-Former), the only trainable component."
url: "https://swe.quest/articles/zero-shot-image-to-text-generation-with-blip-2-6253370fee81620e"
canonical: "https://huggingface.co/blog/blip-2"
source: "Hugging Face"
published: "2023-02-15"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["computer-vision","llms","machine-learning","python"]
---

# Zero-shot image-to-text generation with BLIP-2

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-02-15, 7 min read, How-to. Original: [https://huggingface.co/blog/blip-2](https://huggingface.co/blog/blip-2)

## Summary

BLIP-2 connects a frozen pre-trained image encoder (ViT) to a frozen LLM (OPT or Flan-T5) through a lightweight Querying Transformer (Q-Former), the only trainable component. This cuts trainable parameters and pre-training cost while reaching state-of-the-art results on visual-language tasks. Q-Former is pre-trained in two stages. The first uses image-text contrastive, image-grounded generation and image-text matching losses. The second feeds its query outputs to the LLM as a visual prefix under a causal LM loss. Code examples with Hugging Face Transformers show zero-shot image captioning, prompted captioning, visual question answering with a "Question: Answer:" template, and chat-style prompting, which must stay within the 512-token context.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Computer vision](https://swe.quest/tags/computer-vision.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Hugging Face Transformers basics
- PyTorch
- transformer architectures

## Keywords

BLIP-2, Q-Former, Hugging Face Transformers, OPT, Flan-T5, ViT, Salesforce Research, LAVIS

## Related posts

- [A Dive into Vision-Language Models](https://swe.quest/articles/a-dive-into-vision-language-models-1c708a70976bf2ac.md): Hugging Face · 2023-02-03 · Hard · 18 min read · Computer vision, Model training, Machine learning, LLMs
  Vision-language models pair an image encoder and a text encoder with a strategy for fusing the two, and the pre-training objective determines which downstream tasks they handle well.
- [Vision Language Models Explained](https://swe.quest/articles/vision-language-models-explained-92cc71897cd36570.md): Hugging Face · 2024-04-11 · Medium · 8 min read · Computer vision, LLMs, Model training, Evaluation, Python
  Vision language models combine an image encoder, a projector that aligns image features with text embeddings, and a text decoder that generates text from both inputs.
- [PaliGemma – Google's Cutting-Edge Open Vision Language Model](https://swe.quest/articles/paligemma-google-s-cutting-edge-open-vision-language-model-eb2d13e0b9bcd3ec.md): Hugging Face · 2024-05-14 · Medium · 11 min read · Computer vision, LLMs, Model training, Inference & serving, Python
  PaliGemma is Google's open vision-language model family that pairs a SigLIP-So400m image encoder with a Gemma-2B text decoder through a linear projector.
- [SigLIP 2: A better multilingual vision language encoder](https://swe.quest/articles/siglip-2-a-better-multilingual-vision-language-encoder-713e22379218069c.md): Hugging Face · 2025-02-21 · Medium · 11 min read · Computer vision, Model training, Machine learning
  SigLIP 2 is Google's new family of multilingual vision-language encoders that extends SigLIP's sigmoid loss with extra training objectives.
- [SmolVLM - small yet mighty Vision Language Model](https://swe.quest/articles/smolvlm-small-yet-mighty-vision-language-model-c85d5a6cd246b728.md): Hugging Face · 2024-11-26 · Hard · 12 min read · Computer vision, Model training, Inference & serving, LLMs, Evaluation
  SmolVLM is a 2B-parameter open vision language model built on the Idefics3 architecture with a SmolLM2 1.7B backbone.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
