UIE标注微调流程
更新时间:2022-12-17
对于简单/通用的抽取目标可以直接调用UIE实现零样本(zero-shot)抽取。但对于大多数实际业务场景,我们推荐使用进一步对UIE进行定制(即标注少量数据进行模型微调),以进一步提升模型效果,最终满足实际使用需求
现在UIE提供标注微调全流程工具适配,方便用户使用
准备工作,下载模型:
# 进入模型文件夹
cd ../../models_hub
# 下载所有uie模型
sh ernie_uie_xxxxxx
数据标注
标注工具
-
外部或开源工具
- doccano:好用的开源标注工具,主要支持文本数据实体、关系等任务标注(https://doccano.github.io/doccano/)(可以参考PaddleUIE上的用法)
- labelstudio:功能强大的开源标注工具,支持文本、文档等各类数据的标注,正在对接中(使用文档)
-
baidu商业化平台或工具
- textmind:Textmind平台导出的标注数据,主要为文档类标注数据。
- 知言:NLP知言平台导出的标注数据,特指其中文本类格式,文档类格式与textmind格式一致。
数据转换
训练前需要将标注数据格式转换为UIE训练数据格式。(如果有问题删除应用包前面的点:.schema -> schema;.utils -> utils)
python3 convert_data.py \
--from doieat \
--to monet_train \
--input-path $INPUT_PATH \
--output-path $OUTPUT_DIR \
--split-ratio 0.8 0.2 0.0 \
--negative-ratio 3 \
--shuffle True
训练微调
使用转换后的UIE训练数据格式,对模型进行微调。(目前支持单卡训练)
# 先更改.json中的数据路径
python3 run_trainer.py --param_path ./examples/ernie_uie_v1_large.json
or
python3 run_trainer.py --param_path ./examples/ernie_uie_v2_large.json
or
python3 run_trainer.py --param_path ./examples/ernie_uie_v3_large.json
# 训练好的模型保存在output对应文件夹中,json中有对应设置
- 训练运行的日志会自动保存在./log/test.log文件中
-
训练结束后产生的结果:
-
模型文件会默认保存在./output/xxx目录下,xxx根据json中的设置来
- 其中save_inference_model/文件夹会保存用于预测的模型文件
- 其中save_checkpoint/文件夹会保存用于热启动的模型文件
- 训练中产生评估结果和对评估集的预测结果分别保存在output/evaluations/文件夹和output/predictions文件夹
-
加载训好的模型
使用预测函数功能,只需要修改如下对应json中加载的预测模型位置即可
# uie v1
inner_config/ernie_uie_large_extraction_ch_infer.json
# uie v3
inner_config/ernie_uie_v3_large_extraction_infer.json
# json修改内容
"inference": {
"inference_model_path": "../../models_hub/ernie_uie_v3_large_extraction_dir/inference_model", # 这里改为新预测模型位置
...
}
之后使用预测函数即可使用新模型,使用方法参考UIE预测章节