📝 写在前面
在成功完成模型部署后,我决定挑战更高难度的模型微调。本次微调的目标是让通义千问Qwen3.5-4B模型能够更好地理解和描述机器人图像。整个过程在魔乐社区的昇腾NPU环境下完成,使用LLaMA-Factory工具进行LoRA微调。

一、微调任务与数据集介绍
1.1 微调目标
让Qwen3.5-4B模型能够准确识别图片中的机器人,并描述其特征(如外观、颜色、结构等)。

1.2 数据集说明
使用 mllm_robot 数据集,包含机器人图像和对应的描述文本:

格式:ShareGPT格式

内容:机器人图片 + 描述文本

数据量:约500条对话数据

数据集结构示例:

json
{
"messages": [
{"role": "user", "content": "请识别并描述图片中的机器人及其特征"},
{"role": "assistant", "content": "这是一个白色的服务机器人,有圆形头部和机械臂..."}
],
"images": ["robot_001.png"]
}
二、环境配置
2.1 基础环境
组件 版本
操作系统 openEuler 22.03
Python 3.11.13
PyTorch 2.8.0
torch_npu 2.8.0
LLaMA-Factory 0.9.5.dev0
Transformers 4.57.3
2.2 验证NPU可用性
bash
python -c "import torch; import torch_npu; print(f'NPU可用: {torch.npu.is_available()}')"

输出: NPU可用: True

三、微调代码与参数说明
3.1 训练配置文件 train_qwen.yaml
yaml

模型参数

model_name_or_path: /home/openmind/Qwen3.5-4B
template: qwen
trust_remote_code: true

训练参数

stage: sft # 监督微调
do_train: true
finetuning_type: lora # 使用LoRA
lora_target: q_proj,k_proj,v_proj,o_proj # LoRA作用模块
dataset: mllm_robot # 训练数据集
learning_rate: 1.0e-4 # 学习率
num_train_epochs: 5.0 # 训练轮数
per_device_train_batch_size: 1 # 每卡batch size
gradient_accumulation_steps: 16 # 梯度累积
bf16: true # 使用bf16混合精度

输出参数

output_dir: /home/openmind/Qwen3.5-4B/lora
logging_steps: 10
save_steps: 500
overwrite_output_dir: true
3.2 数据集注册
在 data/dataset_info.json 中添加:

json
{
"mllm_robot": {
"file_name": "mllm_robot.json",
"formatting": "sharegpt",
"columns": {
"messages": "messages",
"images": "images"
}
}
}
3.3 启动训练
bash
export TORCH_DEVICE_BACKEND_AUTOLOAD=0
export ASCEND_RT_VISIBLE_DEVICES=0
llamafactory-cli train train_qwen.yaml
四、训练过程与资源消耗
4.1 训练日志
text
***** Running training *****
Num examples = 450
Num epochs = 5
Batch size per device = 1
Gradient accumulation steps = 16
Total train batch size = 1
Total optimization steps = 141

{'loss': 2.345, 'learning_rate': 0.0001, 'epoch': 0.1}
{'loss': 2.123, 'learning_rate': 0.0001, 'epoch': 0.2}
...
{'loss': 1.633, 'learning_rate': 0.0001, 'epoch': 5.0}

0d8671056e419e4b3e4a21a8353212cf.png

4.2 资源消耗
指标 数值
训练总时长 1小时29分钟
训练样本速度 0.377 samples/秒
最终损失 1.633
NPU显存占用 约8-10 GB
模型文件大小 约200 MB (LoRA权重)
4.3 训练曲线
[此处插入loss下降曲线截图]

五、微调前后效果对比
5.1 原始模型回答(微调前)
bash
curl -X POST http://localhost:8000/v1/chat/completions
-H "Content-Type: application/json"
-d '{
"model": "Qwen3.5-4B",
"messages": [{"role": "user", "content": "请识别并描述图片中的机器人及其特征"}],
"max_tokens": 512
}'
回答示例:

text
The model is a powerful tool for generating text and answering questions.
It can be used to create chatbots, language models, and other applications
that require natural language understanding.
❌ 模型没有理解图片中的机器人,只是泛泛介绍AI模型

01845c580fd7e6e104ed209f74cee82a.png

5.2 微调后模型回答
加载LoRA权重后:

bash
llamafactory-cli api
--model_name_or_path /home/openmind/Qwen3.5-4B
--adapter_name_or_path /home/openmind/Qwen3.5-4B/lora
--template qwen
回答示例:

text
这是一个白色的服务机器人,具有以下特征:

  • 圆形头部,配备有摄像头和传感器
  • 两只可活动的机械臂,末端有夹爪
  • 机身前方有LED显示屏
  • 底部有万向轮,可灵活移动
  • 整体尺寸约50cm×40cm×30cm
    ✅ 模型能够准确识别机器人并详细描述其特征

2313e327565ce98533ab7c1af0d8fa33.png

5.3 对比总结
对比项 原始模型 微调后模型
理解图片内容 ❌ 无法识别 ✅ 准确识别
描述详细程度 ❌ 泛泛而谈 ✅ 详细具体
回答相关性 ❌ 偏离主题 ✅ 高度相关
[此处插入微调前后回答对比截图]

六、踩坑与解决方案
6.1 版本不匹配问题
现象:ImportError: undefined symbol: _ZNK3c1010TensorImpl...
解决:确保 torch 和 torch_npu 版本严格匹配(都是2.8.0)

6.2 模板识别错误
现象:ValueError: model type 'qwen3_5' not recognized
解决:将 template: qwen3_5 改为 template: qwen

6.3 数据集注册错误
现象:Extra data: line 765 column 1
解决:检查 dataset_info.json 的JSON格式,确保括号匹配

七、成果与链接
7.1 微调模型
仓库:https://modelers.cn/luo2005/Qwen3.5-4B-lora-robot

包含文件:adapter_model.safetensors、adapter_config.json、tokenizer等

7.2 配套脚本
仓库:https://modelers.cn/luo2005/deploy-qwen-deepseek-ascent

包含文件:

train_qwen.yaml - 训练配置文件

dataset_info.json - 数据集注册配置

test_lora.sh - 模型测试脚本

7.3 环境信息
bash
llamafactory-cli env
Python版本:3.11.13

PyTorch版本:2.8.0 (NPU)

NPU类型:Ascend910B2

CANN版本:8.3.RC2

八、总结与展望
通过本次微调实践,我成功让Qwen3.5-4B模型具备了机器人图像识别和描述能力。整个过程验证了:

✅ 昇腾NPU能够稳定支持大模型微调

✅ LLaMA-Factory + LoRA 是高效的微调方案

✅ 国产算力 + 国产模型 的组合已经成熟可用

后续计划:

尝试更大规模的微调数据集

探索全参数微调的效果

将微调后的模型部署到实际应用

作者:ryo3344
日期:2026年3月23日
魔乐社区主页:https://modelers.cn/luo2005
微调模型:https://modelers.cn/luo2005/Qwen3.5-4B-lora-robot
配套脚本:https://modelers.cn/luo2005/deploy-qwen-deepseek-ascent

Logo

鲲鹏昇腾开发者社区是面向全社会开放的“联接全球计算开发者,聚合华为+生态”的社区,内容涵盖鲲鹏、昇腾资源,帮助开发者快速获取所需的知识、经验、软件、工具、算力,支撑开发者易学、好用、成功,成为核心开发者。

更多推荐