PulseFocusPlatform/deploy/python/README.md

50 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2022-06-01 11:18:00 +08:00
# Python端预测部署
在PaddlePaddle中预测引擎和训练引擎底层有着不同的优化方法, 预测引擎使用了AnalysisPredictor专门针对推理进行了优化是基于[C++预测库](https://www.paddlepaddle.org.cn/documentation/docs/zh/advanced_guide/inference_deployment/inference/native_infer.html)的Python接口该引擎可以对模型进行多项图优化减少不必要的内存拷贝。如果用户在部署已训练模型的过程中对性能有较高的要求我们提供了独立于PaddleDetection的预测脚本方便用户直接集成部署。
主要包含两个步骤:
- 导出预测模型
- 基于Python进行预测
## 1. 导出预测模型
PaddleDetection在训练过程包括网络的前向和优化器相关参数而在部署过程中我们只需要前向参数具体参考:[导出模型](https://github.com/PaddlePaddle/PaddleDetection/blob/develop/deploy/EXPORT_MODEL.md)
导出后目录下,包括`infer_cfg.yml`, `model.pdiparams`, `model.pdiparams.info`, `model.pdmodel`四个文件。
## 2. 基于Python的预测
在终端输入以下命令进行预测:
```bash
python deploy/python/infer.py --model_dir=./inference/yolov3_mobilenet_v1_roadsign --image_file=./demo/road554.png --use_gpu=True
```
参数说明如下:
| 参数 | 是否必须|含义 |
|-------|-------|----------|
| --model_dir | Yes|上述导出的模型路径 |
| --image_file | Option |需要预测的图片 |
| --image_dir | Option | 要预测的图片文件夹路径 |
| --video_file | Option |需要预测的视频 |
| --camera_id | Option | 用来预测的摄像头ID默认为-1(表示不使用摄像头预测可设置为0 - (摄像头数目-1) ),预测过程中在可视化界面按`q`退出输出预测结果到output/output.mp4|
| --use_gpu | No |是否GPU默认为False|
| --run_mode | No |使用GPU时默认为fluid, 可选fluid/trt_fp32/trt_fp16/trt_int8|
| --batch_size | No |预测时的batch size在指定`image_dir`时有效 |
| --threshold | No|预测得分的阈值默认为0.5|
| --output_dir | No|可视化结果保存的根目录默认为output/|
| --run_benchmark | No| 是否运行benchmark同时需指定`--image_file`或`--image_dir` |
| --enable_mkldnn | No | CPU预测中是否开启MKLDNN加速 |
| --cpu_threads | No| 设置cpu线程数默认为1 |
说明:
- 参数优先级顺序:`camera_id` > `video_file` > `image_dir` > `image_file`
- run_modefluid代表使用AnalysisPredictor精度float32来推理其他参数指用AnalysisPredictorTensorRT不同精度来推理。
- 如果安装的PaddlePaddle不支持基于TensorRT进行预测需要自行编译详细可参考[预测库编译教程](https://paddleinference.paddlepaddle.org.cn/user_guides/source_compile.html)。