Skip to content

Commit

Permalink
Update README.md (PaddlePaddle#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizexu123 committed Feb 23, 2024
1 parent 915001f commit dbfdf48
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions example/post_training_quantization/pytorch_yolo_series/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ python eval.py --config_path=./configs/yolov5s_ptq.yaml
#### 3.6 提高离线量化精度

###### 3.6.1 量化分析工具
本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。PaddleSlim提供量化分析工具,会使用接口```paddleslim.quant.AnalysisPTQ```,可视化展示出不适合量化的层,通过跳过这些层,提高离线量化模型精度。```paddleslim.quant.AnalysisPTQ```详解见[AnalysisPTQ.md](../../../docs/zh_cn/tutorials/quant/AnalysisPTQ.md)
本节介绍如何使用量化分析工具提升离线量化精度。离线量化功能仅需使用少量数据,且使用简单、能快速得到量化模型,但往往会造成较大的精度损失。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)


由于YOLOv6离线量化效果较差,以YOLOv6为例,量化分析工具具体使用方法如下:
Expand Down Expand Up @@ -207,7 +207,70 @@ python fine_tune.py --config_path=./configs/yolov6s_fine_tune.yaml --simulate_ac

## 4.预测部署
预测部署可参考[YOLO系列模型自动压缩示例](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/pytorch_yolo_series)


量化模型在GPU上可以使用TensorRT进行加速,在CPU上可以使用MKLDNN进行加速。
| 参数名 | 含义 |
| model_path | inference模型文件所在路径,该目录下需要有文件model.pdmodel和params.pdiparams两个文件 |
| dataset_dir | 指定COCO数据集的目录,这是存储数据集的根目录 |
| image_file | 如果只测试单张图片效果,直接根据image_file指定图片路径 |
| val_image_dir | COCO数据集中验证图像的目录名,默认为val2017 |
| val_anno_path | 指定COCO数据集的注释(annotation)文件路径,这是包含验证集标注信息的JSON文件,默认为annotations/instances_val2017.json |
| benchmark | 指定是否运行性能基准测试。如果设置为True,程序将会进行性能测试 |
| device | 使用GPU或者CPU预测,可选CPU/GPU/XPU,默认设置为GPU |
| use_trt | 是否使用TensorRT进行预测|
| use_mkldnn | 是否使用MKL-DNN加速库,注意use_mkldnn与use_gpu同时为True时,将忽略enable_mkldnn,而使用GPU预测|
| use_dynamic_shape | 是否使用动态形状(dynamic_shape)功能 |
| precision | fp32/fp16/int8|
| arch | 指定所使用的模型架构的名称,例如YOLOv5 |
| img_shape | 指定模型输入的图像尺寸 |
| batch_size | 指定模型输入的批处理大小 |
| use_mkldnn | 指定是否使用MKLDNN加速(主要针对CPU)|
| cpu_threads | 指定在CPU上使用的线程数 |

首先,我们拥有的yolov6.onnx,我们需要把ONNX模型转成paddle模型,具体参考使用[X2Paddle迁移推理模型](https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/model_convert/convert_with_x2paddle_cn.html#x2paddle)
- 安装X2Paddle
方式一:pip 安装
```shell
pip install X2Paddle==1.3.9
```
方式二:源码安装
```shell
git clone https://github.com/PaddlePaddle/X2Paddle.git
cd X2Paddle
python setup.py install
```
使用命令将YOLOv6.onnx模型转换成paddle模型
```shell
x2paddle --framework=onnx --model=yolov6s.onnx --save_dir=yolov6_model
```
- TensorRT Python部署
使用[paddle_inference_eval.py](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/example/auto_compression/pytorch_yolo_series/paddle_inference_eval.py)部署
```shell
python paddle_inference_eval.py --model_path=yolov6_model/inference_model --dataset_dir=datasets/coco --use_trt=True --precision=fp32 --arch=YOLOv6
```
执行int8量化
```shell
python paddle_inference_eval.py --model_path=yolov6s_ptq_out --dataset_dir==datasets/coco --use_trt=True --precision=int8 --arch=YOLOv6
```
- C++部署
具体可参考[运行PP-YOLOE-l目标检测模型样例](https://github.com/PaddlePaddle/Paddle-Inference-Demo/tree/master/c%2B%2B/gpu/ppyoloe_crn_l)
将compile.sh中DEMO_NAME修改为yolov6_test,并且将ppyoloe_crn_l.cc修改为yolov6_test.cc,根据环境修改相关配置库
运行bash compile.sh编译样例。
- 运行样例
-使用原生GPU运行样例(将ONNX模型转成的paddle模型复制到Paddle-Inference-demo/c++/gpu/ppyoloe_crn_l/目录下)
```shell
./build/yolov6_test --model_file yolov6s_infer/model.pdmodel --params_file yolov6s_infer/model.pdiparams
```
- 使用TensorRT FP32运行样例
```shell
./build/yolov6_test --model_file yolov6s_infer/model.pdmodel --params_file yolov6s_infer/model.pdiparams --run_mode=trt_fp32
```
- 使用TensorRT FP16运行样例
```shell
./build/yolov6_test --model_file yolov6s_infer/model.pdmodel --params_file yolov6s_infer/model.pdiparams --run_mode=trt_fp16
```
- 使用TensorRT INT8运行样例
```shell
./build/yolov6_test --model_file yolov6s_infer/model.pdmodel --params_file yolov6s_infer/model.pdiparams --run_mode=trt_int8
```
## 5.FAQ
- 如果想对模型进行自动压缩,可进入[YOLO系列模型自动压缩示例](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/pytorch_yolo_series)中进行实验。

0 comments on commit dbfdf48

Please # to comment.