- 人工智能
- 计算机视觉
- 预训练
【免费下载链接】PaddleSeg
Easy-to-use image segmentation library with awesome pre-trained model zoo, supporting wide-range of practical tasks in Semantic Segmentation, Interactive Segmentation, Panoptic Segmentation, Image Matting, 3D Segmentation, etc.
本文是 PaddleSeg 自检(TIPC,Test In Paddle Cloud)体系中「Linux GPU/CPU PACT 量化训练推理测试」的实战指南。文章以 test_train_pact_inference_python.md 为骨架,结合仓库中deploy/slim/quant的量化训练、量化评估、量化导出源码以及test_tipc/configs下的真实测试配置,完整讲解从环境准备、数据准备、功能测试到日志解读的每一个步骤,并深入剖析 PACT 量化训练在 PaddleSeg 中的底层实现原理。读完本文,你将能够独立复现任一支持模型的 PACT 量化训练推理自检流程,并能够基于源码理解量化范围、量化策略与精度验证的完整链路。
1. 测试概览与结论汇总
PACT 量化训练推理测试是 PaddleSeg 模型库自检体系中的一项重要测试,其目标是在 Linux 环境下,以 GPU/CPU 为运行设备,验证分割模型经过PACT 量化感知训练(Quantization-Aware Training,QAT)后,仍能完成「训练 → 评估 → 导出 → 推理」的完整闭环。
该测试的主程序为 test_train_inference_python.sh,它通过解析test_tipc/configs/下的.txt配置文件,自动编排训练、评估、导出、推理各阶段命令并检查每一步的退出状态。
根据文档给出的测试结论汇总,当前仓库中覆盖了 9 组「算法/模型」的 PACT 量化训练测试,全部采用单机单卡(gpu_list 中首项为 0)运行:
| 算法名称 | 模型名称 | 单机单卡 |
|---|---|---|
| HRNet | pphumanseg_mobile (fcn_hrnetw18_small) | PACT量化训练 |
| HRNet | fcn_hrnetw18 | PACT量化训练 |
| DeepLabV3P | pphumanseg_server (deeplabv3p_resnet50) | PACT量化训练 |
| ConnectNet | pphumanseg_lite | PACT量化训练 |
| OCRNet | ocrnet_hrnetw18 | PACT量化训练 |
| OCRNet | ocrnet_hrnetw48 | PACT量化训练 |
| SegFormer | segformer_b0 | PACT量化训练 |
| PP-LiteSeg | pp_liteseg_stdc1 | PACT量化训练 |
| PP-LiteSeg | pp_liteseg_stdc2 | PACT量化训练 |
上述模型对应的测试配置文件均位于test_tipc/configs/下,文件名统一为train_pact_infer_python.txt,例如 fcn_hrnetw18/train_pact_infer_python.txt、pphumanseg_lite/train_pact_infer_python.txt、ocrnet_hrnetw48/train_pact_infer_python.txt 等。这也意味着,只要你的业务模型属于上述算法族,即可直接复用对应配置跑通 PACT 量化自检。
2. 测试流程总览
整个 PACT 量化训练推理测试的流程可以分为四个阶段:
- 准备数据和模型:通过 prepare.sh 下载/准备轻量训练数据与预训练模型,并生成对应的配置文件;
- 准备环境:安装 PaddlePaddle(2.2+)、PaddleSlim、项目依赖以及 AutoLog 日志工具;
- 功能测试:执行 test_train_inference_python.sh,完成 PACT 量化训练、评估、导出与 int8 推理;
- 日志判定:依据终端输出的
Run successfully with command - ...或失败日志,判断测试是否通过并定位问题。
从源码结构看,test_train_inference_python.sh 是整个流程的调度中枢:它读取配置文件中的trainer:pact_train字段(第 15 行附近),据此将pact_train对应的训练命令(即deploy/slim/quant/qat_train.py)与quant_export对应的导出命令(即deploy/slim/quant/qat_export.py)注入执行序列,并设置flag_quant=True以在推理阶段强制使用 int8 精度(见脚本第 283-287 行)。
3. 准备数据和模型
以fcn_hrnetw18为例,准备 PACT 量化训练推理测试所需的模型和数据,执行如下指令:
bash test_tipc/prepare.sh test_tipc/configs/fcn_hrnetw18/train_pact_infer_python.txt lite_train_lite_infer该命令做了两件事:
- 模式指定:第二个参数
lite_train_lite_infer是 TIPC 预置的测试模式,表示「轻量训练 + 轻量推理」,即用极少的迭代轮次(iters=20)和较小的 batch_size 快速验证链路可用性,而不是跑完整精度; - 配置解析:
prepare.sh读取train_pact_infer_python.txt中 train_params 段的信息,下载或准备训练所需的数据集与预训练权重,并将其放置到脚本约定的路径(如test_tipc/data/cityscapes/cityscapes_val_5.list即推理用图片列表)。
对于其他模型(如pphumanseg_lite、pp_liteseg_stdc1),只需将路径替换为对应的train_pact_infer_python.txt即可。
4. 准备环境
PACT 量化训练依赖 PaddleSlim 的 QAT 能力,因此除常规分割训练环境外,还需要额外安装 PaddleSlim 与 AutoLog。文档给出的环境安装步骤如下。
4.1 安装 PaddlePaddle
需要 2.2 及以上版本的 PaddlePaddle。如果已安装满足版本要求的 paddlepaddle,可跳过此步:
# 安装GPU版本的Paddle pip3 install paddlepaddle-gpu==2.2.0 # 安装CPU版本的Paddle pip3 install paddlepaddle==2.2.0从 deploy/python/infer.py 中的版本检查逻辑(device==gpu && use_trt==True && paddle >= 2.2)也可以印证,仓库的量化推理链路以 Paddle 2.2 为最低版本基线。
4.2 安装 PaddleSlim
PACT 量化训练的核心算子在 PaddleSlim 中实现,必须安装:
pip3 install paddleslim==2.2.04.3 安装依赖
安装 PaddleSeg 项目自身的运行依赖:
pip3 install -r requirements.txt4.4 安装 AutoLog
AutoLog 是规范化日志输出的工具,用于统一记录训练/推理的耗时、吞吐等 benchmark 指标,TIPC 测试的日志解析依赖它:
pip3 install https://paddleocr.bj.bcebos.com/libs/auto_log-1.2.0-py3-none-any.whl5. 功能测试
5.1 执行测试命令
环境就绪后,以fcn_hrnetw18的 Linux GPU/CPU PACT 量化训练推理测试为例,执行:
bash test_tipc/test_train_inference_python.sh test_tipc/configs/fcn_hrnetw18/train_pact_infer_python.txt lite_train_lite_infer主程序会按「PACT 量化训练 → 量化评估 → 量化导出 → int8 推理」的顺序串行执行,并在每个阶段结束后进行状态检查(status_check),任何一步失败都会在状态日志中记录并终止后续阶段。
5.2 期望输出
如果命令运行成功,终端会逐条输出类似下面的日志:
Run successfully with command - fcn_hrnetw18 - python3.7 deploy/slim/quant/qat_train.py --config test_tipc/configs/fcn_hrnetw18/fcn_hrnetw18_1024x512_cityscapes.yml --device gpu --save_interval 500 --seed 100 --num_workers 8 --save_dir=./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null --iters=20 --batch_size=2 - ./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null_nodes_1.log ...... Run successfully with command - fcn_hrnetw18 - python3.7 deploy/slim/quant/qat_val.py --config test_tipc/configs/fcn_hrnetw18/fcn_hrnetw18_1024x512_cityscapes.yml --device gpu --num_workers 8 --model_path=./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null/iter_20/model.pdparams - ./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null_nodes_1_eval.log ...... Run successfully with command - fcn_hrnetw18 - python3.7 deploy/slim/quant/qat_export.py --config test_tipc/configs/fcn_hrnetw18/fcn_hrnetw18_1024x512_cityscapes.yml --model_path=./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null/iter_20/model.pdparams --save_dir=./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null - ./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null_nodes_1_export.log ...... Run successfully with command - fcn_hrnetw18 - python3.7 deploy/python/infer.py --device=cpu --enable_mkldnn=True --cpu_threads=6 --config=./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null//deploy.yaml --batch_size=1 --image_path=test_tipc/data/cityscapes/cityscapes_val_5.list --benchmark=True --precision=int8 --save_dir=./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/python_infer_cpu_usemkldnn_True_threads_6_precision_int8_batchsize_1_results --model_name=fcn_hrnetw18 > ./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/python_infer_cpu_gpu_0_usemkldnn_True_threads_6_precision_int8_batchsize_1.log 2>&1 - ./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/python_infer_cpu_gpu_0_usemkldnn_True_threads_6_precision_int8_batchsize_1.log ......5.3 输出解读
这四条日志分别对应四个阶段,可以按模型名 + 阶段后缀区分:
pact_train_gpus_0_autocast_null(训练):调用 qat_train.py,训练 20 个 iter(--iters=20),batch_size 为 2,保存目录为./test_tipc/output/fcn_hrnetw18/lite_train_lite_infer/pact_train_gpus_0_autocast_null,训练日志落盘为..._nodes_1.log;..._eval.log(评估):调用 qat_val.py,加载iter_20/model.pdparams权重进行量化模型评估;..._export.log(导出):调用 qat_export.py,将训练好的量化模型导出为推理模型,并在同一目录生成deploy.yaml部署配置;python_infer_cpu_usemkldnn_True_threads_6_precision_int8_batchsize_1(推理):调用 deploy/python/infer.py,以 CPU + MKLDNN +--precision=int8对量化导出模型做 benchmark 推理,逐条处理cityscapes_val_5.list中的测试图片。
5.4 失败排查
如果某一步运行失败,终端会输出运行失败的日志信息以及对应的完整运行命令(Run failed with command - ...形式)。此时可以:
- 直接复制失败命令在终端手动执行,观察具体报错;
- 检查对应
.log文件(位于./test_tipc/output/<模型名>/lite_train_lite_infer/下)末尾的堆栈信息; - 按阶段定位问题域:训练失败多与数据路径、显存或 PaddleSlim 版本有关;导出失败多与
--model_path权重路径有关;推理失败多与deploy.yaml生成路径、--precision参数或 MKLDNN 环境有关。
6. 测试配置深度解析
测试的行为完全由train_pact_infer_python.txt控制。以 fcn_hrnetw18/train_pact_infer_python.txt 为例,其关键字段如下:
===========================train_params=========================== model_name:fcn_hrnetw18 python:python3.7 gpu_list:0|0,1 --iters:lite_train_lite_infer=20|lite_train_whole_infer=20|whole_train_whole_infer=1000 --batch_size:lite_train_lite_infer=2|lite_train_whole_infer=2|whole_train_whole_infer=8 train_model_name:best_model/model.pdparams train_infer_img_dir:test_tipc/data/cityscapes/cityscapes_val_5.list ## trainer:pact_train pact_train:deploy/slim/quant/qat_train.py --config test_tipc/configs/fcn_hrnetw18/fcn_hrnetw18_1024x512_cityscapes.yml --device gpu --save_interval 500 --seed 100 --num_workers 8 ## ===========================eval_params=========================== eval:deploy/slim/quant/qat_val.py --config test_tipc/configs/fcn_hrnetw18/fcn_hrnetw18_1024x512_cityscapes.yml --device gpu --num_workers 8 ## ===========================export_params=========================== quant_export:deploy/slim/quant/qat_export.py --config test_tipc/configs/fcn_hrnetw18/fcn_hrnetw18_1024x512_cityscapes.yml ===========================infer_params=========================== inference:deploy/python/infer.py --device:cpu|gpu --enable_mkldnn:True --cpu_threads:6 --precision:int8 --image_path:./test_tipc/data/cityscapes/cityscapes_val_5.list --benchmark:True --model_name:fcn_hrnetw18各段职责:
- train_params:声明训练入口为
pact_train(而不是常规的norm_train),并给出 PACT 训练命令模板deploy/slim/quant/qat_train.py ...;--iters、--batch_size支持按模式取值(lite_train_lite_infer=20表示轻量模式下迭代 20 次、batch 为 2;whole_train_whole_infer=1000表示全量模式下迭代 1000 次、batch 为 8); - eval_params:评估入口为 qat_val.py,注意评估的是「量化后」的模型,而非普通 FP32 模型;
- export_params:导出入口为 qat_export.py,对应
quant_export字段; - infer_params:推理入口为 deploy/python/infer.py,
--precision:int8表明量化模型的推理必须使用 int8 精度,--enable_mkldnn:True与--cpu_threads:6则启用了 CPU 上的 MKLDNN 加速。
从 test_train_inference_python.sh 的调度逻辑(第 283-287 行)可以看到,当trainer等于pact_train时,脚本会同时替换训练与导出命令,并置flag_quant=True;随后在推理阶段(func_inference),脚本会检查precision是否与量化标志匹配——量化模型只允许以 int8 精度推理(脚本第 145-147 行的跳过逻辑保证了「非 int8 精度 + 量化模型」组合会被跳过),从而保证整条链路自洽。
7. 源码级原理:PaddleSeg 如何实现 PACT 量化训练
7.1 量化配置与默认策略
PACT 量化训练的量化参数集中在 qat_config.py 的quant_config字典中,默认策略如下:
| 配置项 | 默认值 | 含义 |
|---|---|---|
weight_preprocess_type | None | 权重预处理类型,默认不做预处理 |
activation_preprocess_type | None | 激活预处理类型,默认不做预处理 |
weight_quantize_type | channel_wise_abs_max | 权重按通道维度的绝对值最大值做对称量化 |
activation_quantize_type | moving_average_abs_max | 激活使用滑动平均绝对值最大值估计量化范围 |
weight_bits | 8 | 权重量化位宽 |
activation_bits | 8 | 激活量化位宽 |
dtype | int8 | 量化后的数据类型 |
window_size | 10000 | range_abs_max量化方式的窗口大小 |
moving_rate | 0.9 | 滑动平均的衰减系数 |
quantizable_layer_type | ['Conv2D', 'Linear'] | 可被量化的算子类型 |
其中channel_wise_abs_max按每个输出通道独立统计权重的绝对值最大值,精度损失更小;moving_average_abs_max则在训练过程中以 0.9 的衰减系数持续估计激活的量化范围,这是 QAT 相比 PTQ(如 ptq.py 对应的后训练量化)的核心差异——量化范围在训练中同步学习。
7.2 量化范围:只量化骨干网络(Backbone)
QAT 训练 中有一个非常关键的细节函数skip_quant(第 125-138 行):
def skip_quant(model): if not hasattr(model, 'backbone'): logger.info("Quantize all target ops") return logger.info("Quantize all target ops in backbone") for name, cur_layer in model.named_sublayers(): if isinstance(cur_layer, (paddle.nn.Conv2D, paddle.nn.Linear)) \ and "backbone" not in name: cur_layer.skip_quant = True其逻辑是:如果模型带有backbone属性,则只对骨干网络中的 Conv2D 与 Linear 层做量化,模型头(head)中的卷积与全连接层全部跳过量化;只有没有 backbone 属性的模型才量化全部目标算子。这一设计在训练(qat_train.py)、评估(qat_val.py)和导出(qat_export.py)三个脚本中通过from qat_train import skip_quant被一致复用,保证量化结构在三阶段完全对齐。
7.3 训练-评估-导出的完整调用链
- 训练:qat_train.py 中
main()依次完成:解析--config与--iters/--batch_size/--learning_rate等覆盖参数 → 用Config+SegBuilder构建模型、数据集、损失与优化器 → 若指定--model_path则加载预训练权重(utils.load_entire_model)→skip_quant跳过 head 量化 →QAT(config=quant_config).quantize(model)完成模型量化改造 → 调用paddleseg.core.train进行量化感知训练; - 评估:qat_val.py 对模型做同样的量化改造后加载
--model_path权重,再调用paddleseg.core.evaluate计算量化模型的精度指标;它还额外支持--aug_eval、--flip_horizontal、--is_slide等增强/滑窗评估选项,以及 DeepLabV3P 特有的--data_format NHWC; - 导出:qat_export.py 通过
quantizer.save_quantized_model导出量化推理模型,并自动生成deploy.yaml(包含 val 数据集 transforms 与model.pdmodel/model.pdiparams文件名);支持--input_shape固定输入形状、--output_op(argmax/softmax/none)输出算子选择,以及--for_fd导出 FastDeploy 兼容格式(此时文件名为inference+inference.yml)。
值得注意的是,PACT 量化训练阶段通常需要一个「普通 FP32 预训练权重」作为起点。在 test_train_inference_python.sh 中可以看到:若配置中同时存在norm_train与pact_train,脚本会先把常规训练产出的权重保存下来(load_norm_train_model),再作为--model_path预训练参数喂给 PACT 训练(脚本第 331-334、371-374 行),保证量化训练从一个已收敛的 FP32 模型出发。
8. 推理阶段:int8 模型如何落地运行
量化导出的模型最终由 deploy/python/infer.py 执行推理。从源码看,其核心逻辑包括:
- 精度映射:内部维护
precision_map,将命令行--precision参数映射为推理引擎精度模式;TIPC 配置固定使用int8(见 infer_params 段的--precision:int8); - MKLDNN 加速:当
--enable_mkldnn=True且运行在 CPU 上时,会调用enable_mkldnn()并设置mkldnn_cache_capacity(10)缓存 10 种输入 shape,从而在 int8 推理时充分发挥 MKLDNN 的整型算子加速能力; - TensorRT 支持:若设备为 GPU 且
--use_trt=True,则要求 Paddle 版本 >= 2.2,并将precision_mode透传给 TensorRT 引擎(--precision=int8时即走 TensorRT INT8 模式); - Benchmark 输出:
--benchmark=True时通过 AutoLog 记录吞吐与延迟指标,输出目录由--save_dir与--model_name共同决定。
从 TIPC 配置看,量化模型的默认推理组合是「CPU + MKLDNN + int8」,这正是边缘端与无 GPU 环境下部署量化分割模型的标准用法。
9. 扩展:将 PACT 量化测试推广到更多模型
文档的结论表列出了 9 组模型,但仓库中实际的train_pact_infer_python.txt配置正好与之对应(test_tipc/configs/下共 9 份:fcn_hrnetw18、fcn_hrnetw18_small、deeplabv3p_resnet50、pphumanseg_lite、ocrnet_hrnetw18、ocrnet_hrnetw48、segformer_b0、pp_liteseg_stdc1、pp_liteseg_stdc2)。如果你想验证其他模型,可以参照现有配置模板:
- 复制一份同算法的
train_pact_infer_python.txt(如pp_liteseg_stdc1的配置适用于pp_liteseg_stdc2),将model_name、--config指向的 yml(位于test_tipc/configs/<模型>/目录,如fcn_hrnetw18_1024x512_cityscapes.yml)与--image_path图片列表替换为对应模型的值; - 确认该模型的网络结构带
backbone属性(大多数 PaddleSeg 模型满足),以便复用「只量化骨干」策略; - 依次执行
prepare.sh与test_train_inference_python.sh完成验证。
需要说明的是,PACT 量化训练属于模型压缩手段,实际部署收益(模型体积、推理加速比)取决于目标硬件与算子支持情况,TIPC 测试的核心价值在于保证量化链路的功能正确性与可复现性。
10. 小结
PaddleSeg 的 Linux GPU/CPU PACT 量化训练推理测试,以 test_train_inference_python.sh 为调度主程序,以train_pact_infer_python.txt为行为配置,通过 qat_train.py(训练)、qat_val.py(评估)、qat_export.py(导出)与 infer.py(int8 推理)四个脚本串起完整闭环。其技术要点可以概括为三句话:
- 量化策略:默认 8bit 权重/激活量化,权重用
channel_wise_abs_max、激活用moving_average_abs_max,量化范围在训练中动态学习(QAT 的本质); - 量化范围:带 backbone 的模型只量化骨干网络,head 通过
skip_quant跳过,训练/评估/导出三阶段严格一致; - 测试判定:任一阶段失败都会打印失败命令与日志路径,通过「复制命令手动复跑 + 查看落盘 log」即可快速定位问题。
- 人工智能
- 计算机视觉
- 预训练
【免费下载链接】PaddleSeg
Easy-to-use image segmentation library with awesome pre-trained model zoo, supporting wide-range of practical tasks in Semantic Segmentation, Interactive Segmentation, Panoptic Segmentation, Image Matting, 3D Segmentation, etc.
相关推荐
PaddleSeg TIPC 混合精度(AMP)训练推理全流程测试指南:Linux GPU/CPU 实战
PaddleSeg TIPC 混合精度(AMP)训练推理全流程测试指南:Linux GPU/CPU 实战 PaddleSeg 的 TIPC(Test Infra
人工智能计算机视觉预训练PaddleOCR Linux GPU/CPU KL离线量化训练与推理全流程测试指南
PaddleOCR Linux GPU/CPU KL离线量化训练与推理全流程测试指南 本文面向希望在 PaddleOCR 仓库中一键验证「KL 离线量化 → i
人工智能计算机视觉OCR深度学习大模型RAGPaddleSeg 离线量化(PTQ)训练推理测试全流程:从准备到 GPU/CPU 推理验证
PaddleSeg 离线量化(PTQ)训练推理测试全流程:从准备到 GPU/CPU 推理验证 导读 本文围绕 PaddleSeg 仓库 TIPC(Trainin
人工智能计算机视觉预训练
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考