train.py 3.9 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
#
# 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

C
chenguowei01 已提交
17 18
import paddle
from paddle.distributed import ParallelEnv
C
chenguowei01 已提交
19

M
michaelowenliu 已提交
20 21 22 23
import paddleseg
from paddleseg.cvlibs import manager
from paddleseg.utils import get_environ_info, logger, Config
from paddleseg.core import train
C
chenguowei01 已提交
24 25 26 27 28


def parse_args():
    parser = argparse.ArgumentParser(description='Model training')
    # params of training
C
chenguowei01 已提交
29
    parser.add_argument(
W
wuzewu 已提交
30
        "--config", dest="cfg", help="The config file.", default=None, type=str)
C
chenguowei01 已提交
31
    parser.add_argument(
C
chenguowei01 已提交
32 33 34
        '--iters',
        dest='iters',
        help='iters for training',
C
chenguowei01 已提交
35
        type=int,
W
wuzewu 已提交
36
        default=None)
C
chenguowei01 已提交
37 38 39
    parser.add_argument(
        '--batch_size',
        dest='batch_size',
C
chenguowei01 已提交
40
        help='Mini batch size of one gpu or cpu',
C
chenguowei01 已提交
41
        type=int,
W
wuzewu 已提交
42
        default=None)
C
chenguowei01 已提交
43 44 45 46 47 48 49
    parser.add_argument(
        '--learning_rate',
        dest='learning_rate',
        help='Learning rate',
        type=float,
        default=None)
    parser.add_argument(
C
chenguowei01 已提交
50 51 52
        '--save_interval_iters',
        dest='save_interval_iters',
        help='The interval iters for save a model snapshot',
C
chenguowei01 已提交
53
        type=int,
C
chenguowei01 已提交
54
        default=1000)
C
chenguowei01 已提交
55 56 57 58 59 60 61 62 63 64 65 66
    parser.add_argument(
        '--save_dir',
        dest='save_dir',
        help='The directory for saving the model snapshot',
        type=str,
        default='./output')
    parser.add_argument(
        '--num_workers',
        dest='num_workers',
        help='Num workers for data loader',
        type=int,
        default=0)
C
chenguowei01 已提交
67 68 69 70 71
    parser.add_argument(
        '--do_eval',
        dest='do_eval',
        help='Eval while training',
        action='store_true')
C
chenguowei01 已提交
72
    parser.add_argument(
C
chenguowei01 已提交
73 74 75
        '--log_iters',
        dest='log_iters',
        help='Display logging information at every log_iters',
C
chenguowei01 已提交
76 77
        default=10,
        type=int)
C
add vdl  
chenguowei01 已提交
78 79 80
    parser.add_argument(
        '--use_vdl',
        dest='use_vdl',
C
chenguowei01 已提交
81
        help='Whether to record the data to VisualDL during training',
C
add vdl  
chenguowei01 已提交
82
        action='store_true')
C
chenguowei01 已提交
83 84 85 86 87

    return parser.parse_args()


def main(args):
C
chenguowei01 已提交
88
    env_info = get_environ_info()
C
chenguowei01 已提交
89 90 91 92 93
    info = ['{}: {}'.format(k, v) for k, v in env_info.items()]
    info = '\n'.join(['\n', format('Environment Information', '-^48s')] + info +
                     ['-' * 48])
    logger.info(info)

C
chenguowei01 已提交
94
    places = paddle.CUDAPlace(ParallelEnv().dev_id) \
C
chenguowei01 已提交
95
        if env_info['Paddle compiled with cuda'] and env_info['GPUs used'] \
C
chenguowei01 已提交
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
        else paddle.CPUPlace()

    paddle.disable_static(places)
    if not args.cfg:
        raise RuntimeError('No configuration file specified.')

    cfg = Config(args.cfg)
    train_dataset = cfg.train_dataset
    if not train_dataset:
        raise RuntimeError(
            'The training dataset is not specified in the configuration file.')

    val_dataset = cfg.val_dataset if args.do_eval else None

    losses = cfg.loss

    train(
        cfg.model,
        train_dataset,
        places=places,
        eval_dataset=val_dataset,
        optimizer=cfg.optimizer,
        save_dir=args.save_dir,
        iters=cfg.iters,
        batch_size=cfg.batch_size,
        save_interval_iters=args.save_interval_iters,
        log_iters=args.log_iters,
        num_classes=train_dataset.num_classes,
        num_workers=args.num_workers,
        use_vdl=args.use_vdl,
        losses=losses,
        ignore_index=losses['types'][0].ignore_index)
C
chenguowei01 已提交
128 129 130 131


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