Skip to content

Commit e72073a

Browse files
committed
修复目标检测模型离线量化示例
1 parent c634106 commit e72073a

File tree

3 files changed

+106
-16
lines changed

3 files changed

+106
-16
lines changed

example/post_training_quantization/detection/README.md

+102-11
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323
| 模型 | 策略 | 输入尺寸 | 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模型 |
2424
| :-------- |:-------- |:--------: | :---------------------: | :----------------: | :----------------: | :---------------: | :-----------------------------: | :-----------------------------: |
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) |
27-
| | | | | | | | | |
2825
| PicoDet-s | Base模型 | 416*416 | 32.5 | - | - | - | - | [Model](https://paddledet.bj.bcebos.com/deploy/Inference/picodet_s_416_coco_lcnet.tar) |
2926
| PicoDet-s | 离线量化(量化分析前) | 416*416 | 0.0 | - | - | - | - | - |
3027
| PicoDet-s | 离线量化(量化分析后) | 416*416 | 24.9 | - | - | - | - | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/picodet_s_ptq.tar) |
@@ -35,22 +32,24 @@
3532
## 3. 离线量化流程
3633

3734
#### 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
35+
- PaddlePaddle 2.5 (可从[Paddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)下载安装)
36+
- PaddleSlim 2.5
4037
- PaddleDet >= 2.4
4138
- opencv-python
4239

4340
安装paddlepaddle:
4441
```shell
4542
# CPU
46-
pip install paddlepaddle
43+
python -m pip install paddlepaddle==2.5.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
4744
# GPU
48-
pip install paddlepaddle-gpu
45+
python -m pip install paddlepaddle-gpu==2.5.0.post116 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
4946
```
5047

5148
安装paddleslim:
49+
注意,需要修改setup.py中slim_version='2.5',否则会安装最新版本的PaddleSlim。
5250
```shell
53-
pip install paddleslim
51+
git clone -b release/2.5 https://github.com/PaddlePaddle/PaddleSlim.git & cd PaddleSlim
52+
python setup.py install
5453
```
5554

5655
安装paddledet:
@@ -66,7 +65,7 @@ pip install paddledet
6665

6766
如果数据集为非COCO格式数据,请修改[configs](./configs)中reader配置文件中的Dataset字段。
6867

69-
以PP-YOLOE模型为例,如果已经准备好数据集,请直接修改[./configs/ppyoloe_s_ptq.yml]`EvalDataset``dataset_dir`字段为自己数据集路径即可。
68+
以PP-YOLOE模型为例,如果已经准备好数据集,请直接修改[./configs/ppyoloe_s_ptq.yml]`EvalDataset``TrainDataset``dataset_dir`字段为自己数据集路径即可。
7069

7170
#### 3.3 准备预测模型
7271

@@ -125,12 +124,17 @@ python post_quant.py --config_path=./configs/picodet_s_ptq.yaml --save_dir=./pic
125124
export CUDA_VISIBLE_DEVICES=0
126125
python eval.py --config_path=./configs/ppyoloe_s_ptq.yaml
127126
```
127+
这个,测试不出来模型精度,因为[ppyoloe_s_ptq.yaml](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/example/post_training_quantization/detection/configs/ppyoloe_s_ptq.yaml)的model_dir是没有NMS的,所以不打印精度
128+
```
129+
export CUDA_VISIBLE_DEVICES=0
130+
python eval.py --config_path=./configs/picodet_s_ptq.yaml
131+
```
128132

129133
**注意**
130134
- 要测试的模型路径可以在配置文件中`model_dir`字段下进行修改。
131135

132136
#### 3.6 提高离线量化精度
133-
本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。PaddleSlim提供量化分析工具,会使用接口```paddleslim.quant.AnalysisPTQ```,可视化展示出不适合量化的层,通过跳过这些层,提高离线量化模型精度。```paddleslim.quant.AnalysisPTQ```详解见[AnalysisPTQ.md](../../../docs/zh_cn/tutorials/quant/AnalysisPTQ.md)
137+
本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。PaddleSlim提供量化分析工具,会使用接口```paddleslim.quant.AnalysisPTQ```,可视化展示出不适合量化的层,通过跳过这些层,提高离线量化模型精度。```paddleslim.quant.AnalysisPTQ```详解见[离线量化](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/tutorials/quant/post_training_quantization.md)
134138

135139

136140
经过多个实验,包括尝试多种激活算法(avg,KL等)、weight的量化方式(abs_max,channel_wise_abs_max),对PicoDet-s进行离线量化后精度均为0,以PicoDet-s为例,量化分析工具具体使用方法如下:
@@ -167,10 +171,97 @@ python post_quant.py --config_path=./configs/picodet_s_analyzed_ptq.yaml --save_
167171
注:分析之后若需要直接产出符合目标精度的量化模型,demo代码不会使用少量数据集验证,会自动使用全量验证数据。
168172

169173

170-
171174
## 4.预测部署
172175
预测部署可参考[Detection模型自动压缩示例](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/detection)
173176

177+
- TensorRT预测
178+
-[picodet_reader.yml](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/example/auto_compression/detection/configs/picodet_reader.yml)中的dataset_dir改成你环境下的数据集路径
179+
180+
```shell
181+
python paddle_inference_eval.py \
182+
--model_path=picodet_s_416_coco_lcnet \
183+
--reader_config=configs/picodet_reader.yml \
184+
--use_trt=True \
185+
--precision=fp16 \
186+
--include_nms=True \
187+
--benchmark=True
188+
```
189+
量化分析前:
190+
```shell
191+
python paddle_inference_eval.py \
192+
--model_path=picodet_s_ptq \
193+
--reader_config=configs/picodet_reader.yml \
194+
--use_trt=True \
195+
--precision= \
196+
--include_nms=True \
197+
--benchmark=True
198+
```
199+
量化分析后:
200+
```shell
201+
python paddle_inference_eval.py \
202+
--model_path=picodet_s_analyzed_ptq_out \
203+
--reader_config=configs/picodet_reader.yml \
204+
--use_trt=True \
205+
--precision=int8 \
206+
--include_nms=True \
207+
--benchmark=True
208+
```
209+
#### 4.1 C++部署
210+
请参考[YOLOv3推理](https://github.com/PaddlePaddle/Paddle-Inference-Demo/tree/master/c%2B%2B/gpu/yolov3)
211+
编译样例
212+
- 文件yolov3_test.cc改成PicoDet-s.cc,为预测的样例程序(程序中的输入为固定值,如果您有opencv或其他方式进行数据读取的需求,需要对程序进行一定的修改)。
213+
- 脚本compile.sh包含了第三方库、预编译库的信息配置。
214+
- 脚本run.sh为一键运行脚本。
215+
编译前,需要根据自己的环境修改compile.sh中的相关代码配置依赖库:
216+
```shell
217+
# 编译的 demo 名称
218+
DEMO_NAME=picoDet-s
219+
220+
# 根据预编译库中的version.txt信息判断是否将以下三个标记打开
221+
WITH_MKL=ON
222+
WITH_GPU=ON
223+
USE_TENSORRT=ON
224+
225+
# 配置预测库的根目录
226+
LIB_DIR=${work_path}/../lib/paddle_inference
227+
228+
# 如果上述的WITH_GPU 或 USE_TENSORRT设为ON,请设置对应的CUDA, CUDNN, TENSORRT的路径。
229+
CUDNN_LIB=/usr/lib/x86_64-linux-gnu/
230+
CUDA_LIB=/usr/local/cuda/lib64
231+
TENSORRT_ROOT=/usr/local/TensorRT-7.1.3.4
232+
```
233+
运行bash compile.sh编译样例
234+
运行样例:
235+
- 使用原生GPU运行样例
236+
```shell
237+
./build/picodet-s --model_file picodet_s_416_coco_lenet/model.pdmodel --params_file picodet_s_416_coco_lenet/model.pdiparams
238+
```
239+
- 使用Trt FP32运行样例
240+
```shell
241+
./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
242+
```
243+
244+
- 使用Trt FP16运行样例
245+
```shell
246+
./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
247+
```
248+
- 使用Trt INT8运行样例
249+
在使用Trt Int8运行样例时,相同的运行命令需要执行两次。
250+
生成量化校准表
251+
```shell
252+
./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
253+
```
254+
执行后,模型文件夹Picodet下的_opt_cache文件夹下会多出一个名字为trt_calib_*的文件,即校准表。
255+
加载校准表执行预测
256+
```shell
257+
./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
258+
```
259+
- 使用Trt dynamic shape运行样例(以FP32为例)
260+
```shell
261+
./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
262+
```
263+
264+
174265
## 5.FAQ
175266

176267
- 如果想对模型进行自动压缩,可进入[Detection模型自动压缩示例](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/detection)中进行实验。

example/post_training_quantization/detection/configs/picodet_s_ptq.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ skip_tensor_list: None
77
metric: COCO
88
num_classes: 80
99

10+
1011
# Datset configuration
1112
TrainDataset:
1213
!COCODataSet
@@ -34,5 +35,4 @@ EvalReader:
3435
- Resize: {interp: 2, target_size: *eval_size, keep_ratio: False}
3536
- NormalizeImage: {is_scale: true, mean: [0.485,0.456,0.406], std: [0.229, 0.224,0.225]}
3637
- Permute: {}
37-
batch_size: 32
38-
38+
batch_size: 16

example/post_training_quantization/detection/configs/ppyoloe_s_ptq.yaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
input_list: ['image']
1+
input_list: ['image', 'scale_factor']
22
arch: PPYOLOE # When export exclude_nms=True, need set arch: PPYOLOE
33
model_dir: ./ppyoloe_crn_s_300e_coco
44
model_filename: model.pdmodel
55
params_filename: model.pdiparams
66
metric: COCO
77
num_classes: 80
88

9-
109
# Datset configuration
1110
TrainDataset:
1211
!COCODataSet
@@ -29,4 +28,4 @@ EvalReader:
2928
- Resize: {target_size: [640, 640], keep_ratio: False, interp: 2}
3029
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
3130
- Permute: {}
32-
batch_size: 32
31+
batch_size: 16

0 commit comments

Comments
 (0)