From 2b68ae333a2e1ecf0b09f56ebadbae797db50f2d Mon Sep 17 00:00:00 2001 From: baiyfbupt Date: Sat, 19 Sep 2020 16:35:54 +0800 Subject: [PATCH] refine slim readme --- deploy/slim/quantization/README.md | 134 ++++++++++++++++++++- deploy/slim/quantization/README_en.md | 167 ++++++++++++++++++++++++++ 2 files changed, 298 insertions(+), 3 deletions(-) create mode 100755 deploy/slim/quantization/README_en.md diff --git a/deploy/slim/quantization/README.md b/deploy/slim/quantization/README.md index f7d87c83..d1aa3d71 100755 --- a/deploy/slim/quantization/README.md +++ b/deploy/slim/quantization/README.md @@ -1,21 +1,148 @@ > 运行示例前请先安装1.2.0或更高版本PaddleSlim + # 模型量化压缩教程 +压缩结果: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
序号任务模型压缩策略精度(自建中文数据集)耗时(ms)整体耗时(ms)加速比整体模型大小(M)压缩比例下载链接
0检测MobileNetV3_DB61.7224375-8.6-
识别MobileNetV3_CRNN62.09.52
1检测SlimTextDetPACT量化训练62.11953488%2.867.82%
识别SlimTextRecPACT量化训练61.488.6
2检测SlimTextDet_quat_pruning剪裁+PACT量化训练60.8614228830%2.867.82%
识别SlimTextRecPACT量化训练61.488.6
3检测SlimTextDet_pruning剪裁61.5713829527%2.966.28%
识别SlimTextRecPACT量化训练61.488.6
+ + + ## 概述 +复杂的模型有利于提高模型的性能,但也导致模型中存在一定冗余,模型量化将全精度缩减到定点数减少这种冗余,达到减少模型计算复杂度,提高模型推理性能的目的。 + 该示例使用PaddleSlim提供的[量化压缩API](https://paddlepaddle.github.io/PaddleSlim/api/quantization_api/)对OCR模型进行压缩。 在阅读该示例前,建议您先了解以下内容: - [OCR模型的常规训练方法](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/detection.md) -- [PaddleSlim使用文档](https://paddlepaddle.github.io/PaddleSlim/) +- [PaddleSlim使用文档](https://paddleslim.readthedocs.io/zh_CN/latest/index.html) + + ## 安装PaddleSlim -可按照[PaddleSlim使用文档](https://paddlepaddle.github.io/PaddleSlim/)中的步骤安装PaddleSlim。 +```bash +git clone https://github.com/PaddlePaddle/PaddleSlim.git + +cd Paddleslim + +python setup.py install +``` + + + +## 获取预训练模型 + +[识别预训练模型下载地址]() + +[检测预训练模型下载地址]() ## 量化训练 +加载预训练模型后,在定义好量化策略后即可对模型进行量化。量化相关功能的使用具体细节见:[模型量化](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/quantization_api.html) 进入PaddleOCR根目录,通过以下命令对模型进行量化: @@ -25,10 +152,11 @@ python deploy/slim/quantization/quant.py -c configs/det/det_mv3_db.yml -o Global + ## 导出模型 在得到量化训练保存的模型后,我们可以将其导出为inference_model,用于预测部署: ```bash -python deploy/slim/quantization/export_model.py -c configs/det/det_mv3_db.yml -o Global.checkpoints=output/quant_model/best_accuracy Global.save_model_dir=./output/quant_model +python deploy/slim/quantization/export_model.py -c configs/det/det_mv3_db.yml -o Global.checkpoints=output/quant_model/best_accuracy Global.save_model_dir=./output/quant_inference_model ``` diff --git a/deploy/slim/quantization/README_en.md b/deploy/slim/quantization/README_en.md new file mode 100755 index 00000000..4b8a2b23 --- /dev/null +++ b/deploy/slim/quantization/README_en.md @@ -0,0 +1,167 @@ +\> PaddleSlim 1.2.0 or higher version should be installed before runing this example. + + + +# Model compress tutorial (Quantization) + +Compress results: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDTaskModelCompress StrategyCriterion(Chinese dataset)Inference Time(ms)Inference Time(Total model)(ms)Acceleration RatioModel Size(MB)Commpress RatioDownload Link
0DetectionMobileNetV3_DBNone61.7224375-8.6-
RecognitionMobileNetV3_CRNNNone62.09.52
1DetectionSlimTextDetPACT Quant Aware Training62.11953488%2.867.82%
RecognitionSlimTextRecPACT Quant Aware Training61.488.6
2DetectionSlimTextDet_quat_pruningPruning+PACT Quant Aware Training60.8614228830%2.867.82%
RecognitionSlimTextRecPPACT Quant Aware Training61.488.6
3DetectionSlimTextDet_pruningPruning61.5713829527%2.966.28%
RecognitionSlimTextRecPACT Quant Aware Training61.488.6
+ + + +## Overview + +Generally, a more complex model would achive better performance in the task, but it also leads to some redundancy in the model. Quantization is a technique that reduces this redundancyby reducing the full precision data to a fixed number, so as to reduce model calculation complexity and improve model inference performance. + +This example uses PaddleSlim provided [APIs of Quantization](https://paddlepaddle.github.io/PaddleSlim/api/quantization_api/) to compress the OCR model. + +It is recommended that you could understand following pages before reading this example,: + + + +- [The training strategy of OCR model](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/detection.md) + +- [PaddleSlim Document](https://paddlepaddle.github.io/PaddleSlim/api/quantization_api/) + + + +## Install PaddleSlim + +```bash +git clone https://github.com/PaddlePaddle/PaddleSlim.git + +cd Paddleslim + +python setup.py install + +``` + + +## Download Pretrain Model + +[Download link of Detection pretrain model]() + +[Download link of recognization pretrain model]() + + +## Quan-Aware Training + +After loading the pre training model, the model can be quantified after defining the quantization strategy. For specific details of quantization method, see:[Model Quantization](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/quantization_api.html) + +Enter the PaddleOCR root directory,perform model quantization with the following command: + +```bash +python deploy/slim/prune/sensitivity_anal.py -c configs/det/det_mv3_db.yml -o Global.pretrain_weights=./deploy/slim/prune/pretrain_models/det_mv3_db/best_accuracy Global.test_batch_size_per_card=1 +``` + + + +## Export inference model + +After getting the model after pruning and finetuning we, can export it as inference_model for predictive deployment: + +```bash +python deploy/slim/quantization/export_model.py -c configs/det/det_mv3_db.yml -o Global.checkpoints=output/quant_model/best_accuracy Global.save_model_dir=./output/quant_inference_model +``` -- GitLab