🚀 BLIP:用於統一視覺語言理解和生成的語言 - 圖像預訓練引導
BLIP是一個在COCO數據集上預訓練的圖像字幕模型,採用基礎架構(帶有ViT大主幹),可靈活應用於視覺語言理解和生成任務。
🚀 快速開始
此模型可用於有條件和無條件的圖像字幕生成。
✨ 主要特性
作者在論文摘要中指出:視覺 - 語言預訓練(VLP)提升了許多視覺語言任務的性能。然而,大多數現有預訓練模型僅在基於理解或基於生成的任務中表現出色。此外,性能提升主要通過擴展從網絡收集的含噪圖像 - 文本對數據集來實現,這並非最佳監督來源。本文提出BLIP,一個可靈活遷移到視覺語言理解和生成任務的新VLP框架。BLIP通過引導字幕有效利用含噪網絡數據,其中字幕生成器生成合成字幕,過濾器去除含噪字幕。我們在廣泛的視覺語言任務上取得了最先進的成果,如圖像 - 文本檢索(平均召回率@1提高2.7%)、圖像字幕生成(CIDEr指標提高2.8%)和視覺問答(VQA分數提高1.6%)。BLIP在零樣本遷移到視頻語言任務時也展現出強大的泛化能力。代碼、模型和數據集均已發佈。
 |
圖片來自BLIP官方倉庫,圖片來源:https://github.com/salesforce/BLIP |
💻 使用示例
基礎用法
使用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))
半精度 (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 - 條款許可證。