Update README.md
Browse files
README.md
CHANGED
@@ -72,6 +72,357 @@ tags:
|
|
72 |
<em> <b>Figure 2.</b> showcases a comprehensive performance analysis of AIN-7B across CAMEL-Bench domains, comparing it with prominent closed-source models as well as open-source counterparts. <strong>OCR:</strong> "OCR & Document Understanding", <strong>Video:</strong> "General Video & Multi-Image Understanding", <strong>RS:</strong> "Remote Sensing Understanding", <strong>CDT:</strong> "Chart, Diagram & Table Understanding", <strong>Agro.:</strong> "Agricultural Image Understanding", <strong>Cultural:</strong> "Cultural-Specific Understanding", <strong>Medical:</strong> "Medical Image Understanding".
|
73 |
</em>
|
74 |
</h6>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
---
|
77 |
## ⚖️ Quantitative Evaluation and Results
|
|
|
72 |
<em> <b>Figure 2.</b> showcases a comprehensive performance analysis of AIN-7B across CAMEL-Bench domains, comparing it with prominent closed-source models as well as open-source counterparts. <strong>OCR:</strong> "OCR & Document Understanding", <strong>Video:</strong> "General Video & Multi-Image Understanding", <strong>RS:</strong> "Remote Sensing Understanding", <strong>CDT:</strong> "Chart, Diagram & Table Understanding", <strong>Agro.:</strong> "Agricultural Image Understanding", <strong>Cultural:</strong> "Cultural-Specific Understanding", <strong>Medical:</strong> "Medical Image Understanding".
|
73 |
</em>
|
74 |
</h6>
|
75 |
+
|
76 |
+
---
|
77 |
+
## ⚖️ Quick Start
|
78 |
+
Please install the qwen vision kit. This includes base64, URLs, and interleaved images and videos. You can install it using the following command:
|
79 |
+
|
80 |
+
```bash
|
81 |
+
pip install qwen-vl-utils
|
82 |
+
```
|
83 |
+
|
84 |
+
Here we show a code snippet to show you how to use the chat model with `transformers` and `qwen_vl_utils`:
|
85 |
+
|
86 |
+
```python
|
87 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
88 |
+
from qwen_vl_utils import process_vision_info
|
89 |
+
# default: Load the model on the available device(s)
|
90 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
91 |
+
"MBZUAI/AIN", torch_dtype="auto", device_map="auto"
|
92 |
+
)
|
93 |
+
# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
|
94 |
+
# model = Qwen2VLForConditionalGeneration.from_pretrained(
|
95 |
+
# "MBZUAI/AIN",
|
96 |
+
# torch_dtype=torch.bfloat16,
|
97 |
+
# attn_implementation="flash_attention_2",
|
98 |
+
# device_map="auto",
|
99 |
+
# )
|
100 |
+
# default processer
|
101 |
+
processor = AutoProcessor.from_pretrained("MBZUAI/AIN")
|
102 |
+
# 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.
|
103 |
+
# min_pixels = 256*28*28
|
104 |
+
# max_pixels = 1280*28*28
|
105 |
+
# processor = AutoProcessor.from_pretrained("MBZUAI/AIN", min_pixels=min_pixels, max_pixels=max_pixels)
|
106 |
+
messages = [
|
107 |
+
{
|
108 |
+
"role": "user",
|
109 |
+
"content": [
|
110 |
+
{
|
111 |
+
"type": "image",
|
112 |
+
"image": "https://huggingface.co/MBZUAI/AIN/resolve/main/assets_hf/demo_image.jpeg",
|
113 |
+
},
|
114 |
+
{"type": "text", "text": "يرجى وصف هذه الصورة."},
|
115 |
+
],
|
116 |
+
}
|
117 |
+
]
|
118 |
+
# Preparation for inference
|
119 |
+
text = processor.apply_chat_template(
|
120 |
+
messages, tokenize=False, add_generation_prompt=True
|
121 |
+
)
|
122 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
123 |
+
inputs = processor(
|
124 |
+
text=[text],
|
125 |
+
images=image_inputs,
|
126 |
+
videos=video_inputs,
|
127 |
+
padding=True,
|
128 |
+
return_tensors="pt",
|
129 |
+
)
|
130 |
+
inputs = inputs.to("cuda")
|
131 |
+
# Inference: Generation of the output
|
132 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
133 |
+
generated_ids_trimmed = [
|
134 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
135 |
+
]
|
136 |
+
output_text = processor.batch_decode(
|
137 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
138 |
+
)
|
139 |
+
print(output_text)
|
140 |
+
```
|
141 |
+
<details>
|
142 |
+
<summary>Without qwen_vl_utils</summary>
|
143 |
+
|
144 |
+
```python
|
145 |
+
from PIL import Image
|
146 |
+
import requests
|
147 |
+
import torch
|
148 |
+
from torchvision import io
|
149 |
+
from typing import Dict
|
150 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
151 |
+
# Load the model in half-precision on the available device(s)
|
152 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
153 |
+
"MBZUAI/AIN", torch_dtype="auto", device_map="auto"
|
154 |
+
)
|
155 |
+
processor = AutoProcessor.from_pretrained("MBZUAI/AIN")
|
156 |
+
# Image
|
157 |
+
url = "https://huggingface.co/MBZUAI/AIN/resolve/main/assets_hf/demo_image.jpeg"
|
158 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
159 |
+
conversation = [
|
160 |
+
{
|
161 |
+
"role": "user",
|
162 |
+
"content": [
|
163 |
+
{
|
164 |
+
"type": "image",
|
165 |
+
},
|
166 |
+
{"type": "text", "text": "Describe this image in Arabic."},
|
167 |
+
],
|
168 |
+
}
|
169 |
+
]
|
170 |
+
# Preprocess the inputs
|
171 |
+
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
172 |
+
# 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'
|
173 |
+
inputs = processor(
|
174 |
+
text=[text_prompt], images=[image], padding=True, return_tensors="pt"
|
175 |
+
)
|
176 |
+
inputs = inputs.to("cuda")
|
177 |
+
# Inference: Generation of the output
|
178 |
+
output_ids = model.generate(**inputs, max_new_tokens=128)
|
179 |
+
generated_ids = [
|
180 |
+
output_ids[len(input_ids) :]
|
181 |
+
for input_ids, output_ids in zip(inputs.input_ids, output_ids)
|
182 |
+
]
|
183 |
+
output_text = processor.batch_decode(
|
184 |
+
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
|
185 |
+
)
|
186 |
+
print(output_text)
|
187 |
+
```
|
188 |
+
</details>
|
189 |
+
<details>
|
190 |
+
<summary>Multi image inference</summary>
|
191 |
+
|
192 |
+
```python
|
193 |
+
# Messages containing multiple images and a text query
|
194 |
+
messages = [
|
195 |
+
{
|
196 |
+
"role": "user",
|
197 |
+
"content": [
|
198 |
+
{"type": "image", "image": "file:///path/to/image1.jpg"},
|
199 |
+
{"type": "image", "image": "file:///path/to/image2.jpg"},
|
200 |
+
{"type": "text", "text": "Identify the similarities between these images."},
|
201 |
+
],
|
202 |
+
}
|
203 |
+
]
|
204 |
+
# Preparation for inference
|
205 |
+
text = processor.apply_chat_template(
|
206 |
+
messages, tokenize=False, add_generation_prompt=True
|
207 |
+
)
|
208 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
209 |
+
inputs = processor(
|
210 |
+
text=[text],
|
211 |
+
images=image_inputs,
|
212 |
+
videos=video_inputs,
|
213 |
+
padding=True,
|
214 |
+
return_tensors="pt",
|
215 |
+
)
|
216 |
+
inputs = inputs.to("cuda")
|
217 |
+
# Inference
|
218 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
219 |
+
generated_ids_trimmed = [
|
220 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
221 |
+
]
|
222 |
+
output_text = processor.batch_decode(
|
223 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
224 |
+
)
|
225 |
+
print(output_text)
|
226 |
+
```
|
227 |
+
</details>
|
228 |
+
|
229 |
+
<details>
|
230 |
+
<summary>Video inference</summary>
|
231 |
+
|
232 |
+
```python
|
233 |
+
# Messages containing a images list as a video and a text query
|
234 |
+
messages = [
|
235 |
+
{
|
236 |
+
"role": "user",
|
237 |
+
"content": [
|
238 |
+
{
|
239 |
+
"type": "video",
|
240 |
+
"video": [
|
241 |
+
"file:///path/to/frame1.jpg",
|
242 |
+
"file:///path/to/frame2.jpg",
|
243 |
+
"file:///path/to/frame3.jpg",
|
244 |
+
"file:///path/to/frame4.jpg",
|
245 |
+
],
|
246 |
+
"fps": 1.0,
|
247 |
+
},
|
248 |
+
{"type": "text", "text": "Describe this video."},
|
249 |
+
],
|
250 |
+
}
|
251 |
+
]
|
252 |
+
# Messages containing a video and a text query
|
253 |
+
messages = [
|
254 |
+
{
|
255 |
+
"role": "user",
|
256 |
+
"content": [
|
257 |
+
{
|
258 |
+
"type": "video",
|
259 |
+
"video": "file:///path/to/video1.mp4",
|
260 |
+
"max_pixels": 360 * 420,
|
261 |
+
"fps": 1.0,
|
262 |
+
},
|
263 |
+
{"type": "text", "text": "Describe this video."},
|
264 |
+
],
|
265 |
+
}
|
266 |
+
]
|
267 |
+
# Preparation for inference
|
268 |
+
text = processor.apply_chat_template(
|
269 |
+
messages, tokenize=False, add_generation_prompt=True
|
270 |
+
)
|
271 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
272 |
+
inputs = processor(
|
273 |
+
text=[text],
|
274 |
+
images=image_inputs,
|
275 |
+
videos=video_inputs,
|
276 |
+
padding=True,
|
277 |
+
return_tensors="pt",
|
278 |
+
)
|
279 |
+
inputs = inputs.to("cuda")
|
280 |
+
# Inference
|
281 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
282 |
+
generated_ids_trimmed = [
|
283 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
284 |
+
]
|
285 |
+
output_text = processor.batch_decode(
|
286 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
287 |
+
)
|
288 |
+
print(output_text)
|
289 |
+
```
|
290 |
+
</details>
|
291 |
+
|
292 |
+
<details>
|
293 |
+
<summary>Batch inference</summary>
|
294 |
+
|
295 |
+
```python
|
296 |
+
# Sample messages for batch inference
|
297 |
+
messages1 = [
|
298 |
+
{
|
299 |
+
"role": "user",
|
300 |
+
"content": [
|
301 |
+
{"type": "image", "image": "file:///path/to/image1.jpg"},
|
302 |
+
{"type": "image", "image": "file:///path/to/image2.jpg"},
|
303 |
+
{"type": "text", "text": "What are the common elements in these pictures?"},
|
304 |
+
],
|
305 |
+
}
|
306 |
+
]
|
307 |
+
messages2 = [
|
308 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
309 |
+
{"role": "user", "content": "Who are you?"},
|
310 |
+
]
|
311 |
+
# Combine messages for batch processing
|
312 |
+
messages = [messages1, messages1]
|
313 |
+
# Preparation for batch inference
|
314 |
+
texts = [
|
315 |
+
processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
|
316 |
+
for msg in messages
|
317 |
+
]
|
318 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
319 |
+
inputs = processor(
|
320 |
+
text=texts,
|
321 |
+
images=image_inputs,
|
322 |
+
videos=video_inputs,
|
323 |
+
padding=True,
|
324 |
+
return_tensors="pt",
|
325 |
+
)
|
326 |
+
inputs = inputs.to("cuda")
|
327 |
+
# Batch Inference
|
328 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
329 |
+
generated_ids_trimmed = [
|
330 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
331 |
+
]
|
332 |
+
output_texts = processor.batch_decode(
|
333 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
334 |
+
)
|
335 |
+
print(output_texts)
|
336 |
+
```
|
337 |
+
</details>
|
338 |
+
|
339 |
+
### More Usage Tips
|
340 |
+
|
341 |
+
For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
|
342 |
+
|
343 |
+
```python
|
344 |
+
# You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
|
345 |
+
## Local file path
|
346 |
+
messages = [
|
347 |
+
{
|
348 |
+
"role": "user",
|
349 |
+
"content": [
|
350 |
+
{"type": "image", "image": "file:///path/to/your/image.jpg"},
|
351 |
+
{"type": "text", "text": "Describe this image."},
|
352 |
+
],
|
353 |
+
}
|
354 |
+
]
|
355 |
+
## Image URL
|
356 |
+
messages = [
|
357 |
+
{
|
358 |
+
"role": "user",
|
359 |
+
"content": [
|
360 |
+
{"type": "image", "image": "http://path/to/your/image.jpg"},
|
361 |
+
{"type": "text", "text": "Describe this image."},
|
362 |
+
],
|
363 |
+
}
|
364 |
+
]
|
365 |
+
## Base64 encoded image
|
366 |
+
messages = [
|
367 |
+
{
|
368 |
+
"role": "user",
|
369 |
+
"content": [
|
370 |
+
{"type": "image", "image": "data:image;base64,/9j/..."},
|
371 |
+
{"type": "text", "text": "Describe this image."},
|
372 |
+
],
|
373 |
+
}
|
374 |
+
]
|
375 |
+
```
|
376 |
+
#### Image Resolution for performance boost
|
377 |
+
|
378 |
+
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.
|
379 |
+
|
380 |
+
```python
|
381 |
+
min_pixels = 256 * 28 * 28
|
382 |
+
max_pixels = 1280 * 28 * 28
|
383 |
+
processor = AutoProcessor.from_pretrained(
|
384 |
+
"MBZUAI/AIN", min_pixels=min_pixels, max_pixels=max_pixels
|
385 |
+
)
|
386 |
+
```
|
387 |
+
|
388 |
+
Besides, We provide two methods for fine-grained control over the image size input to the model:
|
389 |
+
|
390 |
+
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.
|
391 |
+
|
392 |
+
2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
|
393 |
+
|
394 |
+
```python
|
395 |
+
# min_pixels and max_pixels
|
396 |
+
messages = [
|
397 |
+
{
|
398 |
+
"role": "user",
|
399 |
+
"content": [
|
400 |
+
{
|
401 |
+
"type": "image",
|
402 |
+
"image": "file:///path/to/your/image.jpg",
|
403 |
+
"resized_height": 280,
|
404 |
+
"resized_width": 420,
|
405 |
+
},
|
406 |
+
{"type": "text", "text": "Describe this image."},
|
407 |
+
],
|
408 |
+
}
|
409 |
+
]
|
410 |
+
# resized_height and resized_width
|
411 |
+
messages = [
|
412 |
+
{
|
413 |
+
"role": "user",
|
414 |
+
"content": [
|
415 |
+
{
|
416 |
+
"type": "image",
|
417 |
+
"image": "file:///path/to/your/image.jpg",
|
418 |
+
"min_pixels": 50176,
|
419 |
+
"max_pixels": 50176,
|
420 |
+
},
|
421 |
+
{"type": "text", "text": "Describe this image."},
|
422 |
+
],
|
423 |
+
}
|
424 |
+
]
|
425 |
+
```
|
426 |
|
427 |
---
|
428 |
## ⚖️ Quantitative Evaluation and Results
|