From 450871cd64731b081196a4796286ca17072eedfa Mon Sep 17 00:00:00 2001 From: Guanghua Yu <742925032@qq.com> Date: Mon, 24 Feb 2020 13:10:56 +0800 Subject: [PATCH] [cherry-pick] Fix v0.2 docs (#258) * fix save checkpoint in quantization (#257) * fix save checkpoint in quantization * fix details * Fix softlink (#256) Co-authored-by: Liufang Sang --- README.md | 4 +-- README_en.md | 11 +++---- docs/advanced_tutorials/slim/DISTILLATION.md | 1 - docs/advanced_tutorials/slim/MODEL_ZOO.md | 2 +- docs/advanced_tutorials/slim/NAS.md | 1 - docs/advanced_tutorials/slim/QUANTIZATION.md | 1 - .../slim/distillation/DISTILLATION.md | 1 + .../slim/distillation/index.rst | 7 +++++ docs/advanced_tutorials/slim/index.rst | 8 ++--- docs/advanced_tutorials/slim/nas/NAS.md | 1 + docs/advanced_tutorials/slim/nas/index.rst | 7 +++++ docs/advanced_tutorials/slim/prune/index.rst | 2 +- .../slim/quantization/QUANTIZATION.md | 1 + .../slim/quantization/index.rst | 7 +++++ slim/README.md | 10 +++---- slim/quantization/train.py | 29 +++++++++++++++---- 16 files changed, 66 insertions(+), 27 deletions(-) delete mode 120000 docs/advanced_tutorials/slim/DISTILLATION.md delete mode 120000 docs/advanced_tutorials/slim/NAS.md delete mode 120000 docs/advanced_tutorials/slim/QUANTIZATION.md create mode 120000 docs/advanced_tutorials/slim/distillation/DISTILLATION.md create mode 100644 docs/advanced_tutorials/slim/distillation/index.rst create mode 120000 docs/advanced_tutorials/slim/nas/NAS.md create mode 100644 docs/advanced_tutorials/slim/nas/index.rst create mode 120000 docs/advanced_tutorials/slim/quantization/QUANTIZATION.md create mode 100644 docs/advanced_tutorials/slim/quantization/index.rst diff --git a/README.md b/README.md index 3daf6d9af..7351e2772 100644 --- a/README.md +++ b/README.md @@ -80,11 +80,11 @@ PaddleDetection的目的是为工业界和学术界提供丰富、易用的目 - [IPython Notebook demo](demo/mask_rcnn_demo.ipynb) - [迁移学习教程](docs/advanced_tutorials/TRANSFER_LEARNING_cn.md) - [模型压缩](slim) + - [压缩benchmark](slim) - [量化](slim/quantization) - [剪枝](slim/prune) - [蒸馏](slim/distillation) - [神经网络搜索](slim/nas) - - [压缩benchmark](slim) - [推理部署](inference) - [模型导出教程](docs/advanced_tutorials/inference/EXPORT_MODEL.md) - [预测引擎Python API使用示例](docs/advanced_tutorials/inference/INFERENCE.md) @@ -105,7 +105,7 @@ PaddleDetection的目的是为工业界和学术界提供丰富、易用的目 本项目的发布受[Apache 2.0 license](LICENSE)许可认证。 ## 版本更新 -v0.2.0版本已经在`01/2020`发布,增加多个模型,升级数据处理模块,拆分YOLOv3的loss,修复已知诸多bug等, +v0.2.0版本已经在`02/2020`发布,增加多个模型,升级数据处理模块,拆分YOLOv3的loss,修复已知诸多bug等, 详细内容请参考[版本更新文档](docs/CHANGELOG.md)。 ## 如何贡献代码 diff --git a/README_en.md b/README_en.md index 5371e3e8a..290ae0107 100644 --- a/README_en.md +++ b/README_en.md @@ -91,10 +91,11 @@ Advanced Features: - [IPython Notebook demo](demo/mask_rcnn_demo.ipynb) - [Transfer learning document](docs/advanced_tutorials/TRANSFER_LEARNING.md) - [Model compression](slim) - - [Quantization-aware training example](slim/quantization) - - [Model pruning example](slim/prune) - - [Model distillation example](slim/distillation) - - [Neural Architecture Search example](slim/nas) + - [Model compression benchmark](slim) + - [Quantization](slim/quantization) + - [Model pruning](slim/prune) + - [Model distillation](slim/distillation) + - [Neural Architecture Search](slim/nas) - [Deployment](inference) - [Export model for inference](docs/advanced_tutorials/inference/EXPORT_MODEL.md) - [Model inference](docs/advanced_tutorials/inference/INFERENCE.md) @@ -115,7 +116,7 @@ Advanced Features: PaddleDetection is released under the [Apache 2.0 license](LICENSE). ## Updates -v0.2.0 was released at `01/2020`, add some models,Upgrade data processing module, Split YOLOv3's loss, fix many known bugs, etc. +v0.2.0 was released at `02/2020`, add some models,Upgrade data processing module, Split YOLOv3's loss, fix many known bugs, etc. Please refer to [版本更新文档](docs/CHANGELOG.md) for details. ## Contributing diff --git a/docs/advanced_tutorials/slim/DISTILLATION.md b/docs/advanced_tutorials/slim/DISTILLATION.md deleted file mode 120000 index f728fe62e..000000000 --- a/docs/advanced_tutorials/slim/DISTILLATION.md +++ /dev/null @@ -1 +0,0 @@ -../../../slim/distillation/README.md \ No newline at end of file diff --git a/docs/advanced_tutorials/slim/MODEL_ZOO.md b/docs/advanced_tutorials/slim/MODEL_ZOO.md index 7917472b6..5e2728515 120000 --- a/docs/advanced_tutorials/slim/MODEL_ZOO.md +++ b/docs/advanced_tutorials/slim/MODEL_ZOO.md @@ -1 +1 @@ -slim/README.md \ No newline at end of file +../../../slim/README.md \ No newline at end of file diff --git a/docs/advanced_tutorials/slim/NAS.md b/docs/advanced_tutorials/slim/NAS.md deleted file mode 120000 index 9515d6312..000000000 --- a/docs/advanced_tutorials/slim/NAS.md +++ /dev/null @@ -1 +0,0 @@ -../../../slim/nas/README.md \ No newline at end of file diff --git a/docs/advanced_tutorials/slim/QUANTIZATION.md b/docs/advanced_tutorials/slim/QUANTIZATION.md deleted file mode 120000 index e4e95a4aa..000000000 --- a/docs/advanced_tutorials/slim/QUANTIZATION.md +++ /dev/null @@ -1 +0,0 @@ -../../../slim/quantization/README.md \ No newline at end of file diff --git a/docs/advanced_tutorials/slim/distillation/DISTILLATION.md b/docs/advanced_tutorials/slim/distillation/DISTILLATION.md new file mode 120000 index 000000000..21f601f55 --- /dev/null +++ b/docs/advanced_tutorials/slim/distillation/DISTILLATION.md @@ -0,0 +1 @@ +../../../../slim/distillation/README.md \ No newline at end of file diff --git a/docs/advanced_tutorials/slim/distillation/index.rst b/docs/advanced_tutorials/slim/distillation/index.rst new file mode 100644 index 000000000..5d1a2326a --- /dev/null +++ b/docs/advanced_tutorials/slim/distillation/index.rst @@ -0,0 +1,7 @@ +模型蒸馏 +=========================================== + +.. toctree:: + :maxdepth: 2 + + DISTILLATION.md diff --git a/docs/advanced_tutorials/slim/index.rst b/docs/advanced_tutorials/slim/index.rst index 592313990..a8af21d6f 100644 --- a/docs/advanced_tutorials/slim/index.rst +++ b/docs/advanced_tutorials/slim/index.rst @@ -4,8 +4,8 @@ .. toctree:: :maxdepth: 2 - DISTILLATION.md - QUANTIZATION.md - NAS.md - prune/index MODEL_ZOO.md + distillation/index + quantization/index + nas/index + prune/index diff --git a/docs/advanced_tutorials/slim/nas/NAS.md b/docs/advanced_tutorials/slim/nas/NAS.md new file mode 120000 index 000000000..0d75ab343 --- /dev/null +++ b/docs/advanced_tutorials/slim/nas/NAS.md @@ -0,0 +1 @@ +../../../../slim/nas/README.md \ No newline at end of file diff --git a/docs/advanced_tutorials/slim/nas/index.rst b/docs/advanced_tutorials/slim/nas/index.rst new file mode 100644 index 000000000..fd181e55f --- /dev/null +++ b/docs/advanced_tutorials/slim/nas/index.rst @@ -0,0 +1,7 @@ +神经网络搜索 +=========================================== + +.. toctree:: + :maxdepth: 2 + + NAS.md diff --git a/docs/advanced_tutorials/slim/prune/index.rst b/docs/advanced_tutorials/slim/prune/index.rst index 68d00e509..88a783f08 100644 --- a/docs/advanced_tutorials/slim/prune/index.rst +++ b/docs/advanced_tutorials/slim/prune/index.rst @@ -1,4 +1,4 @@ -模型剪枝教程 +模型剪枝 =========================================== .. toctree:: diff --git a/docs/advanced_tutorials/slim/quantization/QUANTIZATION.md b/docs/advanced_tutorials/slim/quantization/QUANTIZATION.md new file mode 120000 index 000000000..ef66629b4 --- /dev/null +++ b/docs/advanced_tutorials/slim/quantization/QUANTIZATION.md @@ -0,0 +1 @@ +../../../../slim/quantization/README.md \ No newline at end of file diff --git a/docs/advanced_tutorials/slim/quantization/index.rst b/docs/advanced_tutorials/slim/quantization/index.rst new file mode 100644 index 000000000..38899a450 --- /dev/null +++ b/docs/advanced_tutorials/slim/quantization/index.rst @@ -0,0 +1,7 @@ +模型量化 +=========================================== + +.. toctree:: + :maxdepth: 2 + + QUANTIZATION.md diff --git a/slim/README.md b/slim/README.md index 19fbbe3bf..237b11a6b 100644 --- a/slim/README.md +++ b/slim/README.md @@ -1,4 +1,4 @@ -# 简介 +# 压缩benchmark 在PaddleDetection, 提供了基于PaddleSlim进行模型压缩的完整教程和实验结果。详细教程请参考: @@ -9,8 +9,6 @@ 下面给出压缩的benchmark实验结果。 -# 压缩模型库 - ## 测试环境 - Python 2.7.1 @@ -23,7 +21,7 @@ ### 训练策略 -- 剪裁模型训练时使用[PaddleDetection模型库](../docs/MODEL_ZOO_cn.md)发布的模型权重作为预训练权重。 +- 剪裁模型训练时使用[PaddleDetection模型库](https://paddledetection.readthedocs.io/zh/latest/MODEL_ZOO_cn.html)发布的模型权重作为预训练权重。 - 剪裁训练使用模型默认配置,即除`pretrained_weights`外配置不变。 - 剪裁模型全部为基于敏感度的卷积通道剪裁。 - YOLOv3模型主要剪裁`yolo_head`部分,即剪裁参数如下。 @@ -69,7 +67,7 @@ ### 蒸馏通道剪裁模型 -可通过高精度模型蒸馏通道剪裁后模型的方式,训练方法及相关示例见[蒸馏通道剪裁模型](./extensions/distill_pruned_model/distill_pruned_model_demo.ipynb)。 +可通过高精度模型蒸馏通道剪裁后模型的方式,训练方法及相关示例见[蒸馏通道剪裁模型](https://github.com/PaddlePaddle/PaddleDetection/blob/master/slim/extensions/distill_pruned_model/distill_pruned_model_demo.ipynb)。 COCO数据集上蒸馏通道剪裁模型库如下。 @@ -93,7 +91,7 @@ Pascal VOC数据集上蒸馏通道剪裁模型库如下。 ### 训练策略 -- 蒸馏模型训练时teacher模型使用[PaddleDetection模型库](../docs/MODEL_ZOO_cn.md)发布的模型权重作为预训练权重。 +- 蒸馏模型训练时teacher模型使用[PaddleDetection模型库](https://paddledetection.readthedocs.io/zh/latest/MODEL_ZOO_cn.html)发布的模型权重作为预训练权重。 - 蒸馏模型训练时student模型使用backbone的预训练权重 ### YOLOv3 on COCO diff --git a/slim/quantization/train.py b/slim/quantization/train.py index 2fa8884f5..f8a7d6fae 100644 --- a/slim/quantization/train.py +++ b/slim/quantization/train.py @@ -22,6 +22,7 @@ import time import numpy as np import datetime from collections import deque +import shutil from paddle import fluid @@ -42,6 +43,21 @@ logging.basicConfig(level=logging.INFO, format=FORMAT) logger = logging.getLogger(__name__) +def save_checkpoint(exe, prog, path, train_prog): + if os.path.isdir(path): + shutil.rmtree(path) + logger.info('Save model to {}.'.format(path)) + fluid.io.save_persistables(exe, path, main_program=prog) + + v = train_prog.global_block().var('@LR_DECAY_COUNTER@') + fluid.io.save_vars(exe, dirname=path, vars=[v]) + + +def load_global_step(exe, prog, path): + v = prog.global_block().var('@LR_DECAY_COUNTER@') + fluid.io.load_vars(exe, path, prog, [v]) + + def main(): env = os.environ FLAGS.dist = 'PADDLE_TRAINER_ID' in env and 'PADDLE_TRAINERS_NUM' in env @@ -176,9 +192,9 @@ def main(): cfg.pretrain_weights, ignore_params=ignore_params) # insert quantize op in train_prog, return type is CompiledProgram - train_prog = quant_aware(train_prog, place, config, for_test=False) + train_prog_quant = quant_aware(train_prog, place, config, for_test=False) - compiled_train_prog = train_prog.with_data_parallel( + compiled_train_prog = train_prog_quant.with_data_parallel( loss_name=loss.name, build_strategy=build_strategy, exec_strategy=exec_strategy) @@ -192,6 +208,7 @@ def main(): start_iter = 0 if FLAGS.resume_checkpoint: checkpoint.load_checkpoint(exe, eval_prog, FLAGS.resume_checkpoint) + load_global_step(exe, train_prog, FLAGS.resume_checkpoint) start_iter = checkpoint.global_step() train_reader = create_reader(cfg.TrainReader, @@ -237,7 +254,8 @@ def main(): if (it > 0 and it % cfg.snapshot_iter == 0 or it == cfg.max_iters - 1) \ and (not FLAGS.dist or trainer_id == 0): save_name = str(it) if it != cfg.max_iters - 1 else "model_final" - checkpoint.save(exe, eval_prog, os.path.join(save_dir, save_name)) + save_checkpoint(exe, eval_prog, + os.path.join(save_dir, save_name), train_prog) if FLAGS.eval: # evaluation @@ -254,8 +272,9 @@ def main(): if box_ap_stats[0] > best_box_ap_list[0]: best_box_ap_list[0] = box_ap_stats[0] best_box_ap_list[1] = it - checkpoint.save(exe, eval_prog, - os.path.join(save_dir, "best_model")) + save_checkpoint(exe, eval_prog, + os.path.join(save_dir, "best_model"), + train_prog) logger.info("Best test box ap: {}, in iter: {}".format( best_box_ap_list[0], best_box_ap_list[1])) -- GitLab