export_model.py 5.4 KB
Newer Older
B
baiyfbupt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# 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.

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 argparse

import paddle
from paddle.jit import to_static

from ppocr.modeling.architectures import build_model
from ppocr.postprocess import build_post_process
31
from ppocr.utils.save_load import load_model
B
baiyfbupt 已提交
32 33 34 35 36 37 38 39
from ppocr.utils.logging import get_logger
from tools.program import load_config, merge_config, ArgsParser
from ppocr.metrics import build_metric
import tools.program as program
from paddleslim.dygraph.quant import QAT
from ppocr.data import build_dataloader


40 41 42 43 44 45 46 47 48 49 50
def export_single_model(quanter, model, infer_shape, save_path, logger):
    quanter.save_quantized_model(
        model,
        save_path,
        input_spec=[
            paddle.static.InputSpec(
                shape=[None] + infer_shape, dtype='float32')
        ])
    logger.info('inference QAT model is saved to {}'.format(save_path))


B
baiyfbupt 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
def main():
    ############################################################################################################
    # 1. quantization configs
    ############################################################################################################
    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'],
    }
    FLAGS = ArgsParser().parse_args()
    config = load_config(FLAGS.config)
文幕地方's avatar
文幕地方 已提交
79
    config = merge_config(config, FLAGS.opt)
B
baiyfbupt 已提交
80 81 82 83 84 85 86 87 88 89
    logger = get_logger()
    # build post process

    post_process_class = build_post_process(config['PostProcess'],
                                            config['Global'])

    # build model
    # for rec algorithm
    if hasattr(post_process_class, 'character'):
        char_num = len(getattr(post_process_class, 'character'))
90 91 92 93 94 95 96 97
        if config['Architecture']["algorithm"] in ["Distillation",
                                                   ]:  # distillation model
            for key in config['Architecture']["Models"]:
                config['Architecture']["Models"][key]["Head"][
                    'out_channels'] = char_num
        else:  # base rec model
            config['Architecture']["Head"]['out_channels'] = char_num

B
baiyfbupt 已提交
98 99 100 101 102 103
    model = build_model(config['Architecture'])

    # get QAT model
    quanter = QAT(config=quant_config)
    quanter.quantize(model)

104
    load_model(config, model)
B
baiyfbupt 已提交
105 106 107 108 109 110 111 112
    model.eval()

    # build metric
    eval_class = build_metric(config['Metric'])

    # build dataloader
    valid_dataloader = build_dataloader(config, 'Eval', device, logger)

L
LDOUBLEV 已提交
113
    use_srn = config['Architecture']['algorithm'] == "SRN"
A
andyjpaddle 已提交
114
    model_type = config['Architecture'].get('model_type', None)
B
baiyfbupt 已提交
115
    # start eval
L
LDOUBLEV 已提交
116
    metric = program.eval(model, valid_dataloader, post_process_class,
L
LDOUBLEV 已提交
117
                          eval_class, model_type, use_srn)
D
Double_V 已提交
118

B
baiyfbupt 已提交
119
    logger.info('metric eval ***************')
120
    for k, v in metric.items():
B
baiyfbupt 已提交
121 122
        logger.info('{}:{}'.format(k, v))

A
andyjpaddle 已提交
123
    infer_shape = [3, 32, 100] if model_type == "rec" else [3, 640, 640]
B
baiyfbupt 已提交
124

125 126 127 128 129
    save_path = config["Global"]["save_inference_dir"]

    arch_config = config["Architecture"]
    if arch_config["algorithm"] in ["Distillation", ]:  # distillation model
        for idx, name in enumerate(model.model_name_list):
L
LDOUBLEV 已提交
130
            model.model_list[idx].eval()
131 132 133 134 135 136
            sub_model_save_path = os.path.join(save_path, name, "inference")
            export_single_model(quanter, model.model_list[idx], infer_shape,
                                sub_model_save_path, logger)
    else:
        save_path = os.path.join(save_path, "inference")
        export_single_model(quanter, model, infer_shape, save_path, logger)
B
baiyfbupt 已提交
137 138 139 140 141


if __name__ == "__main__":
    config, device, logger, vdl_writer = program.preprocess()
    main()