2020-11-05 15:13:36 +08:00
|
|
|
# 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.
|
|
|
|
|
2020-11-09 18:19:30 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
__dir__ = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
sys.path.append(__dir__)
|
2021-06-03 14:53:24 +08:00
|
|
|
sys.path.append(os.path.abspath(os.path.join(__dir__, "..")))
|
2020-11-09 18:19:30 +08:00
|
|
|
|
2020-11-05 15:13:36 +08:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
import paddle
|
|
|
|
from paddle.jit import to_static
|
|
|
|
|
|
|
|
from ppocr.modeling.architectures import build_model
|
|
|
|
from ppocr.postprocess import build_post_process
|
|
|
|
from ppocr.utils.save_load import init_model
|
2020-11-09 18:19:30 +08:00
|
|
|
from ppocr.utils.logging import get_logger
|
2020-12-11 18:48:23 +08:00
|
|
|
from tools.program import load_config, merge_config, ArgsParser
|
2020-11-05 15:13:36 +08:00
|
|
|
|
|
|
|
|
2021-06-03 14:53:24 +08:00
|
|
|
def export_single_model(model, arch_config, save_path, logger):
|
|
|
|
if arch_config["algorithm"] == "SRN":
|
|
|
|
max_text_length = arch_config["Head"]["max_text_length"]
|
2020-12-30 16:15:49 +08:00
|
|
|
other_shape = [
|
2020-12-09 19:56:37 +08:00
|
|
|
paddle.static.InputSpec(
|
2021-06-03 14:53:24 +08:00
|
|
|
shape=[None, 1, 64, 256], dtype="float32"), [
|
2020-12-30 16:15:49 +08:00
|
|
|
paddle.static.InputSpec(
|
|
|
|
shape=[None, 256, 1],
|
|
|
|
dtype="int64"), paddle.static.InputSpec(
|
2021-04-28 13:36:16 +08:00
|
|
|
shape=[None, max_text_length, 1], dtype="int64"),
|
2020-12-30 16:15:49 +08:00
|
|
|
paddle.static.InputSpec(
|
2021-04-28 13:36:16 +08:00
|
|
|
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")
|
2020-12-30 16:15:49 +08:00
|
|
|
]
|
|
|
|
]
|
|
|
|
model = to_static(model, input_spec=other_shape)
|
|
|
|
else:
|
2021-01-29 11:37:19 +08:00
|
|
|
infer_shape = [3, -1, -1]
|
2021-06-03 14:53:24 +08:00
|
|
|
if arch_config["model_type"] == "rec":
|
2021-01-29 11:37:19 +08:00
|
|
|
infer_shape = [3, 32, -1] # for rec model, H must be 32
|
2021-06-03 14:53:24 +08:00
|
|
|
if "Transform" in arch_config and arch_config[
|
|
|
|
"Transform"] is not None and arch_config["Transform"][
|
|
|
|
"name"] == "TPS":
|
2021-01-29 11:37:19 +08:00
|
|
|
logger.info(
|
2021-06-03 14:53:24 +08:00
|
|
|
"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"
|
2021-01-29 11:37:19 +08:00
|
|
|
)
|
|
|
|
infer_shape[-1] = 100
|
2021-06-21 20:20:25 +08:00
|
|
|
elif arch_config["model_type"] == "table":
|
|
|
|
infer_shape = [3, 488, 488]
|
2020-12-30 16:15:49 +08:00
|
|
|
model = to_static(
|
|
|
|
model,
|
|
|
|
input_spec=[
|
|
|
|
paddle.static.InputSpec(
|
2021-06-03 14:53:24 +08:00
|
|
|
shape=[None] + infer_shape, dtype="float32")
|
2020-12-30 16:15:49 +08:00
|
|
|
])
|
|
|
|
|
2020-11-09 18:19:30 +08:00
|
|
|
paddle.jit.save(model, save_path)
|
2021-06-03 14:53:24 +08:00
|
|
|
logger.info("inference model is saved to {}".format(save_path))
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
FLAGS = ArgsParser().parse_args()
|
|
|
|
config = load_config(FLAGS.config)
|
|
|
|
merge_config(FLAGS.opt)
|
|
|
|
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
|
2021-09-01 19:36:16 +08:00
|
|
|
# just one final tensor needs to to exported for inference
|
|
|
|
config["Architecture"]["Models"][key][
|
|
|
|
"return_all_feats"] = False
|
2021-06-03 14:53:24 +08:00
|
|
|
else: # base rec model
|
|
|
|
config["Architecture"]["Head"]["out_channels"] = char_num
|
|
|
|
model = build_model(config["Architecture"])
|
2021-06-05 14:52:45 +08:00
|
|
|
init_model(config, model)
|
2021-06-03 14:53:24 +08:00
|
|
|
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)
|
2020-11-05 15:13:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|