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 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

W
wuzewu 已提交
20
import dygraph
21
from dygraph.cvlibs import manager
C
chenguowei01 已提交
22
from dygraph.utils import get_environ_info
C
chenguowei01 已提交
23
from dygraph.utils import logger
W
wuzewu 已提交
24
from dygraph.utils import Config
C
chenguowei01 已提交
25
from dygraph.core import train
C
chenguowei01 已提交
26 27 28 29 30


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

    return parser.parse_args()


def main(args):
C
chenguowei01 已提交
90
    env_info = get_environ_info()
C
chenguowei01 已提交
91 92 93 94 95
    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 已提交
96
    places = fluid.CUDAPlace(ParallelEnv().dev_id) \
C
chenguowei01 已提交
97
        if env_info['Paddle compiled with cuda'] and env_info['GPUs used'] \
C
chenguowei01 已提交
98 99
        else fluid.CPUPlace()

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

W
wuzewu 已提交
104 105 106 107 108 109
        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 已提交
110

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

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 126
            num_workers=args.num_workers,
            use_vdl=args.use_vdl)
C
chenguowei01 已提交
127 128 129 130


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