🚀 BLIP-2, OPT-2.7b,仅预训练版本
BLIP-2模型借助了 OPT-2.7b(一个拥有27亿参数的大语言模型)的能力。它由Li等人在论文 BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models 中提出,并首次在 此代码库 中发布。
免责声明:发布BLIP-2的团队并未为此模型撰写模型卡片,此模型卡片由Hugging Face团队撰写。
✨ 主要特性
- 量化支持:支持使用 bitsandbytes 进行量化,包括8位、fp4、float16以及Safetensors格式。
- 多任务能力:可用于图像描述、视觉问答(VQA)以及类聊天对话等任务。
📚 详细文档
模型描述
BLIP-2由3个模型组成:一个类似CLIP的图像编码器、一个查询变换器(Q-Former)和一个大语言模型。
作者从预训练检查点初始化图像编码器和大语言模型的权重,并在训练查询变换器时保持它们冻结。查询变换器是一个类似BERT的变换器编码器,它将一组“查询令牌”映射到查询嵌入,从而弥合图像编码器和大语言模型的嵌入空间之间的差距。
该模型的目标很简单,即给定查询嵌入和先前的文本,预测下一个文本令牌。

这使得该模型可用于以下任务:
- 图像描述
- 视觉问答(VQA)
- 通过将图像和先前的对话作为提示输入模型,进行类聊天对话
直接使用和下游使用
你可以使用原始模型,在给定图像和可选文本的情况下进行条件文本生成。请查看 模型中心,以查找针对你感兴趣的任务进行微调的版本。
偏差、风险、局限性和伦理考量
BLIP2-OPT使用现成的OPT作为语言模型,它继承了Meta模型卡片中提到的相同风险和局限性。
与其他大语言模型一样,训练数据的多样性(或缺乏多样性)会对我们模型的质量产生下游影响,OPT-175B在偏差和安全性方面存在局限性。OPT-175B在生成多样性和幻觉方面也可能存在质量问题。一般来说,OPT-175B无法避免困扰现代大语言模型的诸多问题。
BLIP2在从互联网收集的图像 - 文本数据集(例如 LAION )上进行了微调。因此,该模型本身可能容易生成同样不适当的内容,或者复制底层数据中固有的偏差。
BLIP2尚未在实际应用中进行测试,不应直接部署到任何应用程序中。研究人员应首先仔细评估该模型在其部署的特定环境中的安全性和公平性。
💻 使用示例
基础用法
在CPU上运行模型
点击展开
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
高级用法
在GPU上以全精度运行模型
点击展开
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", device_map="auto")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt").to("cuda")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
在GPU上以半精度(float16
)运行模型
点击展开
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16, device_map="auto")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
在GPU上以8位精度(int8
)运行模型
点击展开
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", load_in_8bit=True, device_map="auto")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
📄 许可证
本项目采用MIT许可证。