diff for compatibility
Browse files- README.md +12 -244
- tokenizer_config.json +2 -1
README.md
CHANGED
@@ -7,254 +7,22 @@ datasets:
|
|
7 |
- lmms-lab/VideoChatGPT
|
8 |
---
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
Check out also the Google Colab demo to run Llava on a free-tier Google Colab instance: [](https://colab.research.google.com/drive/1CZggLHrjxMReG-FNOmqSOdi4z7NPq6SO?usp=sharing)
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
LLaVA-Next-Video is an open-source chatbot trained by fine-tuning LLM on multimodal instruction-following data. The model is buit on top of LLaVa-NeXT by tuning on a mix of video and image data to achieve better video understanding capabilities. The videos were sampled uniformly to be 32 frames per clip.
|
20 |
-
The model is a current SOTA among open-source models on [VideoMME bench](https://arxiv.org/abs/2405.21075).
|
21 |
-
Base LLM: [lmsys/vicuna-7b-v1.5](https://huggingface.co/lmsys/vicuna-13b-v1.5)
|
22 |
|
23 |
-
|
24 |
|
|
|
25 |
|
26 |
-
|
27 |
-
LLaVA-Next-Video-7B was trained in April 2024.
|
28 |
-
|
29 |
-
**Paper or resources for more information:** https://github.com/LLaVA-VL/LLaVA-NeXT
|
30 |
-
|
31 |
-
|
32 |
-
## π Training dataset
|
33 |
-
|
34 |
-
### Image
|
35 |
-
- 558K filtered image-text pairs from LAION/CC/SBU, captioned by BLIP.
|
36 |
-
- 158K GPT-generated multimodal instruction-following data.
|
37 |
-
- 500K academic-task-oriented VQA data mixture.
|
38 |
-
- 50K GPT-4V data mixture.
|
39 |
-
- 40K ShareGPT data.
|
40 |
-
|
41 |
-
### Video
|
42 |
-
- 100K VideoChatGPT-Instruct.
|
43 |
-
|
44 |
-
## π Evaluation dataset
|
45 |
-
A collection of 4 benchmarks, including 3 academic VQA benchmarks and 1 captioning benchmark.
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
## π How to use the model
|
50 |
-
|
51 |
-
First, make sure to have `transformers >= 4.42.0`.
|
52 |
-
The model supports multi-visual and multi-prompt generation. Meaning that you can pass multiple images/videos in your prompt. Make sure also to follow the correct prompt template (`USER: xxx\nASSISTANT:`) and add the token `<image>` or `<video>` to the location where you want to query images/videos:
|
53 |
-
|
54 |
-
Below is an example script to run generation in `float16` precision on a GPU device:
|
55 |
-
|
56 |
-
```python
|
57 |
-
import av
|
58 |
-
import torch
|
59 |
-
import numpy as np
|
60 |
-
from huggingface_hub import hf_hub_download
|
61 |
-
from transformers import LlavaNextVideoProcessor, LlavaNextVideoForConditionalGeneration
|
62 |
-
|
63 |
-
model_id = "llava-hf/LLaVA-NeXT-Video-7B-hf"
|
64 |
-
|
65 |
-
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
|
66 |
-
model_id,
|
67 |
-
torch_dtype=torch.float16,
|
68 |
-
low_cpu_mem_usage=True,
|
69 |
-
).to(0)
|
70 |
-
|
71 |
-
processor = LlavaNextVideoProcessor.from_pretrained(model_id)
|
72 |
-
|
73 |
-
def read_video_pyav(container, indices):
|
74 |
-
'''
|
75 |
-
Decode the video with PyAV decoder.
|
76 |
-
Args:
|
77 |
-
container (`av.container.input.InputContainer`): PyAV container.
|
78 |
-
indices (`List[int]`): List of frame indices to decode.
|
79 |
-
Returns:
|
80 |
-
result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
|
81 |
-
'''
|
82 |
-
frames = []
|
83 |
-
container.seek(0)
|
84 |
-
start_index = indices[0]
|
85 |
-
end_index = indices[-1]
|
86 |
-
for i, frame in enumerate(container.decode(video=0)):
|
87 |
-
if i > end_index:
|
88 |
-
break
|
89 |
-
if i >= start_index and i in indices:
|
90 |
-
frames.append(frame)
|
91 |
-
return np.stack([x.to_ndarray(format="rgb24") for x in frames])
|
92 |
-
|
93 |
-
|
94 |
-
# define a chat history and use `apply_chat_template` to get correctly formatted prompt
|
95 |
-
# Each value in "content" has to be a list of dicts with types ("text", "image", "video")
|
96 |
-
conversation = [
|
97 |
-
{
|
98 |
-
|
99 |
-
"role": "user",
|
100 |
-
"content": [
|
101 |
-
{"type": "text", "text": "Why is this video funny?"},
|
102 |
-
{"type": "video"},
|
103 |
-
],
|
104 |
-
},
|
105 |
-
]
|
106 |
-
|
107 |
-
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
108 |
-
|
109 |
-
video_path = hf_hub_download(repo_id="raushan-testing-hf/videos-test", filename="sample_demo_1.mp4", repo_type="dataset")
|
110 |
-
container = av.open(video_path)
|
111 |
-
|
112 |
-
# sample uniformly 8 frames from the video, can sample more for longer videos
|
113 |
-
total_frames = container.streams.video[0].frames
|
114 |
-
indices = np.arange(0, total_frames, total_frames / 8).astype(int)
|
115 |
-
clip = read_video_pyav(container, indices)
|
116 |
-
inputs_video = processor(text=prompt, videos=clip, padding=True, return_tensors="pt").to(model.device)
|
117 |
-
|
118 |
-
output = model.generate(**inputs_video, max_new_tokens=100, do_sample=False)
|
119 |
-
print(processor.decode(output[0][2:], skip_special_tokens=True))
|
120 |
-
```
|
121 |
-
|
122 |
-
-----------
|
123 |
-
From transformers>=v4.48, you can also pass image/video url or local path to the conversation history, and let the chat template handle the rest.
|
124 |
-
For video you also need to indicate how many `num_frames` to sample from video, otherwise the whole video will be loaded.
|
125 |
-
Chat template will load the image/video for you and return inputs in `torch.Tensor` which you can pass directly to `model.generate()`.
|
126 |
-
|
127 |
-
```python
|
128 |
-
messages = [
|
129 |
-
{
|
130 |
-
"role": "user",
|
131 |
-
"content": [
|
132 |
-
{"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"}
|
133 |
-
{"type": "video", "path": "my_video.mp4"},
|
134 |
-
{"type": "text", "text": "What is shown in this image and video?"},
|
135 |
-
],
|
136 |
-
},
|
137 |
-
]
|
138 |
-
|
139 |
-
inputs = processor.apply_chat_template(messages, num_frames=8, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors"pt")
|
140 |
-
output = model.generate(**inputs, max_new_tokens=50)
|
141 |
-
```
|
142 |
-
|
143 |
-
|
144 |
-
### Inference with images as inputs
|
145 |
-
|
146 |
-
To generate from images use the below code after loading the model as shown above:
|
147 |
-
|
148 |
-
```python
|
149 |
-
import requests
|
150 |
-
from PIL import Image
|
151 |
-
|
152 |
-
conversation = [
|
153 |
-
{
|
154 |
-
"role": "user",
|
155 |
-
"content": [
|
156 |
-
{"type": "text", "text": "What are these?"},
|
157 |
-
{"type": "image"},
|
158 |
-
],
|
159 |
-
},
|
160 |
-
]
|
161 |
-
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
162 |
-
|
163 |
-
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
164 |
-
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
165 |
-
inputs_image = processor(text=prompt, images=raw_image, return_tensors='pt').to(0, torch.float16)
|
166 |
-
|
167 |
-
output = model.generate(**inputs_video, max_new_tokens=100, do_sample=False)
|
168 |
-
print(processor.decode(output[0][2:], skip_special_tokens=True))
|
169 |
-
```
|
170 |
-
|
171 |
-
### Inference with images and videos as inputs
|
172 |
-
|
173 |
-
To generate from images and videos in one generate use the below code after loading the model as shown above:
|
174 |
-
|
175 |
-
```python
|
176 |
-
conversation_1 = [
|
177 |
-
{
|
178 |
-
"role": "user",
|
179 |
-
"content": [
|
180 |
-
{"type": "text", "text": "What's the content of the image>"},
|
181 |
-
{"type": "image"},
|
182 |
-
],
|
183 |
-
}
|
184 |
-
]
|
185 |
-
conversation_2 = [
|
186 |
-
{
|
187 |
-
"role": "user",
|
188 |
-
"content": [
|
189 |
-
{"type": "text", "text": "Why is this video funny?"},
|
190 |
-
{"type": "video"},
|
191 |
-
],
|
192 |
-
},
|
193 |
-
]
|
194 |
-
prompt_1 = processor.apply_chat_template(conversation_1, add_generation_prompt=True)
|
195 |
-
prompt_2 = processor.apply_chat_template(conversation_2, add_generation_prompt=True)
|
196 |
-
|
197 |
-
s = processor(text=[prompt_1, prompt_2], images=image, videos=clip, padding=True, return_tensors="pt").to(model.device)
|
198 |
-
|
199 |
-
# Generate
|
200 |
-
generate_ids = model.generate(**inputs, max_new_tokens=100)
|
201 |
-
out = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
202 |
-
print(out)
|
203 |
-
```
|
204 |
-
|
205 |
-
### Model optimization
|
206 |
-
|
207 |
-
#### 4-bit quantization through `bitsandbytes` library
|
208 |
-
|
209 |
-
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:
|
210 |
-
|
211 |
-
```diff
|
212 |
-
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
|
213 |
-
model_id,
|
214 |
-
torch_dtype=torch.float16,
|
215 |
-
low_cpu_mem_usage=True,
|
216 |
-
+ load_in_4bit=True
|
217 |
-
)
|
218 |
-
```
|
219 |
-
|
220 |
-
#### Use Flash-Attention 2 to further speed-up generation
|
221 |
-
|
222 |
-
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:
|
223 |
-
|
224 |
-
```diff
|
225 |
-
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
|
226 |
-
model_id,
|
227 |
-
torch_dtype=torch.float16,
|
228 |
-
low_cpu_mem_usage=True,
|
229 |
-
+ use_flash_attention_2=True
|
230 |
-
).to(0)
|
231 |
-
```
|
232 |
-
|
233 |
-
|
234 |
-
## π License
|
235 |
-
Llama 2 is licensed under the LLAMA 2 Community License,
|
236 |
-
Copyright (c) Meta Platforms, Inc. All Rights Reserved.
|
237 |
-
|
238 |
-
|
239 |
-
## βοΈ Citation
|
240 |
-
If you find our paper and code useful in your research:
|
241 |
-
|
242 |
-
```BibTeX
|
243 |
-
@misc{zhang2024llavanextvideo,
|
244 |
-
title={LLaVA-NeXT: A Strong Zero-shot Video Understanding Model},
|
245 |
-
url={https://llava-vl.github.io/blog/2024-04-30-llava-next-video/},
|
246 |
-
author={Zhang, Yuanhan and Li, Bo and Liu, haotian and Lee, Yong jae and Gui, Liangke and Fu, Di and Feng, Jiashi and Liu, Ziwei and Li, Chunyuan},
|
247 |
-
month={April},
|
248 |
-
year={2024}
|
249 |
-
}
|
250 |
-
```
|
251 |
-
|
252 |
-
```BibTeX
|
253 |
-
@misc{liu2024llavanext,
|
254 |
-
title={LLaVA-NeXT: Improved reasoning, OCR, and world knowledge},
|
255 |
-
url={https://llava-vl.github.io/blog/2024-01-30-llava-next/},
|
256 |
-
author={Liu, Haotian and Li, Chunyuan and Li, Yuheng and Li, Bo and Zhang, Yuanhan and Shen, Sheng and Lee, Yong Jae},
|
257 |
-
month={January},
|
258 |
-
year={2024}
|
259 |
-
}
|
260 |
-
```
|
|
|
7 |
- lmms-lab/VideoChatGPT
|
8 |
---
|
9 |
|
10 |
+
<!-- header start -->
|
11 |
+
<p align="center">
|
12 |
+
<img src="https://huggingface.co/datasets/FriendliAI/documentation-images/resolve/main/model-card-assets/friendliai.png" width="100%" alt="FriendliAI Logo">
|
13 |
+
</p>
|
14 |
+
<!-- header end -->
|
15 |
|
|
|
16 |
|
17 |
+
# llava-hf/LLaVA-NeXT-Video-7B-hf
|
18 |
|
19 |
+
* Model creator: [llava-hf](https://huggingface.co/llava-hf)
|
20 |
+
* Original model: [LLaVA-NeXT-Video-7B-hf](https://huggingface.co/llava-hf/LLaVA-NeXT-Video-7B-hf)
|
21 |
|
22 |
+
## Differences
|
|
|
|
|
|
|
23 |
|
24 |
+
* Added missing chat template to tokenizer_config.json
|
25 |
|
26 |
+
## License
|
27 |
|
28 |
+
Refer to the license of the original model card.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenizer_config.json
CHANGED
@@ -45,6 +45,7 @@
|
|
45 |
}
|
46 |
},
|
47 |
"bos_token": "<s>",
|
|
|
48 |
"clean_up_tokenization_spaces": false,
|
49 |
"eos_token": "</s>",
|
50 |
"extra_special_tokens": {
|
@@ -63,4 +64,4 @@
|
|
63 |
"unk_token": "<unk>",
|
64 |
"use_default_system_prompt": false,
|
65 |
"video_token": "<video>"
|
66 |
-
}
|
|
|
45 |
}
|
46 |
},
|
47 |
"bos_token": "<s>",
|
48 |
+
"chat_template": "{% for message in messages %}{% if message['role'] != 'system' %}{{ message['role'].upper() + ': '}}{% endif %}{# Render all images first #}{% for content in message['content'] | selectattr('type', 'equalto', 'image') %}{{ '<image>\n' }}{% endfor %}{# Render all images first #}{% for content in message['content'] | selectattr('type', 'equalto', 'video') %}{{ '<video>\n' }}{% endfor %}{# Render all text next #}{% if message['role'] != 'assistant' %}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{{ content['text'] + ' '}}{% endfor %}{% else %}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{% generation %}{{ content['text'] + ' '}}{% endgeneration %}{% endfor %}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ 'ASSISTANT:' }}{% endif %}",
|
49 |
"clean_up_tokenization_spaces": false,
|
50 |
"eos_token": "</s>",
|
51 |
"extra_special_tokens": {
|
|
|
64 |
"unk_token": "<unk>",
|
65 |
"use_default_system_prompt": false,
|
66 |
"video_token": "<video>"
|
67 |
+
}
|