模型:
ethzanalytics/blip2-flan-t5-xl-sharded
这是一个分片版本的 blip2-flan-t5-xl ,它利用 Flan T5-xl 进行图像到文本任务,例如图像字幕和视觉问答。
有关详细信息,请参阅原始模型卡或查看 this blog post 。这是如何在CPU上使用它的:
安装
要求当前的transformers主分支(在编写时):
pip install accelerate git+https://github.com/huggingface/transformers.git -U -q
用法(这是用于CPU的,请查看原始模型卡/博客了解fp16和int8的用法)
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))