🚀 BLIP:用于统一视觉语言理解和生成的语言 - 图像预训练引导
BLIP是一个新的视觉 - 语言预训练(VLP)框架,能灵活迁移到视觉 - 语言理解和生成任务。该模型在COCO数据集上进行预训练(基础架构,采用ViT基础骨干网络),并在足球数据集上进行微调,可用于图像描述任务。
用于微调的Google Colab笔记本:https://colab.research.google.com/drive/1lbqiSiA0sDF7JDWPeS0tccrM85LloVha?usp=sharing
 |
图片来源于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在以零样本方式直接迁移到视频 - 语言任务时也表现出强大的泛化能力。代码、模型和数据集已发布。
💻 使用示例
基础用法
使用PyTorch模型进行图像描述。
在CPU上运行模型
点击展开
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
processor = BlipProcessor.from_pretrained("ybelkada/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("ybelkada/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("Salesfoce/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 - 条款许可证。