val.py 2.0 KB
Newer Older
C
chenguowei01 已提交
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
C
chenguowei01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# 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.

import argparse

import paddle.fluid as fluid
C
chenguowei01 已提交
18
from paddle.fluid.dygraph.parallel import ParallelEnv
C
chenguowei01 已提交
19

W
wuzewu 已提交
20
import dygraph
21
from dygraph.cvlibs import manager
C
chenguowei01 已提交
22
from dygraph.utils import get_environ_info
W
wuzewu 已提交
23
from dygraph.utils import Config
C
chenguowei01 已提交
24
from dygraph.core import evaluate
C
chenguowei01 已提交
25 26 27


def parse_args():
C
chenguowei01 已提交
28
    parser = argparse.ArgumentParser(description='Model evaluation')
C
chenguowei01 已提交
29 30

    # params of evaluate
C
chenguowei01 已提交
31
    parser.add_argument(
W
wuzewu 已提交
32
        "--config", dest="cfg", help="The config file.", default=None, type=str)
C
chenguowei01 已提交
33 34 35 36 37 38
    parser.add_argument(
        '--model_dir',
        dest='model_dir',
        help='The path of model for evaluation',
        type=str,
        default=None)
C
chenguowei01 已提交
39 40 41 42 43

    return parser.parse_args()


def main(args):
C
chenguowei01 已提交
44
    env_info = get_environ_info()
C
chenguowei01 已提交
45
    places = fluid.CUDAPlace(ParallelEnv().dev_id) \
C
chenguowei01 已提交
46
        if env_info['Paddle compiled with cuda'] and env_info['GPUs used'] \
C
chenguowei01 已提交
47
        else fluid.CPUPlace()
C
chenguowei01 已提交
48

C
chenguowei01 已提交
49
    with fluid.dygraph.guard(places):
W
wuzewu 已提交
50 51 52 53 54 55 56 57 58
        if not args.cfg:
            raise RuntimeError('No configuration file specified.')

        cfg = Config(args.cfg)
        val_dataset = cfg.val_dataset
        if not val_dataset:
            raise RuntimeError(
                'The verification dataset is not specified in the configuration file.'
            )
C
chenguowei01 已提交
59
        evaluate(
W
wuzewu 已提交
60 61
            cfg.model,
            val_dataset,
C
chenguowei01 已提交
62
            model_dir=args.model_dir,
W
wuzewu 已提交
63
            num_classes=val_dataset.num_classes)
C
chenguowei01 已提交
64 65 66 67


if __name__ == '__main__':
    args = parse_args()
C
chenguowei01 已提交
68
    main(args)