模型简介
模型特点
模型能力
使用案例
🚀 修复Qwen/Qwen2.5-VL-72B-Instruct-AWQ中的Bug
本项目是Qwen/Qwen2.5-VL-72B-Instruct-AWQ的一个分支,权重完全相同。通过对preprocessor_config.json应用补丁,修复了原模型中的此问题。
✨ 主要特性
在Qwen2-VL发布后的五个月里,众多开发者基于Qwen2-VL视觉语言模型构建了新的模型,并为我们提供了宝贵的反馈。在此期间,我们专注于构建更有用的视觉语言模型。如今,我们很高兴地向大家介绍Qwen家族的最新成员:Qwen2.5-VL。
关键增强功能:
- 视觉理解能力:Qwen2.5-VL不仅擅长识别花卉、鸟类、鱼类和昆虫等常见物体,还具备强大的图像文本、图表、图标、图形和布局分析能力。
- 自主智能体能力:Qwen2.5-VL可直接作为视觉智能体,能够进行推理并动态调用工具,具备计算机和手机使用能力。
- 长视频理解与事件捕捉:Qwen2.5-VL能够理解时长超过1小时的视频,并且此次新增了通过定位相关视频片段来捕捉事件的能力。
- 多格式视觉定位:Qwen2.5-VL可以通过生成边界框或点来精确地在图像中定位物体,并能为坐标和属性提供稳定的JSON输出。
- 结构化输出生成:对于发票、表单、表格等扫描数据,Qwen2.5-VL支持对其内容进行结构化输出,有助于金融、商业等领域的应用。
模型架构更新:
- 视频理解的动态分辨率和帧率训练:通过采用动态FPS采样,将动态分辨率扩展到时间维度,使模型能够理解不同采样率的视频。相应地,我们在时间维度上使用ID和绝对时间对齐更新了mRoPE,使模型能够学习时间序列和速度,并最终获得定位特定时刻的能力。
- 精简高效的视觉编码器:通过将窗口注意力策略性地应用于ViT,提高了训练和推理速度。ViT架构进一步通过SwiGLU和RMSNorm进行了优化,使其与Qwen2.5 LLM的结构保持一致。
我们有三个参数分别为30亿、70亿和720亿的模型。本仓库包含经过指令微调的72B Qwen2.5-VL模型。更多信息,请访问我们的博客和GitHub。
📦 安装指南
Qwen2.5-VL的代码已集成在最新的Hugging face transformers中,我们建议您使用以下命令从源代码进行安装:
pip install git+https://github.com/huggingface/transformers accelerate
否则,您可能会遇到以下错误:
KeyError: 'qwen2_5_vl'
我们还提供了一个工具包,帮助您更方便地处理各种类型的视觉输入,就像使用API一样。这包括base64编码、URL链接以及交错的图像和视频。您可以使用以下命令进行安装:
# 强烈建议使用 `[decord]` 特性以加快视频加载速度。
pip install qwen-vl-utils[decord]==0.0.8
如果您使用的不是Linux系统,可能无法从PyPI安装decord
。在这种情况下,您可以使用pip install qwen-vl-utils
,它将回退到使用torchvision进行视频处理。不过,您仍然可以从源代码安装decord,以便在加载视频时使用decord。
💻 使用示例
基础用法
以下是使用🤗 Transformers与Qwen2.5-VL进行聊天的代码示例:
from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
# 默认:将模型加载到可用设备上
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
"Qwen/Qwen2.5-VL-72B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
)
# 我们建议启用flash_attention_2以获得更好的加速和内存节省效果,特别是在多图像和视频场景中。
# model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
# "Qwen/Qwen2.5-VL-72B-Instruct-AWQ",
# torch_dtype=torch.bfloat16,
# attn_implementation="flash_attention_2",
# device_map="auto",
# )
# 默认处理器
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-72B-Instruct-AWQ")
# 模型中每张图像的视觉令牌数量默认范围是4 - 16384。
# 您可以根据需要设置min_pixels和max_pixels,例如令牌范围为256 - 1280,以平衡性能和成本。
# min_pixels = 256*28*28
# max_pixels = 1280*28*28
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-72B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels)
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
},
{"type": "text", "text": "Describe this image."},
],
}
]
# 推理准备
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to("cuda")
# 推理:生成输出
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
高级用法
更多使用提示
对于输入图像,我们支持本地文件、base64编码和URL链接。对于视频,目前我们仅支持本地文件。
# 您可以直接在文本中需要的位置插入本地文件路径、URL链接或base64编码的图像。
## 本地文件路径
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": "file:///path/to/your/image.jpg"},
{"type": "text", "text": "Describe this image."},
],
}
]
## 图像URL
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": "http://path/to/your/image.jpg"},
{"type": "text", "text": "Describe this image."},
],
}
]
## Base64编码的图像
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": "data:image;base64,/9j/..."},
{"type": "text", "text": "Describe this image."},
],
}
]
图像分辨率以提升性能
模型支持广泛的分辨率输入。默认情况下,它使用原始分辨率进行输入,但更高的分辨率可以提高性能,但会增加计算量。用户可以设置最小和最大像素数,以实现满足自身需求的最佳配置,例如令牌数量范围为256 - 1280,以平衡速度和内存使用。
min_pixels = 256 * 28 * 28
max_pixels = 1280 * 28 * 28
processor = AutoProcessor.from_pretrained(
"Qwen/Qwen2.5-VL-72B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels
)
此外,我们提供了两种方法来对输入到模型的图像大小进行细粒度控制:
- 定义min_pixels和max_pixels:图像将被调整大小以保持其纵横比在min_pixels和max_pixels的范围内。
- 指定确切的尺寸:直接设置
resized_height
和resized_width
。这些值将被四舍五入到最接近的28的倍数。
# min_pixels和max_pixels
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "file:///path/to/your/image.jpg",
"resized_height": 280,
"resized_width": 420,
},
{"type": "text", "text": "Describe this image."},
],
}
]
# resized_height和resized_width
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "file:///path/to/your/image.jpg",
"min_pixels": 50176,
"max_pixels": 50176,
},
{"type": "text", "text": "Describe this image."},
],
}
]
处理长文本
当前的config.json
设置的上下文长度最大为32,768个令牌。
为了处理超过32,768个令牌的大量输入,我们采用了YaRN技术,这是一种增强模型长度外推能力的技术,确保在长文本上的最佳性能。
对于支持的框架,您可以在config.json
中添加以下内容以启用YaRN:
{
...,
"type": "yarn",
"mrope_section": [
16,
24,
24
],
"factor": 4,
"original_max_position_embeddings": 32768
}
不过,需要注意的是,这种方法对时间和空间定位任务的性能有显著影响,因此不建议使用。
同时,对于长视频输入,由于MRoPE本身在使用ids方面更经济,因此可以直接将max_position_embeddings修改为更大的值,例如64k。
基准测试
量化模型的性能
本节报告了Qwen2.5-VL系列量化模型(包括GPTQ和AWQ)的生成性能。具体来说,我们报告以下指标:
- MMMU_VAL(准确率)
- DocVQA_VAL(准确率)
- MMBench_DEV_EN(准确率)
- MathVista_MINI(准确率)
我们使用VLMEvalkit对所有模型进行评估。
模型大小 | 量化方式 | 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 |
📚 详细文档
使用ModelScope
我们强烈建议用户(特别是中国大陆的用户)使用ModelScope。snapshot_download
可以帮助您解决下载检查点的问题。
📄 许可证
本项目采用Qwen许可证。
📖 引用
如果您觉得我们的工作有帮助,请随意引用我们的成果。
@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}
}








