Merge pull request #778 from LDOUBLEV/fixslim
opt slim doc and add res18 yml
This commit is contained in:
commit
e086071da2
|
@ -0,0 +1,53 @@
|
|||
Global:
|
||||
algorithm: DB
|
||||
use_gpu: true
|
||||
epoch_num: 1200
|
||||
log_smooth_window: 20
|
||||
print_batch_step: 2
|
||||
save_model_dir: ./output/det_r_18_vd_db/
|
||||
save_epoch_step: 200
|
||||
eval_batch_step: [3000, 2000]
|
||||
train_batch_size_per_card: 8
|
||||
test_batch_size_per_card: 1
|
||||
image_shape: [3, 640, 640]
|
||||
reader_yml: ./configs/det/det_db_icdar15_reader.yml
|
||||
pretrain_weights: ./pretrain_models/ResNet18_vd_pretrained/
|
||||
save_res_path: ./output/det_r18_vd_db/predicts_db.txt
|
||||
checkpoints:
|
||||
save_inference_dir:
|
||||
|
||||
Architecture:
|
||||
function: ppocr.modeling.architectures.det_model,DetModel
|
||||
|
||||
Backbone:
|
||||
function: ppocr.modeling.backbones.det_resnet_vd,ResNet
|
||||
layers: 18
|
||||
|
||||
Head:
|
||||
function: ppocr.modeling.heads.det_db_head,DBHead
|
||||
model_name: large
|
||||
k: 50
|
||||
inner_channels: 256
|
||||
out_channels: 2
|
||||
|
||||
Loss:
|
||||
function: ppocr.modeling.losses.det_db_loss,DBLoss
|
||||
balance_loss: true
|
||||
main_loss_type: DiceLoss
|
||||
alpha: 5
|
||||
beta: 10
|
||||
ohem_ratio: 3
|
||||
|
||||
Optimizer:
|
||||
function: ppocr.optimizer,AdamDecay
|
||||
base_lr: 0.001
|
||||
beta1: 0.9
|
||||
beta2: 0.999
|
||||
|
||||
PostProcess:
|
||||
function: ppocr.postprocess.db_postprocess,DBPostProcess
|
||||
thresh: 0.3
|
||||
box_thresh: 0.6
|
||||
max_candidates: 1000
|
||||
unclip_ratio: 1.5
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
|
||||
## 介绍
|
||||
|
||||
复杂的模型有利于提高模型的性能,但也导致模型中存在一定冗余,模型裁剪通过移出网络模型中的子模型来减少这种冗余,达到减少模型计算复杂度,提高模型推理性能的目的。
|
||||
|
||||
本教程将介绍如何使用PaddleSlim量化PaddleOCR的模型。
|
||||
|
||||
在开始本教程之前,建议先了解
|
||||
1. [PaddleOCR模型的训练方法](../../../doc/doc_ch/quickstart.md)
|
||||
2. [分类模型裁剪教程](https://paddlepaddle.github.io/PaddleSlim/tutorials/pruning_tutorial/)
|
||||
3. [PaddleSlim 裁剪压缩API](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/)
|
||||
|
||||
|
||||
## 快速开始
|
||||
|
||||
模型裁剪主要包括五个步骤:
|
||||
1. 安装 PaddleSlim
|
||||
2. 准备训练好的模型
|
||||
3. 敏感度分析、训练
|
||||
4. 模型裁剪训练
|
||||
5. 导出模型、预测部署
|
||||
|
||||
### 1. 安装PaddleSlim
|
||||
|
||||
```bash
|
||||
git clone https://github.com/PaddlePaddle/PaddleSlim.git
|
||||
cd Paddleslim
|
||||
python setup.py install
|
||||
```
|
||||
|
||||
### 2. 获取预训练模型
|
||||
模型裁剪需要加载事先训练好的模型,PaddleOCR也提供了一系列模型[../../../doc/doc_ch/models_list.md],开发者可根据需要自行选择模型或使用自己的模型。
|
||||
|
||||
### 3. 敏感度分析训练
|
||||
|
||||
加载预训练模型后,通过对现有模型的每个网络层进行敏感度分析,了解各网络层冗余度,从而决定每个网络层的裁剪比例。
|
||||
敏感度分析的具体细节见:[敏感度分析](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/tutorials/image_classification_sensitivity_analysis_tutorial.md)
|
||||
|
||||
进入PaddleOCR根目录,通过以下命令对模型进行敏感度分析训练:
|
||||
```bash
|
||||
python deploy/slim/prune/sensitivity_anal.py -c configs/det/det_mv3_db.yml -o Global.pretrain_weights="your trained model" Global.test_batch_size_per_card=1
|
||||
```
|
||||
|
||||
### 4. 模型裁剪训练
|
||||
裁剪时通过之前的敏感度分析文件决定每个网络层的裁剪比例。在具体实现时,为了尽可能多的保留从图像中提取的低阶特征,我们跳过了backbone中靠近输入的4个卷积层。同样,为了减少由于裁剪导致的模型性能损失,我们通过之前敏感度分析所获得的敏感度表,挑选出了一些冗余较少,对裁剪较为敏感的[网络层](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/deploy/slim/prune/pruning_and_finetune.py#L41),并在之后的裁剪过程中选择避开这些网络层。裁剪过后finetune的过程沿用OCR检测模型原始的训练策略。
|
||||
|
||||
```bash
|
||||
python deploy/slim/prune/pruning_and_finetune.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
|
||||
```
|
||||
通过对比可以发现,经过裁剪训练保存的模型更小。
|
||||
|
||||
### 5. 导出模型、预测部署
|
||||
|
||||
在得到裁剪训练保存的模型后,我们可以将其导出为inference_model:
|
||||
```bash
|
||||
python deploy/slim/prune/export_prune_model.py -c configs/det/det_mv3_db.yml -o Global.pretrain_weights=./output/det_db/best_accuracy Global.test_batch_size_per_card=1 Global.save_inference_dir=inference_model
|
||||
```
|
||||
|
||||
inference model的预测和部署参考:
|
||||
1. [inference model python端预测](../../../doc/doc_ch/inference.md)
|
||||
2. [inference model C++预测](../../cpp_infer/readme.md)
|
||||
3. [inference model在移动端部署](../../lite/readme.md)
|
|
@ -1,180 +0,0 @@
|
|||
\> 运行示例前请先安装develop版本PaddleSlim
|
||||
|
||||
|
||||
|
||||
# 模型裁剪压缩教程
|
||||
|
||||
压缩结果:
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>序号</th>
|
||||
<th>任务</th>
|
||||
<th>模型</th>
|
||||
<th>压缩策略<sup><a href="#quant">[3]</a><a href="#prune">[4]</a><sup></th>
|
||||
<th>精度(自建中文数据集)</th>
|
||||
<th>耗时<sup><a href="#latency">[1]</a></sup>(ms)</th>
|
||||
<th>整体耗时<sup><a href="#rec">[2]</a></sup>(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/prune_api/)对OCR模型进行压缩。
|
||||
|
||||
在阅读该示例前,建议您先了解以下内容:
|
||||
|
||||
|
||||
|
||||
\- [OCR模型的常规训练方法](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/detection.md)
|
||||
|
||||
\- [PaddleSlim使用文档](https://paddlepaddle.github.io/PaddleSlim/)
|
||||
|
||||
|
||||
|
||||
## 安装PaddleSlim
|
||||
|
||||
```bash
|
||||
|
||||
git clone https://github.com/PaddlePaddle/PaddleSlim.git
|
||||
|
||||
cd Paddleslim
|
||||
|
||||
python setup.py install
|
||||
|
||||
```
|
||||
|
||||
|
||||
## 获取预训练模型
|
||||
[检测预训练模型下载地址]()
|
||||
|
||||
|
||||
## 敏感度分析训练
|
||||
加载预训练模型后,通过对现有模型的每个网络层进行敏感度分析,了解各网络层冗余度,从而决定每个网络层的裁剪比例。敏感度分析的具体细节见:[敏感度分析](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/tutorials/image_classification_sensitivity_analysis_tutorial.md)
|
||||
|
||||
进入PaddleOCR根目录,通过以下命令对模型进行敏感度分析:
|
||||
|
||||
```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
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 裁剪模型与fine-tune
|
||||
裁剪时通过之前的敏感度分析文件决定每个网络层的裁剪比例。在具体实现时,为了尽可能多的保留从图像中提取的低阶特征,我们跳过了backbone中靠近输入的4个卷积层。同样,为了减少由于裁剪导致的模型性能损失,我们通过之前敏感度分析所获得的敏感度表,挑选出了一些冗余较少,对裁剪较为敏感的[网络层](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/deploy/slim/prune/pruning_and_finetune.py#L41),并在之后的裁剪过程中选择避开这些网络层。裁剪过后finetune的过程沿用OCR检测模型原始的训练策略。
|
||||
|
||||
```bash
|
||||
|
||||
python deploy/slim/prune/pruning_and_finetune.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
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 导出模型
|
||||
|
||||
在得到裁剪训练保存的模型后,我们可以将其导出为inference_model,用于预测部署:
|
||||
|
||||
```bash
|
||||
|
||||
python deploy/slim/prune/export_prune_model.py -c configs/det/det_mv3_db.yml -o Global.pretrain_weights=./output/det_db/best_accuracy Global.test_batch_size_per_card=1 Global.save_inference_dir=inference_model
|
||||
|
||||
```
|
|
@ -1,162 +1,60 @@
|
|||
> 运行示例前请先安装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>
|
||||
|
||||
|
||||
|
||||
## 概述
|
||||
|
||||
## 介绍
|
||||
复杂的模型有利于提高模型的性能,但也导致模型中存在一定冗余,模型量化将全精度缩减到定点数减少这种冗余,达到减少模型计算复杂度,提高模型推理性能的目的。
|
||||
模型量化可以在基本不损失模型的精度的情况下,将FP32精度的模型参数转换为Int8精度,减小模型参数大小并加速计算,使用量化后的模型在移动端等部署时更具备速度优势。
|
||||
|
||||
该示例使用PaddleSlim提供的[量化压缩API](https://paddlepaddle.github.io/PaddleSlim/api/quantization_api/)对OCR模型进行压缩。
|
||||
在阅读该示例前,建议您先了解以下内容:
|
||||
本教程将介绍如何使用PaddleSlim量化PaddleOCR的模型。
|
||||
|
||||
- [OCR模型的常规训练方法](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/detection.md)
|
||||
- [PaddleSlim使用文档](https://paddleslim.readthedocs.io/zh_CN/latest/index.html)
|
||||
在开始本教程之前,建议先了解[PaddleOCR模型的训练方法](../../../doc/doc_ch/quickstart.md)以及[PaddleSlim](https://paddleslim.readthedocs.io/zh_CN/latest/index.html)
|
||||
|
||||
|
||||
## 快速开始
|
||||
量化多适用于轻量模型在移动端的部署,当训练出一个模型后,如果希望进一步的压缩模型大小并加速预测,可使用量化的方法压缩模型。
|
||||
|
||||
## 安装PaddleSlim
|
||||
模型量化主要包括五个步骤:
|
||||
1. 安装 PaddleSlim
|
||||
2. 准备训练好的模型
|
||||
3. 量化训练
|
||||
4. 导出量化推理模型
|
||||
5. 量化模型预测部署
|
||||
|
||||
### 1. 安装PaddleSlim
|
||||
|
||||
```bash
|
||||
git clone https://github.com/PaddlePaddle/PaddleSlim.git
|
||||
|
||||
cd Paddleslim
|
||||
|
||||
python setup.py install
|
||||
```
|
||||
|
||||
### 2. 准备训练好的模型
|
||||
|
||||
PaddleOCR提供了一系列训练好的[模型](../../../doc/doc_ch/models_list.md),如果待量化的模型不在列表中,需要按照[常规训练](../../../doc/doc_ch/quickstart.md)方法得到训练好的模型。
|
||||
|
||||
### 3. 量化训练
|
||||
量化训练包括离线量化训练和在线量化训练,在线量化训练效果更好,需加载预训练模型,在定义好量化策略后即可对模型进行量化。
|
||||
|
||||
|
||||
## 获取预训练模型
|
||||
|
||||
[识别预训练模型下载地址]()
|
||||
|
||||
[检测预训练模型下载地址]()
|
||||
|
||||
|
||||
## 量化训练
|
||||
加载预训练模型后,在定义好量化策略后即可对模型进行量化。量化相关功能的使用具体细节见:[模型量化](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/quantization_api.html)
|
||||
|
||||
进入PaddleOCR根目录,通过以下命令对模型进行量化:
|
||||
|
||||
量化训练的代码位于slim/quantization/quant/py 中,比如训练检测模型,训练指令如下:
|
||||
```bash
|
||||
python deploy/slim/quantization/quant.py -c configs/det/det_mv3_db.yml -o Global.pretrain_weights=det_mv3_db/best_accuracy Global.save_model_dir=./output/quant_model
|
||||
python deploy/slim/quantization/quant.py -c configs/det/det_mv3_db.yml -o Global.pretrain_weights='your trained model' Global.save_model_dir=./output/quant_model
|
||||
|
||||
# 比如下载提供的训练模型
|
||||
wget https://paddleocr.bj.bcebos.com/20-09-22/mobile/det/ch_ppocr_mobile_v1.1_det_train.tar
|
||||
tar xf ch_ppocr_mobile_v1.1_det_train.tar
|
||||
python deploy/slim/quantization/quant.py -c configs/det/det_mv3_db.yml -o Global.pretrain_weights=./ch_ppocr_mobile_v1.1_det_train/best_accuracy Global.save_model_dir=./output/quant_model
|
||||
|
||||
```
|
||||
如果要训练识别模型的量化,修改配置文件和加载的模型参数即可。
|
||||
|
||||
|
||||
|
||||
|
||||
## 导出模型
|
||||
### 4. 导出模型
|
||||
|
||||
在得到量化训练保存的模型后,我们可以将其导出为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_inference_model
|
||||
```
|
||||
|
||||
### 5. 量化模型部署
|
||||
|
||||
上述步骤导出的量化模型,参数精度仍然是FP32,但是参数的数值范围是int8,导出的模型可以通过PaddleLite的opt模型转换工具完成模型转换。
|
||||
量化模型部署的可参考 [移动端模型部署](../lite/readme.md)
|
||||
|
|
|
@ -117,7 +117,7 @@ class TextDetector(object):
|
|||
box = self.clip_det_res(box, img_height, img_width)
|
||||
rect_width = int(np.linalg.norm(box[0] - box[1]))
|
||||
rect_height = int(np.linalg.norm(box[0] - box[3]))
|
||||
if rect_width <= 10 or rect_height <= 10:
|
||||
if rect_width <= 3 or rect_height <= 3:
|
||||
continue
|
||||
dt_boxes_new.append(box)
|
||||
dt_boxes = np.array(dt_boxes_new)
|
||||
|
|
Loading…
Reference in New Issue