🚀 BLIP:用於統一視覺 - 語言理解與生成的語言 - 圖像預訓練引導
BLIP是一個在COCO數據集上預訓練的圖像描述模型,採用基礎架構(帶有ViT大骨幹網絡),可有效處理視覺 - 語言理解和生成任務。
 |
圖片取自BLIP官方倉庫 |
🚀 快速開始
此模型可用於條件和無條件圖像描述任務。
✨ 主要特性
論文BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation的作者在摘要中指出:視覺 - 語言預訓練(VLP)提升了許多視覺 - 語言任務的性能。然而,大多數現有預訓練模型僅在基於理解或基於生成的任務中表現出色。此外,性能提升主要通過擴大從網絡收集的含噪聲圖像 - 文本對數據集來實現,這是一種次優的監督來源。在本文中,作者提出了BLIP,一種新的VLP框架,可靈活遷移到視覺 - 語言理解和生成任務。BLIP通過引導字幕有效利用含噪聲的網絡數據,其中字幕生成器生成合成字幕,過濾器去除含噪聲的字幕。作者在廣泛的視覺 - 語言任務上取得了最先進的結果,如圖像 - 文本檢索(平均召回率@1提高2.7%)、圖像描述(CIDEr提高2.8%)和視覺問答(VQA分數提高1.6%)。BLIP在以零樣本方式直接遷移到視頻 - 語言任務時也表現出強大的泛化能力。代碼、模型和數據集均已發佈。
📦 安裝指南
使用此模型前,需要安裝相關依賴庫,可使用以下命令:
pip install requests pillow transformers
💻 使用示例
基礎用法
使用PyTorch模型在CPU上運行模型
點擊展開
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
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')
text = "a photography of"
inputs = processor(raw_image, text, return_tensors="pt")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
inputs = processor(raw_image, 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 BlipProcessor, BlipForConditionalGeneration
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large").to("cuda")
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')
text = "a photography of"
inputs = processor(raw_image, text, return_tensors="pt").to("cuda")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
inputs = processor(raw_image, return_tensors="pt").to("cuda")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
在GPU上以半精度(float16
)運行模型
點擊展開
import torch
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large", torch_dtype=torch.float16).to("cuda")
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')
text = "a photography of"
inputs = processor(raw_image, text, return_tensors="pt").to("cuda", torch.float16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
inputs = processor(raw_image, return_tensors="pt").to("cuda", torch.float16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
>>> a woman sitting on the beach with her dog
📚 詳細文檔
BibTex引用信息
@misc{https://doi.org/10.48550/arxiv.2201.12086,
doi = {10.48550/ARXIV.2201.12086},
url = {https://arxiv.org/abs/2201.12086},
author = {Li, Junnan and Li, Dongxu and Xiong, Caiming and Hoi, Steven},
keywords = {Computer Vision and Pattern Recognition (cs.CV), FOS: Computer and information sciences, FOS: Computer and information sciences},
title = {BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation},
publisher = {arXiv},
year = {2022},
copyright = {Creative Commons Attribution 4.0 International}
}
📄 許可證
本項目採用BSD 3 - 條款許可證。