🚀 BLIP-2, Flan T5-xxl,僅預訓練版本
BLIP-2模型藉助了Flan T5-xxl(一個大型語言模型)的能力。該模型由Li等人在論文BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models中提出,並首次在此倉庫發佈。
免責聲明:發佈BLIP-2的團隊並未為此模型撰寫模型卡片,本模型卡片由Hugging Face團隊撰寫。
🚀 快速開始
你可以直接使用該原始模型,根據圖像和可選文本進行條件文本生成。你可以在模型中心查找針對你感興趣的任務進行微調後的版本。
✨ 主要特性
BLIP-2可用於以下任務:
- 圖像描述
- 視覺問答(VQA)
- 通過將圖像和之前的對話作為提示輸入模型,進行類似聊天的對話
📚 詳細文檔
模型描述
BLIP-2由3個模型組成:一個類似CLIP的圖像編碼器、一個查詢變換器(Q-Former)和一個大型語言模型。
作者從預訓練的檢查點初始化圖像編碼器和大型語言模型的權重,並在訓練查詢變換器時保持它們不變。查詢變換器是一個類似BERT的變換器編碼器,它將一組“查詢令牌”映射到查詢嵌入,從而彌合圖像編碼器的嵌入空間和大型語言模型之間的差距。
該模型的目標很簡單,即根據查詢嵌入和之前的文本預測下一個文本令牌。

直接使用和下游使用
你可以使用原始模型根據圖像和可選文本進行條件文本生成。可查看模型中心,查找針對你感興趣的任務微調後的版本。
偏差、風險、侷限性和倫理考量
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-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))
📄 許可證
本項目採用MIT許可證。