模型简介
模型特点
模型能力
使用案例
🚀 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团队撰写。
✨ 主要特性
- 多任务支持:可用于图像描述、视觉问答(VQA)以及类聊天对话等任务。
- 架构创新:通过查询变换器(Q-Former)桥接图像编码器和大语言模型的嵌入空间。
📚 详细文档
模型描述
BLIP-2由3个模型组成:一个类似CLIP的图像编码器、一个查询变换器(Q-Former)和一个大语言模型。
作者从预训练检查点初始化图像编码器和大语言模型的权重,并在训练查询变换器时保持它们不变。查询变换器是一个类似BERT的Transformer编码器,它将一组“查询令牌”映射到查询嵌入,从而弥合图像编码器和大语言模型的嵌入空间之间的差距。
该模型的目标很简单,即根据查询嵌入和之前的文本预测下一个文本令牌。
这使得该模型可用于以下任务:
- 图像描述
- 视觉问答(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上运行模型
全精度
# pip install accelerate
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))
半精度(float16
)
# pip install accelerate
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))
8位精度(int8
)
# pip install accelerate bitsandbytes
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许可证。








