语言: 英语
许可证: MIT
标签:
- 视觉
- 图像到文本
- 图像描述生成
- 视觉问答
流水线任务: 图像文本到文本
推理: 不支持
BLIP-2, Flan T5-xxl, 仅预训练版本
BLIP-2模型,利用了Flan T5-xxl(一种大型语言模型)。
该模型由Li等人在论文BLIP-2: 通过冻结图像编码器和大型语言模型引导语言-图像预训练中提出,并首次发布于此代码库。
免责声明:发布BLIP-2的团队未为此模型编写模型卡片,因此本模型卡片由Hugging Face团队撰写。
模型描述
BLIP-2包含三个模型:一个类似CLIP的图像编码器、一个查询转换器(Q-Former)和一个大型语言模型。
作者从预训练检查点初始化图像编码器和大型语言模型的权重,并保持它们冻结,同时训练查询转换器。查询转换器是一个类似BERT的转换器编码器,它将一组“查询令牌”映射为查询嵌入,从而弥合图像编码器和大型语言模型之间的嵌入空间差距。
模型的目标是简单地预测下一个文本令牌,给定查询嵌入和先前的文本。
这使得模型可用于以下任务:
- 图像描述生成
- 视觉问答(VQA)
- 通过将图像和先前的对话作为提示输入模型,进行类似聊天的对话
直接使用与下游使用
您可以使用原始模型在给定图像和可选文本的情况下进行条件文本生成。请查看模型中心以寻找您感兴趣任务的微调版本。
偏见、风险、限制与伦理考量
BLIP2-FlanT5使用了现成的Flan-T5作为语言模型。它继承了Flan-T5的相同风险和限制:
语言模型,包括Flan-T5,可能被用于有害的语言生成,如Rae等人(2021)所述。Flan-T5不应直接用于任何应用程序,而应在应用前评估安全和公平性问题。
BLIP2在从互联网收集的图像-文本数据集(如LAION)上进行了微调。因此,模型本身可能容易生成类似的不当内容或复制底层数据中的固有偏见。
BLIP2尚未在现实世界应用中进行测试。它不应直接部署在任何应用中。研究人员应首先仔细评估模型在特定部署环境中的安全性和公平性。
伦理考量
此版本仅用于支持学术论文的研究目的。我们的模型、数据集和代码并非为所有下游用途设计或评估。我们强烈建议用户在部署此模型之前评估并解决与准确性、安全性和公平性相关的潜在问题。我们鼓励用户考虑AI的常见限制,遵守适用法律,并在选择用例时采用最佳实践,特别是对于错误或滥用可能显著影响人们生活、权利或安全的高风险场景。有关用例的进一步指导,请参考我们的AUP和AI AUP。
使用方法
有关代码示例,请参阅文档,或根据您的用例参考以下片段:
在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))