Views
No views yet

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.1from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor, AutoModelForImageTextToText
2from qwen_vl_utils import process_vision_info
3from peft import PeftModel
4
5# default: Load the model on the available device(s)
6model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
7 "Qwen/Qwen2.5-VL-3B-Instruct",
8 device_map='auto',
9 torch_dtype=torch.bfloat16
10)
11processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct")
12
13# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
14# model = AutoModelForImageTextToText.from_pretrained(
15# "kxxinDave/Qwen2.5-VL-instruct-3B-Geo",
16# torch_dtype=torch.bfloat16,
17# attn_implementation="flash_attention_2",
18# device_map="auto",
19# )
20
21# Merge it with the adapters.
22adapterID = 'kxxinDave/Qwen2.5-VL-instruct-3B-Geo'
23model = PeftModel.from_pretrained(model, adapterID)
24
25
26# The default range for the number of visual tokens per image in the model is 4-16384.
27# 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.
28# min_pixels = 256*28*28
29# max_pixels = 1280*28*28
30# processor = AutoProcessor.from_pretrained("kxxinDave/Qwen2.5-VL-instruct-3B-Geo", min_pixels=min_pixels, max_pixels=max_pixels)
31
32messages = [
33 {
34 "role": "user",
35 "content": [
36 {
37 "type": "image",
38 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
39 },
40 {"type": "text", "text": "Describe this image."},
41 ],
42 }
43]
44
45# Preparation for inference
46text = processor.apply_chat_template(
47 messages, tokenize=False, add_generation_prompt=True
48)
49image_inputs, video_inputs = process_vision_info(messages)
50inputs = processor(
51 text=[text],
52 images=image_inputs,
53 videos=video_inputs,
54 padding=True,
55 return_tensors="pt",
56)
57inputs = inputs.to("cuda")
58
59# Inference: Generation of the output
60generated_ids = model.generate(**inputs, max_new_tokens=128)
61generated_ids_trimmed = [
62 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
63]
64output_text = processor.batch_decode(
65 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
66)
67print(output_text)@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}
}