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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ pipeline_tag: image-text-to-text
6
+ tags:
7
+ - multimodal
8
+ library_name: transformers
9
+ ---
10
+
11
+ # Qwen2-VL-7B-Instruct
12
+
13
+ ## Introduction
14
+
15
+ We're excited to unveil **Qwen2-VL**, the latest iteration of our Qwen-VL model, representing nearly a year of innovation.
16
+
17
+ ### What’s New in Qwen2-VL?
18
+
19
+ #### Key Enhancements:
20
+
21
+
22
+ * **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.
23
+
24
+ * **Understanding videos of 20min+**: Qwen2-VL can understand videos over 20 minutes for high-quality video-based question answering, dialog, content creation, etc.
25
+
26
+ * **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.
27
+
28
+ * **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.
29
+
30
+
31
+ #### Model Architecture Updates:
32
+
33
+ * **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.
34
+
35
+ <p align="center">
36
+ <img src="https://qianwen-res.oss-accelerate-overseas.aliyuncs.com/Qwen2-VL/qwen2_vl.jpg" width="80%"/>
37
+ <p>
38
+
39
+ * **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.
40
+
41
+ <p align="center">
42
+ <img src="http://qianwen-res.oss-accelerate-overseas.aliyuncs.com/Qwen2-VL/mrope.png" width="80%"/>
43
+ <p>
44
+
45
+ We have three models with 2, 7 and 72 billion parameters. This repo contains the instruction-tuned 7B Qwen2-VL model. For more information, visit our [Blog](https://qwenlm.github.io/blog/qwen2-vl/) and [GitHub](https://github.com/QwenLM/Qwen2-VL).
46
+
47
+
48
+
49
+ ## Evaluation
50
+
51
+ ### Image Benchmarks
52
+
53
+ | Benchmark | InternVL2-8B | MiniCPM-V 2.6 | GPT-4o-mini | **Qwen2-VL-7B** |
54
+ | :--- | :---: | :---: | :---: | :---: |
55
+ | MMMU<sub>val</sub> | 51.8 | 49.8 | **60**| 54.1 |
56
+ | DocVQA<sub>test</sub> | 91.6 | 90.8 | - | **94.5** |
57
+ | InfoVQA<sub>test</sub> | 74.8 | - | - |**76.5** |
58
+ | ChartQA<sub>test</sub> | **83.3** | - |- | 83.0 |
59
+ | TextVQA<sub>val</sub> | 77.4 | 80.1 | -| **84.3** |
60
+ | OCRBench | 794 | **852** | 785 | 845 |
61
+ | MTVQA | - | - | -| **26.3** |
62
+ | RealWorldQA | 64.4 | - | - | **70.1** |
63
+ | MME<sub>sum</sub> | 2210.3 | **2348.4** | 2003.4| 2326.8 |
64
+ | MMBench-EN<sub>test</sub> | 81.7 | - | - | **83.0** |
65
+ | MMBench-CN<sub>test</sub> | **81.2** | - | - | 80.5 |
66
+ | MMBench-V1.1<sub>test</sub> | 79.4 | 78.0 | 76.0| **80.7** |
67
+ | MMT-Bench<sub>test</sub> | - | - | - |**63.7** |
68
+ | MMStar | **61.5** | 57.5 | 54.8 | 60.7 |
69
+ | MMVet<sub>GPT-4-Turbo</sub> | 54.2 | 60.0 | **66.9** | 62.0 |
70
+ | HallBench<sub>avg</sub> | 45.2 | 48.1 | 46.1| **50.6** |
71
+ | MathVista<sub>testmini</sub> | 58.3 | **60.6** | 52.4 | 58.2 |
72
+ | MathVision | - | - | - | **16.3** |
73
+
74
+ ### Video Benchmarks
75
+
76
+ | Benchmark | Internvl2-8B | LLaVA-OneVision-7B | MiniCPM-V 2.6 | **Qwen2-VL-7B** |
77
+ | :--- | :---: | :---: | :---: | :---: |
78
+ | MVBench | 66.4 | 56.7 | - | **67.0** |
79
+ | PerceptionTest<sub>test</sub> | - | 57.1 | - | **62.3** |
80
+ | EgoSchema<sub>test</sub> | - | 60.1 | - | **66.7** |
81
+ | Video-MME<sub>wo/w subs</sub> | 54.0/56.9 | 58.2/- | 60.9/63.6 | **63.3**/**69.0** |
82
+
83
+
84
+
85
+
86
+ ## Requirements
87
+ 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:
88
+ ```
89
+ KeyError: 'qwen2_vl'
90
+ ```
91
+
92
+ ## Quickstart
93
+ 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:
94
+
95
+ ```bash
96
+ pip install qwen-vl-utils
97
+ ```
98
+
99
+ Here we show a code snippet to show you how to use the chat model with `transformers` and `qwen_vl_utils`:
100
+
101
+ ```python
102
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
103
+ from qwen_vl_utils import process_vision_info
104
+
105
+ # default: Load the model on the available device(s)
106
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
107
+ "Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
108
+ )
109
+
110
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
111
+ # model = Qwen2VLForConditionalGeneration.from_pretrained(
112
+ # "Qwen/Qwen2-VL-7B-Instruct",
113
+ # torch_dtype=torch.bfloat16,
114
+ # attn_implementation="flash_attention_2",
115
+ # device_map="auto",
116
+ # )
117
+
118
+ # default processer
119
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
120
+
121
+ # 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.
122
+ # min_pixels = 256*28*28
123
+ # max_pixels = 1280*28*28
124
+ # processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
125
+
126
+ messages = [
127
+ {
128
+ "role": "user",
129
+ "content": [
130
+ {
131
+ "type": "image",
132
+ "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
133
+ },
134
+ {"type": "text", "text": "Describe this image."},
135
+ ],
136
+ }
137
+ ]
138
+
139
+ # Preparation for inference
140
+ text = processor.apply_chat_template(
141
+ messages, tokenize=False, add_generation_prompt=True
142
+ )
143
+ image_inputs, video_inputs = process_vision_info(messages)
144
+ inputs = processor(
145
+ text=[text],
146
+ images=image_inputs,
147
+ videos=video_inputs,
148
+ padding=True,
149
+ return_tensors="pt",
150
+ )
151
+ inputs = inputs.to("cuda")
152
+
153
+ # Inference: Generation of the output
154
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
155
+ generated_ids_trimmed = [
156
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
157
+ ]
158
+ output_text = processor.batch_decode(
159
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
160
+ )
161
+ print(output_text)
162
+ ```
163
+ <details>
164
+ <summary>Without qwen_vl_utils</summary>
165
+
166
+ ```python
167
+ from PIL import Image
168
+ import requests
169
+ import torch
170
+ from torchvision import io
171
+ from typing import Dict
172
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
173
+
174
+ # Load the model in half-precision on the available device(s)
175
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
176
+ "Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
177
+ )
178
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
179
+
180
+ # Image
181
+ url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
182
+ image = Image.open(requests.get(url, stream=True).raw)
183
+
184
+ conversation = [
185
+ {
186
+ "role": "user",
187
+ "content": [
188
+ {
189
+ "type": "image",
190
+ },
191
+ {"type": "text", "text": "Describe this image."},
192
+ ],
193
+ }
194
+ ]
195
+
196
+
197
+ # Preprocess the inputs
198
+ text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
199
+ # 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'
200
+
201
+ inputs = processor(
202
+ text=[text_prompt], images=[image], padding=True, return_tensors="pt"
203
+ )
204
+ inputs = inputs.to("cuda")
205
+
206
+ # Inference: Generation of the output
207
+ output_ids = model.generate(**inputs, max_new_tokens=128)
208
+ generated_ids = [
209
+ output_ids[len(input_ids) :]
210
+ for input_ids, output_ids in zip(inputs.input_ids, output_ids)
211
+ ]
212
+ output_text = processor.batch_decode(
213
+ generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
214
+ )
215
+ print(output_text)
216
+ ```
217
+ </details>
218
+ <details>
219
+ <summary>Multi image inference</summary>
220
+
221
+ ```python
222
+ # Messages containing multiple images and a text query
223
+ messages = [
224
+ {
225
+ "role": "user",
226
+ "content": [
227
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
228
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
229
+ {"type": "text", "text": "Identify the similarities between these images."},
230
+ ],
231
+ }
232
+ ]
233
+
234
+ # Preparation for inference
235
+ text = processor.apply_chat_template(
236
+ messages, tokenize=False, add_generation_prompt=True
237
+ )
238
+ image_inputs, video_inputs = process_vision_info(messages)
239
+ inputs = processor(
240
+ text=[text],
241
+ images=image_inputs,
242
+ videos=video_inputs,
243
+ padding=True,
244
+ return_tensors="pt",
245
+ )
246
+ inputs = inputs.to("cuda")
247
+
248
+ # Inference
249
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
250
+ generated_ids_trimmed = [
251
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
252
+ ]
253
+ output_text = processor.batch_decode(
254
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
255
+ )
256
+ print(output_text)
257
+ ```
258
+ </details>
259
+
260
+ <details>
261
+ <summary>Video inference</summary>
262
+
263
+ ```python
264
+ # Messages containing a images list as a video and a text query
265
+ messages = [
266
+ {
267
+ "role": "user",
268
+ "content": [
269
+ {
270
+ "type": "video",
271
+ "video": [
272
+ "file:///path/to/frame1.jpg",
273
+ "file:///path/to/frame2.jpg",
274
+ "file:///path/to/frame3.jpg",
275
+ "file:///path/to/frame4.jpg",
276
+ ],
277
+ "fps": 1.0,
278
+ },
279
+ {"type": "text", "text": "Describe this video."},
280
+ ],
281
+ }
282
+ ]
283
+ # Messages containing a video and a text query
284
+ messages = [
285
+ {
286
+ "role": "user",
287
+ "content": [
288
+ {
289
+ "type": "video",
290
+ "video": "file:///path/to/video1.mp4",
291
+ "max_pixels": 360 * 420,
292
+ "fps": 1.0,
293
+ },
294
+ {"type": "text", "text": "Describe this video."},
295
+ ],
296
+ }
297
+ ]
298
+
299
+ # Preparation for inference
300
+ text = processor.apply_chat_template(
301
+ messages, tokenize=False, add_generation_prompt=True
302
+ )
303
+ image_inputs, video_inputs = process_vision_info(messages)
304
+ inputs = processor(
305
+ text=[text],
306
+ images=image_inputs,
307
+ videos=video_inputs,
308
+ padding=True,
309
+ return_tensors="pt",
310
+ )
311
+ inputs = inputs.to("cuda")
312
+
313
+ # Inference
314
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
315
+ generated_ids_trimmed = [
316
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
317
+ ]
318
+ output_text = processor.batch_decode(
319
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
320
+ )
321
+ print(output_text)
322
+ ```
323
+ </details>
324
+
325
+ <details>
326
+ <summary>Batch inference</summary>
327
+
328
+ ```python
329
+ # Sample messages for batch inference
330
+ messages1 = [
331
+ {
332
+ "role": "user",
333
+ "content": [
334
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
335
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
336
+ {"type": "text", "text": "What are the common elements in these pictures?"},
337
+ ],
338
+ }
339
+ ]
340
+ messages2 = [
341
+ {"role": "system", "content": "You are a helpful assistant."},
342
+ {"role": "user", "content": "Who are you?"},
343
+ ]
344
+ # Combine messages for batch processing
345
+ messages = [messages1, messages1]
346
+
347
+ # Preparation for batch inference
348
+ texts = [
349
+ processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
350
+ for msg in messages
351
+ ]
352
+ image_inputs, video_inputs = process_vision_info(messages)
353
+ inputs = processor(
354
+ text=texts,
355
+ images=image_inputs,
356
+ videos=video_inputs,
357
+ padding=True,
358
+ return_tensors="pt",
359
+ )
360
+ inputs = inputs.to("cuda")
361
+
362
+ # Batch Inference
363
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
364
+ generated_ids_trimmed = [
365
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
366
+ ]
367
+ output_texts = processor.batch_decode(
368
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
369
+ )
370
+ print(output_texts)
371
+ ```
372
+ </details>
373
+
374
+ ### More Usage Tips
375
+
376
+ For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
377
+
378
+ ```python
379
+ # You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
380
+ ## Local file path
381
+ messages = [
382
+ {
383
+ "role": "user",
384
+ "content": [
385
+ {"type": "image", "image": "file:///path/to/your/image.jpg"},
386
+ {"type": "text", "text": "Describe this image."},
387
+ ],
388
+ }
389
+ ]
390
+ ## Image URL
391
+ messages = [
392
+ {
393
+ "role": "user",
394
+ "content": [
395
+ {"type": "image", "image": "http://path/to/your/image.jpg"},
396
+ {"type": "text", "text": "Describe this image."},
397
+ ],
398
+ }
399
+ ]
400
+ ## Base64 encoded image
401
+ messages = [
402
+ {
403
+ "role": "user",
404
+ "content": [
405
+ {"type": "image", "image": "data:image;base64,/9j/..."},
406
+ {"type": "text", "text": "Describe this image."},
407
+ ],
408
+ }
409
+ ]
410
+ ```
411
+ #### Image Resolution for performance boost
412
+
413
+ 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.
414
+
415
+ ```python
416
+ min_pixels = 256 * 28 * 28
417
+ max_pixels = 1280 * 28 * 28
418
+ processor = AutoProcessor.from_pretrained(
419
+ "Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels
420
+ )
421
+ ```
422
+
423
+ Besides, We provide two methods for fine-grained control over the image size input to the model:
424
+
425
+ 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.
426
+
427
+ 2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
428
+
429
+ ```python
430
+ # min_pixels and max_pixels
431
+ messages = [
432
+ {
433
+ "role": "user",
434
+ "content": [
435
+ {
436
+ "type": "image",
437
+ "image": "file:///path/to/your/image.jpg",
438
+ "resized_height": 280,
439
+ "resized_width": 420,
440
+ },
441
+ {"type": "text", "text": "Describe this image."},
442
+ ],
443
+ }
444
+ ]
445
+ # resized_height and resized_width
446
+ messages = [
447
+ {
448
+ "role": "user",
449
+ "content": [
450
+ {
451
+ "type": "image",
452
+ "image": "file:///path/to/your/image.jpg",
453
+ "min_pixels": 50176,
454
+ "max_pixels": 50176,
455
+ },
456
+ {"type": "text", "text": "Describe this image."},
457
+ ],
458
+ }
459
+ ]
460
+ ```
461
+
462
+ ## Limitations
463
+
464
+ 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:
465
+
466
+ 1. Lack of Audio Support: The current model does **not comprehend audio information** within videos.
467
+ 2. Data timeliness: Our image dataset is **updated until June 2023**, and information subsequent to this date may not be covered.
468
+ 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.
469
+ 4. Limited Capacity for Complex Instruction: When faced with intricate multi-step instructions, the model's understanding and execution capabilities require enhancement.
470
+ 5. Insufficient Counting Accuracy: Particularly in complex scenes, the accuracy of object counting is not high, necessitating further improvements.
471
+ 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.
472
+
473
+ 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.
474
+
475
+
476
+ ## Citation
477
+
478
+ If you find our work helpful, feel free to give us a cite.
479
+
480
+ ```
481
+ @article{Qwen2-VL,
482
+ title={Qwen2-VL},
483
+ author={Qwen team},
484
+ year={2024}
485
+ }
486
+
487
+ @article{Qwen-VL,
488
+ title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
489
+ 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},
490
+ journal={arXiv preprint arXiv:2308.12966},
491
+ year={2023}
492
+ }
493
+ ```