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

這使得該模型可用於以下任務:
- 圖像描述
- 視覺問答(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).strip())
高級用法
在 GPU 上以全精度運行模型
點擊展開
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).strip())
在 GPU 上以半精度(float16
)運行模型
點擊展開
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).strip())
在 GPU 上以 8 位精度(int8
)運行模型
點擊展開
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).strip())
📄 許可證
本項目採用 MIT 許可證。