16
16
17
17
## 1. 简介
18
18
本示例将以目标检测模型PP-YOLOE和PicoDet为例,介绍如何使用PaddleDetection中Inference部署模型,使用离线量化功能进行压缩,并使用敏感度分析功能提升离线量化精度。
19
-
19
+ 注意: [ Paddle-Inference-demo/c++/gpu/yolov3 ] ( https://github.com/PaddlePaddle/Paddle-Inference-Demo/tree/master/python/gpu/yolov3 ) 使用量化校准表会有精度不对齐的情况,可对yolov3_r50vd_dcn_270e_coco模型进行离线量化。
20
20
21
21
## 2.Benchmark
22
22
23
23
| 模型 | 策略 | 输入尺寸 | mAP<sup >val<br >0.5:0.95 | 预测时延<sup ><small >FP32</small ><sup ><br ><sup >(ms) | 预测时延<sup ><small >FP16</small ><sup ><br ><sup >(ms) | 预测时延<sup ><small >INT8</small ><sup ><br ><sup >(ms) | 配置文件 | Inference模型 |
24
24
| :-------- | :-------- | :--------: | :---------------------: | :----------------: | :----------------: | :---------------: | :-----------------------------: | :-----------------------------: |
25
- | PP-YOLOE-s | Base模型 | 640 * 640 | 43.1 | 11 .2ms | 7.7ms | - | - | [ Model] ( https://bj.bcebos.com/v1/paddle-slim-models/act/ppyoloe_crn_s_300e_coco.tar ) |
26
- | PP-YOLOE-s | 离线量化 | 640 * 640 | 42.6 | - | - | 6.7ms | - | [ Model ] ( https://bj.bcebos.com/v1/paddle-slim-models/act/ppyoloe_s_ptq.tar ) |
25
+ | yolov3_r50vd_dcn_270e_coco | Base模型 | 608 * 608 | 40.6 | 92 .2ms | 41.3ms | - | - | [ Model] ( https://paddle-inference-dist. bj.bcebos.com/Paddle-Inference-Demo/yolov3_r50vd_dcn_270e_coco.tgz ) |
26
+ | yolov3_r50vd_dcn_270e_coco | 离线量化 | 608 * 608 | 40.3 | - | - | 27.9ms | - | |
27
27
| | | | | | | | | |
28
- | PicoDet-s | Base模型 | 416* 416 | 32.5 | - | - | - | - | [ Model] ( https://paddledet.bj.bcebos.com/deploy/Inference/picodet_s_416_coco_lcnet.tar ) |
29
- | PicoDet-s | 离线量化(量化分析前) | 416* 416 | 0.0 | - | - | - | - | - |
30
- | PicoDet-s | 离线量化(量化分析后) | 416* 416 | 24.9 | - | - | - | - | [ Infer Model] ( https://bj.bcebos.com/v1/paddle-slim-models/act/picodet_s_ptq.tar ) |
28
+ | PicoDet-s | Base模型 | 416* 416 | 32.5 | 82.5ms | 59.7ms | - | - | [ Model] ( https://paddledet.bj.bcebos.com/deploy/Inference/picodet_s_416_coco_lcnet.tar ) |
29
+ | PicoDet-s | 离线量化(量化分析前) | 416* 416 | 0.0 | - | - | 39.1ms | - | - |
30
+ | PicoDet-s | 离线量化(量化分析后) | 416* 416 | 24.9 | - | - | 64.8ms | - | [ Infer Model] ( https://bj.bcebos.com/v1/paddle-slim-models/act/picodet_s_ptq.tar ) |
31
31
32
+ - mAP较低,导致目标框增多,NMS会增加耗时。
32
33
- mAP的指标均在COCO val2017数据集中评测得到,IoU=0.5:0.95。
33
-
34
+ 测速环境 : Tesla T4,TensorRT 8.6.1,CUDA 11.2,batch_size=1,cudnn 8.2.0 Intel(R)Xeon(R)Gold 6271C CPU,测速脚本 [ paddle_inference_eval.py ] ( https://github.com/PaddlePaddle/PaddleSlim/blob/develop/example/auto_compression/detection/paddle_inference_eval.py )
34
35
35
36
## 3. 离线量化流程
36
37
37
38
#### 3.1 准备环境
38
- - PaddlePaddle >= 2.3 (可从[ Paddle官网] ( https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html ) 下载安装)
39
- - PaddleSlim >= 2.3
39
+ - PaddlePaddle 2.6 (可从[ Paddle官网] ( https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html ) 下载安装)
40
+ - PaddleSlim 2.6
40
41
- PaddleDet >= 2.4
41
42
- opencv-python
42
43
43
44
安装paddlepaddle:
44
45
``` shell
45
46
# CPU
46
- pip install paddlepaddle
47
- # GPU
48
- pip install paddlepaddle-gpu
47
+ python -m pip install paddlepaddle==2.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
48
+ # GPU 以cuda11.2为例子
49
+ python -m pip install paddlepaddle-gpu==2.6.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
49
50
```
50
51
51
52
安装paddleslim:
@@ -103,11 +104,11 @@ python tools/export_model.py -c configs/picodet/picodet_s_416_coco_lcnet.yml \
103
104
104
105
离线量化示例通过post_quant.py脚本启动,会使用接口``` paddleslim.quant.quant_post_static ``` 对模型进行量化。配置config文件中模型路径、数据路径和量化相关的参数,配置完成后便可对模型进行离线量化。具体运行命令为:
105
106
106
- - PPYOLOE-s :
107
+ - yolov3_r50vd_dcn_270e_coco :
107
108
108
109
```
109
110
export CUDA_VISIBLE_DEVICES=0
110
- python post_quant.py --config_path=./configs/ppyoloe_s_ptq .yaml --save_dir=./ppyoloe_s_ptq
111
+ python post_quant.py --config_path=./configs/yolov3_r50vd_dcn .yaml --save_dir=./yolov3_r50vd_dcn_270e_coco_ptq
111
112
```
112
113
113
114
- PicoDet-s:
@@ -121,16 +122,21 @@ python post_quant.py --config_path=./configs/picodet_s_ptq.yaml --save_dir=./pic
121
122
#### 3.5 测试模型精度
122
123
123
124
使用eval.py脚本得到模型的mAP:
125
+ ppyoloe_s这个模型测试不出来精度,因为没有NMS
126
+ ```
127
+ export CUDA_VISIBLE_DEVICES=0
128
+ python eval.py --config_path=./configs/picodet_s_ptq.yaml
129
+ ```
124
130
```
125
131
export CUDA_VISIBLE_DEVICES=0
126
- python eval.py --config_path=./configs/ppyoloe_s_ptq .yaml
132
+ python eval.py --config_path=./configs/yolov3_r50vd_dcn .yaml
127
133
```
128
134
129
135
** 注意** :
130
136
- 要测试的模型路径可以在配置文件中` model_dir ` 字段下进行修改。
131
137
132
138
#### 3.6 提高离线量化精度
133
- 本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。PaddleSlim提供量化分析工具,会使用接口``` paddleslim.quant.AnalysisPTQ ``` ,可视化展示出不适合量化的层,通过跳过这些层,提高离线量化模型精度。``` paddleslim.quant.AnalysisPTQ ``` 详解见[ AnalysisPTQ.md] ( ../../../ docs/zh_cn/tutorials/quant/AnalysisPTQ .md) 。
139
+ 本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。PaddleSlim提供量化分析工具,会使用接口``` paddleslim.quant.AnalysisPTQ ``` ,可视化展示出不适合量化的层,通过跳过这些层,提高离线量化模型精度。``` paddleslim.quant.AnalysisPTQ ``` 详解见[ AnalysisPTQ.md] ( https://github.com/PaddlePaddle/PaddleSlim/blob/develop/ docs/zh_cn/tutorials/quant/post_training_quantization .md) 。
134
140
135
141
136
142
经过多个实验,包括尝试多种激活算法(avg,KL等)、weight的量化方式(abs_max,channel_wise_abs_max),对PicoDet-s进行离线量化后精度均为0,以PicoDet-s为例,量化分析工具具体使用方法如下:
@@ -171,6 +177,141 @@ python post_quant.py --config_path=./configs/picodet_s_analyzed_ptq.yaml --save_
171
177
## 4.预测部署
172
178
预测部署可参考[ Detection模型自动压缩示例] ( https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/detection )
173
179
180
+
181
+ 量化模型可在GPU上可以使用TensorRT进行预测,在CPU上可以使用MKLDNN进行预测。
182
+
183
+ 以下字段可用于配置预测参数:
184
+
185
+ | 参数名 | 含义 |
186
+ | :------:| :------:|
187
+ | model_path | inference 模型文件所在目录,该目录下需要有文件 model.pdmodel 和 model.pdiparams 两个文件 |
188
+ | reader_config | eval时模型reader的配置文件路径 |
189
+ | image_file | 如果只测试单张图片效果,直接根据image_file指定图片路径 |
190
+ | device | 使用GPU或者CPU预测,可选CPU/GPU |
191
+ | use_trt | 是否使用 TesorRT 预测引擎 |
192
+ | use_mkldnn | 是否启用``` MKL-DNN ``` 加速库,注意``` use_mkldnn ``` 与``` use_gpu ``` 同时为``` True ``` 时,将忽略``` enable_mkldnn ``` ,而使用``` GPU ``` 预测 |
193
+ | cpu_threads | CPU预测时,使用CPU线程数量,默认10 |
194
+ | precision | 预测精度,包括` fp32/fp16/int8 ` |
195
+ | include_nms | 是否包含nms,如果不包含nms,则设置False,如果包含nms,则设置为True |
196
+ | use_dynamic_shape | 是否使用动态shape,如果使用动态shape,则设置为True,否则设置为False |
197
+ | img_shape | 输入图片的大小。这里默认为640,意味着图像将被调整到640* 640 |
198
+ | trt_calib_mode | 如果模型是通过TensorRT离线量化校准生成的,那么需要将此参数设置为True。|
199
+
200
+ -Paddle-TesorRT预测示例:
201
+
202
+ yolov3_r50vd_dcn_270e_coco模型
203
+ ``` shell
204
+ python paddle_inference_eval.py \
205
+ --model_path=yolov3_r50vd_dcn_270e_coco \
206
+ --reader_config=configs/yolov3_r50vd_dcn.yml \
207
+ --use_trt=True \
208
+ --precision=fp32 \
209
+ --include_nms=True \
210
+ --benchmark=True
211
+ ```
212
+ ``` shell
213
+ python paddle_inference_eval.py \
214
+ --model_path=yolov3_r50vd_dcn_270e_coco_ptq \
215
+ --reader_config=configs/yolov3_r50vd_dcn.yml \
216
+ --use_trt=True \
217
+ --precision=int8 \
218
+ --include_nms=True \
219
+ --benchmark=True
220
+ ```
221
+ picodet_s模型
222
+ ``` shell
223
+ python paddle_inference_eval.py \
224
+ --model_path=picodet_s_416_coco_lcnet \
225
+ --reader_config=configs/picodet_reader.yml \
226
+ --use_trt=True \
227
+ --precision=fp16 \
228
+ --include_nms=True \
229
+ --benchmark=True
230
+ ```
231
+ 量化分析前
232
+ ``` shell
233
+ python paddle_inference_eval.py \
234
+ --model_path=picodet_s_ptq \
235
+ --reader_config=configs/picodet_reader.yml \
236
+ --use_trt=True \
237
+ --precision= \
238
+ --include_nms=True \
239
+ --benchmark=True
240
+ ```
241
+ 量化分析后
242
+ ``` shell
243
+ python paddle_inference_eval.py \
244
+ --model_path=picodet_s_analyzed_ptq_out \
245
+ --reader_config=configs/picodet_reader.yml \
246
+ --use_trt=True \
247
+ --precision=int8 \
248
+ --include_nms=True \
249
+ --benchmark=True
250
+ ```
251
+ #### 4.1 C++部署
252
+ 请参考[ YOLOv3推理] ( https://github.com/PaddlePaddle/Paddle-Inference-Demo/tree/master/c%2B%2B/gpu/yolov3 )
253
+
254
+ 编译样例
255
+ - 文件yolov3_test.cc改成PicoDet-s.cc,为预测的样例程序(程序中的输入为固定值,如果您有opencv或其他方式进行数据读取的需求,需要对程序进行一定的修改)。
256
+ - 脚本compile.sh包含了第三方库、预编译库的信息配置。
257
+ - 脚本run.sh为一键运行脚本。
258
+ 编译前,需要根据自己的环境修改compile.sh中的相关代码配置依赖库:
259
+
260
+ ``` shell
261
+ # 编译的 demo 名称
262
+ DEMO_NAME=picoDet-s
263
+
264
+ # 根据预编译库中的version.txt信息判断是否将以下三个标记打开
265
+ WITH_MKL=ON
266
+ WITH_GPU=ON
267
+ USE_TENSORRT=ON
268
+
269
+ # 配置预测库的根目录
270
+ LIB_DIR=${work_path} /../lib/paddle_inference
271
+
272
+ # 如果上述的WITH_GPU 或 USE_TENSORRT设为ON,请设置对应的CUDA, CUDNN, TENSORRT的路径。
273
+ CUDNN_LIB=/usr/lib/x86_64-linux-gnu/
274
+ CUDA_LIB=/usr/local/cuda/lib64
275
+ TENSORRT_ROOT=/usr/local/TensorRT-7.1.3.4
276
+ ```
277
+ 运行bash compile.sh编译样例
278
+
279
+ - 运行样例
280
+ 使用原生GPU运行样例
281
+ ``` shell
282
+ ./build/picodet-s --model_file picodet_s_416_coco_lenet/model.pdmodel --params_file picodet_s_416_coco_lenet/model.pdiparams
283
+ ```
284
+ 使用Trt FP32运行样例
285
+ ``` shell
286
+ ./build/picodet-s --model_file picodet_s_416_coco_lenet/model.pdmodel --params_file picodet_s_416_coco_lenet/model.pdiparams --run_mode=trt_fp32
287
+ ```
288
+ 使用Trt FP16运行样例
289
+ ``` shell
290
+ ./build/picodet-s --model_file picodet_s_416_coco_lenet/model.pdmodel --params_file picodet_s_416_coco_lenet/model.pdiparams --run_mode=trt_fp16
291
+ ```
292
+ 使用Trt Int8运行样例
293
+ 在使用Trt Int8运行样例时,相同的运行命令需要执行两次。
294
+ 生成量化校准表
295
+ ``` shell
296
+ ./build/picodet-s --model_file picodet_s_416_coco_lcnet/model.pdmodel --params_file picodet_s_416_coco_lcnet/model.pdiparams --run_mode=trt_int8
297
+ ```
298
+ 加载校准表预测的log:
299
+ ``` shell
300
+ I0623 08:40:49.386909 107053 tensorrt_engine_op.h:159] This process is generating calibration table for Paddle TRT int8...
301
+ I0623 08:40:49.387279 107057 tensorrt_engine_op.h:352] Prepare TRT engine (Optimize model structure, Select OP kernel etc). This process may cost a lot of time.
302
+ I0623 08:41:13.784473 107053 analysis_predictor.cc:791] Wait for calib threads done.
303
+ I0623 08:41:14.419198 107053 analysis_predictor.cc:793] Generating TRT Calibration table data, this may cost a lot of time...
304
+ ```
305
+ 使用Trt dynamic shape运行样例(以Trt FP32为例)
306
+ ``` shell
307
+ ./build/picodet-s --model_file picodet_s_416_coco_lcnet/model.pdmodel --params_file picodet_s_416_coco_lcnet/model.pdiparams --run_mode=trt_fp32 --use_dynamic_shape=1
308
+ ```
309
+ | 模型 | trt-fp32 | trt-fp16 | trt-int8 | paddle_gpu fp32 | trt_fp32(dynamic_shape) |
310
+ | :------:| :------:| :------:| :------:| :------:| :------:|
311
+ | PicoDet-s | 3.05ms | 2.66ms | 2.40ms | 7.51ms | 2.82ms |
312
+
313
+ - 测速环境: Tesla T4,TensorRT 8.6.1,CUDA 11.6,batch_size=1,cudnn 8.4.0 Intel(R)Xeon(R)Gold 6271C CPU
314
+
174
315
## 5.FAQ
175
316
176
317
- 如果想对模型进行自动压缩,可进入[ Detection模型自动压缩示例] ( https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/detection ) 中进行实验。
0 commit comments