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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: other
|
3 |
+
license_name: tongyi-qianwen
|
4 |
+
license_link: https://huggingface.co/Qwen/Qwen2-VL-72B-Instruct-AWQ/blob/main/LICENSE
|
5 |
+
language:
|
6 |
+
- en
|
7 |
+
pipeline_tag: image-text-to-text
|
8 |
+
tags:
|
9 |
+
- multimodal
|
10 |
+
base_model: Qwen/Qwen2-VL-72B-Instruct
|
11 |
+
---
|
12 |
+
|
13 |
+
# Qwen2-VL-72B-Instruct-AWQ
|
14 |
+
|
15 |
+
## Introduction
|
16 |
+
|
17 |
+
We're excited to unveil **Qwen2-VL**, the latest iteration of our Qwen-VL model, representing nearly a year of innovation.
|
18 |
+
|
19 |
+
### What’s New in Qwen2-VL?
|
20 |
+
|
21 |
+
#### Key Enhancements:
|
22 |
+
|
23 |
+
|
24 |
+
* **SoTA understanding of images of various resolution & ratio**: Qwen2-VL achieves state-of-the-art performance on visual understanding benchmarks, including MathVista, DocVQA, RealWorldQA, MTVQA, etc.
|
25 |
+
|
26 |
+
* **Understanding videos of 20min+**: Qwen2-VL can understand videos over 20 minutes for high-quality video-based question answering, dialog, content creation, etc.
|
27 |
+
|
28 |
+
* **Agent that can operate your mobiles, robots, etc.**: with the abilities of complex reasoning and decision making, Qwen2-VL can be integrated with devices like mobile phones, robots, etc., for automatic operation based on visual environment and text instructions.
|
29 |
+
|
30 |
+
* **Multilingual Support**: to serve global users, besides English and Chinese, Qwen2-VL now supports the understanding of texts in different languages inside images, including most European languages, Japanese, Korean, Arabic, Vietnamese, etc.
|
31 |
+
|
32 |
+
|
33 |
+
#### Model Architecture Updates:
|
34 |
+
|
35 |
+
* **Naive Dynamic Resolution**: Unlike before, Qwen2-VL can handle arbitrary image resolutions, mapping them into a dynamic number of visual tokens, offering a more human-like visual processing experience.
|
36 |
+
|
37 |
+
<p align="center">
|
38 |
+
<img src="https://qianwen-res.oss-accelerate-overseas.aliyuncs.com/Qwen2-VL/qwen2_vl.jpg" width="80%"/>
|
39 |
+
<p>
|
40 |
+
|
41 |
+
* **Multimodal Rotary Position Embedding (M-ROPE)**: Decomposes positional embedding into parts to capture 1D textual, 2D visual, and 3D video positional information, enhancing its multimodal processing capabilities.
|
42 |
+
|
43 |
+
<p align="center">
|
44 |
+
<img src="http://qianwen-res.oss-accelerate-overseas.aliyuncs.com/Qwen2-VL/mrope.png" width="80%"/>
|
45 |
+
<p>
|
46 |
+
|
47 |
+
We have three models with 2, 8 and 72 billion parameters. This repo contains the quantized instruction-tuned 72B Qwen2-VL model. For more information, visit our [Blog](https://qwenlm.github.io/blog/qwen2-vl/) and [GitHub](https://github.com/QwenLM/Qwen2-VL).
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
### Benchmark
|
52 |
+
#### Performance of Quantized Models
|
53 |
+
This section reports the generation performance of quantized models (including GPTQ and AWQ) of the Qwen2-VL series. Specifically, we report:
|
54 |
+
|
55 |
+
- MMMU_VAL (Accuracy)
|
56 |
+
- DocVQA_VAL (Accuracy)
|
57 |
+
- MMBench_DEV_EN (Accuracy)
|
58 |
+
- MathVista_MINI (Accuracy)
|
59 |
+
|
60 |
+
We use [VLMEvalkit](https://github.com/kq-chen/VLMEvalKit/tree/add_qwen2vl) to evaluate all models.
|
61 |
+
|
62 |
+
|
63 |
+
| Model Size | Quantization | MMMU | DocVQA | MMBench | MathVista |
|
64 |
+
| --- | --- | --- | --- | --- | --- |
|
65 |
+
| Qwen2-VL-72B-Instruct | BF16<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-72B-Instruct)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-72B-Instruct)) | 65.44 | 95.79 | 86.94 | 70.19 |
|
66 |
+
| | GPTQ-Int8<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-72B-Instruct-GPTQ-Int8)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-72B-Instruct-GPTQ-Int8)) | 64.56 | 95.84 | 87.03 | 68.90 |
|
67 |
+
| | GPTQ-Int4<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-72B-Instruct-GPTQ-Int4)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-72B-Instruct-GPTQ-Int4)) | 64.00 | 95.70 | 86.68 | 69.20 |
|
68 |
+
| | AWQ<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-72B-Instruct-AWQ)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-72B-Instruct-AWQ)) | 64.22 | 95.72 | 86.43 | 68.40 |
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
#### Speed Benchmark
|
73 |
+
This section reports the speed performance of bf16 models, quantized models (including GPTQ-Int4, GPTQ-Int8 and AWQ) of the Qwen2-VL series. Specifically, we report the inference speed (tokens/s) as well as memory footprint (GB) under the conditions of different context lengths.
|
74 |
+
|
75 |
+
The environment of the evaluation with huggingface transformers is:
|
76 |
+
|
77 |
+
- NVIDIA A100 80GB
|
78 |
+
- CUDA 11.8
|
79 |
+
- Pytorch 2.2.1+cu118
|
80 |
+
- Flash Attention 2.6.1
|
81 |
+
- Transformers 4.38.2
|
82 |
+
- AutoGPTQ 0.6.0+cu118
|
83 |
+
- AutoAWQ 0.2.5+cu118 (autoawq_kernels 0.0.6+cu118)
|
84 |
+
|
85 |
+
Note:
|
86 |
+
|
87 |
+
- We use the batch size of 1 and the least number of GPUs as possible for the evalution.
|
88 |
+
- We test the speed and memory of generating 2048 tokens with the input lengths of 1, 6144, 14336, 30720, 63488, and 129024 tokens (>32k is only avaliable for Qwen2-72B-Instuct and Qwen2-7B-Instuct).
|
89 |
+
|
90 |
+
- Qwen2-VL-72B (transformers)
|
91 |
+
|
92 |
+
| Model | Input Length | Quantization | GPU Num | Speed(tokens/s) | GPU Memory(GB) |
|
93 |
+
| --- | --- | --- | --- | --- | --- |
|
94 |
+
| Qwen2-VL-72B-Instruct | 1 | BF16 | 2 | 8.90 | 138.74 |
|
95 |
+
| | | GPTQ-Int8 | 2 | 9.53 | 75.173 |
|
96 |
+
| | | GPTQ-Int4 | 1 | 11.04 | 42.46 |
|
97 |
+
| | | AWQ | 1 | 12.00 | 41.98 |
|
98 |
+
| | 6144 | BF16 | 2 | 6.53 | 148.66 |
|
99 |
+
| | | GPTQ-Int8 | 2 | 6.97 | 85.09 |
|
100 |
+
| | | GPTQ-Int4 | 1 | 7.62 | 49.05 |
|
101 |
+
| | | AWQ | 1 | 8.33 | 48.58 |
|
102 |
+
| | 14336 | BF16 | 3 | 4.39 | 165.92 |
|
103 |
+
| | | GPTQ-Int8 | 2 | 5.04 | 99.31 |
|
104 |
+
| | | GPTQ-Int4 | 1 | 5.39 | 58.76 |
|
105 |
+
| | | AWQ | 1 | 5.72 | 58.29 |
|
106 |
+
| | 30720 | BF16 | 4 | 2.93 | 204.33 |
|
107 |
+
| | | GPTQ-Int8 | 2 | 3.16 | 127.77 |
|
108 |
+
| | | GPTQ-Int4 | 2 | 3.27 | 85.13 |
|
109 |
+
| | | AWQ | 2 | 3.39 | 94.65 |
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
## Requirements
|
116 |
+
The code of Qwen2-VL has been in the latest Hugging face transformers and we advise you to build from source with command `pip install git+https://github.com/huggingface/transformers`, or you might encounter the following error:
|
117 |
+
```
|
118 |
+
KeyError: 'qwen2_vl'
|
119 |
+
```
|
120 |
+
|
121 |
+
## Quickstart
|
122 |
+
We offer a toolkit to help you handle various types of visual input more conveniently. This includes base64, URLs, and interleaved images and videos. You can install it using the following command:
|
123 |
+
|
124 |
+
```bash
|
125 |
+
pip install qwen-vl-utils
|
126 |
+
```
|
127 |
+
|
128 |
+
Here we show a code snippet to show you how to use the chat model with `transformers` and `qwen_vl_utils`:
|
129 |
+
|
130 |
+
```python
|
131 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
132 |
+
from qwen_vl_utils import process_vision_info
|
133 |
+
|
134 |
+
# default: Load the model on the available device(s)
|
135 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
136 |
+
"Qwen/Qwen2-VL-72B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
|
137 |
+
)
|
138 |
+
|
139 |
+
# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
|
140 |
+
# model = Qwen2VLForConditionalGeneration.from_pretrained(
|
141 |
+
# "Qwen/Qwen2-VL-72B-Instruct-AWQ",
|
142 |
+
# torch_dtype=torch.bfloat16,
|
143 |
+
# attn_implementation="flash_attention_2",
|
144 |
+
# device_map="auto",
|
145 |
+
# )
|
146 |
+
|
147 |
+
# default processer
|
148 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-72B-Instruct-AWQ")
|
149 |
+
|
150 |
+
# The default range for the number of visual tokens per image in the model is 4-16384. You can set min_pixels and max_pixels according to your needs, such as a token count range of 256-1280, to balance speed and memory usage.
|
151 |
+
# min_pixels = 256*28*28
|
152 |
+
# max_pixels = 1280*28*28
|
153 |
+
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-72B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels)
|
154 |
+
|
155 |
+
messages = [
|
156 |
+
{
|
157 |
+
"role": "user",
|
158 |
+
"content": [
|
159 |
+
{
|
160 |
+
"type": "image",
|
161 |
+
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
|
162 |
+
},
|
163 |
+
{"type": "text", "text": "Describe this image."},
|
164 |
+
],
|
165 |
+
}
|
166 |
+
]
|
167 |
+
|
168 |
+
# Preparation for inference
|
169 |
+
text = processor.apply_chat_template(
|
170 |
+
messages, tokenize=False, add_generation_prompt=True
|
171 |
+
)
|
172 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
173 |
+
inputs = processor(
|
174 |
+
text=[text],
|
175 |
+
images=image_inputs,
|
176 |
+
videos=video_inputs,
|
177 |
+
padding=True,
|
178 |
+
return_tensors="pt",
|
179 |
+
)
|
180 |
+
inputs = inputs.to("cuda")
|
181 |
+
|
182 |
+
# Inference: Generation of the output
|
183 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
184 |
+
generated_ids_trimmed = [
|
185 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
186 |
+
]
|
187 |
+
output_text = processor.batch_decode(
|
188 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
189 |
+
)
|
190 |
+
print(output_text)
|
191 |
+
```
|
192 |
+
<details>
|
193 |
+
<summary>Without qwen_vl_utils</summary>
|
194 |
+
|
195 |
+
```python
|
196 |
+
from PIL import Image
|
197 |
+
import requests
|
198 |
+
import torch
|
199 |
+
from torchvision import io
|
200 |
+
from typing import Dict
|
201 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
202 |
+
|
203 |
+
# Load the model in half-precision on the available device(s)
|
204 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
205 |
+
"Qwen/Qwen2-VL-72B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
|
206 |
+
)
|
207 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-72B-Instruct-AWQ")
|
208 |
+
|
209 |
+
# Image
|
210 |
+
url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
|
211 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
212 |
+
|
213 |
+
conversation = [
|
214 |
+
{
|
215 |
+
"role": "user",
|
216 |
+
"content": [
|
217 |
+
{
|
218 |
+
"type": "image",
|
219 |
+
},
|
220 |
+
{"type": "text", "text": "Describe this image."},
|
221 |
+
],
|
222 |
+
}
|
223 |
+
]
|
224 |
+
|
225 |
+
|
226 |
+
# Preprocess the inputs
|
227 |
+
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
228 |
+
# Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
|
229 |
+
|
230 |
+
inputs = processor(
|
231 |
+
text=[text_prompt], images=[image], padding=True, return_tensors="pt"
|
232 |
+
)
|
233 |
+
inputs = inputs.to("cuda")
|
234 |
+
|
235 |
+
# Inference: Generation of the output
|
236 |
+
output_ids = model.generate(**inputs, max_new_tokens=128)
|
237 |
+
generated_ids = [
|
238 |
+
output_ids[len(input_ids) :]
|
239 |
+
for input_ids, output_ids in zip(inputs.input_ids, output_ids)
|
240 |
+
]
|
241 |
+
output_text = processor.batch_decode(
|
242 |
+
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
|
243 |
+
)
|
244 |
+
print(output_text)
|
245 |
+
```
|
246 |
+
</details>
|
247 |
+
<details>
|
248 |
+
<summary>Multi image inference</summary>
|
249 |
+
|
250 |
+
```python
|
251 |
+
# Messages containing multiple images and a text query
|
252 |
+
messages = [
|
253 |
+
{
|
254 |
+
"role": "user",
|
255 |
+
"content": [
|
256 |
+
{"type": "image", "image": "file:///path/to/image1.jpg"},
|
257 |
+
{"type": "image", "image": "file:///path/to/image2.jpg"},
|
258 |
+
{"type": "text", "text": "Identify the similarities between these images."},
|
259 |
+
],
|
260 |
+
}
|
261 |
+
]
|
262 |
+
|
263 |
+
# Preparation for inference
|
264 |
+
text = processor.apply_chat_template(
|
265 |
+
messages, tokenize=False, add_generation_prompt=True
|
266 |
+
)
|
267 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
268 |
+
inputs = processor(
|
269 |
+
text=[text],
|
270 |
+
images=image_inputs,
|
271 |
+
videos=video_inputs,
|
272 |
+
padding=True,
|
273 |
+
return_tensors="pt",
|
274 |
+
)
|
275 |
+
inputs = inputs.to("cuda")
|
276 |
+
|
277 |
+
# Inference
|
278 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
279 |
+
generated_ids_trimmed = [
|
280 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
281 |
+
]
|
282 |
+
output_text = processor.batch_decode(
|
283 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
284 |
+
)
|
285 |
+
print(output_text)
|
286 |
+
```
|
287 |
+
</details>
|
288 |
+
|
289 |
+
<details>
|
290 |
+
<summary>Video inference</summary>
|
291 |
+
|
292 |
+
```python
|
293 |
+
# Messages containing a images list as a video and a text query
|
294 |
+
messages = [
|
295 |
+
{
|
296 |
+
"role": "user",
|
297 |
+
"content": [
|
298 |
+
{
|
299 |
+
"type": "video",
|
300 |
+
"video": [
|
301 |
+
"file:///path/to/frame1.jpg",
|
302 |
+
"file:///path/to/frame2.jpg",
|
303 |
+
"file:///path/to/frame3.jpg",
|
304 |
+
"file:///path/to/frame4.jpg",
|
305 |
+
],
|
306 |
+
"fps": 1.0,
|
307 |
+
},
|
308 |
+
{"type": "text", "text": "Describe this video."},
|
309 |
+
],
|
310 |
+
}
|
311 |
+
]
|
312 |
+
# Messages containing a video and a text query
|
313 |
+
messages = [
|
314 |
+
{
|
315 |
+
"role": "user",
|
316 |
+
"content": [
|
317 |
+
{
|
318 |
+
"type": "video",
|
319 |
+
"video": "file:///path/to/video1.mp4",
|
320 |
+
"max_pixels": 360 * 420,
|
321 |
+
"fps": 1.0,
|
322 |
+
},
|
323 |
+
{"type": "text", "text": "Describe this video."},
|
324 |
+
],
|
325 |
+
}
|
326 |
+
]
|
327 |
+
|
328 |
+
# Preparation for inference
|
329 |
+
text = processor.apply_chat_template(
|
330 |
+
messages, tokenize=False, add_generation_prompt=True
|
331 |
+
)
|
332 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
333 |
+
inputs = processor(
|
334 |
+
text=[text],
|
335 |
+
images=image_inputs,
|
336 |
+
videos=video_inputs,
|
337 |
+
padding=True,
|
338 |
+
return_tensors="pt",
|
339 |
+
)
|
340 |
+
inputs = inputs.to("cuda")
|
341 |
+
|
342 |
+
# Inference
|
343 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
344 |
+
generated_ids_trimmed = [
|
345 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
346 |
+
]
|
347 |
+
output_text = processor.batch_decode(
|
348 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
349 |
+
)
|
350 |
+
print(output_text)
|
351 |
+
```
|
352 |
+
</details>
|
353 |
+
|
354 |
+
<details>
|
355 |
+
<summary>Batch inference</summary>
|
356 |
+
|
357 |
+
```python
|
358 |
+
# Sample messages for batch inference
|
359 |
+
messages1 = [
|
360 |
+
{
|
361 |
+
"role": "user",
|
362 |
+
"content": [
|
363 |
+
{"type": "image", "image": "file:///path/to/image1.jpg"},
|
364 |
+
{"type": "image", "image": "file:///path/to/image2.jpg"},
|
365 |
+
{"type": "text", "text": "What are the common elements in these pictures?"},
|
366 |
+
],
|
367 |
+
}
|
368 |
+
]
|
369 |
+
messages2 = [
|
370 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
371 |
+
{"role": "user", "content": "Who are you?"},
|
372 |
+
]
|
373 |
+
# Combine messages for batch processing
|
374 |
+
messages = [messages1, messages1]
|
375 |
+
|
376 |
+
# Preparation for batch inference
|
377 |
+
texts = [
|
378 |
+
processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
|
379 |
+
for msg in messages
|
380 |
+
]
|
381 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
382 |
+
inputs = processor(
|
383 |
+
text=texts,
|
384 |
+
images=image_inputs,
|
385 |
+
videos=video_inputs,
|
386 |
+
padding=True,
|
387 |
+
return_tensors="pt",
|
388 |
+
)
|
389 |
+
inputs = inputs.to("cuda")
|
390 |
+
|
391 |
+
# Batch Inference
|
392 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
393 |
+
generated_ids_trimmed = [
|
394 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
395 |
+
]
|
396 |
+
output_texts = processor.batch_decode(
|
397 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
398 |
+
)
|
399 |
+
print(output_texts)
|
400 |
+
```
|
401 |
+
</details>
|
402 |
+
|
403 |
+
### More Usage Tips
|
404 |
+
|
405 |
+
For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
|
406 |
+
|
407 |
+
```python
|
408 |
+
# You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
|
409 |
+
## Local file path
|
410 |
+
messages = [
|
411 |
+
{
|
412 |
+
"role": "user",
|
413 |
+
"content": [
|
414 |
+
{"type": "image", "image": "file:///path/to/your/image.jpg"},
|
415 |
+
{"type": "text", "text": "Describe this image."},
|
416 |
+
],
|
417 |
+
}
|
418 |
+
]
|
419 |
+
## Image URL
|
420 |
+
messages = [
|
421 |
+
{
|
422 |
+
"role": "user",
|
423 |
+
"content": [
|
424 |
+
{"type": "image", "image": "http://path/to/your/image.jpg"},
|
425 |
+
{"type": "text", "text": "Describe this image."},
|
426 |
+
],
|
427 |
+
}
|
428 |
+
]
|
429 |
+
## Base64 encoded image
|
430 |
+
messages = [
|
431 |
+
{
|
432 |
+
"role": "user",
|
433 |
+
"content": [
|
434 |
+
{"type": "image", "image": "data:image;base64,/9j/..."},
|
435 |
+
{"type": "text", "text": "Describe this image."},
|
436 |
+
],
|
437 |
+
}
|
438 |
+
]
|
439 |
+
```
|
440 |
+
#### Image Resolution for performance boost
|
441 |
+
|
442 |
+
The model supports a wide range of resolution inputs. By default, it uses the native resolution for input, but higher resolutions can enhance performance at the cost of more computation. Users can set the minimum and maximum number of pixels to achieve an optimal configuration for their needs, such as a token count range of 256-1280, to balance speed and memory usage.
|
443 |
+
|
444 |
+
```python
|
445 |
+
min_pixels = 256 * 28 * 28
|
446 |
+
max_pixels = 1280 * 28 * 28
|
447 |
+
processor = AutoProcessor.from_pretrained(
|
448 |
+
"Qwen/Qwen2-VL-72B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels
|
449 |
+
)
|
450 |
+
```
|
451 |
+
|
452 |
+
Besides, We provide two methods for fine-grained control over the image size input to the model:
|
453 |
+
|
454 |
+
1. Define min_pixels and max_pixels: Images will be resized to maintain their aspect ratio within the range of min_pixels and max_pixels.
|
455 |
+
|
456 |
+
2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
|
457 |
+
|
458 |
+
```python
|
459 |
+
# min_pixels and max_pixels
|
460 |
+
messages = [
|
461 |
+
{
|
462 |
+
"role": "user",
|
463 |
+
"content": [
|
464 |
+
{
|
465 |
+
"type": "image",
|
466 |
+
"image": "file:///path/to/your/image.jpg",
|
467 |
+
"resized_height": 280,
|
468 |
+
"resized_width": 420,
|
469 |
+
},
|
470 |
+
{"type": "text", "text": "Describe this image."},
|
471 |
+
],
|
472 |
+
}
|
473 |
+
]
|
474 |
+
# resized_height and resized_width
|
475 |
+
messages = [
|
476 |
+
{
|
477 |
+
"role": "user",
|
478 |
+
"content": [
|
479 |
+
{
|
480 |
+
"type": "image",
|
481 |
+
"image": "file:///path/to/your/image.jpg",
|
482 |
+
"min_pixels": 50176,
|
483 |
+
"max_pixels": 50176,
|
484 |
+
},
|
485 |
+
{"type": "text", "text": "Describe this image."},
|
486 |
+
],
|
487 |
+
}
|
488 |
+
]
|
489 |
+
```
|
490 |
+
|
491 |
+
## Limitations
|
492 |
+
|
493 |
+
While Qwen2-VL are applicable to a wide range of visual tasks, it is equally important to understand its limitations. Here are some known restrictions:
|
494 |
+
|
495 |
+
1. Lack of Audio Support: The current model does **not comprehend audio information** within videos.
|
496 |
+
2. Data timeliness: Our image dataset is **updated until June 2023**, and information subsequent to this date may not be covered.
|
497 |
+
3. Constraints in Individuals and Intellectual Property (IP): The model's capacity to recognize specific individuals or IPs is limited, potentially failing to comprehensively cover all well-known personalities or brands.
|
498 |
+
4. Limited Capacity for Complex Instruction: When faced with intricate multi-step instructions, the model's understanding and execution capabilities require enhancement.
|
499 |
+
5. Insufficient Counting Accuracy: Particularly in complex scenes, the accuracy of object counting is not high, necessitating further improvements.
|
500 |
+
6. Weak Spatial Reasoning Skills: Especially in 3D spaces, the model's inference of object positional relationships is inadequate, making it difficult to precisely judge the relative positions of objects.
|
501 |
+
|
502 |
+
These limitations serve as ongoing directions for model optimization and improvement, and we are committed to continually enhancing the model's performance and scope of application.
|
503 |
+
|
504 |
+
|
505 |
+
## Citation
|
506 |
+
|
507 |
+
If you find our work helpful, feel free to give us a cite.
|
508 |
+
|
509 |
+
```
|
510 |
+
@article{Qwen2-VL,
|
511 |
+
title={Qwen2-VL},
|
512 |
+
author={Qwen team},
|
513 |
+
year={2024}
|
514 |
+
}
|
515 |
+
|
516 |
+
@article{Qwen-VL,
|
517 |
+
title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
|
518 |
+
author={Bai, Jinze and Bai, Shuai and Yang, Shusheng and Wang, Shijie and Tan, Sinan and Wang, Peng and Lin, Junyang and Zhou, Chang and Zhou, Jingren},
|
519 |
+
journal={arXiv preprint arXiv:2308.12966},
|
520 |
+
year={2023}
|
521 |
+
}
|
522 |
+
```
|