🚀 BLIP-2, Flan T5-xl,仅预训练版本
BLIP-2模型借助了Flan T5-xl(一个大语言模型)的能力。该模型由Li等人在论文BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models中提出,并首次在此仓库中发布。
声明:发布BLIP-2的团队并未为此模型撰写模型卡片,此模型卡片由Hugging Face团队撰写。
✨ 主要特性
- 多任务处理:可用于图像描述、视觉问答以及类似聊天的对话等任务。
- 模型架构:由类似CLIP的图像编码器、查询变换器(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-xl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl")
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-xl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl", 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-xl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl", 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-xl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl", 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许可证。