LLM - 使用 LLaMA-Factory 微调大模型 Qwen2-VL SFT(LoRA) 图像数据集 教程 (2)

欢迎关注我的CSDN:https://spike.blog.csdn.net/

本文地址:https://spike.blog.csdn.net/article/details/143725947

免责声明:本文来源于个人知识与公开资料,仅用于学术交流,欢迎讨论,不支持转载。


LLaMA-Factory 是开源的大模型微调框架,用于高效地微调和部署大语言模型,支持多种预训练模型和微调算法,提供完整的工具和接口,对于预训练的模型进行定制化的训练和调整,以适应特定的应用场景。

Paper: LlamaFactory: Unified Efficient Fine-Tuning of 100+ Language Models

配置环境参考:使用 LLaMA-Factory 微调大模型 环境配置与训练推理 教程 (1)


1. 准备数据

测试使用的开源数据集:Qwen2-VL-History.zip

其中,在 Qwen2-VL-History.zip 中,数据集样式 dataset_info.json,即:

json 复制代码
{
  "train": {
    "file_name": "train.json",
    "formatting": "sharegpt",
    "columns": {
      "messages": "conversations",
      "images": "images"
    }
  }
}

其中,样本主要是图像描述,其中,一张图像可以包括多种不同形式的描述,即:

bash 复制代码
{
    "conversations": [
        {
            "from": "system",
            "value": "你是一个文博工作人员,请严谨认真地回答用户提出的问题"
        },
        {
            "from": "human",
            "value": "<image>给我讲讲这个东西"
        },
        {
            "from": "gpt",
            "value": "# 文物名称\n神面纹玉戚\n# 时代\n新石器时代\n# 类别\n玉器\n# 收藏机构\n山西博物院\n# 尺寸\n高20.6厘米,宽13.1厘米,厚0.4厘米\n# 出土时间\n1964年\n# 出土地点\n黎城县后庄村广志山\n# 特征描述\n线刻,一侧为神人半侧面头像,头戴冠饰,披拂长发,眼眉清晰可辨;一侧为一方形台座上置神人冠饰的形象,神秘威严\n# 象征意义\n象征军事统帅权力"
        }
    ],
    "images": [
        "images/instance_1579398113581395972.jpg"
    ]
},

images/instance_1579398113581395972.jpg 图像:

准备数据集的 dataset_info.json 文件:

bash 复制代码
"qwen2_vl_history": {
  "file_name": "Qwen2-VL-History/train.json",
  "formatting": "sharegpt",
  "columns": {
    "messages": "conversations",
    "images": "images"
  }
}

2. 训练模型

修改训练的 Yaml 文件 qwen2vl_lora_sft_my20241112.yaml,即:

  • 使用本地的 Qwen2-VL-7B-Instruct 模型,或者下载 HuggingFace 模型。
  • dataset 数据集增加 qwen2_vl_history,来源于 注册的 dataset_info.json 数据集。
  • image_dir 图像数据位置,与 train.json 共同组成 Image 数据。
  • num_train_epochs: 100.0,训练 100 个 epoch。
yaml 复制代码
### model
model_name_or_path: [your path]/llm/Qwen/Qwen2-VL-7B-Instruct/

### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all

### dataset
dataset: qwen2_vl_history,identity  # video: mllm_video_demo
template: qwen2_vl
cutoff_len: 1024
max_samples: 100000
overwrite_cache: true
preprocessing_num_workers: 16
image_dir: [your path]/llm/LLaMA-Factory/data/Qwen2-VL-History/

### output
output_dir: saves/qwen2_vl-7b/lora/sft-2
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true

### train
per_device_train_batch_size: 2
gradient_accumulation_steps: 8
learning_rate: 1.0e-4
num_train_epochs: 100.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000

### eval
val_size: 0.1
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 500

max_samples 是最大的训练数据量,即:

python 复制代码
if data_args.max_samples is not None:  # truncate dataset
    max_samples = min(data_args.max_samples, len(dataset))
    dataset = dataset.select(range(max_samples))

训练模型:

bash 复制代码
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 llamafactory-cli train /nfs_beijing_ai/chenlong/llm/LLaMA-Factory/examples/train_lora/qwen2vl_lora_sft_my20241112.yaml

训练日志:

bash 复制代码
[INFO|tokenization_utils_base.py:2646] 2024-11-12 13:10:21,050 >> tokenizer config file saved in saves/qwen2_vl-7b/lora/sft-2/tokenizer_config.json
[INFO|tokenization_utils_base.py:2655] 2024-11-12 13:10:21,056 >> Special tokens file saved in saves/qwen2_vl-7b/lora/sft-2/special_tokens_map.json
***** train metrics *****
  epoch                    =        80.0
  total_flos               = 551442980GF
  train_loss               =      0.6243
  train_runtime            =  0:14:05.42
  train_samples_per_second =      37.377
  train_steps_per_second   =       0.237
Figure saved at: saves/qwen2_vl-7b/lora/sft-2/training_loss.png
[WARNING|2024-11-12 13:10:21] llamafactory.extras.ploting:162 >> No metric eval_loss to plot.
[WARNING|2024-11-12 13:10:21] llamafactory.extras.ploting:162 >> No metric eval_accuracy to plot.
[INFO|trainer.py:4117] 2024-11-12 13:10:21,387 >> 
***** Running Evaluation *****
[INFO|trainer.py:4119] 2024-11-12 13:10:21,388 >>   Num examples = 36
[INFO|trainer.py:4122] 2024-11-12 13:10:21,388 >>   Batch size = 1
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00,  8.60it/s]
***** eval metrics *****
  epoch                   =       80.0
  eval_loss               =     0.3075
  eval_runtime            = 0:00:00.64
  eval_samples_per_second =     55.459
  eval_steps_per_second   =      7.703

Loss 情况,注意 Loss 需要收敛,否则效果非常一般,即:

3. 测试效果

使用 LoRA 测试效果:

bash 复制代码
CUDA_VISIBLE_DEVICES=1 llamafactory-cli webchat \
--model_name_or_path [your path]/llm/Qwen/Qwen2-VL-7B-Instruct/ \
--adapter_name_or_path [your path]/llm/LLaMA-Factory/saves/qwen2_vl-7b/lora/sft-2/  \
--template qwen2_vl \
--finetuning_type lora

测试效果:

相关推荐
opentrending3 小时前
Github 热点项目 awesome-mcp-servers MCP 服务器合集,3分钟实现AI模型自由操控万物!
服务器·人工智能·github
lisw054 小时前
DeepSeek原生稀疏注意力(Native Sparse Attention, NSA)算法介绍
人工智能·深度学习·算法
whaosoft-1434 小时前
51c深度学习~合集4
人工智能
逢生博客4 小时前
阿里 FunASR 开源中文语音识别大模型应用示例(准确率比faster-whisper高)
人工智能·python·语音识别·funasr
哲讯智能科技5 小时前
智慧能源新篇章:SAP如何赋能光伏行业数字化转型
大数据·人工智能
云卓SKYDROID5 小时前
无人机DSP处理器工作要点!
人工智能·无人机·科普·云卓科技
gang_unerry5 小时前
量子退火与机器学习(2):少量实验即可找到新材料,黑盒优化➕量子退火
人工智能·机器学习·量子计算·量子退火
訾博ZiBo5 小时前
AI日报 - 2025年4月2日
人工智能
说私域5 小时前
消费品行业创新创业中品类创新与数字化工具的融合:以开源 AI 智能客服、AI 智能名片及 S2B2C 商城小程序为例
人工智能·小程序·开源
说私域6 小时前
开源AI大模型赋能的S2B2C商业生态重构研究——基于智能名片系统的体验认知与KOC背书机制
人工智能·小程序·重构·开源