🚀 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团队编写。
✨ 主要特性
- 多任务处理:可用于图像描述、视觉问答和类聊天对话等任务。
- 架构创新:通过Querying Transformer(Q-Former)桥接图像编码器和大语言模型的嵌入空间。
📚 详细文档
模型描述
BLIP-2由3个模型组成:一个类似CLIP的图像编码器、一个查询变换器(Q-Former)和一个大语言模型。
作者从预训练检查点初始化图像编码器和大语言模型的权重,并在训练查询变换器时保持它们冻结。查询变换器是一个类似BERT的变换器编码器,它将一组“查询令牌”映射到查询嵌入,从而弥合图像编码器和大语言模型嵌入空间之间的差距。
该模型的目标很简单,即根据查询嵌入和之前的文本预测下一个文本令牌。

这使得该模型可用于以下任务:
- 图像描述
- 视觉问答(VQA)
- 通过将图像和之前的对话作为提示输入模型进行类聊天对话
直接使用和下游使用
你可以使用原始模型在给定图像和可选文本的情况下进行条件文本生成。请查看模型中心以查找针对你感兴趣的任务进行微调的版本。
偏差、风险、局限性和伦理考量
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许可证。