eval.py 2.3 KB
Newer Older
W
WuHaobo 已提交
1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
W
WuHaobo 已提交
2
#
W
WuHaobo 已提交
3 4 5
# 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
W
WuHaobo 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
W
WuHaobo 已提交
9 10 11 12 13
# 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.
W
WuHaobo 已提交
14

littletomatodonkey's avatar
littletomatodonkey 已提交
15
import paddle
littletomatodonkey's avatar
littletomatodonkey 已提交
16 17
from paddle.distributed import ParallelEnv

18
import argparse
W
WuHaobo 已提交
19
import os
20 21 22 23
import sys
__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.append(__dir__)
sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
W
WuHaobo 已提交
24

littletomatodonkey's avatar
littletomatodonkey 已提交
25 26 27 28 29 30
from ppcls.utils import logger
from ppcls.utils.save_load import init_model
from ppcls.utils.config import get_config
from ppcls.data import Reader
import program

W
WuHaobo 已提交
31 32 33 34 35 36 37

def parse_args():
    parser = argparse.ArgumentParser("PaddleClas eval script")
    parser.add_argument(
        '-c',
        '--config',
        type=str,
W
WuHaobo 已提交
38
        default='./configs/eval.yaml',
W
WuHaobo 已提交
39 40 41 42 43 44 45 46 47 48 49
        help='config file path')
    parser.add_argument(
        '-o',
        '--override',
        action='append',
        default=[],
        help='config options to be overridden')
    args = parser.parse_args()
    return args


littletomatodonkey's avatar
littletomatodonkey 已提交
50
def main(args, return_dict={}):
51 52 53 54
    config = get_config(args.config, overrides=args.override, show=True)
    # assign place
    use_gpu = config.get("use_gpu", True)
    if use_gpu:
littletomatodonkey's avatar
littletomatodonkey 已提交
55 56
        gpu_id = ParallelEnv().dev_id
        place = paddle.CUDAPlace(gpu_id)
57
    else:
littletomatodonkey's avatar
littletomatodonkey 已提交
58 59 60 61 62 63 64 65
        place = paddle.CPUPlace()

    paddle.disable_static(place)

    strategy = paddle.distributed.init_parallel_env()
    net = program.create_model(config.ARCHITECTURE, config.classes_num)
    net = paddle.DataParallel(net, strategy)
    init_model(config, net, optimizer=None)
littletomatodonkey's avatar
littletomatodonkey 已提交
66
    valid_dataloader = Reader(config, 'valid', places=place)()
littletomatodonkey's avatar
littletomatodonkey 已提交
67
    net.eval()
littletomatodonkey's avatar
littletomatodonkey 已提交
68

littletomatodonkey's avatar
littletomatodonkey 已提交
69 70
    top1_acc = program.run(valid_dataloader, config, net, None, None, 0,
                           'valid')
littletomatodonkey's avatar
littletomatodonkey 已提交
71 72
    return_dict["top1_acc"] = top1_acc
    return top1_acc
W
WuHaobo 已提交
73

74

W
WuHaobo 已提交
75 76 77
if __name__ == '__main__':
    args = parse_args()
    main(args)