Commit
•
bfae628
1
Parent(s):
776416a
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,164 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
pipeline_tag: image-to-text
|
5 |
+
inference: false
|
6 |
+
arxiv: 2408.03326
|
7 |
+
license: apache-2.0
|
8 |
+
tags:
|
9 |
+
- vision
|
10 |
+
- image-text-to-text
|
11 |
+
datasets:
|
12 |
+
- lmms-lab/LLaVA-OneVision-Data
|
13 |
+
---
|
14 |
+
# LLaVA-Onevision Model Card
|
15 |
+
|
16 |
+
![image/png](llava_onevision_arch.png)
|
17 |
+
|
18 |
+
Below is the model card of 0.5B LLaVA-Onevision model which is copied from the original LLaVA-Onevision model card that you can find [here](https://huggingface.co/lmms-lab/llava-onevision-qwen2-0.5b-si).
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
## Model details
|
23 |
+
|
24 |
+
**Model type:**
|
25 |
+
LLaVA-Onevision is an open-source multimodal LLM trained by fine-tuning Qwen2 on GPT-generated multimodal instruction-following data.
|
26 |
+
LLaVA-OneVision is the first single model that can simultaneously push the performance boundaries of open LMMs in three important computer
|
27 |
+
vision scenarios: single-image, multi-image, and video scenarios. Importantly, the design of LLaVA-OneVision allows strong transfer learning
|
28 |
+
across different modalities/scenarios, yielding new emerging capabilities. In particular, strong video understanding and cross-scenario
|
29 |
+
capabilities are demonstrated through task transfer from images to videos.
|
30 |
+
|
31 |
+
**Model date:**
|
32 |
+
LLaVA-Onevision-0.5-ov was added in August 2024.
|
33 |
+
|
34 |
+
**Paper or resources for more information:**
|
35 |
+
https://llava-vl.github.io/
|
36 |
+
|
37 |
+
- **Architecture:** SO400M + Qwen2
|
38 |
+
- **Pretraining Stage:** LCS-558K, 1 epoch, projector
|
39 |
+
- **Mid Stage:** A mixture of 4.7M high-quality synthetic data, 1 epoch, full model
|
40 |
+
- **Final-Image Stage:** A mixture of 3.6M single-image data, 1 epoch, full model
|
41 |
+
- **OneVision Stage:** A mixture of 1.6M single-image/multi-image/video data, 1 epoch, full model
|
42 |
+
- **Precision:** bfloat16
|
43 |
+
|
44 |
+
|
45 |
+
## How to use the model
|
46 |
+
|
47 |
+
First, make sure to have `transformers >= 4.35.3`.
|
48 |
+
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 by applyong chat template:
|
49 |
+
|
50 |
+
### Using `pipeline`:
|
51 |
+
|
52 |
+
Below we used [`"llava-hf/llava-onevision-qwen2-0.5b-ov-hf"`](https://huggingface.co/llava-hf/llava-onevision-qwen2-0.5b-ov-hf) checkpoint.
|
53 |
+
|
54 |
+
```python
|
55 |
+
from transformers import pipeline
|
56 |
+
from PIL import Image
|
57 |
+
import requests
|
58 |
+
|
59 |
+
model_id = "llava-hf/llava-onevision-qwen2-0.5b-ov-hf"
|
60 |
+
pipe = pipeline("image-to-text", model=model_id)
|
61 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
|
62 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
63 |
+
|
64 |
+
# Define a chat history and use `apply_chat_template` to get correctly formatted prompt
|
65 |
+
# Each value in "content" has to be a list of dicts with types ("text", "image")
|
66 |
+
conversation = [
|
67 |
+
{
|
68 |
+
|
69 |
+
"role": "user",
|
70 |
+
"content": [
|
71 |
+
{"type": "text", "text": "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"},
|
72 |
+
{"type": "image"},
|
73 |
+
],
|
74 |
+
},
|
75 |
+
]
|
76 |
+
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
77 |
+
|
78 |
+
outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
79 |
+
print(outputs)
|
80 |
+
>>> {"generated_text": "user\n\nWhat does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nassistant\nLava"}
|
81 |
+
```
|
82 |
+
|
83 |
+
### Using pure `transformers`:
|
84 |
+
|
85 |
+
Below is an example script to run generation in `float16` precision on a GPU device:
|
86 |
+
|
87 |
+
```python
|
88 |
+
import requests
|
89 |
+
from PIL import Image
|
90 |
+
|
91 |
+
import torch
|
92 |
+
from transformers import AutoProcessor, LlavaNextForConditionalGeneration
|
93 |
+
|
94 |
+
model_id = "llava-hf/llava-onevision-qwen2-0.5b-ov-hf"
|
95 |
+
model = LlavaNextForConditionalGeneration.from_pretrained(
|
96 |
+
model_id,
|
97 |
+
torch_dtype=torch.float16,
|
98 |
+
low_cpu_mem_usage=True,
|
99 |
+
).to(0)
|
100 |
+
|
101 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
102 |
+
|
103 |
+
# Define a chat history and use `apply_chat_template` to get correctly formatted prompt
|
104 |
+
# Each value in "content" has to be a list of dicts with types ("text", "image")
|
105 |
+
conversation = [
|
106 |
+
{
|
107 |
+
|
108 |
+
"role": "user",
|
109 |
+
"content": [
|
110 |
+
{"type": "text", "text": "What are these?"},
|
111 |
+
{"type": "image"},
|
112 |
+
],
|
113 |
+
},
|
114 |
+
]
|
115 |
+
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
116 |
+
|
117 |
+
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
118 |
+
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
119 |
+
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
|
120 |
+
|
121 |
+
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
|
122 |
+
print(processor.decode(output[0][2:], skip_special_tokens=True))
|
123 |
+
```
|
124 |
+
|
125 |
+
### Model optimization
|
126 |
+
|
127 |
+
#### 4-bit quantization through `bitsandbytes` library
|
128 |
+
|
129 |
+
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:
|
130 |
+
|
131 |
+
```diff
|
132 |
+
model = LlavaNextForConditionalGeneration.from_pretrained(
|
133 |
+
model_id,
|
134 |
+
torch_dtype=torch.float16,
|
135 |
+
low_cpu_mem_usage=True,
|
136 |
+
+ load_in_4bit=True
|
137 |
+
)
|
138 |
+
```
|
139 |
+
|
140 |
+
#### Use Flash-Attention 2 to further speed-up generation
|
141 |
+
|
142 |
+
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:
|
143 |
+
|
144 |
+
```diff
|
145 |
+
model = LlavaNextForConditionalGeneration.from_pretrained(
|
146 |
+
model_id,
|
147 |
+
torch_dtype=torch.float16,
|
148 |
+
low_cpu_mem_usage=True,
|
149 |
+
+ use_flash_attention_2=True
|
150 |
+
).to(0)
|
151 |
+
```
|
152 |
+
|
153 |
+
# Citation
|
154 |
+
```
|
155 |
+
@misc{li2024llavaonevisioneasyvisualtask,
|
156 |
+
title={LLaVA-OneVision: Easy Visual Task Transfer},
|
157 |
+
author={Bo Li and Yuanhan Zhang and Dong Guo and Renrui Zhang and Feng Li and Hao Zhang and Kaichen Zhang and Yanwei Li and Ziwei Liu and Chunyuan Li},
|
158 |
+
year={2024},
|
159 |
+
eprint={2408.03326},
|
160 |
+
archivePrefix={arXiv},
|
161 |
+
primaryClass={cs.CV},
|
162 |
+
url={https://arxiv.org/abs/2408.03326},
|
163 |
+
}
|
164 |
+
```
|