From b0feca82d549b8f1a4d32d0cb2021f0df654fff5 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 08:28:58 +0000 Subject: [PATCH 1/9] fix nonfinite --- tests/configs/det_mv3_db.yml | 6 +++--- tests/ocr_det_params.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/configs/det_mv3_db.yml b/tests/configs/det_mv3_db.yml index d6d4c26cbc..5eada6d53d 100644 --- a/tests/configs/det_mv3_db.yml +++ b/tests/configs/det_mv3_db.yml @@ -23,10 +23,10 @@ Architecture: name: MobileNetV3 scale: 0.5 model_name: large - disable_se: True + disable_se: False Neck: name: DBFPN - out_channels: 96 + out_channels: 256 Head: name: DBHead k: 50 @@ -74,7 +74,7 @@ Train: channel_first: False - DetLabelEncode: # Class handling label - Resize: - # size: [640, 640] + size: [640, 640] - MakeBorderMap: shrink_ratio: 0.4 thresh_min: 0.3 diff --git a/tests/ocr_det_params.txt b/tests/ocr_det_params.txt index 7be1430a1a..bbe6a604d8 100644 --- a/tests/ocr_det_params.txt +++ b/tests/ocr_det_params.txt @@ -35,7 +35,7 @@ export1:null export2:null ## train_model:./inference/ch_ppocr_mobile_v2.0_det_train/best_accuracy -infer_export:tools/export_model.py -c configs/det/det_mv3_db.yml -o +infer_export:tools/export_model.py -c tests/configs/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o infer_quant:False inference:tools/infer/predict_det.py --use_gpu:True|False From 76c0cf320105295bef7d86a393b522c66f8a10e8 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 10:54:52 +0000 Subject: [PATCH 2/9] add kl --- deploy/slim/quantization/quant_kl.py | 145 +++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100755 deploy/slim/quantization/quant_kl.py diff --git a/deploy/slim/quantization/quant_kl.py b/deploy/slim/quantization/quant_kl.py new file mode 100755 index 0000000000..83bd671456 --- /dev/null +++ b/deploy/slim/quantization/quant_kl.py @@ -0,0 +1,145 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import sys + +__dir__ = os.path.dirname(os.path.abspath(__file__)) +sys.path.append(__dir__) +sys.path.append(os.path.abspath(os.path.join(__dir__, '..', '..', '..'))) +sys.path.append( + os.path.abspath(os.path.join(__dir__, '..', '..', '..', 'tools'))) + +import yaml +import paddle +import paddle.distributed as dist + +paddle.seed(2) + +from ppocr.data import build_dataloader +from ppocr.modeling.architectures import build_model +from ppocr.losses import build_loss +from ppocr.optimizer import build_optimizer +from ppocr.postprocess import build_post_process +from ppocr.metrics import build_metric +from ppocr.utils.save_load import init_model +import tools.program as program +import paddleslim +from paddleslim.dygraph.quant import QAT +import numpy as np + +dist.get_world_size() + + +class PACT(paddle.nn.Layer): + def __init__(self): + super(PACT, self).__init__() + alpha_attr = paddle.ParamAttr( + name=self.full_name() + ".pact", + initializer=paddle.nn.initializer.Constant(value=20), + learning_rate=1.0, + regularizer=paddle.regularizer.L2Decay(2e-5)) + + self.alpha = self.create_parameter( + shape=[1], attr=alpha_attr, dtype='float32') + + def forward(self, x): + out_left = paddle.nn.functional.relu(x - self.alpha) + out_right = paddle.nn.functional.relu(-self.alpha - x) + x = x - out_left + out_right + return x + + +quant_config = { + # weight preprocess type, default is None and no preprocessing is performed. + 'weight_preprocess_type': None, + # activation preprocess type, default is None and no preprocessing is performed. + 'activation_preprocess_type': None, + # weight quantize type, default is 'channel_wise_abs_max' + 'weight_quantize_type': 'channel_wise_abs_max', + # activation quantize type, default is 'moving_average_abs_max' + 'activation_quantize_type': 'moving_average_abs_max', + # weight quantize bit num, default is 8 + 'weight_bits': 8, + # activation quantize bit num, default is 8 + 'activation_bits': 8, + # data type after quantization, such as 'uint8', 'int8', etc. default is 'int8' + 'dtype': 'int8', + # window size for 'range_abs_max' quantization. default is 10000 + 'window_size': 10000, + # The decay coefficient of moving average, default is 0.9 + 'moving_rate': 0.9, + # for dygraph quantization, layers of type in quantizable_layer_type will be quantized + 'quantizable_layer_type': ['Conv2D', 'Linear'], +} + + +def sample_generator(loader): + def __reader__(): + for indx, data in enumerate(loader): + images = np.array(data[0]) + yield images + + return __reader__ + + +def main(config, device, logger, vdl_writer): + # init dist environment + if config['Global']['distributed']: + dist.init_parallel_env() + + global_config = config['Global'] + + # build dataloader + config['Train']['loader']['num_workers'] = 0 + train_dataloader = build_dataloader(config, 'Train', device, logger) + if config['Eval']: + config['Eval']['loader']['num_workers'] = 0 + valid_dataloader = build_dataloader(config, 'Eval', device, logger) + else: + valid_dataloader = None + + paddle.enable_static() + place = paddle.CPUPlace() + exe = paddle.static.Executor(place) + + if 'inference_model' in global_config.keys(): # , 'inference_model'): + inference_model_dir = global_config['inference_model'] + else: + inference_model_dir = os.path.dirname(global_config['pretrained_model']) + if not (os.path.exists(os.path.join(inference_model_dir, "inference.pdmodel")) and \ + os.path.exists(os.path.join(inference_model_dir, "inference.pdiparams")) ): + raise ValueError( + "Please set inference model dir in Global.inference_model or Global.pretrained_model for post-quantazition" + ) + + paddleslim.quant.quant_post_static( + executor=exe, + model_dir=inference_model_dir, + model_filename='inference.pdmodel', + params_filename='inference.pdiparams', + quantize_model_path=global_config['save_inference_dir'], + sample_generator=sample_generator(train_dataloader), + save_model_filename='inference.pdmodel', + save_params_filename='inference.pdiparams', + batch_nums=10) + + +if __name__ == '__main__': + config, device, logger, vdl_writer = program.preprocess(is_train=True) + main(config, device, logger, vdl_writer) From 42126b4da1a000e8e6f12059d0d0999a60b02928 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 11:04:52 +0000 Subject: [PATCH 3/9] add kl quant --- tests/ocr_rec_params.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ocr_rec_params.txt b/tests/ocr_rec_params.txt index 2d27a9ee3b..504f45b8b9 100644 --- a/tests/ocr_rec_params.txt +++ b/tests/ocr_rec_params.txt @@ -17,11 +17,11 @@ norm_train:tools/train.py -c configs/rec/rec_icdar15_train.yml -o pact_train:deploy/slim/quantization/quant.py -c configs/rec/rec_icdar15_train.yml -o fpgm_train:null distill_train:null -null:null +kl_quant:deploy/slim/quantization/quant_kl.py -c configs/rec/rec_icdar15_train.yml -o Global.pretrained_model=./inference/ch_ppocr_mobile_v2.0_rec_infer/ null:null ## ===========================eval_params=========================== -eval:tools/eval.py -c configs/rec/rec_icdar15_train.yml -o +eval:null null:null ## ===========================infer_params=========================== From d57049bc7514b92b9d60c84d84176a47b3d406d3 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 11:08:32 +0000 Subject: [PATCH 4/9] update yml --- tests/ocr_det_params.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ocr_det_params.txt b/tests/ocr_det_params.txt index bbe6a604d8..7f6481177a 100644 --- a/tests/ocr_det_params.txt +++ b/tests/ocr_det_params.txt @@ -27,7 +27,7 @@ null:null ===========================infer_params=========================== Global.save_inference_dir:./output/ Global.pretrained_model: -norm_export:tools/export_model.py -c tests/configs/det_mv3_db.yml -o +norm_export:tools/export_model.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o quant_export:deploy/slim/quantization/export_model.py -c tests/configs/det_mv3_db.yml -o fpgm_export:deploy/slim/prune/export_prune_model.py -c tests/configs/det_mv3_db.yml -o distill_export:null From 2d124b74ed0de6c3ed7022ffa93ab721ed83443f Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 12:21:59 +0000 Subject: [PATCH 5/9] set batch_size=1 in kl quant --- deploy/slim/quantization/quant_kl.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deploy/slim/quantization/quant_kl.py b/deploy/slim/quantization/quant_kl.py index 83bd671456..d866784ae6 100755 --- a/deploy/slim/quantization/quant_kl.py +++ b/deploy/slim/quantization/quant_kl.py @@ -137,7 +137,8 @@ def main(config, device, logger, vdl_writer): sample_generator=sample_generator(train_dataloader), save_model_filename='inference.pdmodel', save_params_filename='inference.pdiparams', - batch_nums=10) + batch_size=1, + batch_nums=None) if __name__ == '__main__': From 4967ec0aafcec033fd4b639cf39fdbd0d30823b9 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 12:22:23 +0000 Subject: [PATCH 6/9] fix yml --- tests/ocr_det_params.txt | 8 ++++---- tests/ocr_rec_params.txt | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ocr_det_params.txt b/tests/ocr_det_params.txt index 7f6481177a..1773bf3b93 100644 --- a/tests/ocr_det_params.txt +++ b/tests/ocr_det_params.txt @@ -12,7 +12,7 @@ train_model_name:latest train_infer_img_dir:./train_data/icdar2015/text_localization/ch4_test_images/ null:null ## -trainer:norm_train|pact_train +trainer:norm_train|pact_train|fpgm_train norm_train:tools/train.py -c tests/configs/det_mv3_db.yml -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained pact_train:deploy/slim/quantization/quant.py -c tests/configs/det_mv3_db.yml -o fpgm_train:deploy/slim/prune/sensitivity_anal.py -c tests/configs/det_mv3_db.yml -o Global.pretrained_model=./pretrain_models/det_mv3_db_v2.0_train/best_accuracy @@ -21,13 +21,13 @@ null:null null:null ## ===========================eval_params=========================== -eval:tools/eval.py -c tests/configs/det_mv3_db.yml -o +eval:null null:null ## ===========================infer_params=========================== Global.save_inference_dir:./output/ Global.pretrained_model: -norm_export:tools/export_model.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o +norm_export:tools/export_model.py -c tests/configs/det_mv3_db.yml -o quant_export:deploy/slim/quantization/export_model.py -c tests/configs/det_mv3_db.yml -o fpgm_export:deploy/slim/prune/export_prune_model.py -c tests/configs/det_mv3_db.yml -o distill_export:null @@ -35,7 +35,7 @@ export1:null export2:null ## train_model:./inference/ch_ppocr_mobile_v2.0_det_train/best_accuracy -infer_export:tools/export_model.py -c tests/configs/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o +infer_export:tools/export_model.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o infer_quant:False inference:tools/infer/predict_det.py --use_gpu:True|False diff --git a/tests/ocr_rec_params.txt b/tests/ocr_rec_params.txt index 504f45b8b9..60cd19785c 100644 --- a/tests/ocr_rec_params.txt +++ b/tests/ocr_rec_params.txt @@ -17,7 +17,7 @@ norm_train:tools/train.py -c configs/rec/rec_icdar15_train.yml -o pact_train:deploy/slim/quantization/quant.py -c configs/rec/rec_icdar15_train.yml -o fpgm_train:null distill_train:null -kl_quant:deploy/slim/quantization/quant_kl.py -c configs/rec/rec_icdar15_train.yml -o Global.pretrained_model=./inference/ch_ppocr_mobile_v2.0_rec_infer/ +null:null null:null ## ===========================eval_params=========================== From 2daf1aff6ef8a76bc397a3542a65fdf8190a6160 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 12:27:08 +0000 Subject: [PATCH 7/9] restore rec_params.txt --- tests/ocr_rec_params.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ocr_rec_params.txt b/tests/ocr_rec_params.txt index 60cd19785c..2d27a9ee3b 100644 --- a/tests/ocr_rec_params.txt +++ b/tests/ocr_rec_params.txt @@ -21,7 +21,7 @@ null:null null:null ## ===========================eval_params=========================== -eval:null +eval:tools/eval.py -c configs/rec/rec_icdar15_train.yml -o null:null ## ===========================infer_params=========================== From e247e63d52c979b3bb50e73c751d49b3166e71a2 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 13:03:45 +0000 Subject: [PATCH 8/9] fix export bug in test.sh for infer mode --- tests/test.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test.sh b/tests/test.sh index 4a67366748..6714578d32 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -430,7 +430,9 @@ if [ ${MODE} = "infer" ]; then save_infer_dir=$(dirname $infer_model) set_export_weight=$(func_set_params "${export_weight}" "${infer_model}") set_save_infer_key=$(func_set_params "${save_infer_key}" "${save_infer_dir}") - export_cmd="${python} ${norm_export} ${set_export_weight} ${set_save_infer_key}" + export_cmd="${python} ${infer_run_exports[Count]} ${set_export_weight} ${set_save_infer_key}" + echo ${infer_run_exports[Count]} + echo $export_cmd eval $export_cmd status_export=$? status_check $status_export "${export_cmd}" "${status_log}" From edfbb24a1b5c07afabd6ed6dd47db6df01968e45 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Thu, 9 Sep 2021 13:05:50 +0000 Subject: [PATCH 9/9] add params.txt for ocr det kl quant --- tests/ocr_kl_quant_params.txt | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/ocr_kl_quant_params.txt diff --git a/tests/ocr_kl_quant_params.txt b/tests/ocr_kl_quant_params.txt new file mode 100644 index 0000000000..c6ee97dca4 --- /dev/null +++ b/tests/ocr_kl_quant_params.txt @@ -0,0 +1,51 @@ +===========================train_params=========================== +model_name:ocr_system +python:python3.7 +gpu_list:null +Global.use_gpu:null +Global.auto_cast:null +Global.epoch_num:null +Global.save_model_dir:./output/ +Train.loader.batch_size_per_card:null +Global.pretrained_model:null +train_model_name:null +train_infer_img_dir:null +null:null +## +trainer: +norm_train:null +pact_train:null +fpgm_train:null +distill_train:null +null:null +null:null +## +===========================eval_params=========================== +eval:null +null:null +## +===========================infer_params=========================== +Global.save_inference_dir:./output/ +Global.pretrained_model: +norm_export:null +quant_export:null +fpgm_export:null +distill_export:null +export1:null +export2:null +## +infer_model:./inference/ch_ppocr_mobile_v2.0_det_infer/ +kl_quant:deploy/slim/quantization/quant_kl.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o +infer_quant:True +inference:tools/infer/predict_det.py +--use_gpu:TrueFalse +--enable_mkldnn:True|False +--cpu_threads:1|6 +--rec_batch_num:1 +--use_tensorrt:False|True +--precision:fp32|fp16|int8 +--det_model_dir: +--image_dir:./inference/ch_det_data_50/all-sum-510/ +--save_log_path:null +--benchmark:True +null:null