欢迎关注我的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
,即:
{"train": {"file_name": "train.json","formatting": "sharegpt","columns": {"messages": "conversations","images": "images"}}
}
其中,样本主要是图像描述,其中,一张图像可以包括多种不同形式的描述,即:
{"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
文件:
"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。
### 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
是最大的训练数据量,即:
if data_args.max_samples is not None: # truncate datasetmax_samples = min(data_args.max_samples, len(dataset))dataset = dataset.select(range(max_samples))
训练模型:
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
训练日志:
[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.0total_flos = 551442980GFtrain_loss = 0.6243train_runtime = 0:14:05.42train_samples_per_second = 37.377train_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.0eval_loss = 0.3075eval_runtime = 0:00:00.64eval_samples_per_second = 55.459eval_steps_per_second = 7.703
Loss 情况,注意 Loss 需要收敛,否则效果非常一般,即:
3. 测试效果
使用 LoRA 测试效果:
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
测试效果: