config.py 4.6 KB
Newer Older
L
lvmengsi 已提交
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
#copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#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 sys
import six
import argparse
import functools
import distutils.util
import trainer


def print_arguments(args):
    ''' Print argparse's argument
    Usage:

    .. code-block:: python

        parser = argparse.ArgumentParser()
        parser.add_argument("name", default="Jonh", type=str, help="User name.")
        args = parser.parse_args()
        print_arguments(args)

    :param args: Input argparse.Namespace for printing.
    :type args: argparse.Namespace
    '''
    print("-----------  Configuration Arguments -----------")
    for arg, value in sorted(six.iteritems(vars(args))):
        print("%s: %s" % (arg, value))
    print("------------------------------------------------")


def add_arguments(argname, type, default, help, argparser, **kwargs):
    """Add argparse's argument.

    Usage:

    .. code-block:: python

        parser = argparse.ArgumentParser()
        add_argument("name", str, "Jonh", "User name.", parser)
        args = parser.parse_args()
    """
    type = distutils.util.strtobool if type == bool else type
    argparser.add_argument(
        "--" + argname,
        default=default,
        type=type,
        help=help + ' Default: %(default)s.',
        **kwargs)


def base_parse_args(parser):
    add_arg = functools.partial(add_arguments, argparser=parser)
    # yapf: disable
L
lvmengsi 已提交
71
    add_arg('model_net', str, "CGAN", "The model used.")
L
lvmengsi 已提交
72 73
    add_arg('dataset', str, "mnist", "The dataset used.")
    add_arg('data_dir', str, "./data", "The dataset root directory")
L
lvmengsi 已提交
74 75
    add_arg('train_list', str, None, "The train list file name")
    add_arg('test_list', str, None, "The test list file name")
L
lvmengsi 已提交
76 77
    add_arg('batch_size', int, 1, "Minibatch size.")
    add_arg('epoch', int, 200, "The number of epoch to be trained.")
L
lvmengsi 已提交
78 79
    add_arg('g_base_dims', int, 64, "Base channels in generator")
    add_arg('d_base_dims', int, 64, "Base channels in discriminator")
L
lvmengsi 已提交
80
    add_arg('image_size', int, 286, "the image size when load the image")
L
lvmengsi 已提交
81 82 83 84 85 86 87
    add_arg('crop_type', str, 'Centor',
            "the crop type, choose = ['Centor', 'Random']")
    add_arg('crop_size', int, 256, "crop size when preprocess image")
    add_arg('save_checkpoints', bool, True, "Whether to save checkpoints.")
    add_arg('run_test', bool, True, "Whether to run test.")
    add_arg('use_gpu', bool, True, "Whether to use GPU to train.")
    add_arg('profile', bool, False, "Whether to profile.")
H
hysunflower 已提交
88 89 90 91 92

    # NOTE: add args for profiler, used for benchmark
    add_arg('profiler_path', str, '/tmp/profile', "the  profiler output files. (used for benchmark)")
    add_arg('max_iter', int, 0, "the max iter to train. (used for benchmark)")

L
lvmengsi 已提交
93 94 95 96 97 98 99
    add_arg('dropout', bool, False, "Whether to use drouput.")
    add_arg('drop_last', bool, False,
            "Whether to drop the last images that cannot form a batch")
    add_arg('shuffle', bool, True, "Whether to shuffle data")
    add_arg('output', str, "./output",
            "The directory the model and the test result to be saved to.")
    add_arg('init_model', str, None, "The init model file of directory.")
Z
zhumanyu 已提交
100
    add_arg('gan_mode', str, "vanilla", "The init model file of directory.")
L
lvmengsi 已提交
101
    add_arg('norm_type', str, "batch_norm", "Which normalization to used")
Z
zhumanyu 已提交
102
    add_arg('learning_rate', float, 0.0002, "the initialize learning rate")
L
lvmengsi 已提交
103
    add_arg('lambda_L1', float, 100.0, "the initialize lambda parameter for L1 loss")
L
lvmengsi 已提交
104 105
    add_arg('num_generator_time', int, 1,
            "the generator run times in training each epoch")
L
lvmengsi 已提交
106 107
    add_arg('num_discriminator_time', int, 1,
            "the discriminator run times in training each epoch")
L
lvmengsi 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    add_arg('print_freq', int, 10, "the frequency of print loss")
    # yapf: enable

    return parser


def parse_args():
    parser = argparse.ArgumentParser(description=__doc__)
    parser = base_parse_args(parser)
    cfg, _ = parser.parse_known_args()
    model_name = cfg.model_net
    model_cfg = trainer.get_special_cfg(model_name)
    parser = model_cfg(parser)
    args = parser.parse_args()
    return args