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

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
J
jerrywgz 已提交
18
import os
19
import sys
J
jerrywgz 已提交
20
import numpy as np
21
import time
J
jerrywgz 已提交
22
import shutil
J
jerrywgz 已提交
23
from utility import parse_args, print_arguments, SmoothedValue
J
jerrywgz 已提交
24 25 26 27

import paddle
import paddle.fluid as fluid
import reader
28 29
import models.model_builder as model_builder
import models.resnet as resnet
J
jerrywgz 已提交
30
from learning_rate import exponential_with_warmup_decay
J
jerrywgz 已提交
31
from config import cfg
J
jerrywgz 已提交
32 33


J
jerrywgz 已提交
34
def train():
35
    learning_rate = cfg.learning_rate
J
jerrywgz 已提交
36
    image_shape = [3, cfg.TRAIN.max_size, cfg.TRAIN.max_size]
J
jerrywgz 已提交
37

Z
zhengya01 已提交
38
    if cfg.debug or cfg.enable_ce:
J
jerrywgz 已提交
39 40 41 42 43 44 45 46
        fluid.default_startup_program().random_seed = 1000
        fluid.default_main_program().random_seed = 1000
        import random
        random.seed(0)
        np.random.seed(0)

    devices = os.getenv("CUDA_VISIBLE_DEVICES") or ""
    devices_num = len(devices.split(","))
J
jerrywgz 已提交
47
    total_batch_size = devices_num * cfg.TRAIN.im_per_batch
J
jerrywgz 已提交
48

Z
add ce  
zhengya01 已提交
49 50 51
    use_random = True
    if cfg.enable_ce:
        use_random = False
52 53 54 55
    model = model_builder.FasterRCNN(
        add_conv_body_func=resnet.add_ResNet50_conv4_body,
        add_roi_box_head_func=resnet.add_ResNet_roi_conv5_head,
        use_pyreader=cfg.use_pyreader,
Z
add ce  
zhengya01 已提交
56
        use_random=use_random)
57 58
    model.build_model(image_shape)
    loss_cls, loss_bbox, rpn_cls_loss, rpn_reg_loss = model.loss()
J
jerrywgz 已提交
59 60 61 62
    loss_cls.persistable = True
    loss_bbox.persistable = True
    rpn_cls_loss.persistable = True
    rpn_reg_loss.persistable = True
63
    loss = loss_cls + loss_bbox + rpn_cls_loss + rpn_reg_loss
J
jerrywgz 已提交
64

J
jerrywgz 已提交
65 66
    boundaries = cfg.lr_steps
    gamma = cfg.lr_gamma
67
    step_num = len(cfg.lr_steps)
J
jerrywgz 已提交
68
    values = [learning_rate * (gamma**i) for i in range(step_num + 1)]
J
jerrywgz 已提交
69 70

    optimizer = fluid.optimizer.Momentum(
J
jerrywgz 已提交
71 72
        learning_rate=exponential_with_warmup_decay(
            learning_rate=learning_rate,
J
jerrywgz 已提交
73 74
            boundaries=boundaries,
            values=values,
J
jerrywgz 已提交
75 76 77 78
            warmup_iter=cfg.warm_up_iter,
            warmup_factor=cfg.warm_up_factor),
        regularization=fluid.regularizer.L2Decay(cfg.weight_decay),
        momentum=cfg.momentum)
J
jerrywgz 已提交
79 80 81 82
    optimizer.minimize(loss)

    fluid.memory_optimize(fluid.default_main_program())

83
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
J
jerrywgz 已提交
84 85 86
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

87
    if cfg.pretrained_model:
J
jerrywgz 已提交
88

J
jerrywgz 已提交
89
        def if_exist(var):
90
            return os.path.exists(os.path.join(cfg.pretrained_model, var.name))
J
jerrywgz 已提交
91

J
jerrywgz 已提交
92
        fluid.io.load_vars(exe, cfg.pretrained_model, predicate=if_exist)
J
jerrywgz 已提交
93

94
    if cfg.parallel:
J
jerrywgz 已提交
95
        train_exe = fluid.ParallelExecutor(
96 97
            use_cuda=bool(cfg.use_gpu), loss_name=loss.name)

Z
add ce  
zhengya01 已提交
98 99 100
    shuffle = True
    if cfg.enable_ce:
        shuffle = False
101
    if cfg.use_pyreader:
J
jerrywgz 已提交
102
        train_reader = reader.train(
J
jerrywgz 已提交
103 104 105
            batch_size=cfg.TRAIN.im_per_batch,
            total_batch_size=total_batch_size,
            padding_total=cfg.TRAIN.padding_minibatch,
Z
add ce  
zhengya01 已提交
106
            shuffle=shuffle)
107 108 109
        py_reader = model.py_reader
        py_reader.decorate_paddle_reader(train_reader)
    else:
Z
add ce  
zhengya01 已提交
110
        train_reader = reader.train(batch_size=total_batch_size, shuffle=shuffle)
111
        feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())
J
jerrywgz 已提交
112 113

    def save_model(postfix):
114
        model_path = os.path.join(cfg.model_save_dir, postfix)
J
jerrywgz 已提交
115 116 117 118
        if os.path.isdir(model_path):
            shutil.rmtree(model_path)
        fluid.io.save_persistables(exe, model_path)

119
    fetch_list = [loss, rpn_cls_loss, rpn_reg_loss, loss_cls, loss_bbox]
J
jerrywgz 已提交
120

J
jerrywgz 已提交
121
    def train_loop_pyreader():
122
        py_reader.start()
123
        smoothed_loss = SmoothedValue(cfg.log_window)
124 125 126
        try:
            start_time = time.time()
            prev_start_time = start_time
Z
zhengya01 已提交
127 128
            total_time = 0
            last_loss = 0
129
            every_pass_loss = []
130
            for iter_id in range(cfg.max_iter):
131 132 133 134
                prev_start_time = start_time
                start_time = time.time()
                losses = train_exe.run(fetch_list=[v.name for v in fetch_list])
                every_pass_loss.append(np.mean(np.array(losses[0])))
135
                smoothed_loss.add_value(np.mean(np.array(losses[0])))
J
jerrywgz 已提交
136 137
                lr = np.array(fluid.global_scope().find_var('learning_rate')
                              .get_tensor())
138
                print("Iter {:d}, lr {:.6f}, loss {:.6f}, time {:.5f}".format(
J
jerrywgz 已提交
139 140 141
                    iter_id, lr[0],
                    smoothed_loss.get_median_value(
                    ), start_time - prev_start_time))
Z
zhengya01 已提交
142 143 144 145
                end_time = time.time()
                total_time += end_time - start_time
                last_loss = np.mean(np.array(losses[0]))

146
                sys.stdout.flush()
J
jerrywgz 已提交
147
                if (iter_id + 1) % cfg.TRAIN.snapshot_iter == 0:
148
                    save_model("model_iter{}".format(iter_id))
Z
zhengya01 已提交
149 150
            # only for ce
            if cfg.enable_ce:
Z
add ce  
zhengya01 已提交
151
                gpu_num = devices_num
Z
zhengya01 已提交
152 153 154 155 156 157 158
                epoch_idx = iter_id + 1
                loss = last_loss
                print("kpis\teach_pass_duration_card%s\t%s" %
                        (gpu_num, total_time / epoch_idx))
                print("kpis\ttrain_loss_card%s\t%s" %
                        (gpu_num, loss))

159 160 161
        except fluid.core.EOFException:
            py_reader.reset()
        return np.mean(every_pass_loss)
J
jerrywgz 已提交
162

J
jerrywgz 已提交
163
    def train_loop():
J
jerrywgz 已提交
164 165
        start_time = time.time()
        prev_start_time = start_time
J
jerrywgz 已提交
166
        start = start_time
Z
zhengya01 已提交
167 168
        total_time = 0
        last_loss = 0
J
jerrywgz 已提交
169
        every_pass_loss = []
170 171
        smoothed_loss = SmoothedValue(cfg.log_window)
        for iter_id, data in enumerate(train_reader()):
J
jerrywgz 已提交
172 173
            prev_start_time = start_time
            start_time = time.time()
174 175
            losses = train_exe.run(fetch_list=[v.name for v in fetch_list],
                                   feed=feeder.feed(data))
J
jerrywgz 已提交
176 177
            loss_v = np.mean(np.array(losses[0]))
            every_pass_loss.append(loss_v)
178
            smoothed_loss.add_value(loss_v)
J
jerrywgz 已提交
179 180
            lr = np.array(fluid.global_scope().find_var('learning_rate')
                          .get_tensor())
Z
zhengya01 已提交
181 182 183
            end_time = time.time()
            total_time += end_time - start_time
            last_loss = loss_v
184
            print("Iter {:d}, lr {:.6f}, loss {:.6f}, time {:.5f}".format(
J
jerrywgz 已提交
185 186
                iter_id, lr[0],
                smoothed_loss.get_median_value(), start_time - prev_start_time))
187
            sys.stdout.flush()
J
jerrywgz 已提交
188
            if (iter_id + 1) % cfg.TRAIN.snapshot_iter == 0:
189 190 191
                save_model("model_iter{}".format(iter_id))
            if (iter_id + 1) == cfg.max_iter:
                break
Z
zhengya01 已提交
192 193
        # only for ce
        if cfg.enable_ce:
Z
add ce  
zhengya01 已提交
194
            gpu_num = devices_num
Z
zhengya01 已提交
195 196 197 198 199 200 201
            epoch_idx = iter_id + 1
            loss = last_loss
            print("kpis\teach_pass_duration_card%s\t%s" %
                    (gpu_num, total_time / epoch_idx))
            print("kpis\ttrain_loss_card%s\t%s" %
                    (gpu_num, loss))

202
        return np.mean(every_pass_loss)
J
jerrywgz 已提交
203

204
    if cfg.use_pyreader:
J
jerrywgz 已提交
205
        train_loop_pyreader()
206
    else:
J
jerrywgz 已提交
207
        train_loop()
208
    save_model('model_final')
J
jerrywgz 已提交
209

J
jerrywgz 已提交
210

J
jerrywgz 已提交
211
if __name__ == '__main__':
J
jerrywgz 已提交
212
    args = parse_args()
J
jerrywgz 已提交
213
    print_arguments(args)
J
jerrywgz 已提交
214
    train()