MrLight commited on
Commit
01c67cc
1 Parent(s): 73cd5ff

Upload folder using huggingface_hub

Browse files
LICENSE ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2024 Alibaba Cloud
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
README.md CHANGED
@@ -1,14 +1,494 @@
1
  ---
 
2
  language:
3
  - en
4
- license: mit
5
- library_name: Tevatron
6
  tags:
7
- - vidore
8
- datasets:
9
- - Tevatron/docmatix-ir
10
- - HuggingFaceM4/Docmatix
11
- - Tevatron/msmarco-passage-aug
12
- - vidore/colpali_train_set
13
- - Tevatron/wiki-ss-nq
14
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-2B-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 2B 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-2B | MiniCPM-V 2.0 | **Qwen2-VL-2B** |
54
+ | :--- | :---: | :---: | :---: |
55
+ | MMMU<sub>val</sub> | 36.3 | 38.2 | **41.1** |
56
+ | DocVQA<sub>test</sub> | 86.9 | - | **90.1** |
57
+ | InfoVQA<sub>test</sub> | 58.9 | - | **65.5** |
58
+ | ChartQA<sub>test</sub> | **76.2** | - | 73.5 |
59
+ | TextVQA<sub>val</sub> | 73.4 | - | **79.7** |
60
+ | OCRBench | 781 | 605 | **794** |
61
+ | MTVQA | - | - | **20.0** |
62
+ | VCR<sub>en easy</sub> | - | - | **81.45**
63
+ | VCR<sub>zh easy</sub> | - | - | **46.16**
64
+ | RealWorldQA | 57.3 | 55.8 | **62.9** |
65
+ | MME<sub>sum</sub> | **1876.8** | 1808.6 | 1872.0 |
66
+ | MMBench-EN<sub>test</sub> | 73.2 | 69.1 | **74.9** |
67
+ | MMBench-CN<sub>test</sub> | 70.9 | 66.5 | **73.5** |
68
+ | MMBench-V1.1<sub>test</sub> | 69.6 | 65.8 | **72.2** |
69
+ | MMT-Bench<sub>test</sub> | - | - | **54.5** |
70
+ | MMStar | **49.8** | 39.1 | 48.0 |
71
+ | MMVet<sub>GPT-4-Turbo</sub> | 39.7 | 41.0 | **49.5** |
72
+ | HallBench<sub>avg</sub> | 38.0 | 36.1 | **41.7** |
73
+ | MathVista<sub>testmini</sub> | **46.0** | 39.8 | 43.0 |
74
+ | MathVision | - | - | **12.4** |
75
+
76
+ ### Video Benchmarks
77
+
78
+ | Benchmark | **Qwen2-VL-2B** |
79
+ | :--- | :---: |
80
+ | MVBench | **63.2** |
81
+ | PerceptionTest<sub>test</sub> | **53.9** |
82
+ | EgoSchema<sub>test</sub> | **54.9** |
83
+ | Video-MME<sub>wo/w subs</sub> | **55.6**/**60.4** |
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-2B-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-2B-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-2B-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-2B-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-2B-Instruct", torch_dtype="auto", device_map="auto"
177
+ )
178
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-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
+
219
+ <details>
220
+ <summary>Multi image inference</summary>
221
+
222
+ ```python
223
+ # Messages containing multiple images and a text query
224
+ messages = [
225
+ {
226
+ "role": "user",
227
+ "content": [
228
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
229
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
230
+ {"type": "text", "text": "Identify the similarities between these images."},
231
+ ],
232
+ }
233
+ ]
234
+
235
+ # Preparation for inference
236
+ text = processor.apply_chat_template(
237
+ messages, tokenize=False, add_generation_prompt=True
238
+ )
239
+ image_inputs, video_inputs = process_vision_info(messages)
240
+ inputs = processor(
241
+ text=[text],
242
+ images=image_inputs,
243
+ videos=video_inputs,
244
+ padding=True,
245
+ return_tensors="pt",
246
+ )
247
+ inputs = inputs.to("cuda")
248
+
249
+ # Inference
250
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
251
+ generated_ids_trimmed = [
252
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
253
+ ]
254
+ output_text = processor.batch_decode(
255
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
256
+ )
257
+ print(output_text)
258
+ ```
259
+ </details>
260
+
261
+ <details>
262
+ <summary>Video inference</summary>
263
+
264
+ ```python
265
+ # Messages containing a images list as a video and a text query
266
+ messages = [
267
+ {
268
+ "role": "user",
269
+ "content": [
270
+ {
271
+ "type": "video",
272
+ "video": [
273
+ "file:///path/to/frame1.jpg",
274
+ "file:///path/to/frame2.jpg",
275
+ "file:///path/to/frame3.jpg",
276
+ "file:///path/to/frame4.jpg",
277
+ ],
278
+ "fps": 1.0,
279
+ },
280
+ {"type": "text", "text": "Describe this video."},
281
+ ],
282
+ }
283
+ ]
284
+ # Messages containing a video and a text query
285
+ messages = [
286
+ {
287
+ "role": "user",
288
+ "content": [
289
+ {
290
+ "type": "video",
291
+ "video": "file:///path/to/video1.mp4",
292
+ "max_pixels": 360 * 420,
293
+ "fps": 1.0,
294
+ },
295
+ {"type": "text", "text": "Describe this video."},
296
+ ],
297
+ }
298
+ ]
299
+
300
+ # Preparation for inference
301
+ text = processor.apply_chat_template(
302
+ messages, tokenize=False, add_generation_prompt=True
303
+ )
304
+ image_inputs, video_inputs = process_vision_info(messages)
305
+ inputs = processor(
306
+ text=[text],
307
+ images=image_inputs,
308
+ videos=video_inputs,
309
+ padding=True,
310
+ return_tensors="pt",
311
+ )
312
+ inputs = inputs.to("cuda")
313
+
314
+ # Inference
315
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
316
+ generated_ids_trimmed = [
317
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
318
+ ]
319
+ output_text = processor.batch_decode(
320
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
321
+ )
322
+ print(output_text)
323
+ ```
324
+ </details>
325
+
326
+ <details>
327
+ <summary>Batch inference</summary>
328
+
329
+ ```python
330
+ # Sample messages for batch inference
331
+ messages1 = [
332
+ {
333
+ "role": "user",
334
+ "content": [
335
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
336
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
337
+ {"type": "text", "text": "What are the common elements in these pictures?"},
338
+ ],
339
+ }
340
+ ]
341
+ messages2 = [
342
+ {"role": "system", "content": "You are a helpful assistant."},
343
+ {"role": "user", "content": "Who are you?"},
344
+ ]
345
+ # Combine messages for batch processing
346
+ messages = [messages1, messages1]
347
+
348
+ # Preparation for batch inference
349
+ texts = [
350
+ processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
351
+ for msg in messages
352
+ ]
353
+ image_inputs, video_inputs = process_vision_info(messages)
354
+ inputs = processor(
355
+ text=texts,
356
+ images=image_inputs,
357
+ videos=video_inputs,
358
+ padding=True,
359
+ return_tensors="pt",
360
+ )
361
+ inputs = inputs.to("cuda")
362
+
363
+ # Batch Inference
364
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
365
+ generated_ids_trimmed = [
366
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
367
+ ]
368
+ output_texts = processor.batch_decode(
369
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
370
+ )
371
+ print(output_texts)
372
+ ```
373
+ </details>
374
+
375
+ ### More Usage Tips
376
+
377
+ For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
378
+
379
+ ```python
380
+ # You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
381
+ ## Local file path
382
+ messages = [
383
+ {
384
+ "role": "user",
385
+ "content": [
386
+ {"type": "image", "image": "file:///path/to/your/image.jpg"},
387
+ {"type": "text", "text": "Describe this image."},
388
+ ],
389
+ }
390
+ ]
391
+ ## Image URL
392
+ messages = [
393
+ {
394
+ "role": "user",
395
+ "content": [
396
+ {"type": "image", "image": "http://path/to/your/image.jpg"},
397
+ {"type": "text", "text": "Describe this image."},
398
+ ],
399
+ }
400
+ ]
401
+ ## Base64 encoded image
402
+ messages = [
403
+ {
404
+ "role": "user",
405
+ "content": [
406
+ {"type": "image", "image": "data:image;base64,/9j/..."},
407
+ {"type": "text", "text": "Describe this image."},
408
+ ],
409
+ }
410
+ ]
411
+ ```
412
+ #### Image Resolution for performance boost
413
+
414
+ 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.
415
+
416
+ ```python
417
+ min_pixels = 256 * 28 * 28
418
+ max_pixels = 1280 * 28 * 28
419
+ processor = AutoProcessor.from_pretrained(
420
+ "Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels
421
+ )
422
+ ```
423
+
424
+ Besides, We provide two methods for fine-grained control over the image size input to the model:
425
+
426
+ 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.
427
+
428
+ 2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
429
+
430
+ ```python
431
+ # min_pixels and max_pixels
432
+ messages = [
433
+ {
434
+ "role": "user",
435
+ "content": [
436
+ {
437
+ "type": "image",
438
+ "image": "file:///path/to/your/image.jpg",
439
+ "resized_height": 280,
440
+ "resized_width": 420,
441
+ },
442
+ {"type": "text", "text": "Describe this image."},
443
+ ],
444
+ }
445
+ ]
446
+ # resized_height and resized_width
447
+ messages = [
448
+ {
449
+ "role": "user",
450
+ "content": [
451
+ {
452
+ "type": "image",
453
+ "image": "file:///path/to/your/image.jpg",
454
+ "min_pixels": 50176,
455
+ "max_pixels": 50176,
456
+ },
457
+ {"type": "text", "text": "Describe this image."},
458
+ ],
459
+ }
460
+ ]
461
+ ```
462
+
463
+ ## Limitations
464
+
465
+ 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:
466
+
467
+ 1. Lack of Audio Support: The current model does **not comprehend audio information** within videos.
468
+ 2. Data timeliness: Our image dataset is **updated until June 2023**, and information subsequent to this date may not be covered.
469
+ 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.
470
+ 4. Limited Capacity for Complex Instruction: When faced with intricate multi-step instructions, the model's understanding and execution capabilities require enhancement.
471
+ 5. Insufficient Counting Accuracy: Particularly in complex scenes, the accuracy of object counting is not high, necessitating further improvements.
472
+ 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.
473
+
474
+ 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.
475
+
476
+
477
+ ## Citation
478
+
479
+ If you find our work helpful, feel free to give us a cite.
480
+
481
+ ```
482
+ @article{Qwen2-VL,
483
+ title={Qwen2-VL},
484
+ author={Qwen team},
485
+ year={2024}
486
+ }
487
+
488
+ @article{Qwen-VL,
489
+ title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
490
+ 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},
491
+ journal={arXiv preprint arXiv:2308.12966},
492
+ year={2023}
493
+ }
494
+ ```
chat_template.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "chat_template": "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}<|im_end|>\n{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}{% set image_count.value = image_count.value + 1 %}{% if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'video' or 'video' in content %}{% set video_count.value = video_count.value + 1 %}{% if add_vision_id %}Video {{ video_count.value }}: {% endif %}<|vision_start|><|video_pad|><|vision_end|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}<|im_end|>\n{% endif %}{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
3
+ }
config.json ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Qwen2VLForConditionalGeneration"
4
+ ],
5
+ "attention_dropout": 0.0,
6
+ "bos_token_id": 151643,
7
+ "eos_token_id": 151645,
8
+ "vision_start_token_id": 151652,
9
+ "vision_end_token_id": 151653,
10
+ "vision_token_id": 151654,
11
+ "image_token_id": 151655,
12
+ "video_token_id": 151656,
13
+ "hidden_act": "silu",
14
+ "hidden_size": 1536,
15
+ "initializer_range": 0.02,
16
+ "intermediate_size": 8960,
17
+ "max_position_embeddings": 32768,
18
+ "max_window_layers": 28,
19
+ "model_type": "qwen2_vl",
20
+ "num_attention_heads": 12,
21
+ "num_hidden_layers": 28,
22
+ "num_key_value_heads": 2,
23
+ "rms_norm_eps": 1e-06,
24
+ "rope_theta": 1000000.0,
25
+ "sliding_window": 32768,
26
+ "tie_word_embeddings": true,
27
+ "torch_dtype": "bfloat16",
28
+ "transformers_version": "4.41.2",
29
+ "use_cache": true,
30
+ "use_sliding_window": false,
31
+ "vision_config": {
32
+ "depth": 32,
33
+ "embed_dim": 1280,
34
+ "mlp_ratio": 4,
35
+ "num_heads": 16,
36
+ "in_chans": 3,
37
+ "hidden_size": 1536,
38
+ "patch_size": 14,
39
+ "spatial_merge_size": 2,
40
+ "spatial_patch_size": 14,
41
+ "temporal_patch_size": 2
42
+ },
43
+ "rope_scaling": {
44
+ "type": "mrope",
45
+ "mrope_section": [
46
+ 16,
47
+ 24,
48
+ 24
49
+ ]
50
+ },
51
+ "vocab_size": 151936
52
+ }
generation_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 151643,
3
+ "pad_token_id": 151643,
4
+ "do_sample": true,
5
+ "eos_token_id": [
6
+ 151645,
7
+ 151643
8
+ ],
9
+ "repetition_penalty": 1.0,
10
+ "temperature": 0.01,
11
+ "top_p": 0.001,
12
+ "top_k": 1,
13
+ "transformers_version": "4.37.0"
14
+ }
15
+
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
preprocessor_config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "min_pixels": 3136,
3
+ "max_pixels": 12845056,
4
+ "patch_size": 14,
5
+ "temporal_patch_size": 2,
6
+ "merge_size": 2,
7
+ "image_mean": [
8
+ 0.48145466,
9
+ 0.4578275,
10
+ 0.40821073
11
+ ],
12
+ "image_std": [
13
+ 0.26862954,
14
+ 0.26130258,
15
+ 0.27577711
16
+ ],
17
+ "image_processor_type": "Qwen2VLImageProcessor",
18
+ "processor_class": "Qwen2VLProcessor"
19
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cfca512a577da5343792bcfa53bd10e0019549b81537e694d578e539bc111798
3
+ size 4418066606
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "added_tokens_decoder": {
4
+ "151643": {
5
+ "content": "<|endoftext|>",
6
+ "lstrip": false,
7
+ "normalized": false,
8
+ "rstrip": false,
9
+ "single_word": false,
10
+ "special": true
11
+ },
12
+ "151644": {
13
+ "content": "<|im_start|>",
14
+ "lstrip": false,
15
+ "normalized": false,
16
+ "rstrip": false,
17
+ "single_word": false,
18
+ "special": true
19
+ },
20
+ "151645": {
21
+ "content": "<|im_end|>",
22
+ "lstrip": false,
23
+ "normalized": false,
24
+ "rstrip": false,
25
+ "single_word": false,
26
+ "special": true
27
+ },
28
+ "151646": {
29
+ "content": "<|object_ref_start|>",
30
+ "lstrip": false,
31
+ "normalized": false,
32
+ "rstrip": false,
33
+ "single_word": false,
34
+ "special": true
35
+ },
36
+ "151647": {
37
+ "content": "<|object_ref_end|>",
38
+ "lstrip": false,
39
+ "normalized": false,
40
+ "rstrip": false,
41
+ "single_word": false,
42
+ "special": true
43
+ },
44
+ "151648": {
45
+ "content": "<|box_start|>",
46
+ "lstrip": false,
47
+ "normalized": false,
48
+ "rstrip": false,
49
+ "single_word": false,
50
+ "special": true
51
+ },
52
+ "151649": {
53
+ "content": "<|box_end|>",
54
+ "lstrip": false,
55
+ "normalized": false,
56
+ "rstrip": false,
57
+ "single_word": false,
58
+ "special": true
59
+ },
60
+ "151650": {
61
+ "content": "<|quad_start|>",
62
+ "lstrip": false,
63
+ "normalized": false,
64
+ "rstrip": false,
65
+ "single_word": false,
66
+ "special": true
67
+ },
68
+ "151651": {
69
+ "content": "<|quad_end|>",
70
+ "lstrip": false,
71
+ "normalized": false,
72
+ "rstrip": false,
73
+ "single_word": false,
74
+ "special": true
75
+ },
76
+ "151652": {
77
+ "content": "<|vision_start|>",
78
+ "lstrip": false,
79
+ "normalized": false,
80
+ "rstrip": false,
81
+ "single_word": false,
82
+ "special": true
83
+ },
84
+ "151653": {
85
+ "content": "<|vision_end|>",
86
+ "lstrip": false,
87
+ "normalized": false,
88
+ "rstrip": false,
89
+ "single_word": false,
90
+ "special": true
91
+ },
92
+ "151654": {
93
+ "content": "<|vision_pad|>",
94
+ "lstrip": false,
95
+ "normalized": false,
96
+ "rstrip": false,
97
+ "single_word": false,
98
+ "special": true
99
+ },
100
+ "151655": {
101
+ "content": "<|image_pad|>",
102
+ "lstrip": false,
103
+ "normalized": false,
104
+ "rstrip": false,
105
+ "single_word": false,
106
+ "special": true
107
+ },
108
+ "151656": {
109
+ "content": "<|video_pad|>",
110
+ "lstrip": false,
111
+ "normalized": false,
112
+ "rstrip": false,
113
+ "single_word": false,
114
+ "special": true
115
+ }
116
+ },
117
+ "additional_special_tokens": ["<|im_start|>", "<|im_end|>", "<|object_ref_start|>","<|object_ref_end|>","<|box_start|>","<|box_end|>","<|quad_start|>","<|quad_end|>","<|vision_start|>","<|vision_end|>","<|vision_pad|>","<|image_pad|>","<|video_pad|>"],
118
+ "bos_token": null,
119
+ "chat_template": "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}<|im_end|>\n{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}{% set image_count.value = image_count.value + 1 %}{% if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'video' or 'video' in content %}{% set video_count.value = video_count.value + 1 %}{% if add_vision_id %}Video {{ video_count.value }}: {% endif %}<|vision_start|><|video_pad|><|vision_end|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}<|im_end|>\n{% endif %}{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}",
120
+ "clean_up_tokenization_spaces": false,
121
+ "eos_token": "<|im_end|>",
122
+ "padding_side": "left",
123
+ "errors": "replace",
124
+ "model_max_length": 32768,
125
+ "pad_token": "<|endoftext|>",
126
+ "split_special_tokens": false,
127
+ "tokenizer_class": "Qwen2Tokenizer",
128
+ "unk_token": null
129
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff