eval.py 3.8 KB
Newer Older
L
LDOUBLEV 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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

L
LDOUBLEV 已提交
19 20
import os
import sys
21
__dir__ = os.path.dirname(os.path.abspath(__file__))
L
LDOUBLEV 已提交
22
sys.path.append(__dir__)
23
sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
L
LDOUBLEV 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41


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
# not take any effect.
set_paddle_flags(
    FLAGS_eager_delete_tensor_gb=0,  # enable GC to save memory
)

import program
from paddle import fluid
from ppocr.utils.utility import initial_logger
42
from ppocr.utils.utility import enable_static_mode
L
LDOUBLEV 已提交
43 44 45 46 47 48
logger = initial_logger()
from ppocr.data.reader_main import reader_main
from ppocr.utils.save_load import init_model
from eval_utils.eval_det_utils import eval_det_run
from eval_utils.eval_rec_utils import test_rec_benchmark
from eval_utils.eval_rec_utils import eval_rec_run
W
WenmuZhou 已提交
49
from eval_utils.eval_cls_utils import eval_cls_run
L
LDOUBLEV 已提交
50 51 52


def main():
W
WenmuZhou 已提交
53 54
    startup_prog, eval_program, place, config, train_alg_type = program.preprocess(
    )
L
LDOUBLEV 已提交
55 56 57 58 59 60 61 62 63 64
    eval_build_outputs = program.build(
        config, eval_program, startup_prog, mode='test')
    eval_fetch_name_list = eval_build_outputs[1]
    eval_fetch_varname_list = eval_build_outputs[2]
    eval_program = eval_program.clone(for_test=True)
    exe = fluid.Executor(place)
    exe.run(startup_prog)

    init_model(config, eval_program, exe)

S
shaohua.zhang 已提交
65
    if train_alg_type == 'det':
L
LDOUBLEV 已提交
66
        eval_reader = reader_main(config=config, mode="eval")
L
LDOUBLEV 已提交
67 68 69 70
        eval_info_dict = {'program':eval_program,\
            'reader':eval_reader,\
            'fetch_name_list':eval_fetch_name_list,\
            'fetch_varname_list':eval_fetch_varname_list}
L
LDOUBLEV 已提交
71
        metrics = eval_det_run(exe, config, eval_info_dict, "eval")
littletomatodonkey's avatar
littletomatodonkey 已提交
72
        logger.info("Eval result: {}".format(metrics))
W
WenmuZhou 已提交
73 74 75 76 77 78 79 80
    elif train_alg_type == 'cls':
        eval_reader = reader_main(config=config, mode="eval")
        eval_info_dict = {'program': eval_program, \
                          'reader': eval_reader, \
                          'fetch_name_list': eval_fetch_name_list, \
                          'fetch_varname_list': eval_fetch_varname_list}
        metrics = eval_cls_run(exe, eval_info_dict)
        logger.info("Eval result: {}".format(metrics))
L
LDOUBLEV 已提交
81
    else:
T
tink2123 已提交
82
        reader_type = config['Global']['reader_yml']
T
tink2123 已提交
83
        if "benchmark" not in reader_type:
L
LDOUBLEV 已提交
84 85 86 87 88 89
            eval_reader = reader_main(config=config, mode="eval")
            eval_info_dict = {'program': eval_program, \
                              'reader': eval_reader, \
                              'fetch_name_list': eval_fetch_name_list, \
                              'fetch_varname_list': eval_fetch_varname_list}
            metrics = eval_rec_run(exe, config, eval_info_dict, "eval")
littletomatodonkey's avatar
littletomatodonkey 已提交
90
            logger.info("Eval result: {}".format(metrics))
L
LDOUBLEV 已提交
91 92 93 94 95 96 97 98
        else:
            eval_info_dict = {'program':eval_program,\
                'fetch_name_list':eval_fetch_name_list,\
                'fetch_varname_list':eval_fetch_varname_list}
            test_rec_benchmark(exe, config, eval_info_dict)


if __name__ == '__main__':
99
    enable_static_mode()
L
LDOUBLEV 已提交
100
    main()