00.引言
年前,阿里通义千问团队推出了 Qwen2.5-VL 系列模型,获得了社区的广泛关注和积极反馈。在 Qwen2.5-VL 系列的基础上,研究团队使用强化学习持续优化模型,并使用 Apache 2.0 协议开源 32B 这个备受喜爱的参数规模的新 VL 模型—— Qwen2.5-VL-32B-Instruct。相比此前发布的 Qwen2.5-VL 系列模型,本次推出的 32B 模型的特点如下:
- 回复更符合人类主观偏好:调整了输出风格,使回答更加详细、格式更规范,并更符合人类偏好。
- 数学推理能力:复杂数学问题求解的准确性显著提升。
- 图像细粒度理解与推理:在图像解析、内容识别以及视觉逻辑推导等任务中表现出更强的准确性和细粒度分析能力。
01.性能表现
Qwen2.5-VL-32B与业内先进的同规模模型进行比较,包括近期推出的 Mistral-Small-3.1-24B 和 Gemma-3-27B-IT, Qwen2.5-VL-32B-Instruct 展现出了明显的优势,甚至超越了更大规模的 Qwen2-VL-72B-Instruct 模型。尤其是在多模态任务中,例如 MMMU、MMMU-Pro 和 MathVista,这些任务强调复杂的多步骤推理,Qwen2.5-VL-32B-Instruct 表现尤为突出。在注重主观用户体验评估的 MM-MT-Bench 基准测试中,该模型相较于其前代 Qwen2-VL-72B-Instruct 取得了显著进步。
除了在视觉能力上优秀,Qwen2.5-VL-32B-Instruct 在纯文本能力上也达到了同规模的最优表现。
关键词小编敲黑板:
- 性能卓越的开源模型 :作为一款开源的32B参数模型,在中等规模模型中表现尤为突出,性能可媲美GPT-4o,超越近期发布的Mistral和Gemma等热门模型,堪称同类中的佼佼者。
- 友好且灵活的开源协议 :采用Apache 2.0开源协议,赋予用户极大的自由度和灵活性,便于商业应用与二次开发,为开发者提供无忧的使用体验。
- 强化学习驱动的显著提升 :通过前沿的强化学习技术,重点优化了用户的主观满意度以及数学推理能力,带来更智能、更贴心的交互体验,满足多样化需求。
02.使用transformers本地推理
Qwen2.5的推理代码已经合入官方transformers库
from modelscope import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor from qwen_vl_utils import process_vision_info # default: Load the model on the available device(s) model = Qwen2_5_VLForConditionalGeneration.from_pretrained( "Qwen/Qwen2.5-VL-32B-Instruct", torch_dtype="auto", device_map="auto" ) # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios. # model = Qwen2_5_VLForConditionalGeneration.from_pretrained( # "Qwen/Qwen2.5-VL-32B-Instruct", # torch_dtype=torch.bfloat16, # attn_implementation="flash_attention_2", # device_map="auto", # ) # default processer processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct") # The default range for the number of visual tokens per image in the model is 4-16384. # 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. # min_pixels = 256*28*28 # max_pixels = 1280*28*28 # processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct", 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."}, ], } ] # Preparation for inference 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") # Inference: Generation of the output 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)
03.使用魔搭API-Inference直接调用
魔搭平台的API-Inference,也第一时间为Qwen2.5-VL系列模型提供了支持。魔搭的用户可通过API调用的方式,直接使用。具体API-Inference的使用方式可参见模型页面(例如 https://www.modelscope.cn/models/Qwen/Qwen2.5-VL-32B-Instruct)说明:
或者参见API-Inference文档:
https://www.modelscope.cn/docs/model-service/API-Inference/intro
这里以如下图片为例,调用API使用Qwen/Qwen2.5-VL-32B-Instruct模型。
from openai import OpenAI client = OpenAI( api_key="<MODELSCOPE_SDK_TOKEN>", # ModelScope Token base_url="https://api-inference.modelscope.cn/v1" ) response = client.chat.completions.create( model="Qwen/Qwen2.5-VL-32B-Instruct", # ModleScope Model-Id messages = [ { "role": "user", "content": [ { "type": "image_url", "image_url": {"url": "https://modelscope.oss-cn-beijing.aliyuncs.com/demo/images/bird-vl.jpg"} }, { "type": "text", "text": "Count the number of birds in the figure, including those that are only showing their heads. To ensure accuracy, first detect their key points, then give the total number." }, ], } ], stream=True ) for chunk in response: print(chunk.choices[0].delta.content, end='', flush=True)
感谢阿里云百炼平台提供背后算力支持。
04.模型微调
我们介绍使用ms-swift对Qwen/Qwen2.5-VL-32B-Instruct进行微调。ms-swift是魔搭社区官方提供的大模型与多模态大模型训练部署框架。ms-swift开源地址:https://github.com/modelscope/ms-swift
我们将展示可运行的微调demo,并给出自定义数据集的格式。
在开始微调之前,请确保您的环境已准备妥当。
git clone https://github.com/modelscope/ms-swift.git cd ms-swift pip install -e .
图像OCR微调脚本如下。更多任务,包括视频微调、grounding任务微调,可以参考这里:https://github.com/modelscope/ms-swift/tree/main/examples/train/multimodal
MAX_PIXELS=1003520 \ CUDA_VISIBLE_DEVICES=0 \ swift sft \ --model Qwen/Qwen2.5-VL-32B-Instruct \ --dataset AI-ModelScope/LaTeX_OCR:human_handwrite#20000 \ --train_type lora \ --torch_dtype bfloat16 \ --num_train_epochs 1 \ --per_device_train_batch_size 1 \ --per_device_eval_batch_size 1 \ --learning_rate 1e-4 \ --lora_rank 8 \ --lora_alpha 32 \ --target_modules all-linear \ --freeze_vit true \ --gradient_accumulation_steps 16 \ --eval_steps 50 \ --save_steps 50 \ --save_total_limit 5 \ --logging_steps 5 \ --max_length 2048 \ --output_dir output \ --warmup_ratio 0.05 \ --dataloader_num_workers 4
训练显存资源:
自定义数据集格式如下(system字段可选),只需要指定`--dataset <dataset_path>`即可:
{"messages": [{"role": "user", "content": "浙江的省会在哪?"}, {"role": "assistant", "content": "浙江的省会在杭州。"}]} {"messages": [{"role": "user", "content": "<image><image>两张图片有什么区别"}, {"role": "assistant", "content": "前一张是小猫,后一张是小狗"}], "images": ["/xxx/x.jpg", "/xxx/x.png"]} {"messages": [{"role": "system", "content": "你是个有用无害的助手"}, {"role": "user", "content": "<video>视频中是什么"}, {"role": "assistant", "content": "视频中是一只小狗在草地上奔跑"}], "videos": ["/xxx/x.mp4"]}
训练完成后,使用以下命令对训练时的验证集进行推理:
CUDA_VISIBLE_DEVICES=0 \ swift infer \ --adapters output/vx-xxx/checkpoint-xxx \ --stream false \ --max_batch_size 1 \ --load_data_args true \ --max_new_tokens 2048
推送模型到ModelScope:
CUDA_VISIBLE_DEVICES=0 \ swift export \ --adapters output/vx-xxx/checkpoint-xxx \ --push_to_hub true \ --hub_model_id '<your-model-id>' \ --hub_token '<your-sdk-token>'
05.下一步计划
尽管 Qwen2.5-VL-32B 在强化学习框架下优化了主观体验和数学推理能力——这主要基于“快速思考”模式,但研究团队下一步研究将聚焦于长且有效的推理过程,以突破视觉模型在处理高度复杂、多步骤视觉推理任务中的边界。
点击链接,即可跳转模型链接~