diff --git a/deploy/slim/quantization/README.md b/deploy/slim/quantization/README.md index f7d87c83602f69ada46b35e7d63260fe8bc6e055..d1aa3d71e5254cf6b5b2be7fdf6943903d42fafd 100755 --- a/deploy/slim/quantization/README.md +++ b/deploy/slim/quantization/README.md @@ -1,21 +1,148 @@ > è¿è¡Œç¤ºä¾‹å‰è¯·å…ˆå®‰è£…1.2.0或更高版本PaddleSlim + # 模型é‡åŒ–压缩教程 +压缩结果: +<table> +<thead> + <tr> + <th>åºå·</th> + <th>任务</th> + <th>模型</th> + <th>压缩ç–ç•¥</th> + <th>精度(自建ä¸æ–‡æ•°æ®é›†)</th> + <th>耗时(ms)</th> + <th>整体耗时(ms)</th> + <th>åŠ é€Ÿæ¯”</th> + <th>整体模型大å°(M)</th> + <th>压缩比例</th> + <th>下载链接</th> + </tr> +</thead> +<tbody> + <tr> + <td rowspan="2">0</td> + <td>检测</td> + <td>MobileNetV3_DB</td> + <td>æ— </td> + <td>61.7</td> + <td>224</td> + <td rowspan="2">375</td> + <td rowspan="2">-</td> + <td rowspan="2">8.6</td> + <td rowspan="2">-</td> + <td></td> + </tr> + <tr> + <td>识别</td> + <td>MobileNetV3_CRNN</td> + <td>æ— </td> + <td>62.0</td> + <td>9.52</td> + <td></td> + </tr> + <tr> + <td rowspan="2">1</td> + <td>检测</td> + <td>SlimTextDet</td> + <td>PACTé‡åŒ–è®ç»ƒ</td> + <td>62.1</td> + <td>195</td> + <td rowspan="2">348</td> + <td rowspan="2">8%</td> + <td rowspan="2">2.8</td> + <td rowspan="2">67.82%</td> + <td></td> + </tr> + <tr> + <td>识别</td> + <td>SlimTextRec</td> + <td>PACTé‡åŒ–è®ç»ƒ</td> + <td>61.48</td> + <td>8.6</td> + <td></td> + </tr> + <tr> + <td rowspan="2">2</td> + <td>检测</td> + <td>SlimTextDet_quat_pruning</td> + <td>剪è£+PACTé‡åŒ–è®ç»ƒ</td> + <td>60.86</td> + <td>142</td> + <td rowspan="2">288</td> + <td rowspan="2">30%</td> + <td rowspan="2">2.8</td> + <td rowspan="2">67.82%</td> + <td></td> + </tr> + <tr> + <td>识别</td> + <td>SlimTextRec</td> + <td>PACTé‡åŒ–è®ç»ƒ</td> + <td>61.48</td> + <td>8.6</td> + <td></td> + </tr> + <tr> + <td rowspan="2">3</td> + <td>检测</td> + <td>SlimTextDet_pruning</td> + <td>剪è£</td> + <td>61.57</td> + <td>138</td> + <td rowspan="2">295</td> + <td rowspan="2">27%</td> + <td rowspan="2">2.9</td> + <td rowspan="2">66.28%</td> + <td></td> + </tr> + <tr> + <td>识别</td> + <td>SlimTextRec</td> + <td>PACTé‡åŒ–è®ç»ƒ</td> + <td>61.48</td> + <td>8.6</td> + <td></td> + </tr> +</tbody> +</table> + + + ## 概述 +å¤æ‚的模型有利于æ高模型的性能,但也导致模型ä¸å˜åœ¨ä¸€å®šå†—余,模型é‡åŒ–将全精度缩å‡åˆ°å®šç‚¹æ•°å‡å°‘è¿™ç§å†—余,达到å‡å°‘模型计算å¤æ‚度,æ高模型推ç†æ€§èƒ½çš„目的。 + 该示例使用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 0000000000000000000000000000000000000000..4b8a2b23a254b143cd230c81a7e433d251e10ff2 --- /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: +<table> +<thead> + <tr> + <th>ID</th> + <th>Task</th> + <th>Model</th> + <th>Compress Strategy</th> + <th>Criterion(Chinese dataset)</th> + <th>Inference Time(ms)</th> + <th>Inference Time(Total model)(ms)</th> + <th>Acceleration Ratio</th> + <th>Model Size(MB)</th> + <th>Commpress Ratio</th> + <th>Download Link</th> + </tr> +</thead> +<tbody> + <tr> + <td rowspan="2">0</td> + <td>Detection</td> + <td>MobileNetV3_DB</td> + <td>None</td> + <td>61.7</td> + <td>224</td> + <td rowspan="2">375</td> + <td rowspan="2">-</td> + <td rowspan="2">8.6</td> + <td rowspan="2">-</td> + <td></td> + </tr> + <tr> + <td>Recognition</td> + <td>MobileNetV3_CRNN</td> + <td>None</td> + <td>62.0</td> + <td>9.52</td> + <td></td> + </tr> + <tr> + <td rowspan="2">1</td> + <td>Detection</td> + <td>SlimTextDet</td> + <td>PACT Quant Aware Training</td> + <td>62.1</td> + <td>195</td> + <td rowspan="2">348</td> + <td rowspan="2">8%</td> + <td rowspan="2">2.8</td> + <td rowspan="2">67.82%</td> + <td></td> + </tr> + <tr> + <td>Recognition</td> + <td>SlimTextRec</td> + <td>PACT Quant Aware Training</td> + <td>61.48</td> + <td>8.6</td> + <td></td> + </tr> + <tr> + <td rowspan="2">2</td> + <td>Detection</td> + <td>SlimTextDet_quat_pruning</td> + <td>Pruning+PACT Quant Aware Training</td> + <td>60.86</td> + <td>142</td> + <td rowspan="2">288</td> + <td rowspan="2">30%</td> + <td rowspan="2">2.8</td> + <td rowspan="2">67.82%</td> + <td></td> + </tr> + <tr> + <td>Recognition</td> + <td>SlimTextRec</td> + <td>PPACT Quant Aware Training</td> + <td>61.48</td> + <td>8.6</td> + <td></td> + </tr> + <tr> + <td rowspan="2">3</td> + <td>Detection</td> + <td>SlimTextDet_pruning</td> + <td>Pruning</td> + <td>61.57</td> + <td>138</td> + <td rowspan="2">295</td> + <td rowspan="2">27%</td> + <td rowspan="2">2.9</td> + <td rowspan="2">66.28%</td> + <td></td> + </tr> + <tr> + <td>Recognition</td> + <td>SlimTextRec</td> + <td>PACT Quant Aware Training</td> + <td>61.48</td> + <td>8.6</td> + <td></td> + </tr> +</tbody> +</table> + + + +## 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 +```