export_model.py 5.3 KB
Newer Older
D
dyning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15 16 17 18 19
import os
import sys

__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.append(__dir__)
20
sys.path.append(os.path.abspath(os.path.join(__dir__, "..")))
21

D
dyning 已提交
22 23 24 25 26 27 28
import argparse

import paddle
from paddle.jit import to_static

from ppocr.modeling.architectures import build_model
from ppocr.postprocess import build_post_process
29
from ppocr.utils.save_load import load_model
30
from ppocr.utils.logging import get_logger
W
WenmuZhou 已提交
31
from tools.program import load_config, merge_config, ArgsParser
D
dyning 已提交
32 33


34 35 36
def export_single_model(model, arch_config, save_path, logger):
    if arch_config["algorithm"] == "SRN":
        max_text_length = arch_config["Head"]["max_text_length"]
T
tink2123 已提交
37
        other_shape = [
T
tink2123 已提交
38
            paddle.static.InputSpec(
39
                shape=[None, 1, 64, 256], dtype="float32"), [
T
tink2123 已提交
40 41 42
                    paddle.static.InputSpec(
                        shape=[None, 256, 1],
                        dtype="int64"), paddle.static.InputSpec(
W
WenmuZhou 已提交
43
                            shape=[None, max_text_length, 1], dtype="int64"),
T
tink2123 已提交
44
                    paddle.static.InputSpec(
W
WenmuZhou 已提交
45 46 47 48
                        shape=[None, 8, max_text_length, max_text_length],
                        dtype="int64"), paddle.static.InputSpec(
                            shape=[None, 8, max_text_length, max_text_length],
                            dtype="int64")
T
tink2123 已提交
49 50 51
                ]
        ]
        model = to_static(model, input_spec=other_shape)
T
Topdu 已提交
52 53 54 55 56 57
    elif arch_config["algorithm"] == "SAR":
        other_shape = [
            paddle.static.InputSpec(
                shape=[None, 3, 48, 160], dtype="float32"),
        ]
        model = to_static(model, input_spec=other_shape)
58 59 60 61 62 63
    elif arch_config["algorithm"] == "PREN":
        other_shape = [
            paddle.static.InputSpec(
                shape=[None, 3, 64, 512], dtype="float32"),
        ]
        model = to_static(model, input_spec=other_shape)
T
tink2123 已提交
64
    else:
65
        infer_shape = [3, -1, -1]
66
        if arch_config["model_type"] == "rec":
67
            infer_shape = [3, 32, -1]  # for rec model, H must be 32
68 69 70
            if "Transform" in arch_config and arch_config[
                    "Transform"] is not None and arch_config["Transform"][
                        "name"] == "TPS":
71
                logger.info(
72
                    "When there is tps in the network, variable length input is not supported, and the input size needs to be the same as during training"
73 74
                )
                infer_shape[-1] = 100
T
Topdu 已提交
75 76
            if arch_config["algorithm"] == "NRTR":
                infer_shape = [1, 32, 100]
M
refine  
MissPenguin 已提交
77 78
        elif arch_config["model_type"] == "table":
            infer_shape = [3, 488, 488]
T
tink2123 已提交
79 80 81 82
        model = to_static(
            model,
            input_spec=[
                paddle.static.InputSpec(
83
                    shape=[None] + infer_shape, dtype="float32")
T
tink2123 已提交
84 85
            ])

86
    paddle.jit.save(model, save_path)
87 88 89 90 91 92 93
    logger.info("inference model is saved to {}".format(save_path))
    return


def main():
    FLAGS = ArgsParser().parse_args()
    config = load_config(FLAGS.config)
文幕地方's avatar
文幕地方 已提交
94
    config = merge_config(config, FLAGS.opt)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    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"))
        if config["Architecture"]["algorithm"] in ["Distillation",
                                                   ]:  # distillation model
            for key in config["Architecture"]["Models"]:
                config["Architecture"]["Models"][key]["Head"][
                    "out_channels"] = char_num
110 111 112
                # just one final tensor needs to to exported for inference
                config["Architecture"]["Models"][key][
                    "return_all_feats"] = False
113 114 115
        else:  # base rec model
            config["Architecture"]["Head"]["out_channels"] = char_num
    model = build_model(config["Architecture"])
116
    load_model(config, model)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    model.eval()

    save_path = config["Global"]["save_inference_dir"]

    arch_config = config["Architecture"]

    if arch_config["algorithm"] in ["Distillation", ]:  # distillation model
        archs = list(arch_config["Models"].values())
        for idx, name in enumerate(model.model_name_list):
            sub_model_save_path = os.path.join(save_path, name, "inference")
            export_single_model(model.model_list[idx], archs[idx],
                                sub_model_save_path, logger)
    else:
        save_path = os.path.join(save_path, "inference")
        export_single_model(model, arch_config, save_path, logger)
D
dyning 已提交
132 133 134 135


if __name__ == "__main__":
    main()