模型概述
模型特點
模型能力
使用案例
🚀 BLIP-2, OPT-2.7b,僅預訓練版本
BLIP-2模型藉助了 OPT-2.7b(一個擁有27億參數的大語言模型)。它由Li等人在論文 BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models 中提出,並首次在 此倉庫 發佈。
聲明:發佈BLIP-2的團隊並未為此模型撰寫模型卡片,此模型卡片由Hugging Face團隊撰寫。
🚀 快速開始
對現有Salesforce模型卡片的更新
添加了在Hugging Face推理管道上運行模型的處理程序。
輸入:
{
"inputs": "<Base64 Image>",
"prompts": "<Prompt Text here>"
}
輸出:
{
"captions":"<Generated Image caption>"
}
✨ 主要特性
- 多任務支持:可用於圖像描述、視覺問答、類聊天對話等任務。
- 架構優勢:由類似CLIP的圖像編碼器、查詢變壓器(Q-Former)和大語言模型組成,能有效彌合圖像編碼器和大語言模型嵌入空間的差距。
📚 詳細文檔
模型描述
BLIP-2由3個模型組成:一個類似CLIP的圖像編碼器、一個查詢變壓器(Q-Former)和一個大語言模型。
作者從預訓練檢查點初始化圖像編碼器和大語言模型的權重,並在訓練查詢變壓器時保持它們不變。查詢變壓器是一個類似BERT的變壓器編碼器,它將一組“查詢令牌”映射到查詢嵌入,從而彌合圖像編碼器和大語言模型的嵌入空間之間的差距。
該模型的目標很簡單,即根據查詢嵌入和先前的文本預測下一個文本令牌。
這使得該模型可用於以下任務:
- 圖像描述
- 視覺問答(VQA)
- 通過將圖像和先前的對話作為提示輸入模型進行類聊天對話
直接使用和下游使用
你可以使用原始模型根據圖像和可選文本進行條件文本生成。請參閱 模型中心 以查找針對你感興趣的任務進行微調的版本。
偏差、風險、侷限性和倫理考量
BLIP2-OPT使用現成的OPT作為語言模型,它繼承了Meta模型卡片中提到的相同風險和侷限性。
與其他因訓練數據多樣性(或缺乏多樣性)對模型質量產生下游影響的大語言模型一樣,OPT-175B在偏差和安全性方面存在侷限性。OPT-175B在生成多樣性和幻覺方面也可能存在質量問題。一般來說,OPT-175B無法避免困擾現代大語言模型的諸多問題。
BLIP2在從互聯網收集的圖像文本數據集(例如 LAION )上進行了微調。因此,該模型本身可能容易生成同樣不適當的內容或複製底層數據中固有的偏差。
BLIP2尚未在現實世界應用中進行測試,不應直接部署到任何應用程序中。研究人員應首先仔細評估該模型在其部署的特定環境中的安全性和公平性。
💻 使用示例
基礎用法
有關代碼示例,請參考 文檔。
在CPU上運行模型
點擊展開
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.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 = "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上運行模型
全精度
點擊展開
# pip install accelerate
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.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 = "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
)
點擊展開
# pip install accelerate
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", 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
)
點擊展開
# pip install accelerate bitsandbytes
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.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 = "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許可證。








