多模态学习-周报三十八
摘要
本周聚焦多模态大模型实践与推理模型技术突破。系统完成了Qwen2.5-VL多模态模型的完整部署流程,包括测试版Transformers库源码编译、多规格模型(3B/7B/72B)参数获取及显存需求验证;深入测试了模型的五大核心能力——多图识别、目标定位(边界框坐标输出)、OCR文字提取、文档解析和视频理解;同步研究了DeepSeek-R1推理模型的创新训练范式,重点分析了纯强化学习激励机制(准确率奖励+格式奖励)和多阶段训练策略。研究构建了从视觉理解到逻辑推理的完整技术认知体系。
Abstract
This week centered on multimodal model practice and reasoning model breakthroughs. Systematically completed the full deployment process of Qwen2.5-VL multimodal model, including test-version Transformers library source compilation, multi-scale model (3B/7B/72B) parameter acquisition, and VRAM requirement verification. Conducted in-depth testing of five core capabilities: multi-image recognition, object localization (bounding box coordinate output), OCR text extraction, document parsing, and video understanding. Simultaneously studied DeepSeek-R1’s innovative training paradigm, focusing on pure reinforcement learning (accuracy reward + format reward) and multi-stage training strategy. The research established a complete technical cognition system from visual understanding to logical reasoning.
1、Qwen2.5-VL实践
源码地址:https://github.com/QwenLM/Qwen2.5-VL
Blog地址:https://qwenlm.github.io/zh/blog/qwen2.5-vl/
模型地址:https://modelscope.cn/collections/Qwen25-VL-58fbb5d31f1d47
1.1 环境配置
1.基础环境
首先配置基础的python、pytorch环境和比较好安装的依赖包
# 环境配置
conda create -n qwen-vl python=3.10 -y
conda activate qwen-vl
# torch环境
pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cu118
# 其他依赖包安装
pip install numpy==1.26.2 # 注意这里指明不能安装大于2.x版本以上的numpy库
pip install accelerate
pip install qwen-vl-utils==0.0.10
2.开发测试版Transformers库源码安装
**注意:**需要安装最新版的 transformers 库,由于qwen2.5-vl模型加载方法定义在最新测试版的 transformers库中,普通 pip install transformers 不能安装到该部分方法,因此要从源代码上安装测试版的4.49.0.dev0。
具体方法就是访问下面的源码地址,下载zip包并解压到本地,命令行访问到下载包目录pip install . 安装即可。
源安装transformers库地址:https://github.com/huggingface/transformers
# 源安装transformers指令
cd transformers-main
pip install .


运行下面代码不会有红色报错,则表明安装成功:
# 测试最新测试版transformers安装是否成功
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
3.模型参数下载
这里选择使用魔搭社区的下载地址:https://modelscope.cn/collections/Qwen25-VL-58fbb5d31f1d47
可以看到官方开源了三个量级的多模态模型。分别是3B、7B、72B,下面首先以7B模型作为测试环境样例,后续使用将测试三个模型的不同表现。
pip install modelscope
modelscope download --model Qwen/Qwen2.5-VL-7B-Instruct --local_dir {文件目录}
1.2 基础使用(环境测试)
1.硬件要求
首先测试一下模型加载所需的GPU显存要求,可以用下面代码进行模型加载测试,其中需修改模型加载地址:model_path 和 GPU 选择:“1” 。
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # 设置模型加载GPU(0号GPU)
model_path = './Qwen2.5-VL-main' # 修改为本地模型下载地址
# 加载模型
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(model_path, torch_dtype="auto", device_map="auto")
processor = AutoProcessor.from_pretrained(model_path)
三个模型的加载空间记录在下表。
| 模型类别(根据参数量划分) | GPU显存要求 |
|---|---|
| Qwen2.5-VL-3B | 8G+ |
| Qwen2.5-VL-7B | 20G+ |
| Qwen2.5-VL-72B | 158G+ |
2.推理测试
运行下面代码就可以完成使用模型进行单一图片基本推理的过程了。这里需要修改模型参数地址、输入图片地址和针对输入图片的文本提问内容。
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
from PIL import Image
# 根据实际情况修改
model_path = "path/to/save" # 修改为本地模型下载地址
img_path = "path/to/jpg" # 输入图片地址
question = "描述一下这张图片的内容。" # 针对图片的提问
# 加载模型
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(model_path, torch_dtype="auto", device_map="auto")
processor = AutoProcessor.from_pretrained(model_path)
# 输入配置
image = Image.open(img_path)
messages = [
{
"role": "user",
"content": [
{
"type": "image",
},
{"type": "text", "text": question},
],
}
]
text_prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=[text_prompt], images=[image], 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)
这里测试使用的是目标检测任务中经典的coco8数据集中的一张关于长颈鹿的图片。模型使用Qwen2.5-VL-7B 的模型做推理。


1.3 实验
官方博客指南地址:https://qwenlm.github.io/zh/blog/qwen2.5-vl/
官方博客中介绍了最新更新的 Qwen2.5-VL 多模态大模型的强大功能和可能的应用场景。
1.多图识别
在上一章节测试环境中,已经可以看到 Qwen2.5-VL 对一张图片的内容识别提取能力了,现在将实验扩展到同时输入多张图片,并修改运行代码保存为 run_multi.py 文件。
注意需修改下面的图片地址为保存多张图片的文件夹地址,而且在测试中可以看到英文提问似乎给出的回答效果更好,因此此时提问内容选择英文,只需在最后加上“Please give their names in Chinese”,即可输出中文。
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
import os
model_path = 'path/to/save' # 修改为本地模型下载地址
img_path = "path/to/jpg" # 输入图片地址
question = "Please describe the entity target content in these images,Please give their names in Chinese."
# 加载模型
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(model_path, torch_dtype="auto", device_map="auto")
processor = AutoProcessor.from_pretrained(model_path)
# 输入配置
content = []
for file in os.listdir(img_path):
if file.lower().split('.')[-1] in ['jpg', 'tif', 'jpeg']:
imgdir = os.path.join(img_path, file)
content.append({"type": "image", "image": imgdir})
# 添加文本提问
content.append({"type": "text", "text": question})
messages = [
{
"role": "user",
"content": content,
}
]
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)



2.目标定位
Qwen2.5-VL 还能输出关注目标的定位框坐标信息,这对目标检测领域无疑是一个重大冲击。要得到目标框坐标信息,只需在提问中加入对坐标信息的索要即可,这十分符合人类的自然交流。
对之前的长颈鹿单张图片的测试代码(基础使用部分),现在只需将提问改成下面即可。
# 目标定位框信息提问
question = "Detect all objects in the image and return their locations in the form of coordinates. The format of output should be like {“bbox”: [x1, y1, x2, y2], “label”: the name of this object in Chinese}"
得到结果如下:
# Qwen2.5-VL-3B定位推理结果
['```json[
{"bbox_2d": [389, 67, 554, 350], "label": "长颈鹿"},
{"bbox_2d": [51, 352, 186, 408], "label": "长颈鹿"}
]```']
将其在原图可视化检查一下:
from PIL import Image, ImageDraw, ImageFont
def vis_box(img, cls_lst):
font_path = "C:\Windows\Fonts\SimHei.ttf"
draw = ImageDraw.Draw(img)
for idx, box in enumerate(cls_lst):
if len(box) == 4:
# 图片画框
draw.rectangle(box, outline="black", width=2)
elif len(box) == 5:
bbox= box[:4]
draw.rectangle(bbox, outline="black", width=2)
ord = f'{box[4]}'
conf_x, conf_y = box[2], box[1]
font_conf = ImageFont.truetype(font_path, 30)
draw.text((conf_x, conf_y), ord, fill='red', font=font_conf)
elif len(box) == 6:
b = box[:4]
conf, cls = box[5], box[4]
draw.rectangle(b, outline="black", width=2)
font_conf = ImageFont.truetype(font_path, 30)
conf = f'{conf}/{cls}'
conf_x, conf_y = box[2] + 5, box[1]
draw.text((conf_x, conf_y), conf, fill='red', font=font_conf)
return img
if __name__=='__main__':
cls_lst = [[389, 67, 554, 350, "长颈鹿"], [51, 352, 186, 408, "长颈鹿"]]
savedir = '/vis.jpg'
img = Image.open('/test.jpg')
vis_img = vis_box(img, cls_lst)
vis_img.save(savedir)

3.OCR
官方提到其多模态模型还具备强大的OCR文本提取能力,这里测试一下繁体古文。
官方给出的提示词如下:
question = "Read all texts in the image, output in lines. "
图片如下:

推理结果如下:
# Qwen2.5-VL-3B的OCR推理结果
['仕而未有禄者違而君亮弗爲服也違大夫\n之諸侯違諸侯之大夫不反照世子不爲']
4.文档解析
另一个特殊功能是文档图片解析成 html 格式文本,官方指南给出了很多示例
5.视频理解
视频同样支持输入,这里不做实验记录了。
需要注意的是视频输入的使用方式,写入 run_video.py 文件,代码如下:
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
import os
model_path = 'path/to/save' # 修改为本地模型下载地址
video_path = "path/to/jpg" # 输入视频地址
question = "Describe this video."
# 加载模型
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(model_path, torch_dtype="auto", device_map="auto")
processor = AutoProcessor.from_pretrained(model_path)
# 视频输入
messages = [
{
"role": "user",
"content": [
{
"type": "video",
"video": video_path,
"max_pixels": 360 * 420,
"fps": 1.0,
},
{"type": "text", "text": question},
],
}
]
# 输入配置
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs, video_kwargs = process_vision_info(messages, return_video_kwargs=True)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
fps=fps,
padding=True,
return_tensors="pt",
**video_kwargs,
)
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)
2、DeepSeek-R1

2025年01月20日,deepseek 正式发布 DeepSeek-R1,并同步开源模型权重。
- 开源 DeepSeek-R1 推理大模型,与 o1 性能相近。
- 开源 DeepSeek-R1-Zero,预训练模型直接 RL,不走 SFT。
- 开源用 R1 数据蒸馏的 Qwen、Llama 系列小模型,蒸馏模型超过 o1-mini 和 QWQ。
模型开源的同时,技术报告也同步放出: DeepSeek-R1: Incentivizing Reasoning Capability in LLMs viaReinforcement Learning
2.1 背景
研究问题:如何通过强化学习(RL)有效提升大型语言模型(LLM)的推理能力?
问题背景:
- 近年来,LLM 在各个领域都取得了显著进展,但推理能力仍有提升空间。
- 之前的研究大多依赖于大量的 SFT 数据,但获取高质量的 SFT 数据成本高昂。
- OpenAI 的 o1 系列模型通过增加思维链(Chain-of-Thought, CoT)推理过程的长度来提升推理能力,但如何有效进行测试时(test-time)扩展仍是开放问题。
- 一些研究尝试使用基于过程的奖励模型(PRM)、强化学习和搜索算法(MCTS)来解决推理问题,但没有达到 OpenAI 的 o1 系列模型的通用推理性能水平。
论文动机: 探索是否可以通过纯强化学习来让 LLM 自主发展推理能力,而无需依赖 SFT 数据。
2.2 主要贡献
| 模型 | 方法 |
|---|---|
| DeepSeek-R1-Zero | 纯强化学习 |
| DeepSeek-R1 | 冷启动 SFT -> RL -> COT + 通用数据 SFT(80w)->全场景 RL |
| 蒸馏小模型 | 直接用上面的 80w 数据进行SFT |
- 首次验证了纯强化学习在 LLM 中显著增强推理能力的可行性(DeepSeek-R1-Zero),即无需预先的 SFT 数据,仅通过 RL 即可激励模型学会长链推理和反思等能力。
- 提出了多阶段训练策略(冷启动->RL->SFT->全场景 RL),有效兼顾准确率与可读性,产出 DeepSeek-R1,性能比肩 OpenAI-o1-1217。
- 展示了知识蒸馏在提升小模型推理能力方面的潜力,并开源多个大小不一的蒸馏模型(1.5B~70B),为社区提供了可在低资源环境中也能获得高推理能力的模型选择。
2.3 DeepSeek-R1-Zero
DeepSeek-R1-Zero 直接在基础模型上应用强化学习,不使用任何 SFT 数据。 为了训练 DeepSeek-R1-Zero,deepseek 采用了一种基于规则的奖励系统,该系统主要由两种奖励组成:
- 准确率奖励:准确率奖励模型评估响应是否正确。例如,在具有确定性结果的数学问题中,模型需要以指定的格式(box)提供最终答案,从而能够通过基于规则的验证来可靠地确认正确性。同样,对于 LeetCode 问题,可以使用编译器根据预定义的测试用例生成反馈。
- 格式奖励: 除了准确性奖励模型,还采用了一种格式奖励模型,要求模型将其思考过程放在 ‘’ 和 ‘’ 标签之间。
需要强调的是:deepseek 在训练 DeepSeek-R1-Zero 时没有使用结果奖励(ORM)或者过程奖励(PRM)。
在没有大量带「过程标签」(step-by-step annotation)的数据支撑下,模型如何知道自己的推理过程是否正确?
这里主要通过「结果判定」的方式:对于数学题、编程题等有客观正确答案的任务,可以把最终答案与标准结果对比给出奖励。虽没有逐步的过程标注,但最终答案正确与否足以在 RL 中当作回报(Reward)来引导模型学会更好的推理。
部分中间也会酌情使用格式奖励,用来约束模型输出思考过程,这是一种「作弊少、易维护」的思路。
总结
本周通过深度实践与理论分析相结合,构建了多模态与推理模型的完整技术认知:在Qwen2.5-VL实践层面,攻克了环境配置的关键难点,并全面测试了其多模态能力;在DeepSeek-R1理论层面,深入理解了其突破性训练策略。通过本周研究,不仅掌握了多模态模型部署与评估的全流程,更深入理解了推理能力激发的前沿方法论,为后续探索更复杂的多模态推理任务奠定了坚实基础。
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)