Skip to content

Commit 574c816

Browse files
authored
YOLO系列模型自动压缩示例 (#1851)
1 parent cf494d9 commit 574c816

File tree

5 files changed

+362
-258
lines changed

5 files changed

+362
-258
lines changed

example/auto_compression/pytorch_yolo_series/README.md

+58-52
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,45 @@
1919

2020
| 模型 | 策略 | 输入尺寸 | mAP<sup>val<br>0.5:0.95 | 模型体积 | 预测时延<sup><small>FP32</small><sup><br><sup> |预测时延<sup><small>FP16</small><sup><br><sup> | 预测时延<sup><small>INT8</small><sup><br><sup> | 内存占用 | 显存占用 | 配置文件 | Inference模型 |
2121
|:--------------|:-------- |:--------: |:-----------------------:|:------:| :----------------: | :----------------: |:----------------: | :----------------: | :---------------: |:------------------------------------------------------------------------------------------------------------------------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
22-
| YOLOv5s | Base模型 | 640*640 | 37.4 | 28.1MB | 6.87ms | 3.51ms | - | 1718MB | 705MB | - | [Model](https://paddle-slim-models.bj.bcebos.com/act/yolov5s.onnx) |
23-
| YOLOv5s | 离线量化 | 640*640 | 36.0 | 7.4MB | - | - | 3.17ms | 736MB | 315MB | [config](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/post_training_quantization/pytorch_yolo_series) | - |
24-
| YOLOv5s | ACT量化训练 | 640*640 | **36.9** | 7.4MB | - | - | **3.17ms** | 736MB | 315MB | [config](./configs/yolov5s_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov5s_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov5s_quant_onnx.tar) |
22+
| YOLOv5s | Base模型 | 640*640 | 37.5 | 28.1MB | 14.4ms | 6.9ms | - | 2637MB | 1143MB | - | [Model](https://paddle-slim-models.bj.bcebos.com/act/yolov5s.onnx) |
23+
| YOLOv5s | 离线量化 | 640*640 | 36.7 | 7.5MB | - | - | 6.4ms | 2669MB | 1089MB | [config](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/post_training_quantization/pytorch_yolo_series) | - |
24+
| YOLOv5s | ACT量化训练 | 640*640 | **36.8** | 7.5MB | - | - | **6.8ms** | 2593MB | 1083MB | [config](./configs/yolov5s_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov5s_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov5s_quant_onnx.tar) |
2525
| | | | | | | | | |
26-
| YOLOv6s | Base模型 | 640*640 | 42.4 | 65.9MB | 9.18ms | 3.58ms | - | 1208MB | 555MB | - | [Model](https://paddle-slim-models.bj.bcebos.com/act/yolov6s.onnx) |
27-
| YOLOv6s | KL离线量化 | 640*640 | 30.3 | 16.8MB | - | - | 2.81ms | 736MB | 315MB | [config](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/post_training_quantization/pytorch_yolo_series) | - |
28-
| YOLOv6s | 量化蒸馏训练 | 640*640 | **41.3** | 16.8MB | - | - | **2.81ms** | 736MB | 315MB | [config](./configs/yolov6s_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov6s_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov6s_quant_onnx.tar) |
26+
| YOLOv6s | Base模型 | 640*640 | 42.5 | 65.9MB | 18.3ms | 7.1ms | - | 2660MB | 1183MB | - | [Model](https://paddle-slim-models.bj.bcebos.com/act/yolov6s.onnx) |
27+
| YOLOv6s | KL离线量化 | 640*640 | 34.0 | 17MB | - | - | 4.9ms | 2570MB | 1085MB | [config](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/post_training_quantization/pytorch_yolo_series) | - |
28+
| YOLOv6s | 量化蒸馏训练 | 640*640 | **41.3** | 17MB | - | - | **4.9ms** | 2532MB | 1085MB | [config](./configs/yolov6s_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov6s_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov6s_quant_onnx.tar) |
2929
| | | | | | | | | |
30-
| YOLOv6s_v2 | Base模型 | 640*640 | 43.4 | 67.4MB | 9.18ms | 3.58ms | - | 1208MB | 555MB | - | [Model](https://github.com/meituan/YOLOv6/releases/download/0.2.0/yolov6s.onnx) |
31-
| YOLOv6s_v2 | 量化蒸馏训练 | 640*640 | **43.0** | 16.8MB | - | - | **2.81ms** | 736MB | 315MB | [config](./configs/yolov6s_v2_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov6s_v2_0_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov6s_v2_0_quant_onnx.tar) |
32-
| | | | | | | | | |
33-
| YOLOv7 | Base模型 | 640*640 | 51.1 | 141MB | 26.76ms | 8.16ms | - | 1722MB | 917MB | - | [Model](https://paddle-slim-models.bj.bcebos.com/act/yolov7.onnx) |
34-
| YOLOv7 | 离线量化 | 640*640 | 50.2 | 36MB | - | - | 5.19ms | 827MB | 363MB | [config](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/post_training_quantization/pytorch_yolo_series) | - |
35-
| YOLOv7 | ACT量化训练 | 640*640 | **50.9** | 36MB | - | - | **5.19ms** | 827MB | 363MB | [config](./configs/yolov7_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov7_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov7_quant_onnx.tar) |
36-
| | | | | | | | | |
37-
| YOLOv7-Tiny | Base模型 | 640*640 | 37.3 | 24MB | 5.06ms | 2.32ms | - | 738MB | 349MB | - | [Model](https://paddle-slim-models.bj.bcebos.com/act/yolov7-tiny.onnx) |
38-
| YOLOv7-Tiny | 离线量化 | 640*640 | 35.8 | 6.1MB | - | - | 1.68ms | 729MB | 315MB | - | - |
39-
| YOLOv7-Tiny | ACT量化训练 | 640*640 | **37.0** | 6.1MB | - | - | **1.68ms** | 729MB | 315MB | [config](./configs/yolov7_tiny_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov7_tiny_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov7_tiny_quant_onnx.tar) |
30+
| YOLOv7-Tiny | Base模型 | 640*640 | 37.2 | 24MB | 13.2ms | 8.1ms | - | 2466MB | 1133MB | - | [Model](https://paddle-slim-models.bj.bcebos.com/act/yolov7-tiny.onnx) |
31+
| YOLOv7-Tiny | ACT量化训练 | 640*640 | **36.8** | 6.2MB | - | - | **6.6ms** | 2547MB | 1085MB | [config](./configs/yolov7_tiny_qat_dis.yaml) | [Infer Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov7_tiny_quant.tar) &#124; [ONNX Model](https://bj.bcebos.com/v1/paddle-slim-models/act/yolov7_tiny_quant_onnx.tar) |
4032

4133
说明:
42-
- mAP的指标均在COCO val2017数据集中评测得到。
43-
- YOLOv7模型在Tesla T4的GPU环境下开启TensorRT 8.4.1,batch_size=1, 测试脚本是[cpp_infer](./cpp_infer)
34+
- mAP的指标均在COCO val2017数据集中评测得到,IoU=0.5:0.95
35+
- 测速环境: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](./paddle_inference_eval.py)
4436

4537
## 3. 自动压缩流程
4638

4739
### 3.1 准备环境
48-
- PaddlePaddle >= 2.4版本 (可从[Paddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)根据相应环境的安装指令进行安装)
49-
- PaddleSlim >= 2.4版本
40+
- PaddlePaddle 2.6 (可从[Paddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)根据相应环境的安装指令进行安装)
41+
- PaddleSlim 2.6
5042

5143
(1)安装paddlepaddle
5244
```shell
5345
# CPU
54-
pip install paddlepaddle==2.4.1
46+
python -m pip install paddlepaddle==2.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
5547
# GPU 以Ubuntu、CUDA 11.2为例
56-
python -m pip install paddlepaddle-gpu==2.4.1.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
48+
python -m pip install paddlepaddle-gpu==2.6.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
5749
```
5850

59-
(2)安装paddleslim>=2.4
51+
(2)安装paddleslim 2.6
6052
```shell
6153
pip install paddleslim
6254
```
55+
(3) 安装paddledet
56+
```shell
57+
pip install paddledet
58+
```
59+
注:安装PaddleDet的目的是为了直接使用PaddleDetection中的Dataloader组件。
60+
6361

6462
#### 版本对齐
6563

@@ -135,6 +133,11 @@ pip install paddleslim
135133
```
136134

137135
**注意**:目前ACT支持**不带NMS**模型,使用如上命令导出即可。也可以直接下载我们已经准备好的[yolov7.onnx](https://paddle-slim-models.bj.bcebos.com/act/yolov7-tiny.onnx)
136+
将ONNX模型转换为Paddle模型,举例:
137+
使用命令行将YOLOv6s.onnx转换为paddle模型
138+
```shell
139+
x2paddle --framework=onnx --model=yolov6s.onnx --save_dir=yolov6_model
140+
```
138141

139142
### 3.4 自动压缩并产出模型
140143

@@ -145,13 +148,14 @@ pip install paddleslim
145148
- 单卡训练:
146149
```
147150
export CUDA_VISIBLE_DEVICES=0
148-
python run.py --config_path=./configs/yolov7_tiny_qat_dis.yaml --save_dir='./output/'
151+
python run.py --config_path=./configs/yolov7_tiny_qat_dis.yaml --save_dir='./yolov7-quantAware/'
149152
```
150153

151154
- 多卡训练:
152155
```
153-
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch --log_dir=log --gpus 0,1,2,3 run.py \
154-
--config_path=./configs/yolov7_tiny_qat_dis.yaml --save_dir='./output/'
156+
export CUDA_VISIBLE_DEVICES=0,1,2,3
157+
python -m paddle.distributed.launch --log_dir=log --gpus 0,1,2,3 run.py \
158+
--config_path=./configs/yolov6s_qat_dis.yaml --save_dir='./yolov6s_quantaware/'
155159
```
156160

157161

@@ -177,42 +181,61 @@ CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch --log_dir=log -
177181
| model_path | inference 模型文件所在目录,该目录下需要有文件 model.pdmodel 和 model.pdiparams 两个文件 |
178182
| dataset_dir | eval时数据验证集路径, 默认`dataset/coco` |
179183
| image_file | 如果只测试单张图片效果,直接根据image_file指定图片路径 |
184+
| val_image_dir | COCO数据集中验证图像的目录名,默认为val2017 |
185+
| val_anno_path | 指定COCO数据集的注释(annotation)文件路径,这是包含验证集标注信息的JSON文件,默认为annotations/instances_val2017.json |
186+
| benchmark | 指定是否运行性能基准测试。如果设置为True,程序将会进行性能测试 |
180187
| device | 使用GPU或者CPU预测,可选CPU/GPU |
181188
| use_trt | 是否使用 TesorRT 预测引擎 |
182189
| use_mkldnn | 是否启用```MKL-DNN```加速库,注意```use_mkldnn``````use_gpu```同时为```True```时,将忽略```enable_mkldnn```,而使用```GPU```预测 |
190+
| use_dynamic_shape | 是否使用动态形状(dynamic_shape)功能 |
183191
| cpu_threads | CPU预测时,使用CPU线程数量,默认10 |
184192
| precision | 预测精度,包括`fp32/fp16/int8` |
193+
| arch | 指定所使用的模型架构的名称,例如YOLOv5 |
194+
| img_shape | 指定模型输入的图像尺寸 |
195+
| batch_size | 指定模型输入的批处理大小 |
185196

186-
TensorRT Python部署:
197+
Paddle-TensorRT Python部署:
187198

188199
首先安装带有TensorRT的[Paddle安装包](https://www.paddlepaddle.org.cn/inference/v2.3/user_guides/download_lib.html#python)
189200

190201
然后使用[paddle_inference_eval.py](./paddle_inference_eval.py)进行部署:
191202

203+
204+
- YOLOv5
192205
```shell
193206
python paddle_inference_eval.py \
194-
--model_path=output \
195-
--reader_config=configs/yoloe_reader.yml \
207+
--model_path==yolov5_model/inference_model \
208+
--dataset_dir=/datasets/coco \
196209
--use_trt=True \
197-
--precision=int8
210+
--precision=fp32 \
211+
--arch=YOLOv5
212+
```
213+
```shell
214+
python paddle_inference_eval.py \
215+
--model_path=yolov5s_quantaware \
216+
--dataset_dir=/work/GETR-Lite-paddle-new/inference/datasets/coco \
217+
--use_trt=True \
218+
--precision=int8 \
219+
--arch=YOLOv5
198220
```
199221

200222
- MKLDNN预测:
201223

202224
```shell
203225
python paddle_inference_eval.py \
204-
--model_path=output \
205-
--reader_config=configs/yoloe_reader.yml \
226+
--model_path=yolov5_model/inference_model \
227+
--dataset_dir=/work/GETR-Lite-paddle-new/inference/datasets/coco \
206228
--device=CPU \
207229
--use_mkldnn=True \
208230
--cpu_threads=10 \
209-
--precision=int8
231+
--precision=fp32 \
232+
--arch=YOLOv5
210233
```
211234

212235
- 测试单张图片
213236

214237
```shell
215-
python paddle_inference_eval.py --model_path=output --image_file=images/000000570688.jpg --use_trt=True --precision=int8
238+
python paddle_inference_eval.py --model_path=yolov5_model/inference_model --image_file=images/000000570688.jpg --use_trt=True --precision=fp32 --arch=YOLOv5
216239
```
217240

218241
- C++部署
@@ -222,26 +245,9 @@ python paddle_inference_eval.py --model_path=output --image_file=images/00000057
222245
# 编译
223246
bash compile.sh
224247
# 执行
225-
./build/trt_run --model_file yolov7_quant/model.pdmodel --params_file yolov7_quant/model.pdiparams --run_mode=trt_int8
248+
./build/trt_run --model_file yolov7-quantAware/model.pdmodel --params_file yolov7-quantAware/model.pdiparams --run_mode=trt_int8
226249
```
227250

228-
### 导出至ONNX使用TensorRT部署
229-
230-
加载`quant_model.onnx``calibration.cache`,可以直接使用TensorRT测试脚本进行验证,详细代码可参考[TensorRT部署](./TensorRT)
231-
232-
- python测试:
233-
```shell
234-
cd TensorRT
235-
python trt_eval.py --onnx_model_file=output/ONNX/quant_model.onnx \
236-
--calibration_file=output/ONNX/calibration.cache \
237-
--image_file=../images/000000570688.jpg \
238-
--precision_mode=int8
239-
```
240-
241-
- 速度测试
242-
```shell
243-
trtexec --onnx=output/ONNX/quant_model.onnx --avgRuns=1000 --workspace=1024 --calib=output/ONNX/calibration.cache --int8
244-
```
245251

246252
## 5.FAQ
247253

0 commit comments

Comments
 (0)