语言: 英语
许可证: MIT
标签:
- 视觉
- 图像转文本
- 图像描述生成
- 视觉问答
流水线任务: 图像转文本
复制自: Salesforce/blip2-opt-2.7b
对现有Salesforce模型卡的更新:
添加了处理程序,以便在Hugging Face推理流水线上运行该模型。
输入:
{
"inputs": "<Base64编码图像>",
"prompts": "<此处为提示文本>"
}
输出:
{
"captions": "<生成的图像描述>"
}
BLIP-2,OPT-2.7b,仅预训练版本
BLIP-2模型,利用了OPT-2.7b(一个拥有27亿参数的大型语言模型)。
该模型由Li等人在论文BLIP-2: 通过冻结图像编码器和大型语言模型引导语言-图像预训练中提出,并首次发布于此代码库。
免责声明:发布BLIP-2的团队未为此模型编写模型卡,因此本模型卡由Hugging Face团队编写。
模型描述
BLIP-2由三个模型组成:一个类似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上运行模型
全精度
点击展开
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
)
点击展开
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
)
点击展开
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))