🚀 BLIP-2,仅预训练的Flan T5-xxl
BLIP-2模型借助了Flan T5-xxl(一种大语言模型)的能力。该模型由Li等人在论文BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models中提出,并首次在此仓库发布。
免责声明:发布BLIP-2的团队并未为此模型撰写模型卡片,此模型卡片由Hugging Face团队撰写。
🚀 快速开始
本模型可用于给定图像和可选文本的条件文本生成任务。你可以在模型中心查找针对你感兴趣的任务进行微调后的版本。
✨ 主要特性
- 多模型架构:BLIP-2由3个模型组成,分别是类似CLIP的图像编码器、查询变压器(Q-Former)和大语言模型。
- 跨模态能力:能够用于图像描述、视觉问答(VQA)以及通过将图像和之前的对话作为提示输入模型进行类似聊天的对话等任务。
📚 详细文档
模型描述
BLIP-2由3个模型构成:一个类似CLIP的图像编码器、一个查询变压器(Q-Former)和一个大语言模型。
作者从预训练的检查点初始化图像编码器和大语言模型的权重,并在训练查询变压器时保持它们冻结。查询变压器是一个类似BERT的变压器编码器,它将一组“查询令牌”映射到查询嵌入,从而弥合图像编码器和大语言模型的嵌入空间之间的差距。
该模型的目标很简单,即在给定查询嵌入和先前文本的情况下预测下一个文本令牌。

直接使用和下游使用
你可以使用原始模型进行给定图像和可选文本的条件文本生成。查看模型中心以寻找针对你感兴趣的任务进行微调后的版本。
偏差、风险、局限性和伦理考量
BLIP2-FlanT5使用现成的Flan-T5作为语言模型,它继承了Flan-T5相同的风险和局限性:
根据Rae等人(2021年)的研究,包括Flan-T5在内的语言模型有可能被用于有害的语言生成。在未事先评估特定应用的安全性和公平性问题之前,不应直接在任何应用中使用Flan-T5。
BLIP2在从互联网收集的图像 - 文本数据集(例如LAION)上进行了微调。因此,该模型本身可能容易生成不适当的内容,或者复制底层数据中固有的偏差。
BLIP2尚未在现实世界的应用中进行测试,不应直接部署到任何应用中。研究人员应首先仔细评估该模型在其部署的特定环境中的安全性和公平性。
💻 使用示例
基础用法
在CPU上运行模型
点击展开
import requests
from PIL import Image
from transformers import BlipProcessor, Blip2ForConditionalGeneration
processor = BlipProcessor.from_pretrained("Salesforce/blip2-flan-t5-xxl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl")
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-flan-t5-xxl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", 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
)
点击展开
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xxl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", 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
)
点击展开
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xxl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", 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许可证。