ByteNoteByteNote

字节笔记本

2026年2月18日

BLIP:Salesforce 开源图像描述生成模型

API中转
¥120

本文介绍 BLIP(Bootstrapping Language-Image Pre-training),一个由 Salesforce 开发的统一视觉-语言理解和生成模型。该模型在图像描述生成任务上表现优异,是图像理解领域的重要开源模型。

模型简介

BLIP 是 Salesforce Research 团队开发的视觉-语言预训练模型,采用引导式语言-图像预训练方法,实现了统一的视觉理解和生成能力。该模型在 COCO 数据集上进行预训练,使用 ViT-Large 作为图像编码器 backbone。

在 Hugging Face 上,该模型已获得 1.45k+ 点赞,是图像描述生成任务中最受欢迎的模型之一。

核心能力

  • 图像描述生成: 为输入图像生成自然语言描述
  • 视觉问答: 回答关于图像内容的问题
  • 图像-文本检索: 根据文本检索相关图像,或根据图像检索相关文本
  • 统一架构: 单一模型同时支持理解和生成任务
  • 高质量预训练: 在 COCO 等大型数据集上预训练

模型架构

BLIP 采用多模态 Transformer 架构:

  • 图像编码器: ViT-Large(Vision Transformer)
  • 文本编码器: BERT 风格的 Transformer
  • 文本解码器: 因果语言模型(用于生成任务)
  • 多任务训练: 同时优化多个视觉-语言任务

快速开始

使用 Transformers 库

python
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import requests

# 加载模型和处理器
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")

# 加载图像
url = "https://example.com/image.jpg"
image = Image.open(requests.get(url, stream=True).raw)

# 生成描述
inputs = processor(image, return_tensors="pt")
out = model.generate(**inputs, max_new_tokens=50)
caption = processor.decode(out[0], skip_special_tokens=True)

print(caption)
# 输出: "a dog sitting on the grass with a frisbee in its mouth"

批量处理

python
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import torch

processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")

# 批量处理多张图片
images = [Image.open(f"image_{i}.jpg") for i in range(5)]
inputs = processor(images, return_tensors="pt", padding=True)

outputs = model.generate(**inputs, max_new_tokens=50)
captions = [processor.decode(out, skip_special_tokens=True) for out in outputs]

for caption in captions:
    print(caption)

性能表现

COCO 图像描述基准

模型BLEU-4METEORCIDErSPICE
BLIP-Large39.730.4129.423.0
BLIP-Base36.829.0122.121.8

视觉问答(VQA)

在 VQAv2 数据集上:

  • BLIP-Large: 78.2%(测试集)

应用场景

  • 无障碍辅助: 为视障用户描述图像内容
  • 内容审核: 自动理解图像内容进行分类
  • 搜索引擎优化: 为图片自动生成 alt 文本
  • 社交媒体: 自动为上传的图片生成描述
  • 相册管理: 基于内容描述自动整理照片
  • 教育应用: 帮助儿童学习图像内容描述

使用示例

场景 1:为网站图片生成 alt 文本

python
import requests
from bs4 import BeautifulSoup
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image

processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")

def generate_alt_text(image_url):
    image = Image.open(requests.get(image_url, stream=True).raw)
    inputs = processor(image, return_tensors="pt")
    out = model.generate(**inputs, max_new_tokens=50)
    return processor.decode(out[0], skip_special_tokens=True)

# 为网页所有图片生成 alt 文本
response = requests.get("https://example.com")
soup = BeautifulSoup(response.text, 'html.parser')

for img in soup.find_all('img'):
    if not img.get('alt'):
        caption = generate_alt_text(img['src'])
        img['alt'] = caption
        print(f"Generated alt: {caption}")

场景 2:相册自动标签

python
import os
from pathlib import Path

image_dir = Path("./photos")
captions_file = open("captions.txt", "w")

for image_path in image_dir.glob("*.jpg"):
    image = Image.open(image_path)
    inputs = processor(image, return_tensors="pt")
    out = model.generate(**inputs, max_new_tokens=50)
    caption = processor.decode(out[0], skip_special_tokens=True)
    
    captions_file.write(f"{image_path.name}: {caption}\n")
    
captions_file.close()

模型变体

模型参数量适用场景
blip-image-captioning-base385M资源受限环境
blip-image-captioning-large466M高质量需求
blip-vqa-base385M视觉问答任务
blip-vqa-large466M高质量 VQA

相关资源

引用

bibtex
@inproceedings{li2022blip,
  title={BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation},
  author={Li, Junnan and Li, Dongxu and Xiong, Caiming and Hoi, Steven},
  booktitle={ICML},
  year={2022}
}

许可证

本模型采用 BSD-3-Clause 许可证。

分享: