infer.py 3.5 KB
Newer Older
C
chenguowei01 已提交
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
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
19

C
chenguowei01 已提交
20 21 22 23 24
from dygraph.datasets import DATASETS
import dygraph.transforms as T
from dygraph.models import MODELS
from dygraph.utils import get_environ_info
from dygraph.core import infer
25 26 27 28 29 30


def parse_args():
    parser = argparse.ArgumentParser(description='Model training')

    # params of model
C
chenguowei01 已提交
31 32 33
    parser.add_argument(
        '--model_name',
        dest='model_name',
R
update  
root 已提交
34 35
        help='Model type for testing, which is one of {}'.format(
            str(list(MODELS.keys()))),
C
chenguowei01 已提交
36 37
        type=str,
        default='UNet')
38

C
chenguowei01 已提交
39
    # params of infer
C
chenguowei01 已提交
40
    parser.add_argument(
C
chenguowei01 已提交
41 42
        '--dataset',
        dest='dataset',
C
chenguowei01 已提交
43 44
        help="The dataset you want to test, which is one of {}".format(
            str(list(DATASETS.keys()))),
C
chenguowei01 已提交
45
        type=str,
C
chenguowei01 已提交
46
        default='OpticDiscSeg')
C
chenguowei01 已提交
47 48 49 50 51 52
    parser.add_argument(
        '--dataset_root',
        dest='dataset_root',
        help="dataset root directory",
        type=str,
        default=None)
53 54

    # params of prediction
C
chenguowei01 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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(
        '--batch_size',
        dest='batch_size',
        help='Mini batch size',
        type=int,
        default=2)
    parser.add_argument(
        '--model_dir',
        dest='model_dir',
        help='The path of model for evaluation',
        type=str,
        default=None)
    parser.add_argument(
        '--save_dir',
        dest='save_dir',
        help='The directory for saving the inference results',
        type=str,
        default='./output/result')
80 81 82 83 84

    return parser.parse_args()


def main(args):
C
chenguowei01 已提交
85 86
    env_info = get_environ_info()
    places = fluid.CUDAPlace(ParallelEnv().dev_id) \
C
chenguowei01 已提交
87
        if env_info['Paddle compiled with cuda'] and env_info['GPUs used'] \
C
chenguowei01 已提交
88
        else fluid.CPUPlace()
C
chenguowei01 已提交
89

C
chenguowei01 已提交
90
    if args.dataset not in DATASETS:
C
chenguowei01 已提交
91
        raise Exception('`--dataset` is invalid. it should be one of {}'.format(
C
chenguowei01 已提交
92 93
            str(list(DATASETS.keys()))))
    dataset = DATASETS[args.dataset]
C
chenguowei01 已提交
94

C
chenguowei01 已提交
95 96
    with fluid.dygraph.guard(places):
        test_transforms = T.Compose([T.Resize(args.input_size), T.Normalize()])
C
chenguowei01 已提交
97 98 99 100
        test_dataset = dataset(
            dataset_root=args.dataset_root,
            transforms=test_transforms,
            mode='test')
101

C
chenguowei01 已提交
102 103
        if args.model_name not in MODELS:
            raise Exception(
C
chenguowei01 已提交
104
                '`--model_name` is invalid. it should be one of {}'.format(
C
chenguowei01 已提交
105 106
                    str(list(MODELS.keys()))))
        model = MODELS[args.model_name](num_classes=test_dataset.num_classes)
107

C
chenguowei01 已提交
108 109 110 111 112
        infer(
            model,
            model_dir=args.model_dir,
            test_dataset=test_dataset,
            save_dir=args.save_dir)
113 114 115 116


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