HuggingFace镜像/blip-image-captioning-base
模型介绍文件和版本分析
下载使用量0

BLIP:用于统一视觉-语言理解与生成的自举式语言-图像预训练

在 COCO 数据集上预训练的图像 captioning 模型卡片 - 基础架构(采用 ViT base 骨干网络)。

BLIP.gif
图片来源于 BLIP 官方仓库

简要概述

论文的作者在摘要中写道:

视觉-语言预训练(VLP)提升了众多视觉-语言任务的性能。然而,大多数现有预训练模型仅在基于理解的任务或基于生成的任务中的某一类表现出色。此外,性能提升很大程度上是通过使用从网络上收集的含噪声图像-文本对来扩大数据集实现的,这并非最优的监督信息来源。在本文中,我们提出了 BLIP,一种新的 VLP 框架,能够灵活迁移至视觉-语言理解和生成任务。BLIP 通过自举式 caption 有效利用含噪声的网络数据,其中 caption 生成器生成合成 caption,过滤器去除噪声样本。我们在广泛的视觉-语言任务上取得了最先进的结果,例如图像-文本检索(平均 recall@1 提升 2.7%)、图像 captioning(CIDEr 提升 2.8%)和 VQA(VQA 得分提升 1.6%)。BLIP 在以零样本方式直接迁移到视频-语言任务时也表现出强大的泛化能力。代码、模型和数据集已公开。

使用方法

您可以使用此模型进行有条件和无条件的图像 captioning

使用 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')

# conditional image captioning
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))
# >>> a photography of a woman and her dog

# unconditional image captioning
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')

# conditional image captioning
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))
# >>> a photography of a woman and her dog

# unconditional image captioning
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')

# conditional image captioning
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))
# >>> a photography of a woman and her dog

# unconditional image captioning
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

伦理考量

本版本仅用于学术论文支持的研究目的。我们的模型、数据集和代码并非专门为所有下游用途设计或评估。我们强烈建议用户在部署此模型前,评估并解决与准确性、安全性和公平性相关的潜在问题。我们鼓励用户考虑人工智能的常见局限性,遵守适用法律,并在选择用例时采用最佳实践,特别是在高风险场景中,错误或滥用可能会对人们的生活、权利或安全产生重大影响。有关用例的进一步指导,请参考我们的 AUP 和 AI AUP。

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}
}