train.py 4.1 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
import paddleseg
M
michaelowenliu 已提交
21
from paddleseg.cvlibs import manager, Config
C
chenguowei01 已提交
22
from paddleseg.utils import get_environ_info, logger
M
michaelowenliu 已提交
23
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
    info = ['{}: {}'.format(k, v) for k, v in env_info.items()]
C
chenguowei01 已提交
90
    info = '\n'.join(['', format('Environment Information', '-^48s')] + info +
C
chenguowei01 已提交
91 92 93
                     ['-' * 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
        else paddle.CPUPlace()

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

    cfg = Config(args.cfg)
W
wuzewu 已提交
103 104 105 106 107
    cfg.update(
        learning_rate=args.learning_rate,
        iters=args.iters,
        batch_size=args.batch_size)

C
chenguowei01 已提交
108 109 110 111 112 113 114
    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

W
wuzewu 已提交
115 116 117 118
    msg = '\n---------------Config Information---------------\n'
    msg += str(cfg)
    msg += '------------------------------------------------'
    logger.info(msg)
W
wuzewu 已提交
119

C
chenguowei01 已提交
120 121 122 123
    train(
        cfg.model,
        train_dataset,
        places=places,
124
        val_dataset=val_dataset,
C
chenguowei01 已提交
125 126 127 128 129 130 131 132 133 134 135
        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 已提交
136 137 138 139


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