模型简介
模型特点
模型能力
使用案例
🚀 mBLIP BLOOMZ-7B
mBLIP BLOOMZ-7B是一个多语言视觉大语言模型,可用于图像描述、视觉问答等任务,支持96种语言。
🚀 快速开始
你可以使用原始模型在零样本设置下,根据图像和提示文本进行条件文本生成,或者对其进行微调以用于下游应用。在微调时,我们强烈建议对大语言模型应用LoRA,并使用bf16作为数据类型,因为标准的fp16可能会导致NaN损失。
查看我们的仓库以获取用于训练和微调此模型的代码。使用批量输入时,请使用左填充!
✨ 主要特性
- 多语言支持:mBLIP可在96种语言中用于图像描述、视觉问答等任务。
- 高效引导:通过特定的训练方式,实现多语言视觉大语言模型的高效引导。
📦 安装指南
文档未提及具体安装步骤,可参考我们的仓库获取相关代码。
💻 使用示例
基础用法
在CPU上运行模型
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Gregor/mblip-bloomz-7b")
model = Blip2ForConditionalGeneration.from_pretrained("Gregor/mblip-bloomz-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 = "Describe the image in German."
inputs = processor(raw_image, question, return_tensors="pt")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
高级用法
在GPU上以不同精度运行模型
全精度
# pip install accelerate
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Gregor/mblip-bloomz-7b")
model = Blip2ForConditionalGeneration.from_pretrained("Gregor/mblip-bloomz-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 = "Describe the image in German."
inputs = processor(raw_image, question, return_tensors="pt").to("cuda")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
半精度 (bfloat16
)
# pip install accelerate
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Gregor/mblip-bloomz-7b")
model = Blip2ForConditionalGeneration.from_pretrained("Gregor/mblip-bloomz-7b", torch_dtype=torch.bfloat16, 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 = "Describe the image in German."
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.bfloat16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
8位精度 (int8
)
⚠️ 重要提示
论文结果仅对大语言模型权重使用int8,而此代码将所有权重加载为int8。我们发现这会导致结果稍差,但目前HuggingFace不支持仅对部分模型部件使用int8。
# pip install accelerate bitsandbytes
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Gregor/mblip-bloomz-7b")
model = Blip2ForConditionalGeneration.from_pretrained("Gregor/mblip-bloomz-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 = "Describe the image in German."
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.bfloat16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
4位精度 (int4
)
⚠️ 重要提示
论文结果仅对大语言模型权重使用int4,而此代码将所有权重加载为int8。我们发现这会导致结果稍差,但目前HuggingFace不支持仅对部分模型部件使用int4。
# pip install accelerate bitsandbytes
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Gregor/mblip-bloomz-7b")
model = Blip2ForConditionalGeneration.from_pretrained("Gregor/mblip-bloomz-7b",
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=False,
bnb_4bit_compute_dtype=torch.bfloat16,
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 = "Describe the image in German."
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.bfloat16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
📚 详细文档
模型描述
mBLIP是一个BLIP - 2模型,由3个子模型组成:视觉Transformer(ViT)、查询Transformer(Q - Former)和大语言模型(LLM)。
Q - Former和ViT均由英文BLIP - 2检查点(blip2 - flan - t5 - xl)初始化,然后使用多语言任务混合与多语言大语言模型(bloomz - 7b1)重新对齐。
这使得该模型可用于以下任务:
- 图像描述
- 视觉问答(VQA)
支持96种语言。
支持语言
mBLIP在以下96种语言上进行了训练:
af, am, ar, az, be, bg, bn, ca, ceb, cs, cy, da, de, el, en, eo, es, et, eu, fa, fi, fil, fr, ga, gd, gl, gu, ha, hi, ht, hu, hy, id, ig, is, it, iw, ja, jv, ka, kk, km, kn, ko, ku, ky, lb, lo, lt, lv, mg, mi, mk, ml, mn, mr, ms, mt, my, ne, nl, no, ny, pa, pl, ps, pt, ro, ru, sd, si, sk, sl, sm, sn, so, sq, sr, st, su, sv, sw, ta, te, tg, th, tr, uk, ur, uz, vi, xh, yi, yo, zh, zu
直接使用和下游使用
你可以使用原始模型在零样本设置下,根据图像和提示文本进行条件文本生成,或者对其进行微调以用于下游应用。在微调时,我们强烈建议对大语言模型应用LoRA,并使用bf16作为数据类型,因为标准的fp16可能会导致NaN损失。
查看我们的仓库以获取用于训练和微调此模型的代码。使用批量输入时,请使用左填充!
偏差、风险、限制和伦理考虑
虽然mBLIP理论上可以处理多达100种语言,但实际上,当使用英语、德语、西班牙语等高资源语言进行提示时,我们期望能获得最佳结果。
mBLIP继承了用于初始化它的模型的风险、限制和偏差。mBLIP尚未在实际应用中进行测试,不应直接部署到任何应用程序中。研究人员应首先仔细评估该模型在特定部署环境中的安全性和公平性。
如何使用
有关代码示例,请参考BLIP - 2 文档。
🔧 技术细节
文档未提及足够详细的技术实现细节。
📄 许可证
本项目采用MIT许可证。
📚 引用
如果你使用我们的模型,请引用以下内容:
@article{geigle2023mblip,
author = {Gregor Geigle and
Abhay Jain and
Radu Timofte and
Goran Glava\v{s}},
title = {mBLIP: Efficient Bootstrapping of Multilingual Vision-LLMs},
journal = {arXiv},
volume = {abs/2307.06930},
year = {2023},
url = {https://arxiv.org/abs/2307.06930},
eprinttype = {arXiv},
eprint = {2307.06930},
}








