File size: 2,073 Bytes
551bc08
 
886514c
 
 
 
551bc08
a7be8e9
886514c
5da61fa
 
 
 
e600fae
a7be8e9
e600fae
 
a7be8e9
 
 
 
 
e600fae
 
 
 
 
 
 
 
a7be8e9
 
 
 
 
 
e600fae
a7be8e9
 
 
 
 
 
 
 
 
 
 
 
886514c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---
license: mit
language:
- en
library_name: transformers
inference: False
---
## Sharded BLIP-2 Model Card - flan-t5-xl

<a href="https://colab.research.google.com/gist/pszemraj/0822b7f28b14405f10cfd382296873de/blip2-flan-t5-xl-sharded-example.ipynb">
  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
</a>

This is a sharded version of the [blip2-flan-t5-xl](https://huggingface.co/Salesforce/blip2-flan-t5-xl) which leverages [Flan T5-xl](https://huggingface.co/google/flan-t5-xl) for image-to-text tasks such as image captioning and visual question answering.

- this model repo is sharded so it can be easily loaded on low-RAM Colab runtimes :)
- Refer to the [original model card](https://huggingface.co/Salesforce/blip2-flan-t5-xl) for more details about the model description, intended uses, and limitations, as well as instructions for how to use the model on CPU and GPU in different precisions.

## Usage

Refer to the original model card for details or see [this blog post](https://huggingface.co/blog/blip-2#using-blip-2-with-hugging-face-transformers). Here is how you can use it on CPU:

Install

Requires the current `main` of transformers (_at time of writing_):
```bash
pip install accelerate git+https://github.com/huggingface/transformers.git -U -q
```

Use (_this is for CPU, check out the original model card/blog for `fp16` and `int8` usage_)

```python
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))
```