Views
No views yet
1from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
2from qwen_vl_utils import process_vision_info
3
4# Load the merged model
5model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
6 "YOUR_REPO_NAME/Qwen3-VL-1B-Merged",
7 torch_dtype="auto",
8 device_map="auto"
9)
10
11# Processor (tokenizer + image/video preprocessing)
12processor = AutoProcessor.from_pretrained("YOUR_REPO_NAME/Qwen3-VL-1B-Merged")
13
14# Example: Image + Text prompt
15messages = [
16 {
17 "role": "user",
18 "content": [
19 {"type": "image", "image": "file:///path/to/example.jpg"},
20 {"type": "text", "text": "Describe this image in detail."},
21 ],
22 }
23]
24
25# Prepare inputs
26text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
27image_inputs, video_inputs = process_vision_info(messages)
28inputs = processor(
29 text=[text],
30 images=image_inputs,
31 videos=video_inputs,
32 padding=True,
33 return_tensors="pt"
34).to("cuda")
35
36# Generate
37outputs = model.generate(**inputs, max_new_tokens=128)
38print(processor.batch_decode(outputs, skip_special_tokens=True))1# Messages containing a images list as a video and a text query
2messages = [
3 {
4 "role": "user",
5 "content": [
6 {
7 "type": "video",
8 "video": [
9 "file:///path/to/frame1.jpg",
10 "file:///path/to/frame2.jpg",
11 "file:///path/to/frame3.jpg",
12 "file:///path/to/frame4.jpg",
13 ],
14 },
15 {"type": "text", "text": "Describe this video."},
16 ],
17 }
18]
19
20# Messages containing a local video path and a text query
21messages = [
22 {
23 "role": "user",
24 "content": [
25 {
26 "type": "video",
27 "video": "file:///path/to/video1.mp4",
28 "max_pixels": 360 * 420,
29 "fps": 1.0,
30 },
31 {"type": "text", "text": "Describe this video."},
32 ],
33 }
34]
35
36# Messages containing a video url and a text query
37messages = [
38 {
39 "role": "user",
40 "content": [
41 {
42 "type": "video",
43 "video": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-VL/space_woaudio.mp4",
44 },
45 {"type": "text", "text": "Describe this video."},
46 ],
47 }
48]
49
50#In Qwen 2.5 VL, frame rate information is also input into the model to align with absolute time.
51# Preparation for inference
52text = processor.apply_chat_template(
53 messages, tokenize=False, add_generation_prompt=True
54)
55image_inputs, video_inputs, video_kwargs = process_vision_info(messages, return_video_kwargs=True)
56inputs = processor(
57 text=[text],
58 images=image_inputs,
59 videos=video_inputs,
60 fps=fps,
61 padding=True,
62 return_tensors="pt",
63 **video_kwargs,
64)
65inputs = inputs.to("cuda")
66
67# Inference
68generated_ids = model.generate(**inputs, max_new_tokens=128)
69generated_ids_trimmed = [
70 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
71]
72output_text = processor.batch_decode(
73 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
74)
75print(output_text)FORCE_QWENVL_VIDEO_READER=torchvision or FORCE_QWENVL_VIDEO_READER=decord if you prefer not to use the default one.| Backend | HTTP | HTTPS |
|---|---|---|
| torchvision >= 0.19.0 | ✅ | ✅ |
| torchvision < 0.19.0 | ❌ | ❌ |
| decord | ✅ | ❌ |
1# Sample messages for batch inference
2messages1 = [
3 {
4 "role": "user",
5 "content": [
6 {"type": "image", "image": "file:///path/to/image1.jpg"},
7 {"type": "image", "image": "file:///path/to/image2.jpg"},
8 {"type": "text", "text": "What are the common elements in these pictures?"},
9 ],
10 }
11]
12messages2 = [
13 {"role": "system", "content": "You are a helpful assistant."},
14 {"role": "user", "content": "Who are you?"},
15]
16# Combine messages for batch processing
17messages = [messages1, messages2]
18
19# Preparation for batch inference
20texts = [
21 processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
22 for msg in messages
23]
24image_inputs, video_inputs = process_vision_info(messages)
25inputs = processor(
26 text=texts,
27 images=image_inputs,
28 videos=video_inputs,
29 padding=True,
30 return_tensors="pt",
31)
32inputs = inputs.to("cuda")
33
34# Batch Inference
35generated_ids = model.generate(**inputs, max_new_tokens=128)
36generated_ids_trimmed = [
37 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
38]
39output_texts = processor.batch_decode(
40 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
41)
42print(output_texts)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 "ViFortuneAIViFortuneAI/Qwen3-VL-1B-Merged", 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:{
...,
"type": "yarn",
"mrope_section": [
16,
24,
24
],
"factor": 4,
"original_max_position_embeddings": 32768
}@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}
}