options.py 8.4 KB
Newer Older
E
Exception-star 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
# -*- coding: utf-8 -*-
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import argparse


class Options(object):
    def __init__(self):
        parser = argparse.ArgumentParser(description='Paddle DANet Segmentation')

        # model and dataset
        parser.add_argument('--model', type=str, default='danet',
                            help='model name (default: danet)')
        parser.add_argument('--backbone', type=str, default='resnet101',
                            help='backbone name (default: resnet101)')
        parser.add_argument('--dataset', type=str, default='cityscapes',
                            help='dataset name (default: cityscapes)')
        parser.add_argument('--num_classes', type=int, default=19,
                            help='num_classes (default: cityscapes = 19)')
        parser.add_argument('--data_folder', type=str,
                            default='./dataset',
                            help='training dataset folder (default: ./dataset')
        parser.add_argument('--base_size', type=int, default=1024,
                            help='base image size')
        parser.add_argument('--crop_size', type=int, default=768,
                            help='crop image size')

        # training hyper params
        parser.add_argument('--epoch_num', type=int, default=None, metavar='N',
                            help='number of epochs to train (default: auto)')
        parser.add_argument('--start_epoch', type=int, default=0,
                            metavar='N', help='start epochs (default:0)')
        parser.add_argument('--batch_size', type=int, default=None,
                            metavar='N', help='input batch size for \
                            training (default: auto)')
        parser.add_argument('--test_batch_size', type=int, default=None,
                            metavar='N', help='input batch size for \
                            testing (default: same as batch size)')

        # optimizer params
        parser.add_argument('--lr', type=float, default=None, metavar='LR',
                            help='learning rate (default: auto)')
        parser.add_argument('--lr_scheduler', type=str, default='poly',
                            help='learning rate scheduler (default: poly)')
        parser.add_argument('--lr_pow', type=float, default=0.9,
                            help='learning rate scheduler (default: 0.9)')
        parser.add_argument('--lr_step', type=int, default=None,
                            help='lr step to change lr')
        parser.add_argument('--warm_up', action='store_true', default=False,
                            help='warm_up (default: False)')
        parser.add_argument('--warmup_epoch', type=int, default=5,
                            help='warmup_epoch (default: 5)')
        parser.add_argument('--total_step', type=int, default=None,
                            metavar='N', help='total_step (default: auto)')
        parser.add_argument('--step_per_epoch', type=int, default=None,
                            metavar='N', help='step_per_epoch (default: auto)')
        parser.add_argument('--momentum', type=float, default=0.9,
                            metavar='M', help='momentum (default: 0.9)')
        parser.add_argument('--weight_decay', type=float, default=1e-4,
                            metavar='M', help='w-decay (default: 1e-4)')

        # cuda, seed and logging
        parser.add_argument('--cuda', action='store_true', default=False,
                            help='use CUDA training, (default: False)')
        parser.add_argument('--use_data_parallel', action='store_true', default=False,
                            help='use data_parallel training, (default: False)')
        parser.add_argument('--seed', type=int, default=1, metavar='S',
                            help='random seed (default: 1)')
        parser.add_argument('--log_root', type=str,
                            default='./', help='set a log path folder')

        # checkpoint
        parser.add_argument("--save_model", default='checkpoint/DANet101_better_model_paddle1.6', type=str,
                            help="model path, (default: checkpoint/DANet101_better_model_paddle1.6)")

        # change executor model params to dygraph model params
        parser.add_argument("--change_executor_to_dygraph", action='store_true', default=False,
                            help="change executor model params to dygraph model params (default:False)")

        # finetuning pre-trained models
        parser.add_argument("--load_pretrained_model", action='store_true', default=False,
                            help="load pretrained model (default: False)")
        # load better models
        parser.add_argument("--load_better_model", action='store_true', default=False,
                            help="load better model (default: False)")
        parser.add_argument('--multi_scales', action='store_true', default=False,
                            help="testing scale, (default: False)")
        parser.add_argument('--flip', action='store_true', default=False,
                            help="testing flip image, (default: False)")

        # multi grid dilation option
        parser.add_argument("--dilated", action='store_true', default=False,
                            help="use dilation policy, (default: False)")
        parser.add_argument("--multi_grid", action='store_true', default=False,
                            help="use multi grid dilation policy, default: False")
        parser.add_argument('--multi_dilation', nargs='+', type=int, default=None,
                            help="multi grid dilation list, (default: None), can use --mutil_dilation 4 8 16")
        parser.add_argument('--scale', action='store_true', default=False,
                            help='choose to use random scale transform(0.75-2.0) for train, (default: False)')

        # the parser
        self.parser = parser

    def parse(self):
        args = self.parser.parse_args()
        # default settings for epochs, batch_size and lr
        if args.epoch_num is None:
            epoches = {
                'pascal_voc': 180,
                'pascal_aug': 180,
                'pcontext': 180,
                'ade20k': 180,
                'cityscapes': 350,
            }
            num_class_dict = {
                'pascal_voc': 21,
                'pascal_aug': 21,
                'pcontext': 21,
                'ade20k': None,
                'cityscapes': 19,
            }
            total_steps = {
                'pascal_voc': 200000,
                'pascal_aug': 500000,
                'pcontext': 500000,
                'ade20k': 500000,
                'cityscapes': 150000,
            }
            args.epoch_num = epoches[args.dataset.lower()]
            args.num_classes = num_class_dict[args.dataset.lower()]
            args.total_step = total_steps[args.dataset.lower()]
        if args.batch_size is None:
            args.batch_size = 2
        if args.test_batch_size is None:
            args.test_batch_size = args.batch_size
        if args.step_per_epoch is None:
            step_per_epoch = {
                'pascal_voc': 185,
                'pascal_aug': 185,
                'pcontext': 185,
                'ade20k': 185,
                'cityscapes': 371,  # 2975 // batch_size // GPU_num
            }
            args.step_per_epoch = step_per_epoch[args.dataset.lower()]
        if args.lr is None:
            lrs = {
                'pascal_voc': 0.0001,
                'pascal_aug': 0.001,
                'pcontext': 0.001,
                'ade20k': 0.01,
                'cityscapes': 0.003,
            }
            args.lr = lrs[args.dataset.lower()] / 8 * args.batch_size
        return args

    def print_args(self):
        arg_dict = self.parse().__dict__
        for k, v in arg_dict.items():
            print('{:30s}: {}'.format(k, v))