🚀 BLIP:用於統一視覺語言理解和生成的語言 - 圖像預訓練引導
BLIP是一個在COCO數據集上預訓練的圖像描述模型(基於ViT基礎架構),它能靈活應用於視覺 - 語言理解和生成任務,在多個視覺 - 語言任務中取得了最先進的成果。
 |
圖片來自BLIP官方倉庫,圖片來源:https://github.com/salesforce/BLIP |
🚀 快速開始
模型簡介
作者在論文摘要中寫道:
視覺 - 語言預訓練(VLP)提升了許多視覺 - 語言任務的性能。然而,大多數現有的預訓練模型僅在基於理解的任務或基於生成的任務中表現出色。此外,性能的提升很大程度上是通過擴大從網絡收集的帶有噪聲的圖像 - 文本對數據集來實現的,而網絡數據是一種次優的監督來源。在本文中,我們提出了BLIP,一個新的VLP框架,它可以靈活地遷移到視覺 - 語言理解和生成任務中。BLIP通過引導式生成描述有效地利用了有噪聲的網絡數據,其中描述生成器生成合成描述,過濾器去除有噪聲的描述。我們在廣泛的視覺 - 語言任務中取得了最先進的結果,如圖像 - 文本檢索(平均召回率@1提高2.7%)、圖像描述(CIDEr指標提高2.8%)和視覺問答(VQA得分提高1.6%)。BLIP在以零樣本方式直接遷移到視頻 - 語言任務時也表現出強大的泛化能力。代碼、模型和數據集已發佈。
使用方法
你可以使用此模型進行條件和無條件圖像描述。
💻 使用示例
基礎用法
使用PyTorch模型在CPU上運行
點擊展開
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
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))
>>> a woman sitting on the beach with her dog
高級用法
在GPU上運行模型
全精度
點擊展開
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").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))
>>> a woman sitting on the beach with her dog
半精度 (float16
)
點擊展開
import torch
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base", 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 - 條款許可證。