🚀 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由3個模型組成,分別是類似CLIP的圖像編碼器、查詢變壓器(Q-Former)和大語言模型。
- 跨模態能力:能夠用於圖像描述、視覺問答(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許可證。