PaddleDetection/deploy/python
Birdylx abeaad770e
[Cherry-pick] pick #9014, #9044 (#9098)
* fix solov2 deploy (#9014)

* [NPU] Fix cascade_mask_rcnn training problem (#9044)
2024-08-26 17:00:40 +08:00
..
README.md [deploy] alter save coco format json in deploy/python/infer.py (#6705) 2022-08-22 14:36:01 +08:00
benchmark_utils.py Refactor python deploy (#5253) 2022-03-03 16:10:01 +08:00
clrnet_postprocess.py Update numpy version to support Python 3.12 (#8853) 2024-03-08 14:33:02 +08:00
det_keypoint_unite_infer.py fix box filter when box_num > 0 but with no target class (#6506) 2022-07-25 23:20:52 +08:00
det_keypoint_unite_utils.py update review 2022-07-01 11:17:42 +08:00
infer.py [Cherry-pick] pick #9014, #9044 (#9098) 2024-08-26 17:00:40 +08:00
keypoint_infer.py merge uapi paddledetection (#8957) 2024-05-16 10:44:15 +08:00
keypoint_postprocess.py [cherry-pick] Fix some typos. (#5873) 2022-05-02 11:31:49 +08:00
keypoint_preprocess.py add Lightweight backbone of centernet (#4586) 2021-11-21 16:15:51 +08:00
mot_centertrack_infer.py [npu-tipc] fix npu tipc (#8196) 2023-05-08 19:42:53 +08:00
mot_jde_infer.py [npu-tipc] fix npu tipc (#8196) 2023-05-08 19:42:53 +08:00
mot_keypoint_unite_infer.py [npu-tipc] fix npu tipc (#8196) 2023-05-08 19:42:53 +08:00
mot_keypoint_unite_utils.py [npu-tipc] fix npu tipc (#8196) 2023-05-08 19:42:53 +08:00
mot_sde_infer.py [npu-tipc] fix npu tipc (#8196) 2023-05-08 19:42:53 +08:00
picodet_postprocess.py fix picodet postprocess for none-det case (#4462) 2021-11-05 01:08:43 +08:00
preprocess.py 【Hackathon + No.161】论文复现:CLRNet: Cross Layer Refinement Network for Lane Detection (#8278) 2023-06-01 14:15:48 +08:00
tracker_config.yml [MOT] add centertrack and refine centernet (#7510) 2023-01-09 15:35:57 +08:00
utils.py enable rotate infer results (#8616) 2023-10-20 17:29:50 +08:00
visualize.py [BugFix] Errors in Update pillow (#8460) 2023-07-24 10:09:03 +08:00

README.md

Python端预测部署

在PaddlePaddle中预测引擎和训练引擎底层有着不同的优化方法, 预测引擎使用了AnalysisPredictor专门针对推理进行了优化是基于C++预测库的Python接口该引擎可以对模型进行多项图优化减少不必要的内存拷贝。如果用户在部署已训练模型的过程中对性能有较高的要求我们提供了独立于PaddleDetection的预测脚本方便用户直接集成部署。

Python端预测部署主要包含两个步骤

  • 导出预测模型
  • 基于Python进行预测

1. 导出预测模型

PaddleDetection在训练过程包括网络的前向和优化器相关参数而在部署过程中我们只需要前向参数具体参考:导出模型,例如

# 导出YOLOv3检测模型
python tools/export_model.py -c configs/yolov3/yolov3_darknet53_270e_coco.yml --output_dir=./inference_model \
 -o weights=https://paddledet.bj.bcebos.com/models/yolov3_darknet53_270e_coco.pdparams

# 导出HigherHRNet(bottom-up)关键点检测模型
python tools/export_model.py -c configs/keypoint/higherhrnet/higherhrnet_hrnet_w32_512.yml -o weights=https://paddledet.bj.bcebos.com/models/keypoint/higherhrnet_hrnet_w32_512.pdparams

# 导出HRNet(top-down)关键点检测模型
python tools/export_model.py -c configs/keypoint/hrnet/hrnet_w32_384x288.yml -o weights=https://paddledet.bj.bcebos.com/models/keypoint/hrnet_w32_384x288.pdparams

# 导出FairMOT多目标跟踪模型
python tools/export_model.py -c configs/mot/fairmot/fairmot_dla34_30e_1088x608.yml -o weights=https://paddledet.bj.bcebos.com/models/mot/fairmot_dla34_30e_1088x608.pdparams

# 导出ByteTrack多目标跟踪模型(相当于只导出检测器)
python tools/export_model.py -c configs/mot/bytetrack/detector/ppyoloe_crn_l_36e_640x640_mot17half.yml -o weights=https://paddledet.bj.bcebos.com/models/mot/ppyoloe_crn_l_36e_640x640_mot17half.pdparams

导出后目录下,包括infer_cfg.yml, model.pdiparams, model.pdiparams.info, model.pdmodel四个文件。

2. 基于Python的预测

2.1 通用检测

在终端输入以下命令进行预测:

python deploy/python/infer.py --model_dir=./output_inference/yolov3_darknet53_270e_coco --image_file=./demo/000000014439.jpg --device=GPU

2.2 关键点检测

在终端输入以下命令进行预测:

# keypoint top-down(HRNet)/bottom-up(HigherHRNet)单独推理该模式下top-down模型HRNet只支持单人截图预测
python deploy/python/keypoint_infer.py --model_dir=output_inference/hrnet_w32_384x288/ --image_file=./demo/hrnet_demo.jpg --device=GPU --threshold=0.5
python deploy/python/keypoint_infer.py --model_dir=output_inference/higherhrnet_hrnet_w32_512/ --image_file=./demo/000000014439_640x640.jpg --device=GPU --threshold=0.5

# detector 检测 + keypoint top-down模型联合部署联合推理只支持top-down关键点模型
python deploy/python/det_keypoint_unite_infer.py --det_model_dir=output_inference/yolov3_darknet53_270e_coco/ --keypoint_model_dir=output_inference/hrnet_w32_384x288/ --video_file={your video name}.mp4  --device=GPU

注意:

  • 关键点检测模型导出和预测具体可参照keypoint,可分别在各个模型的文档中查找具体用法;
  • 此目录下的关键点检测部署为基础前向功能更多关键点检测功能可使用PP-Human项目参照pipeline

2.3 多目标跟踪

在终端输入以下命令进行预测:

# FairMOT跟踪
python deploy/python/mot_jde_infer.py --model_dir=output_inference/fairmot_dla34_30e_1088x608 --video_file={your video name}.mp4 --device=GPU

# ByteTrack跟踪
python deploy/python/mot_sde_infer.py --model_dir=output_inference/ppyoloe_crn_l_36e_640x640_mot17half/ --tracker_config=deploy/python/tracker_config.yml --video_file={your video name}.mp4 --device=GPU --scaled=True

# FairMOT多目标跟踪联合HRNet关键点检测联合推理只支持top-down关键点模型
python deploy/python/mot_keypoint_unite_infer.py --mot_model_dir=output_inference/fairmot_dla34_30e_1088x608/ --keypoint_model_dir=output_inference/hrnet_w32_384x288/ --video_file={your video name}.mp4 --device=GPU

注意:

  • 多目标跟踪模型导出和预测具体可参照[mot]](../../configs/mot/README.md),可分别在各个模型的文档中查找具体用法;
  • 此目录下的跟踪部署为基础前向功能以及联合关键点部署更多跟踪功能可使用PP-Human项目参照pipeline或PP-Tracking项目(绘制轨迹、出入口流量计数),参照pptracking

参数说明如下:

参数 是否必须 含义
--model_dir Yes 上述导出的模型路径
--image_file Option 需要预测的图片
--image_dir Option 要预测的图片文件夹路径
--video_file Option 需要预测的视频
--camera_id Option 用来预测的摄像头ID默认为-1(表示不使用摄像头预测可设置为0 - (摄像头数目-1) ),预测过程中在可视化界面按q退出输出预测结果到output/output.mp4
--device Option 运行时的设备,可选择CPU/GPU/XPU,默认为CPU
--run_mode Option 使用GPU时默认为paddle, 可选paddle/trt_fp32/trt_fp16/trt_int8
--batch_size Option 预测时的batch size在指定image_dir时有效默认为1
--threshold Option 预测得分的阈值默认为0.5
--output_dir Option 可视化结果保存的根目录默认为output/
--run_benchmark Option 是否运行benchmark同时需指定--image_file--image_dir默认为False
--enable_mkldnn Option CPU预测中是否开启MKLDNN加速默认为False
--cpu_threads Option 设置cpu线程数默认为1
--trt_calib_mode Option TensorRT是否使用校准功能默认为False。使用TensorRT的int8功能时需设置为True使用PaddleSlim量化后的模型时需要设置为False
--save_images Option 是否保存可视化结果
--save_results Option 是否在文件夹下将图片的预测结果以JSON的形式保存

说明:

  • 参数优先级顺序:camera_id > video_file > image_dir > image_file
  • run_modepaddle代表使用AnalysisPredictor精度float32来推理其他参数指用AnalysisPredictorTensorRT不同精度来推理。
  • 如果安装的PaddlePaddle不支持基于TensorRT进行预测需要自行编译详细可参考预测库编译教程
  • --run_benchmark如果设置为True则需要安装依赖pip install pynvml psutil GPUtil
  • 如果需要使用导出模型在coco数据集上进行评估请在推理时添加--save_results--use_coco_category参数用以保存coco评估所需要的json文件