MiniCPM-V 4
MiniCPM-V 多模态系列
MiniCPM-V 4 是面壁智能推出的 4.1B 参数端侧多模态大模型,采用 SigLIP2-400M 视觉编码器与 MiniCPM4-3B(基于 LLaMA 架构)语言模型。HuggingFace 模型标识:openbmb/MiniCPM-V-4。
支持的微调方式:
- 官方脚本:LoRA 微调(finetune_lora.sh)、全参数微调(finetune_ds.sh)
- LLaMA-Factory:通过 MiniCPM-V 模板支持
数据准备
微调数据格式与 MiniCPM-V 2.6 一致,使用 JSON 文件,每条样本包含 id、image 和 conversations 字段。
单图数据示例:
JSON
[
{
"id": "0",
"image": "path/to/image.jpg",
"conversations": [
{"role": "user", "content": "<image>\n描述这张图片"},
{"role": "assistant", "content": "这是一张风景照片。"}
]
}
]
多图数据示例:
JSON
[
{
"id": "1",
"image": ["path/to/img1.jpg", "path/to/img2.jpg"],
"conversations": [
{"role": "user", "content": "<image><image>\n比较这两张图片"},
{"role": "assistant", "content": "第一张图片展示了...,第二张图片展示了..."}
]
}
]
官方脚本微调
请先克隆 MiniCPM-V 仓库并安装依赖:
Bash
git clone https://github.com/OpenBMB/MiniCPM-V.git
cd MiniCPM-V
pip install -r requirements.txt
LoRA 微调
使用 finetune_lora.sh 进行 LoRA 微调,显存占用较低,适合资源有限的场景。
Bash
# 编辑 finetune_lora.sh 中的数据路径和模型路径
bash finetune/finetune_lora.sh
关键参数说明:
MODEL:模型路径,设为openbmb/MiniCPM-V-4或本地路径DATA:训练数据 JSON 文件路径LLM_TYPE:必须设为llama(MiniCPM4-3B 基于 LLaMA 架构,而非 qwen)
全参数微调
使用 finetune_ds.sh 基于 DeepSpeed 进行全参数微调,需要更多显存。
Bash
# 编辑 finetune_ds.sh 中的数据路径和模型路径
bash finetune/finetune_ds.sh
LoRA 推理加载
微调完成后,使用以下代码加载 LoRA adapter 进行推理:
Python
from peft import PeftModel
from transformers import AutoModel, AutoTokenizer
model_path = "openbmb/MiniCPM-V-4"
lora_path = "your_lora_adapter_path"
model = AutoModel.from_pretrained(model_path, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = PeftModel.from_pretrained(model, lora_path)
model = model.eval().cuda()
LLaMA-Factory 微调
LLaMA-Factory 已支持 MiniCPM-V 系列模型。使用时选择 MiniCPM-V 模板即可,LLM_TYPE 同样需要设置为 llama。
具体配置请参考 LLaMA-Factory 官方文档中的 MiniCPM-V 示例。
相关资源
| 主题 | 入口 |
|---|---|
| 模型概览 | MiniCPM-V 4 概览 |
| 代码仓库 | OpenBMB/MiniCPM-V |
| 官方微调脚本 | finetune 目录 |