export_model.py 2.4 KB
Newer Older
L
LDOUBLEV 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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
SteveApple's avatar
Fix bug  
SteveApple 已提交
18

L
LDOUBLEV 已提交
19 20 21

import os
import sys
22
__dir__ = os.path.dirname(os.path.abspath(__file__))
L
LDOUBLEV 已提交
23
sys.path.append(__dir__)
24
sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
L
LDOUBLEV 已提交
25 26 27 28 29 30 31 32 33 34


def set_paddle_flags(**kwargs):
    for key, value in kwargs.items():
        if os.environ.get(key, None) is None:
            os.environ[key] = str(value)


# NOTE(paddle-dev): All of these flags should be
# set before `import paddle`. Otherwise, it would
T
tink2123 已提交
35
# not take any effect.
L
LDOUBLEV 已提交
36 37 38 39 40 41
set_paddle_flags(
    FLAGS_eager_delete_tensor_gb=0,  # enable GC to save memory
)

import program
from paddle import fluid
SteveApple's avatar
Fix bug  
SteveApple 已提交
42
from ppocr.utils.utility import enable_static_mode
L
LDOUBLEV 已提交
43 44 45 46 47
from ppocr.utils.utility import initial_logger
logger = initial_logger()
from ppocr.utils.save_load import init_model


S
shaohua.zhang 已提交
48
def main():
S
shaohua.zhang 已提交
49
    startup_prog, eval_program, place, config, _ = program.preprocess()
L
LDOUBLEV 已提交
50 51 52 53 54 55 56 57 58

    feeded_var_names, target_vars, fetches_var_name = program.build_export(
        config, eval_program, startup_prog)
    eval_program = eval_program.clone(for_test=True)
    exe = fluid.Executor(place)
    exe.run(startup_prog)

    init_model(config, eval_program, exe)

L
LDOUBLEV 已提交
59 60 61
    save_inference_dir = config['Global']['save_inference_dir']
    if not os.path.exists(save_inference_dir):
        os.makedirs(save_inference_dir)
L
LDOUBLEV 已提交
62
    fluid.io.save_inference_model(
L
LDOUBLEV 已提交
63
        dirname=save_inference_dir,
L
LDOUBLEV 已提交
64 65 66 67 68 69
        feeded_var_names=feeded_var_names,
        main_program=eval_program,
        target_vars=target_vars,
        executor=exe,
        model_filename='model',
        params_filename='params')
L
LDOUBLEV 已提交
70 71
    print("inference model saved in {}/model and {}/params".format(
        save_inference_dir, save_inference_dir))
L
LDOUBLEV 已提交
72 73 74 75
    print("save success, output_name_list:", fetches_var_name)


if __name__ == '__main__':
L
LDOUBLEV 已提交
76
    enable_static_mode()
L
LDOUBLEV 已提交
77
    main()