模型概述
模型特點
模型能力
使用案例
🚀 修復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}
}








