val.py 3.2 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 18 19
#
# 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 os
import math

import numpy as np
20 21 22
import tqdm
import cv2
from paddle.fluid.dygraph.base import to_variable
C
chenguowei01 已提交
23
import paddle.fluid as fluid
C
chenguowei01 已提交
24
from paddle.fluid.dygraph.parallel import ParallelEnv
C
chenguowei01 已提交
25
from paddle.fluid.io import DataLoader
C
chenguowei01 已提交
26
from paddle.fluid.dataloader import BatchSampler
C
chenguowei01 已提交
27

C
chenguowei01 已提交
28
from datasets import DATASETS
C
chenguowei01 已提交
29
import transforms as T
C
chenguowei01 已提交
30
from models import MODELS
C
chenguowei01 已提交
31 32 33
import utils.logging as logging
from utils import get_environ_info
from utils import ConfusionMatrix
C
chenguowei01 已提交
34
from utils import Timer, calculate_eta
35
from core import evaluate
C
chenguowei01 已提交
36 37 38


def parse_args():
C
chenguowei01 已提交
39
    parser = argparse.ArgumentParser(description='Model evaluation')
C
chenguowei01 已提交
40 41

    # params of model
C
chenguowei01 已提交
42 43 44
    parser.add_argument(
        '--model_name',
        dest='model_name',
R
update  
root 已提交
45 46
        help='Model type for evaluation, which is one of {}'.format(
            str(list(MODELS.keys()))),
C
chenguowei01 已提交
47 48
        type=str,
        default='UNet')
C
chenguowei01 已提交
49 50

    # params of dataset
C
chenguowei01 已提交
51
    parser.add_argument(
C
chenguowei01 已提交
52 53
        '--dataset',
        dest='dataset',
C
chenguowei01 已提交
54 55
        help="The dataset you want to evaluation, which is one of {}".format(
            str(list(DATASETS.keys()))),
C
chenguowei01 已提交
56
        type=str,
C
chenguowei01 已提交
57
        default='OpticDiscSeg')
C
chenguowei01 已提交
58 59

    # params of evaluate
C
chenguowei01 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
    parser.add_argument(
        "--input_size",
        dest="input_size",
        help="The image size for net inputs.",
        nargs=2,
        default=[512, 512],
        type=int)
    parser.add_argument(
        '--model_dir',
        dest='model_dir',
        help='The path of model for evaluation',
        type=str,
        default=None)
C
chenguowei01 已提交
73 74 75 76 77

    return parser.parse_args()


def main(args):
C
chenguowei01 已提交
78
    env_info = get_environ_info()
C
chenguowei01 已提交
79 80 81
    places = fluid.CUDAPlace(ParallelEnv().dev_id) \
        if env_info['place'] == 'cuda' and fluid.is_compiled_with_cuda() \
        else fluid.CPUPlace()
C
chenguowei01 已提交
82

C
chenguowei01 已提交
83 84 85 86
    if args.dataset not in DATASETS:
        raise Exception('--dataset is invalid. it should be one of {}'.format(
            str(list(DATASETS.keys()))))
    dataset = DATASETS[args.dataset]
C
chenguowei01 已提交
87

C
chenguowei01 已提交
88 89
    with fluid.dygraph.guard(places):
        eval_transforms = T.Compose([T.Resize(args.input_size), T.Normalize()])
C
chenguowei01 已提交
90
        eval_dataset = dataset(transforms=eval_transforms, mode='val')
C
chenguowei01 已提交
91

C
chenguowei01 已提交
92 93 94 95 96
        if args.model_name not in MODELS:
            raise Exception(
                '--model_name is invalid. it should be one of {}'.format(
                    str(list(MODELS.keys()))))
        model = MODELS[args.model_name](num_classes=eval_dataset.num_classes)
C
chenguowei01 已提交
97

C
chenguowei01 已提交
98 99 100 101
        evaluate(
            model,
            eval_dataset,
            model_dir=args.model_dir,
102
            num_classes=eval_dataset.num_classes)
C
chenguowei01 已提交
103 104 105 106


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