Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
pipeline_tag: image-to-text
|
5 |
+
inference: false
|
6 |
+
arxiv: 2304.08485
|
7 |
+
---
|
8 |
+
# VipLLaVA Model Card
|
9 |
+
|
10 |
+
![image/png](https://github.com/mu-cai/ViP-LLaVA/blob/main/images/vip-llava_arch.png?raw=true)
|
11 |
+
|
12 |
+
Below is the model card of VipLlava model 7b, which is copied from the original Llava model card that you can find [here](https://huggingface.co/liuhaotian/llava-v1.5-13b).
|
13 |
+
|
14 |
+
Check out also the Google Colab demo to run Llava on a free-tier Google Colab instance (the model works similarly as Llava): [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1-0G7Kuj2iQgKux4NJneP2JefFMamxG6Q?usp=sharing)
|
15 |
+
|
16 |
+
Or check out our Spaces demo! [![Open in Spaces](https://huggingface.co/datasets/huggingface/badges/resolve/main/open-in-hf-spaces-md-dark.svg)](https://huggingface.co/spaces/llava-hf/llava-4bit)
|
17 |
+
|
18 |
+
|
19 |
+
## Model details
|
20 |
+
|
21 |
+
**Model type:**
|
22 |
+
LLaVA is an open-source chatbot trained by fine-tuning LLaMA/Vicuna on GPT-generated multimodal instruction-following data.
|
23 |
+
It is an auto-regressive language model, based on the transformer architecture.
|
24 |
+
|
25 |
+
**Model date:**
|
26 |
+
LLaVA-v1.5-7B was trained in September 2023.
|
27 |
+
|
28 |
+
**Paper or resources for more information:**
|
29 |
+
https://llava-vl.github.io/
|
30 |
+
|
31 |
+
## How to use the model
|
32 |
+
|
33 |
+
First, make sure to have `transformers >= 4.35.3`.
|
34 |
+
The model supports multi-image and multi-prompt generation. Meaning that you can pass multiple images in your prompt. Make sure also to follow the correct prompt template and add the token `<image>` to the location where you want to query images:
|
35 |
+
|
36 |
+
According to the official code base, it is recommeneded to use this template:
|
37 |
+
|
38 |
+
```bash
|
39 |
+
A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.###Human: <image>\n<prompt>###Assistant:
|
40 |
+
```
|
41 |
+
|
42 |
+
Where `<prompt>` denotes the prompt asked by the user
|
43 |
+
|
44 |
+
### Using `pipeline`:
|
45 |
+
|
46 |
+
|
47 |
+
```python
|
48 |
+
from transformers import pipeline
|
49 |
+
from PIL import Image
|
50 |
+
import requests
|
51 |
+
|
52 |
+
model_id = "llava-hf/vip-llava-13b-hf"
|
53 |
+
pipe = pipeline("image-to-text", model=model_id)
|
54 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
|
55 |
+
|
56 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
57 |
+
question = "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"
|
58 |
+
prompt = f"A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.###Human: <image>\n{question}###Assistant:"
|
59 |
+
|
60 |
+
outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
61 |
+
print(outputs)
|
62 |
+
```
|
63 |
+
|
64 |
+
### Using pure `transformers`:
|
65 |
+
|
66 |
+
Below is an example script to run generation in `float16` precision on a GPU device:
|
67 |
+
|
68 |
+
```python
|
69 |
+
import requests
|
70 |
+
from PIL import Image
|
71 |
+
|
72 |
+
import torch
|
73 |
+
from transformers import AutoProcessor, VipLlavaForConditionalGeneration
|
74 |
+
|
75 |
+
model_id = "llava-hf/vip-llava-7b-hf"
|
76 |
+
|
77 |
+
question = "What are these?"
|
78 |
+
prompt = f"A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.###Human: <image>\n{question}###Assistant:"
|
79 |
+
|
80 |
+
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
81 |
+
|
82 |
+
model = VipLlavaForConditionalGeneration.from_pretrained(
|
83 |
+
model_id,
|
84 |
+
torch_dtype=torch.float16,
|
85 |
+
low_cpu_mem_usage=True,
|
86 |
+
).to(0)
|
87 |
+
|
88 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
89 |
+
|
90 |
+
|
91 |
+
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
92 |
+
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
|
93 |
+
|
94 |
+
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
|
95 |
+
print(processor.decode(output[0][2:], skip_special_tokens=True))
|
96 |
+
```
|
97 |
+
|
98 |
+
### Model optimization
|
99 |
+
|
100 |
+
#### 4-bit quantization through `bitsandbytes` library
|
101 |
+
|
102 |
+
First make sure to install `bitsandbytes`, `pip install bitsandbytes` and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with:
|
103 |
+
|
104 |
+
```diff
|
105 |
+
model = VipLlavaForConditionalGeneration.from_pretrained(
|
106 |
+
model_id,
|
107 |
+
torch_dtype=torch.float16,
|
108 |
+
low_cpu_mem_usage=True,
|
109 |
+
+ load_in_4bit=True
|
110 |
+
)
|
111 |
+
```
|
112 |
+
|
113 |
+
#### Use Flash-Attention 2 to further speed-up generation
|
114 |
+
|
115 |
+
First make sure to install `flash-attn`. Refer to the [original repository of Flash Attention](https://github.com/Dao-AILab/flash-attention) regarding that package installation. Simply change the snippet above with:
|
116 |
+
|
117 |
+
```diff
|
118 |
+
model = VipLlavaForConditionalGeneration.from_pretrained(
|
119 |
+
model_id,
|
120 |
+
torch_dtype=torch.float16,
|
121 |
+
low_cpu_mem_usage=True,
|
122 |
+
+ use_flash_attention_2=True
|
123 |
+
).to(0)
|
124 |
+
```
|
125 |
+
|
126 |
+
## License
|
127 |
+
Llama 2 is licensed under the LLAMA 2 Community License,
|
128 |
+
Copyright (c) Meta Platforms, Inc. All Rights Reserved.
|