🚀 分片式BLIP - 2模型卡片 - flan - t5 - xl
这是一个基于图像文本到文本处理的模型,借助 [Flan T5 - xl](https://huggingface.co/google/flan - t5 - xl) 来完成图像到文本的任务,例如图像描述和视觉问答。它是 [blip2 - flan - t5 - xl](https://huggingface.co/Salesforce/blip2 - flan - t5 - xl) 的分片版本,能在低内存的Colab运行时轻松加载。
🚀 快速开始
本模型借助 [Flan T5 - xl](https://huggingface.co/google/flan - t5 - xl) 来完成图像到文本的任务,如图像描述和视觉问答。该模型仓库采用了分片技术,可在低内存的Colab运行时轻松加载。更多关于模型描述、预期用途、限制以及如何在CPU和GPU上以不同精度使用模型的说明,请参考 [原始模型卡片](https://huggingface.co/Salesforce/blip2 - flan - t5 - xl)。
📦 安装指南
需要安装当前 transformers
的 main
版本(在编写本文时):
pip install accelerate git+https://github.com/huggingface/transformers.git -U -q
💻 使用示例
基础用法
以下是在CPU上使用该模型的示例代码:
import requests
from PIL import Image
from transformers import BlipProcessor, Blip2ForConditionalGeneration
model_name = "ethzanalytics/blip2-flan-t5-xl-sharded"
processor = BlipProcessor.from_pretrained(model_name)
model = Blip2ForConditionalGeneration.from_pretrained(model_name)
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')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
此示例代码是在CPU上运行的,关于 fp16
和 int8
的使用,请参考原始模型卡片或 [这篇博客文章](https://huggingface.co/blog/blip - 2#using - blip - 2 - with - hugging - face - transformers)。
📄 许可证
本项目采用MIT许可证。