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
3 |
+
language:
|
4 |
+
- en
|
5 |
+
pipeline_tag: text-generation
|
6 |
+
tags:
|
7 |
+
- multimodal
|
8 |
+
base_model: Qwen/Qwen2-VL-7B-Instruct
|
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 |
+
* **Enhanced Image Comprehension**: We've significantly improved the model's ability to understand and interpret visual information, setting new benchmarks across key performance metrics.
|
22 |
+
|
23 |
+
* **Advanced Video Understanding**: Qwen2-VL now features superior online streaming capabilities, enabling real-time analysis of dynamic video content with remarkable accuracy.
|
24 |
+
|
25 |
+
* **Integrated Visual Agent Functionality**: Our model now seamlessly incorporates sophisticated system integration, transforming Qwen2-VL into a powerful visual agent capable of complex reasoning and decision-making.
|
26 |
+
|
27 |
+
* **Expanded Multilingual Support**: We've broadened our language capabilities to better serve a diverse global user base, making Qwen2-VL more accessible and effective across different linguistic contexts.
|
28 |
+
|
29 |
+
#### Model Architecture Updates:
|
30 |
+
|
31 |
+
* **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.
|
32 |
+
|
33 |
+
* **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.
|
34 |
+
|
35 |
+
![](https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-VL/qwen2_vl.jpg)
|
36 |
+
|
37 |
+
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).
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
## Evaluation
|
42 |
+
|
43 |
+
### Image Benchmarks
|
44 |
+
|
45 |
+
| Benchmark | InternVL2-8B | MiniCPM-V 2.6 | GPT-4o-mini | **Qwen2-VL-7B** |
|
46 |
+
| :--- | :---: | :---: | :---: | :---: |
|
47 |
+
| MMMU<sub>val</sub> | 51.8 | 49.8 | **60**| 54.1 |
|
48 |
+
| DocVQA<sub>test</sub> | 91.6 | 90.8 | - | **94.5** |
|
49 |
+
| InfoVQA<sub>test</sub> | 74.8 | - | - |**76.5** |
|
50 |
+
| ChartQA<sub>test</sub> | 83.3 | - |**85.7**| 83.0 |
|
51 |
+
| TextVQA<sub>val</sub> | 77.4 | 80.1 | -| **84.3** |
|
52 |
+
| OCRBench | 794 | **852** | 785 | 845 |
|
53 |
+
| MTVQA | - | - | -| **26.3** |
|
54 |
+
| RealWorldQA | 64.4 | - | - | **70.1** |
|
55 |
+
| MME<sub>sum</sub> | 2210.3 | **2348.4** | 2003.4| 2326.8 |
|
56 |
+
| MMBench-EN<sub>test</sub> | 81.7 | - | - | **83.0** |
|
57 |
+
| MMBench-CN<sub>test</sub> | **81.2** | - | - | 80.5 |
|
58 |
+
| MMBench-V1.1<sub>test</sub> | 79.4 | 78.0 | 76.0| **80.7** |
|
59 |
+
| MMT-Bench<sub>test</sub> | - | - | - |**63.7** |
|
60 |
+
| MMStar | **61.5** | 57.5 | 54.8 | 60.7 |
|
61 |
+
| MMVet<sub>GPT-4-Turbo</sub> | 54.2 | 60.0 | **66.9** | 62.0 |
|
62 |
+
| HallBench<sub>avg</sub> | 45.2 | 48.1 | 46.1| **50.6** |
|
63 |
+
| MathVista<sub>testmini</sub> | 58.3 | **60.6** | 52.4 | 58.2 |
|
64 |
+
| MathVision | - | - | - | **16.3** |
|
65 |
+
|
66 |
+
### Video Benchmarks
|
67 |
+
|
68 |
+
| Benchmark | Internvl2-8B | LLaVA-OneVision-7B | MiniCPM-V 2.6 | **Qwen2-VL-7B** |
|
69 |
+
| :--- | :---: | :---: | :---: | :---: |
|
70 |
+
| MVBench | 66.4 | 56.7 | - | **67.0** |
|
71 |
+
| PerceptionTest<sub>test</sub> | - | 57.1 | - | **62.3** |
|
72 |
+
| EgoSchema<sub>test</sub> | - | 60.1 | - | **66.7** |
|
73 |
+
| Video-MME<sub>wo/w subs</sub> | 54.0/56.9 | 58.2/- | 60.9/63.6 | **63.3**/**69.0** |
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
## Requirements
|
79 |
+
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:
|
80 |
+
```
|
81 |
+
KeyError: 'qwen2_vl'
|
82 |
+
```
|
83 |
+
|
84 |
+
## Quickstart
|
85 |
+
We offer a toolkit to help you handle various types of visual input more conveniently, as if you were using an API. This includes base64, URLs, and interleaved images and videos. You can install it using the following command:
|
86 |
+
|
87 |
+
```bash
|
88 |
+
pip install qwen-vl-utils
|
89 |
+
```
|
90 |
+
|
91 |
+
Here we show a code snippet to show you how to use the chat model with `transformers` and `qwen_vl_utils`:
|
92 |
+
|
93 |
+
```python
|
94 |
+
|
95 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
96 |
+
from qwen_vl_utils import process_vision_info
|
97 |
+
|
98 |
+
# default: Load the model on the available device(s)
|
99 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", device_map="auto")
|
100 |
+
|
101 |
+
# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
|
102 |
+
# model = Qwen2VLForConditionalGeneration.from_pretrained(
|
103 |
+
# "Qwen/Qwen2-VL-7B-Instruct",
|
104 |
+
# torch_dtype=torch.bfloat16,
|
105 |
+
# attn_implementation="flash_attention_2",
|
106 |
+
# device_map="auto",
|
107 |
+
# )
|
108 |
+
|
109 |
+
# default processer
|
110 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
|
111 |
+
|
112 |
+
# 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.
|
113 |
+
# min_pixels = 256*28*28
|
114 |
+
# max_pixels = 1280*28*28
|
115 |
+
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
|
116 |
+
|
117 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"}, {"type": "text", "text": "Describe this image."}]}]
|
118 |
+
|
119 |
+
# Preparation for inference
|
120 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
121 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
122 |
+
inputs = processor(text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt")
|
123 |
+
|
124 |
+
# Inference: Generation of the output
|
125 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
126 |
+
generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
|
127 |
+
output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
128 |
+
print(output_text)
|
129 |
+
```
|
130 |
+
<details>
|
131 |
+
<summary>without qwen_vl_utils</summary>
|
132 |
+
|
133 |
+
```python
|
134 |
+
|
135 |
+
from PIL import Image
|
136 |
+
import requests
|
137 |
+
import torch
|
138 |
+
from torchvision import io
|
139 |
+
from typing import Dict
|
140 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
141 |
+
|
142 |
+
# Load the model in half-precision on the available device(s)
|
143 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", device_map="auto")
|
144 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
|
145 |
+
|
146 |
+
# Image
|
147 |
+
url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
|
148 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
149 |
+
|
150 |
+
conversation = [
|
151 |
+
{
|
152 |
+
"role":"user",
|
153 |
+
"content":[
|
154 |
+
{
|
155 |
+
"type":"image",
|
156 |
+
},
|
157 |
+
{
|
158 |
+
"type":"text",
|
159 |
+
"text":"Describe this image."
|
160 |
+
}
|
161 |
+
]
|
162 |
+
}
|
163 |
+
]
|
164 |
+
|
165 |
+
|
166 |
+
# Preprocess the inputs
|
167 |
+
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
168 |
+
# 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'
|
169 |
+
|
170 |
+
inputs = processor(text=[text_prompt], images=[image], padding=True, return_tensors="pt")
|
171 |
+
inputs = inputs.to('cuda')
|
172 |
+
|
173 |
+
# Inference: Generation of the output
|
174 |
+
output_ids = model.generate(**inputs, max_new_tokens=128)
|
175 |
+
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
|
176 |
+
output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
177 |
+
print(output_text)
|
178 |
+
```
|
179 |
+
</details>
|
180 |
+
|
181 |
+
### More Usage Tips
|
182 |
+
|
183 |
+
For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
|
184 |
+
|
185 |
+
```python
|
186 |
+
# You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
|
187 |
+
## Local file path
|
188 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": "file:///path/to/your/image.jpg"}, {"type": "text", "text": "Describe this image."}]}]
|
189 |
+
## Image URL
|
190 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": "http://path/to/your/image.jpg"}, {"type": "text", "text": "Describe this image."}]}]
|
191 |
+
## Base64 encoded image
|
192 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": "data:image;base64,/9j/..."}, {"type": "text", "text": "Describe this image."}]}]
|
193 |
+
```
|
194 |
+
#### Image Resolution for performance boost
|
195 |
+
|
196 |
+
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.
|
197 |
+
|
198 |
+
```python
|
199 |
+
|
200 |
+
min_pixels = 256*28*28
|
201 |
+
max_pixels = 1280*28*28
|
202 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
|
203 |
+
|
204 |
+
```
|
205 |
+
|
206 |
+
Besides, We provide two methods for fine-grained control over the image size input to the model:
|
207 |
+
|
208 |
+
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.
|
209 |
+
|
210 |
+
2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
|
211 |
+
|
212 |
+
```python
|
213 |
+
# min_pixels and max_pixels
|
214 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": "file:///path/to/your/image.jpg", "resized_height": 280, "resized_width": 420}, {"type": "text", "text": "Describe this image."}]}]
|
215 |
+
# resized_height and resized_width
|
216 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": "file:///path/to/your/image.jpg", "min_pixels": 50176, "max_pixels": 50176}, {"type": "text", "text": "Describe this image."}]}]
|
217 |
+
```
|
218 |
+
|
219 |
+
**Limitations:**
|
220 |
+
|
221 |
+
1. Does not support audio extraction from videos.
|
222 |
+
2. Limited to data available up until June 2023.
|
223 |
+
3. Limited coverage of character/IP recognition.
|
224 |
+
4. Complex instruction following capabilities need enhancement.
|
225 |
+
5. Counting abilities, particularly in complex scenarios, require improvement.
|
226 |
+
6. Handling of complex charts by the model still needs refinement.
|
227 |
+
7. The model performs poorly in spatial relationship reasoning, especially in reasoning about object positions in a 3D space.
|
228 |
+
|
229 |
+
|
230 |
+
## Citation
|
231 |
+
|
232 |
+
If you find our work helpful, feel free to give us a cite.
|
233 |
+
|
234 |
+
```
|
235 |
+
@article{qwen2vl,
|
236 |
+
title={Qwen2-VL Technical Report},
|
237 |
+
year={2024}
|
238 |
+
}
|
239 |
+
```
|