模型概述
模型特點
模型能力
使用案例
🚀 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},
}








