🚀 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團隊編寫。
✨ 主要特性
- 多任務處理:可用於圖像描述、視覺問答和類聊天對話等任務。
- 架構創新:通過Querying Transformer(Q-Former)橋接圖像編碼器和大語言模型的嵌入空間。
📚 詳細文檔
模型描述
BLIP-2由3個模型組成:一個類似CLIP的圖像編碼器、一個查詢變換器(Q-Former)和一個大語言模型。
作者從預訓練檢查點初始化圖像編碼器和大語言模型的權重,並在訓練查詢變換器時保持它們凍結。查詢變換器是一個類似BERT的變換器編碼器,它將一組“查詢令牌”映射到查詢嵌入,從而彌合圖像編碼器和大語言模型嵌入空間之間的差距。
該模型的目標很簡單,即根據查詢嵌入和之前的文本預測下一個文本令牌。

這使得該模型可用於以下任務:
- 圖像描述
- 視覺問答(VQA)
- 通過將圖像和之前的對話作為提示輸入模型進行類聊天對話
直接使用和下游使用
你可以使用原始模型在給定圖像和可選文本的情況下進行條件文本生成。請查看模型中心以查找針對你感興趣的任務進行微調的版本。
偏差、風險、侷限性和倫理考量
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許可證。