train.py 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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 os

import paddle.fluid as fluid
from paddle.fluid.dygraph.parallel import ParallelEnv
from paddle.fluid.io import DataLoader
C
chenguowei01 已提交
20 21
# from paddle.incubate.hapi.distributed import DistributedBatchSampler
from paddle.io import DistributedBatchSampler
22

C
chenguowei01 已提交
23
import dygraph.utils.logger as logger
C
chenguowei01 已提交
24 25 26
from dygraph.utils import load_pretrained_model
from dygraph.utils import resume
from dygraph.utils import Timer, calculate_eta
27 28 29 30 31 32 33 34 35
from .val import evaluate


def train(model,
          train_dataset,
          places=None,
          eval_dataset=None,
          optimizer=None,
          save_dir='output',
C
chenguowei01 已提交
36
          iters=10000,
37 38
          batch_size=2,
          resume_model=None,
C
chenguowei01 已提交
39 40
          save_interval_iters=1000,
          log_iters=10,
41 42 43 44 45 46
          num_classes=None,
          num_workers=8,
          use_vdl=False):
    ignore_index = model.ignore_index
    nranks = ParallelEnv().nranks

C
chenguowei01 已提交
47
    start_iter = 0
48
    if resume_model is not None:
C
chenguowei01 已提交
49
        start_iter = resume(model, optimizer, resume_model)
50 51 52 53 54 55 56 57

    if not os.path.isdir(save_dir):
        if os.path.exists(save_dir):
            os.remove(save_dir)
        os.makedirs(save_dir)

    if nranks > 1:
        strategy = fluid.dygraph.prepare_context()
C
chenguowei01 已提交
58
        ddp_model = fluid.dygraph.DataParallel(model, strategy)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    batch_sampler = DistributedBatchSampler(
        train_dataset, batch_size=batch_size, shuffle=True, drop_last=True)
    loader = DataLoader(
        train_dataset,
        batch_sampler=batch_sampler,
        places=places,
        num_workers=num_workers,
        return_list=True,
    )

    if use_vdl:
        from visualdl import LogWriter
        log_writer = LogWriter(save_dir)

    timer = Timer()
    avg_loss = 0.0
C
chenguowei01 已提交
76
    iters_per_epoch = len(batch_sampler)
77
    best_mean_iou = -1.0
C
chenguowei01 已提交
78
    best_model_iter = -1
79 80
    train_reader_cost = 0.0
    train_batch_cost = 0.0
C
chenguowei01 已提交
81 82
    timer.start()

83
    iter = start_iter
C
chenguowei01 已提交
84 85 86 87 88
    while iter < iters:
        for data in loader:
            iter += 1
            if iter > iters:
                break
89
            train_reader_cost += timer.elapsed_time()
90 91 92
            images = data[0]
            labels = data[1].astype('int64')
            if nranks > 1:
C
chenguowei01 已提交
93
                loss = ddp_model(images, labels)
C
chenguowei01 已提交
94
                # apply_collective_grads sum grads over multiple gpus.
C
chenguowei01 已提交
95
                loss = ddp_model.scale_loss(loss)
96
                loss.backward()
C
chenguowei01 已提交
97
                ddp_model.apply_collective_grads()
98 99 100 101 102 103 104
            else:
                loss = model(images, labels)
                loss.backward()
            optimizer.minimize(loss)
            model.clear_gradients()
            avg_loss += loss.numpy()[0]
            lr = optimizer.current_step_lr()
105
            train_batch_cost += timer.elapsed_time()
C
chenguowei01 已提交
106 107 108 109
            if (iter) % log_iters == 0 and ParallelEnv().local_rank == 0:
                avg_loss /= log_iters
                avg_train_reader_cost = train_reader_cost / log_iters
                avg_train_batch_cost = train_batch_cost / log_iters
110 111
                train_reader_cost = 0.0
                train_batch_cost = 0.0
C
chenguowei01 已提交
112 113
                remain_iters = iters - iter
                eta = calculate_eta(remain_iters, avg_train_batch_cost)
C
chenguowei01 已提交
114
                logger.info(
C
chenguowei01 已提交
115 116
                    "[TRAIN] epoch={}, iter={}/{}, loss={:.4f}, lr={:.6f}, batch_cost={:.4f}, reader_cost={:.4f} | ETA {}"
                    .format((iter - 1) // iters_per_epoch + 1, iter, iters,
117 118
                            avg_loss * nranks, lr, avg_train_batch_cost,
                            avg_train_reader_cost, eta))
119
                if use_vdl:
C
chenguowei01 已提交
120 121
                    log_writer.add_scalar('Train/loss', avg_loss * nranks, iter)
                    log_writer.add_scalar('Train/lr', lr, iter)
122
                    log_writer.add_scalar('Train/batch_cost',
C
chenguowei01 已提交
123
                                          avg_train_batch_cost, iter)
124
                    log_writer.add_scalar('Train/reader_cost',
C
chenguowei01 已提交
125
                                          avg_train_reader_cost, iter)
126 127
                avg_loss = 0.0

C
chenguowei01 已提交
128 129 130 131 132 133 134 135 136 137
            if (iter % save_interval_iters == 0
                    or iter == iters) and ParallelEnv().local_rank == 0:
                current_save_dir = os.path.join(save_dir,
                                                "iter_{}".format(iter))
                if not os.path.isdir(current_save_dir):
                    os.makedirs(current_save_dir)
                fluid.save_dygraph(model.state_dict(),
                                   os.path.join(current_save_dir, 'model'))
                fluid.save_dygraph(optimizer.state_dict(),
                                   os.path.join(current_save_dir, 'model'))
138

C
chenguowei01 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
                if eval_dataset is not None:
                    mean_iou, avg_acc = evaluate(
                        model,
                        eval_dataset,
                        model_dir=current_save_dir,
                        num_classes=num_classes,
                        ignore_index=ignore_index,
                        iter_id=iter)
                    if mean_iou > best_mean_iou:
                        best_mean_iou = mean_iou
                        best_model_iter = iter
                        best_model_dir = os.path.join(save_dir, "best_model")
                        fluid.save_dygraph(
                            model.state_dict(),
                            os.path.join(best_model_dir, 'model'))
                    logger.info(
                        'Current evaluated best model in eval_dataset is iter_{}, miou={:4f}'
                        .format(best_model_iter, best_mean_iou))
157

C
chenguowei01 已提交
158 159 160 161
                    if use_vdl:
                        log_writer.add_scalar('Evaluate/mIoU', mean_iou, iter)
                        log_writer.add_scalar('Evaluate/aAcc', avg_acc, iter)
                    model.train()
162
            timer.restart()
163 164
    if use_vdl:
        log_writer.close()