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

C
chenguowei01 已提交
98
    with fluid.dygraph.guard(places):
W
wuzewu 已提交
99 100
        if not args.cfg:
            raise RuntimeError('No configuration file specified.')
C
chenguowei01 已提交
101

W
wuzewu 已提交
102 103 104 105 106 107
        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.'
            )
C
chenguowei01 已提交
108

W
wuzewu 已提交
109
        val_dataset = cfg.val_dataset if args.do_eval else None
C
chenguowei01 已提交
110

C
chenguowei01 已提交
111 112
        losses = cfg.loss

C
chenguowei01 已提交
113
        train(
W
wuzewu 已提交
114
            cfg.model,
C
chenguowei01 已提交
115 116
            train_dataset,
            places=places,
W
wuzewu 已提交
117 118
            eval_dataset=val_dataset,
            optimizer=cfg.optimizer,
C
chenguowei01 已提交
119
            save_dir=args.save_dir,
W
wuzewu 已提交
120 121
            iters=cfg.iters,
            batch_size=cfg.batch_size,
C
chenguowei01 已提交
122 123
            save_interval_iters=args.save_interval_iters,
            log_iters=args.log_iters,
C
chenguowei01 已提交
124
            num_classes=train_dataset.num_classes,
C
add vdl  
chenguowei01 已提交
125
            num_workers=args.num_workers,
C
chenguowei01 已提交
126
            use_vdl=args.use_vdl,
C
chenguowei01 已提交
127 128
            losses=losses,
            ignore_index=losses['types'][0].ignore_index)
C
chenguowei01 已提交
129 130 131 132


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