
pip install git+https://github.com/huggingface/transformers accelerateKeyError: 'qwen2_5_vl'pip install git+https://github.com/huggingface/transformers accelerateKeyError: 'qwen2_5_vl'1# It's highly recommanded to use `[decord]` feature for faster video loading.
2pip install qwen-vl-utils[decord]==0.0.8decord from PyPI. In that case, you can use pip install qwen-vl-utils which will fall back to using torchvision for video processing. However, you can still install decord from source to get decord used when loading video.transformers and qwen_vl_utils:1from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
2from qwen_vl_utils import process_vision_info
3
4# default: Load the model on the available device(s)
5model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
6 "Qwen/Qwen2.5-VL-7B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
7)
8
9# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
10# model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
11# "Qwen/Qwen2.5-VL-7B-Instruct-AWQ",
12# torch_dtype=torch.bfloat16,
13# attn_implementation="flash_attention_2",
14# device_map="auto",
15# )
16
17# default processer
18processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct-AWQ")
19
20# The default range for the number of visual tokens per image in the model is 4-16384.
21# You can set min_pixels and max_pixels according to your needs, such as a token range of 256-1280, to balance performance and cost.
22# min_pixels = 256*28*28
23# max_pixels = 1280*28*28
24# processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels)
25
26messages = [
27 {
28 "role": "user",
29 "content": [
30 {
31 "type": "image",
32 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
33 },
34 {"type": "text", "text": "Describe this image."},
35 ],
36 }
37]
38
39# Preparation for inference
40text = processor.apply_chat_template(
41 messages, tokenize=False, add_generation_prompt=True
42)
43image_inputs, video_inputs = process_vision_info(messages)
44inputs = processor(
45 text=[text],
46 images=image_inputs,
47 videos=video_inputs,
48 padding=True,
49 return_tensors="pt",
50)
51inputs = inputs.to("cuda")
52
53# Inference: Generation of the output
54generated_ids = model.generate(**inputs, max_new_tokens=128)
55generated_ids_trimmed = [
56 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
57]
58output_text = processor.batch_decode(
59 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
60)
61print(output_text)snapshot_download can help you solve issues concerning downloading checkpoints.1# You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
2## Local file path
3messages = [
4 {
5 "role": "user",
6 "content": [
7 {"type": "image", "image": "file:///path/to/your/image.jpg"},
8 {"type": "text", "text": "Describe this image."},
9 ],
10 }
11]
12## Image URL
13messages = [
14 {
15 "role": "user",
16 "content": [
17 {"type": "image", "image": "http://path/to/your/image.jpg"},
18 {"type": "text", "text": "Describe this image."},
19 ],
20 }
21]
22## Base64 encoded image
23messages = [
24 {
25 "role": "user",
26 "content": [
27 {"type": "image", "image": "data:image;base64,/9j/..."},
28 {"type": "text", "text": "Describe this image."},
29 ],
30 }
31]1min_pixels = 256 * 28 * 28
2max_pixels = 1280 * 28 * 28
3processor = AutoProcessor.from_pretrained(
4 "Qwen/Qwen2.5-VL-7B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels
5)resized_height and resized_width. These values will be rounded to the nearest multiple of 28.1# min_pixels and max_pixels
2messages = [
3 {
4 "role": "user",
5 "content": [
6 {
7 "type": "image",
8 "image": "file:///path/to/your/image.jpg",
9 "resized_height": 280,
10 "resized_width": 420,
11 },
12 {"type": "text", "text": "Describe this image."},
13 ],
14 }
15]
16# resized_height and resized_width
17messages = [
18 {
19 "role": "user",
20 "content": [
21 {
22 "type": "image",
23 "image": "file:///path/to/your/image.jpg",
24 "min_pixels": 50176,
25 "max_pixels": 50176,
26 },
27 {"type": "text", "text": "Describe this image."},
28 ],
29 }
30]config.json is set for context length up to 32,768 tokens.
To handle extensive inputs exceeding 32,768 tokens, we utilize YaRN, a technique for enhancing model length extrapolation, ensuring optimal performance on lengthy texts.config.json to enable YaRN:| Model Size | Quantization | MMMU_VAL | DocVQA_VAL | MMBench_EDV_EN | MathVista_MINI |
|---|---|---|---|---|---|
| Qwen2.5-VL-72B-Instruct | BF16 (🤗🤖) | 70.0 | 96.1 | 88.2 | 75.3 |
| AWQ (🤗🤖) | 69.1 | 96.0 | 87.9 | 73.8 | |
| Qwen2.5-VL-7B-Instruct | BF16 (🤗🤖) | 58.4 | 94.9 | 84.1 | 67.9 |
| AWQ (🤗🤖) | 55.6 | 94.6 | 84.2 | 64.7 | |
| Qwen2.5-VL-3B-Instruct | BF16 (🤗🤖) | 51.7 | 93.0 | 79.8 | 61.4 |
| AWQ (🤗🤖) | 49.1 | 91.8 | 78.0 | 58.8 |
@misc{qwen2.5-VL,
title = {Qwen2.5-VL},
url = {https://qwenlm.github.io/blog/qwen2.5-vl/},
author = {Qwen Team},
month = {January},
year = {2025}
}
@article{Qwen2VL,
title={Qwen2-VL: Enhancing Vision-Language Model's Perception of the World at Any Resolution},
author={Wang, Peng and Bai, Shuai and Tan, Sinan and Wang, Shijie and Fan, Zhihao and Bai, Jinze and Chen, Keqin and Liu, Xuejing and Wang, Jialin and Ge, Wenbin and Fan, Yang and Dang, Kai and Du, Mengfei and Ren, Xuancheng and Men, Rui and Liu, Dayiheng and Zhou, Chang and Zhou, Jingren and Lin, Junyang},
journal={arXiv preprint arXiv:2409.12191},
year={2024}
}
@article{Qwen-VL,
title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
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},
journal={arXiv preprint arXiv:2308.12966},
year={2023}
}