eval.py 2.4 KB
Newer Older
F
FDInSky 已提交
1 2 3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
4 5 6 7 8 9
import os, sys
# add python path of PadleDetection to sys.path
parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 2)))
if parent_path not in sys.path:
    sys.path.append(parent_path)

F
FDInSky 已提交
10 11 12 13 14 15 16 17 18 19 20
import time
# ignore numba warning
import warnings
warnings.filterwarnings('ignore')
import random
import numpy as np
import paddle.fluid as fluid
from ppdet.core.workspace import load_config, merge_config, create
from ppdet.utils.check import check_gpu, check_version, check_config
from ppdet.utils.cli import ArgsParser
from ppdet.utils.eval_utils import coco_eval_results
21
from ppdet.utils.checkpoint import load_dygraph_ckpt, save_dygraph_ckpt
F
FDInSky 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41


def parse_args():
    parser = ArgsParser()
    parser.add_argument(
        "--output_eval",
        default=None,
        type=str,
        help="Evaluation directory, default is current directory.")

    parser.add_argument(
        '--json_eval', action='store_true', default=False, help='')

    parser.add_argument(
        '--use_gpu', action='store_true', default=False, help='')

    args = parser.parse_args()
    return args


42 43 44 45
def run(FLAGS, cfg, place):

    # Data 
    eval_loader, _ = create('EvalReader')(cfg['worker_num'], place)
F
FDInSky 已提交
46 47 48

    # Model
    main_arch = cfg.architecture
49
    model = create(cfg.architecture)
F
FDInSky 已提交
50 51

    # Init Model  
52
    model = load_dygraph_ckpt(model, ckpt=cfg.weights)
F
FDInSky 已提交
53 54 55

    # Run Eval
    outs_res = []
56
    for iter_id, data in enumerate(eval_loader):
F
FDInSky 已提交
57 58 59 60
        start_time = time.time()

        # forward 
        model.eval()
61
        outs = model(data, cfg['EvalReader']['inputs_def']['fields'], 'infer')
F
FDInSky 已提交
62 63 64 65 66 67 68 69 70
        outs_res.append(outs)

        # log 
        cost_time = time.time() - start_time
        print("Eval iter: {}, time: {}".format(iter_id, cost_time))

    # Metric 
    coco_eval_results(
        outs_res,
71
        include_mask=True if 'MaskHead' in cfg else False,
F
FDInSky 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        dataset=cfg['EvalReader']['dataset'])


def main():
    FLAGS = parse_args()

    cfg = load_config(FLAGS.config)
    merge_config(FLAGS.opt)
    check_gpu(cfg.use_gpu)
    check_version()

    place = fluid.CUDAPlace(fluid.dygraph.parallel.Env()
                            .dev_id) if cfg.use_gpu else fluid.CPUPlace()

    with fluid.dygraph.guard(place):
87
        run(FLAGS, cfg, place)
F
FDInSky 已提交
88 89 90 91


if __name__ == '__main__':
    main()