Views
No views yet
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 AutoModelForImageTextToText, AutoTokenizer, AutoProcessor
2from qwen_vl_utils import process_vision_info
3
4# default: Load the model on the available device(s)
5model = AutoModelForImageTextToText.from_pretrained(
6 "kxxinDave/Qwen2_5VL-JudgePO", 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 = AutoModelForImageTextToText.from_pretrained(
11# "kxxinDave/Qwen2_5VL-JudgePO",
12# torch_dtype=torch.bfloat16,
13# attn_implementation="flash_attention_2",
14# device_map="auto",
15# )
16
17# default processer
18processor = AutoProcessor.from_pretrained("kxxinDave/Qwen2_5VL-JudgePO")
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("kxxinDave/Qwen2_5VL-JudgePO", 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)1# Messages containing multiple images and a text query
2messages = [
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": "Identify the similarities between these images."},
9 ],
10 }
11]
12
13# Preparation for inference
14text = processor.apply_chat_template(
15 messages, tokenize=False, add_generation_prompt=True
16)
17image_inputs, video_inputs = process_vision_info(messages)
18inputs = processor(
19 text=[text],
20 images=image_inputs,
21 videos=video_inputs,
22 padding=True,
23 return_tensors="pt",
24)
25inputs = inputs.to("cuda")
26
27# Inference
28generated_ids = model.generate(**inputs, max_new_tokens=128)
29generated_ids_trimmed = [
30 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
31]
32output_text = processor.batch_decode(
33 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
34)
35print(output_text)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)