资讯动态

PaddleOCR TIPC Linux 端基础训练预测功能测试指南:test_train_inference_python.sh 全流程解析

发布时间:2026/9/19 22:08:11 来源:尧图企业网站定制
PaddleOCR TIPC Linux 端基础训练预测功能测试指南test_train_inference_python.sh 全流程解析【免费下载链接】PaddleOCR飞桨多语言OCR工具包实用超轻量OCR系统支持80种语言识别提供数据标注与合成工具支持服务器、移动端、嵌入式及IoT设备端的训练与部署 Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80 languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)项目地址: https://gitcode.com/paddlepaddle/PaddleOCR导读本文围绕 PaddleOCR 仓库test_tipc目录下的 Linux 端基础训练预测功能测试展开核心讲解主程序 test_train_inference_python.sh 与配套脚本 prepare.sh、compare_results.py 的完整使用方式。该测试体系用于自动验证基于 Python 的模型训练、评估、动转静导出与多端推理全链条是否跑通覆盖单机单卡、单机多卡、多机多卡训练以及混合精度训练、PACT 在线量化、FPGM 裁剪等模型压缩能力。读完本文你将掌握 TIPC 四种基础测试模式的选型逻辑、配置文件的字段解析规则、日志产物解读方法与预测精度校验流程可直接在本地复现整套训练预测验证。1. 测试体系概述与结论汇总1.1 测试的主程序与适用平台Linux 端基础训练预测功能测试的主程序为 test_train_inference_python.sh它通过解析 TIPC 配置文件*.txt自动化执行训练调用 tools/train.py 或模型压缩脚本如 deploy/slim/quantization/quant.py完成模型训练评估对训练产出的模型执行评估配置项可选eval:null时跳过动转静导出调用 tools/export_model.py 将动态图模型转换为 inference model推理调用 tools/infer/ 目录下的预测脚本如predict_det.py、predict_rec.py等在 GPU/CPU 上组合不同的 TensorRT、Mkldnn、batchsize、精度等条件逐一验证。Mac 端与 Windows 端的对应测试说明见仓库内的 mac_test_train_inference_python.md 与 win_test_train_inference_python.md。1.2 训练相关测试结论下表为 TIPC 配置覆盖的算法、模型及其在各训练场景下的测试结论以ch_ppocr_mobile_v2_0_det等配置为准其中混合精度指 AMPAutomatic Mixed Precision训练算法名称模型名称单机单卡单机多卡多机多卡模型压缩单机多卡DBch_ppocr_mobile_v2_0_det正常训练混合精度正常训练混合精度正常训练混合精度正常训练FPGM裁剪、PACT量化DBch_ppocr_server_v2_0_det正常训练混合精度正常训练混合精度正常训练混合精度正常训练FPGM裁剪、PACT量化CRNNch_ppocr_mobile_v2_0_rec正常训练混合精度正常训练混合精度正常训练混合精度正常训练PACT量化CRNNch_ppocr_server_v2_0_rec正常训练混合精度正常训练混合精度正常训练混合精度正常训练PACT量化PP-OCRch_ppocr_mobile_v2_0正常训练混合精度正常训练混合精度正常训练混合精度-PP-OCRch_ppocr_server_v2_0正常训练混合精度正常训练混合精度正常训练混合精度-PP-OCRv2ch_PP-OCRv2正常训练混合精度正常训练混合精度正常训练混合精度-PP-OCRv3ch_PP-OCRv3正常训练混合精度正常训练混合精度正常训练混合精度-其中 PACT 量化与 FPGM 裁剪的测试分别依赖 deploy/slim/quantization/ 与 deploy/slim/prune/ 目录下的脚本实现二者均需要安装 PaddleSlim。1.3 预测相关测试结论基于训练是否使用量化训练产出的模型分为正常模型与量化模型两类模型的预测功能覆盖如下模型类型devicebatchsizetensorrtmkldnncpu多线程正常模型GPU1/6fp32/fp16--正常模型CPU1/6-fp32/fp16支持量化模型GPU1/6int8--量化模型CPU1/6-int8支持从 test_train_inference_python.sh 的func_inference实现可以印证这些组合规则CPU 推理use_gpuFalse时循环组合enable_mkldnn、cpu_threads、batch_size、precision四组参数其中关闭 Mkldnn 时跳过 fp16 精度use_mkldnnFalse precisionfp16时continue量化模型推理只允许int8精度GPU 推理use_gpuTrue时循环组合use_tensorrt、precision、batch_size正常模型推理不允许int8fp16/int8精度必须开启 TensorRT关闭 TensorRT 时量化模型只能走int8。2. 运行环境准备2.1 推荐环境运行环境配置请参考 install.md 的详细说明推荐组合包括CUDA 10.1/10.2CUDNN 7.6 / cudnn8.1TensorRT 6.1.0.5 / 7.1 / 7.22.2 Docker 镜像安装推荐Docker 方式可避免繁琐的环境配置将当前目录映射到镜像内/paddle目录后即可使用nvidia-docker run --name paddle -it -v $PWD:/paddle paddlepaddle/paddle:latest-dev-cuda10.1-cudnn7-gcc82 /bin/bash cd /paddle # 安装带 TRT 的 paddle示例为 CUDA10.1 cudnn7 TRT6 的预编译 wheel pip3.7 install https://paddle-wheel.bj.bcebos.com/with-trt/2.1.3/linux-gpu-cuda10.1-cudnn7-mkl-gcc8.2-trt6-avx/paddlepaddle_gpu-2.1.3.post101-cp37-cp37m-linux_x86_64.whl2.3 Python 环境构建非 Docker 环境下推荐三组组合CUDA10.1 CUDNN7.6 TensorRT 6CUDA10.2 CUDNN8.1 TensorRT 7CUDA11.1 CUDNN8.1 TensorRT 7以 CUDA10.2 CUDNN8.1 TensorRT 7 为例流程为先安装 CUDNN三个 deb 包mnistCUDNN运行成功即验证通过再解压安装 TensorRT TAR 包并设置LD_LIBRARY_PATH指向其lib目录最后安装 PaddlePaddle。常见 FAQ若出现 Paddle compiled with TensorRT, but TensorRT dynamic library is not found说明当前 Paddle 版本带 TRT 但本地找不到 TensorRT 预测库需要下载与 Paddle 编译版本匹配的 TensorRT 并设置环境变量例如export LD_LIBRARY_PATH/usr/local/python3.7.0/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64:/paddle/package/TensorRT-6.0.1.5/lib3. 安装依赖在完成上述环境配置后按以下顺序安装 PaddleOCR 运行依赖安装 PaddlePaddle 2.3安装 PaddleOCR 依赖在仓库根目录执行pip3 install -r requirements.txt安装 autolog规范化日志输出工具pip3 install https://paddleocr.bj.bcebos.com/libs/auto_log-1.2.0-py3-none-any.whl安装 PaddleSlim可选仅在测试量化、裁剪等功能时需要pip3 install paddleslim4. 配置文件结构解析TIPC 的每次测试都以一个*.txt配置文件为输入例如 train_infer_python.txt。test_train_inference_python.sh 按行号解析该文件因此字段顺序是约定的主要分为四段4.1 train_params训练参数行字段示例值说明1model_namech_ppocr_mobile_v2_0_det模型名决定test_tipc/output/下的日志目录名2pythonpython3.7解释器命令3gpu_list0|0,1单卡/多卡 GPU 编号-1表示 CPU4Global.use_gpuTrue|True与 gpu_list 一一对应的训练设备开关5Global.auto_castnull / fp32 / amp混合精度开关amp时脚本会追加Global.use_ampTrue Global.scale_loss1024.0 Global.use_dynamic_loss_scalingTrue6Global.epoch_numlite_train_lite_infer100|whole_train_whole_infer50按模式区分 epoch 数7Global.save_model_dir./output/模型保存目录8Train.loader.batch_size_per_cardlite_train_lite_infer2|whole_train_whole_infer4每卡 batch size9Global.pretrained_modelnull预训练模型10train_model_namelatest参与评估/导出的权重名11train_infer_img_dir./train_data/icdar2015/.../ch4_test_images/训练后推理使用的图片目录12train_param_key1/value1null:null附加训练参数键值对4.2 trainer训练器选择第 14 行trainer声明训练器列表如norm_train、pact_train、fpgm_train、distill_train随后各行为每个训练器指定具体命令。例如norm_train正常训练norm_train:tools/train.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o Global.pretrained_model./pretrain_models/MobileNetV3_large_x0_5_pretrained Global.print_batch_step2 Train.loader.shufflefalsepact_trainPACT 在线量化训练见 train_pact_infer_python.txtpact_train:deploy/slim/quantization/quant.py -c configs/det/ch_PP-OCRv2/ch_PP-OCRv2_det_cml.yml -o4.3 eval_params评估参数eval行指定评估脚本null表示跳过评估环节。4.4 infer_params推理参数行字段示例值说明27Global.save_inference_dir./output/导出模型目录28Global.checkpoints空导出时的权重路径前缀29-32norm_export / quant_export / fpgm_export / distill_exporttools/export_model.py -c ... -o各训练器对应的动转静导出命令36inference_dirnull / Student导出后的推理模型子目录量化配置常用Student37train_model./inference/ch_ppocr_mobile_v2.0_det_train/best_accuracywhole_infer 模式直接使用的开源模型权重38infer_exporttools/export_model.py -c ... -owhole_infer 模式下的导出命令39infer_quantFalse推理模型是否为量化模型40-51inference / --use_gpu / --enable_mkldnn / --cpu_threads / --rec_batch_num / --use_tensorrt / --precision / --det_model_dir / --image_dir / --benchmark见配置文件推理脚本及其参数取值组合推理参数中的--use_gpu:True|False等多值字段表示测试时要遍历的组合空间脚本会在 func_inference 中循环展开。4.5 混合精度AMP配置混合精度训练链条使用 train_linux_gpu_normal_amp_infer_python_linux_gpu_cpu.txt其关键差异在于Global.auto_cast:amp。脚本在 L242-L246 检测到amp时会自动向训练命令追加Global.use_ampTrue Global.scale_loss1024.0 Global.use_dynamic_loss_scalingTrue5. 功能测试四种基础运行模式5.1 模式总览test_train_inference_python.sh 包含 4 种基础链条运行模式脚本第 5 行注释声明每种模式的训练数据量与用途不同模式1lite_train_lite_infer使用少量数据训练快速验证训练→预测全流程是否走通不验证精度和速度模式2lite_train_whole_infer少量数据训练 一定量数据预测验证训练后模型执行预测的流程与预测速度是否合理模式3whole_infer不训练全量数据预测走通开源模型评估、动转静检查 inference model 的预测时间与精度模式4whole_train_whole_inferCE全量数据训练 全量数据预测验证模型训练精度、预测精度与预测速度。5.2 模式1lite_train_lite_inferbash test_tipc/prepare.sh ./test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt lite_train_lite_infer bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt lite_train_lite_infer5.3 模式2lite_train_whole_inferbash test_tipc/prepare.sh ./test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt lite_train_whole_infer bash test_tipc/test_train_inference_python.sh ../test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt lite_train_whole_infer5.4 模式3whole_inferbash test_tipc/prepare.sh ./test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt whole_infer # 用法1: 默认 GPU 卡 bash test_tipc/test_train_inference_python.sh ../test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt whole_infer # 用法2: 指定 GPU 卡预测第三个传入参数为 GPU 卡号 bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt whole_infer 1whole_infer模式在脚本中单独走 L181-L215 分支先用第三个参数设置CUDA_VISIBLE_DEVICES然后按配置的infer_model列表执行infer_export导出若不为null并运行func_inference完成推理。5.5 模式4whole_train_whole_inferCEbash test_tipc/prepare.sh ./test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt whole_train_whole_infer bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/ch_ppocr_mobile_v2_0_det/train_infer_python.txt whole_train_whole_infer5.6 训练启动逻辑脚本内幕从脚本 L216-L343 可以看到非whole_infer模式下脚本按gpu_list × autocast × trainer三层循环组织训练任务单卡/CPU 训练GPU 编号长度 ≤ 2直接执行python run_train ...多卡训练GPU 编号长度 2 且 ≤ 15使用python -m paddle.distributed.launch --gpus${gpu} ...多机训练GPU 编号含分号分隔的ips;gpus使用python -m paddle.distributed.launch --ips${ips} --gpus${gpu} ...。训练完成后若配置了eval则执行评估随后执行导出命令生成 inference model最后调用func_inference进行推理并把train.log追加合并到模式日志中。6. PACT 在线量化链条test_train_inference_python.sh 还内置 PACT 在线量化模式。以 ch_PP-OCRv2_det 为例测试其他模型更换配置即可bash test_tipc/prepare.sh ./test_tipc/configs/ch_PP-OCRv2_det/train_pact_infer_python.txt lite_train_lite_infer bash test_tipc/test_train_inference_python.sh ./test_tipc/configs/ch_PP-OCRv2_det/train_pact_infer_python.txt lite_train_lite_infer该配置的关键点训练器为pact_train调用 deploy/slim/quantization/quant.py 进行在线量化训练导出命令为quant_export调用 deploy/slim/quantization/export_model.pyinference_dir:Student表明导出的推理模型位于训练产物目录下的Student子目录脚本在 L248-L252 检测到pact_key时设置flag_quantTrue推理阶段据此只允许int8精度组合。7. 日志产物解读运行相应指令后日志自动保存到test_tipc/output目录。以lite_train_lite_infer模式为例目录结构如下test_tipc/output/model_name/lite_train_lite_infer/ |- results_python.log # 运行指令状态的日志 |- norm_train_gpus_0_autocast_null/ # GPU 0号卡上正常单机单卡训练的训练日志和模型保存文件夹 |- norm_train_gpus_0,1_autocast_null/ # GPU 0,1号卡上正常单机多卡训练的训练日志和模型保存文件夹 ...... |- python_infer_cpu_usemkldnn_False_threads_6_precision_fp32_batchsize_1.log # CPU上关闭Mkldnn线程数设置为6测试batch_size1条件下的fp32精度预测运行日志 |- python_infer_gpu_usetrt_False_precision_fp32_batchsize_1.log # GPU上关闭TensorRT测试batch_size1的fp32精度预测日志 ......results_python.log汇总了每条指令的运行状态。运行成功会输出类似[33m Run successfully with command - ch_ppocr_mobile_v2_0_det - python3.7 tools/train.py -c configs/det/ch_ppocr_v2_0/ch_det_mv3_db_v2_0.yml -o Global.pretrained_model./pretrain_models/MobileNetV3_large_x0_5_pretrained Global.use_gpuTrue Global.save_model_dir./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/norm_train_gpus_0_autocast_null Global.epoch_num100 Train.loader.batch_size_per_card2 ! [0m [33m Run successfully with command - ch_ppocr_mobile_v2_0_det - python3.7 tools/export_model.py -c configs/det/ch_ppocr_v2_0/ch_det_mv3_db_v2_0.yml -o Global.checkpoints./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/norm_train_gpus_0_autocast_null/latest Global.save_inference_dir./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/norm_train_gpus_0_autocast_null ./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/norm_train_gpus_0_autocast_null_nodes_1_export.log 21 ! [0m [33m Run successfully with command - ch_ppocr_mobile_v2_0_det - python3.7 tools/infer/predict_det.py --use_gpuTrue --use_tensorrtFalse --precisionfp32 --det_model_dir./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/norm_train_gpus_0_autocast_null --rec_batch_num1 --image_dir./train_data/icdar2015/text_localization/ch4_test_images/ --benchmarkTrue ./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/python_infer_gpu_usetrt_False_precision_fp32_batchsize_1.log 21 ! [0m [33m Run successfully with command - ch_ppocr_mobile_v2_0_det - python3.7 tools/infer/predict_det.py --use_gpuFalse --enable_mkldnnFalse --cpu_threads6 --det_model_dir./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/norm_train_gpus_0_autocast_null --rec_batch_num1 --image_dir./train_data/icdar2015/text_localization/ch4_test_images/ --benchmarkTrue --precisionfp32 ./test_tipc/output/ch_ppocr_mobile_v2_0_det/lite_train_lite_infer/python_infer_cpu_usemkldnn_False_threads_6_precision_fp32_batchsize_1.log 21 ! [0m ......运行失败会输出Run failed with command - python3.7 tools/train.py -c tests/configs/det_mv3_db.yml -o Global.pretrained_model./pretrain_models/MobileNetV3_large_x0_5_pretrained Global.use_gpuTrue Global.save_model_dir./tests/output/norm_train_gpus_0_autocast_null Global.epoch_num1 Train.loader.batch_size_per_card2 ! Run failed with command - python3.7 tools/export_model.py -c tests/configs/det_mv3_db.yml -o Global.pretrained_model./tests/output/norm_train_gpus_0_autocast_null/latest Global.save_inference_dir./tests/output/norm_train_gpus_0_autocast_null! ......借助results_python.log可以快速定位是哪一条训练/导出/推理指令出错从而缩小排查范围。8. 精度测试compare_results.py 结果校验8.1 测试原理compare_results.py 用于比较模型预测结果是否符合预期主要步骤为提取日志中的预测坐标通过parser_results_from_log_by_name从推理日志中 grep 出结果关键字如文本框坐标并解析为 JSON 或N×4数组从本地文件提取保存好的坐标结果load_gt_from_txts扫描gt_file通配符匹配的所有*.txt基准文件并按文件名中的fp32/fp16/int8关键字分类比较结果是否符合精度预期testing_assert_allclose使用np.testing.assert_allclose对预测值与基准值做逐元素比对误差大于设置的atol/rtol阈值时报错。8.2 使用方式运行命令python3.7 test_tipc/compare_results.py --gt_file./test_tipc/results/python_*.txt --log_file./test_tipc/output/python_*.log --atol1e-3 --rtol1e-3参数说明gt_file指向事先保存好的预测结果路径支持*.txt结尾会自动索引*.txt格式文件基准文件默认保存在test_tipc/results/文件夹下仓库中已有如 python_ppocr_det_mobile_results_fp32.txt 等基准文件log_file指向运行test_tipc/test_train_inference_python.sh的 infer 模式保存的预测日志日志中打印了预测结果文本框、预测文本、类别等支持python_infer_*.log格式传入atol设置的绝对误差rtol设置的相对误差。8.3 运行结果正常运行效果如下输出Assert allclose passed并提示预测结果与基准结果一致出现不一致时运行输出如下抛出AssertionError提示结果不一致inconsistent并给出不满足容差rtol0.001, atol0.001的失配元素个数如Mismatched elements: 1 / 38 (1.1%)及数组逐元素差异9. 测试模式选型建议场景推荐模式理由CI/开发自检快速确认流程通畅lite_train_lite_infer数据量最小、耗时最短验证训练→导出→推理链路验证训练后推理速度是否合理lite_train_whole_infer小训练量 较多推理数据验证开源模型评估与动转静、推理精度速度whole_infer不训练直接用开源权重走导出推理发布前的完整训练精度回归CEwhole_train_whole_infer全量训练 全量预测验证训练/预测精度与速度模型压缩链路验证PACT 在线量化配置train_pact_infer_python.txt验证量化训练int8 推理全链路精度一致性回归compare_results.py将推理日志与test_tipc/results/基准对比10. 更多教程本文档定位为功能测试说明更丰富的训练预测使用教程可参考仓库内以下资料tools/train.py 与 tools/export_model.py 是训练与动转静导出的实际入口tools/infer/ 目录包含各任务检测predict_det.py、识别predict_rec.py、分类、表格等的 Python 预测引擎脚本deploy/slim/quantization/ 与 deploy/slim/prune/ 提供了 PACT 量化与 FPGM 裁剪的实现脚本配套的 Windows/Mac 测试文档win_test_train_inference_python.md、mac_test_train_inference_python.md其他部署形态的测试说明位于 test_tipc/docs/ 目录例如 C 推理测试 test_inference_cpp.md、Paddle2ONNX 测试 test_paddle2onnx.md、Serving 测试 test_serving.md。【免费下载链接】PaddleOCR飞桨多语言OCR工具包实用超轻量OCR系统支持80种语言识别提供数据标注与合成工具支持服务器、移动端、嵌入式及IoT设备端的训练与部署 Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80 languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)项目地址: https://gitcode.com/paddlepaddle/PaddleOCR创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价