train.py 13.8 KB
Newer Older
1
import os
X
Xingyuan Bu 已提交
2
import time
3 4 5
import numpy as np
import argparse
import functools
D
Dang Qingqing 已提交
6
import shutil
7

D
Dang Qingqing 已提交
8 9 10 11 12 13
import paddle
import paddle.fluid as fluid
import reader
from mobilenet_ssd import mobile_net
from utility import add_arguments, print_arguments

14 15
parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
16 17 18
# yapf: disable
add_arg('learning_rate',    float, 0.001,     "Learning rate.")
add_arg('batch_size',       int,   32,        "Minibatch size.")
D
Dang Qingqing 已提交
19
add_arg('num_passes',       int,   120,       "Epoch number.")
20
add_arg('use_gpu',          bool,  True,      "Whether use GPU.")
X
Xingyuan Bu 已提交
21 22
add_arg('parallel',         bool,  True,      "Parallel.")
add_arg('use_nccl',         bool,  True,      "NCCL.")
23 24 25 26 27 28 29 30 31 32 33 34 35 36
add_arg('dataset',          str,   'pascalvoc', "coco2014, coco2017, and pascalvoc.")
add_arg('model_save_dir',   str,   'model',     "The path to save model.")
add_arg('pretrained_model', str,   'pretrained/ssd_mobilenet_v1_coco/', "The init model path.")
add_arg('apply_distort',    bool,  True,   "Whether apply distort.")
add_arg('apply_expand',     bool,  False,  "Whether appley expand.")
add_arg('nms_threshold',    float, 0.45,   "NMS threshold.")
add_arg('ap_version',       str,   'integral',   "integral, 11point.")
add_arg('resize_h',         int,   300,    "The resized image height.")
add_arg('resize_w',         int,   300,    "The resized image height.")
add_arg('mean_value_B',     float, 127.5,  "Mean value for B channel which will be subtracted.")  #123.68
add_arg('mean_value_G',     float, 127.5,  "Mean value for G channel which will be subtracted.")  #116.78
add_arg('mean_value_R',     float, 127.5,  "Mean value for R channel which will be subtracted.")  #103.94
add_arg('is_toy',           int,   0, "Toy for quick debug, 0 means using all data, while n means using only n sample.")
#yapf: enable
37 38 39 40 41 42 43 44 45 46

def parallel_do(args,
                train_file_list,
                val_file_list,
                data_args,
                learning_rate,
                batch_size,
                num_passes,
                model_save_dir,
                pretrained_model=None):
D
dangqingqing 已提交
47
    image_shape = [3, data_args.resize_h, data_args.resize_w]
X
Xingyuan Bu 已提交
48 49 50 51
    if data_args.dataset == 'coco':
        num_classes = 81
    elif data_args.dataset == 'pascalvoc':
        num_classes = 21
D
dangqingqing 已提交
52 53 54 55 56 57 58 59 60

    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    gt_box = fluid.layers.data(
        name='gt_box', shape=[4], dtype='float32', lod_level=1)
    gt_label = fluid.layers.data(
        name='gt_label', shape=[1], dtype='int32', lod_level=1)
    difficult = fluid.layers.data(
        name='gt_difficult', shape=[1], dtype='int32', lod_level=1)

61 62
    if args.parallel:
        places = fluid.layers.get_places()
D
Dang Qingqing 已提交
63
        pd = fluid.layers.ParallelDo(places, use_nccl=args.use_nccl)
64 65 66 67 68
        with pd.do():
            image_ = pd.read_input(image)
            gt_box_ = pd.read_input(gt_box)
            gt_label_ = pd.read_input(gt_label)
            difficult_ = pd.read_input(difficult)
X
Xingyuan Bu 已提交
69 70 71 72
            locs, confs, box, box_var = mobile_net(num_classes, image_,
                                                   image_shape)
            loss = fluid.layers.ssd_loss(locs, confs, gt_box_, gt_label_, box,
                                         box_var)
73 74
            nmsed_out = fluid.layers.detection_output(
                locs, confs, box, box_var, nms_threshold=0.45)
75
            loss = fluid.layers.reduce_sum(loss)
76
            pd.write_output(loss)
77
            pd.write_output(nmsed_out)
78

79
        loss, nmsed_out = pd()
80
        loss = fluid.layers.mean(loss)
81
    else:
X
Xingyuan Bu 已提交
82
        locs, confs, box, box_var = mobile_net(num_classes, image, image_shape)
83
        nmsed_out = fluid.layers.detection_output(
84
            locs, confs, box, box_var, nms_threshold=0.45)
X
Xingyuan Bu 已提交
85 86
        loss = fluid.layers.ssd_loss(locs, confs, gt_box, gt_label, box,
                                     box_var)
87
        loss = fluid.layers.reduce_sum(loss)
D
dangqingqing 已提交
88 89 90

    test_program = fluid.default_main_program().clone(for_test=True)
    with fluid.program_guard(test_program):
91 92 93 94 95
        map_eval = fluid.evaluator.DetectionMAP(
            nmsed_out,
            gt_label,
            gt_box,
            difficult,
X
Xingyuan Bu 已提交
96
            num_classes,
97 98
            overlap_threshold=0.5,
            evaluate_difficult=False,
D
Dang Qingqing 已提交
99
            ap_version=args.ap_version)
X
Xingyuan Bu 已提交
100 101 102 103 104 105 106 107 108 109

    if data_args.dataset == 'coco':
        # learning rate decay in 12, 19 pass, respectively
        if '2014' in train_file_list:
            boundaries = [82783 / batch_size * 12, 82783 / batch_size * 19]
        elif '2017' in train_file_list:
            boundaries = [118287 / batch_size * 12, 118287 / batch_size * 19]
    elif data_args.dataset == 'pascalvoc':
        boundaries = [40000, 60000]
    values = [learning_rate, learning_rate * 0.5, learning_rate * 0.25]
G
gaoyuan 已提交
110 111
    optimizer = fluid.optimizer.RMSProp(
        learning_rate=fluid.layers.piecewise_decay(boundaries, values),
G
gaoyuan 已提交
112
        regularization=fluid.regularizer.L2Decay(0.00005), )
D
dangqingqing 已提交
113

114
    optimizer.minimize(loss)
D
dangqingqing 已提交
115

116
    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
D
dangqingqing 已提交
117 118 119
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

120 121 122 123 124
    if pretrained_model:
        def if_exist(var):
            return os.path.exists(os.path.join(pretrained_model, var.name))
        fluid.io.load_vars(exe, pretrained_model, predicate=if_exist)

D
dangqingqing 已提交
125 126 127 128 129 130 131 132
    train_reader = paddle.batch(
        reader.train(data_args, train_file_list), batch_size=batch_size)
    test_reader = paddle.batch(
        reader.test(data_args, val_file_list), batch_size=batch_size)
    feeder = fluid.DataFeeder(
        place=place, feed_list=[image, gt_box, gt_label, difficult])

    def test(pass_id):
133
        _, accum_map = map_eval.get_map_var()
D
dangqingqing 已提交
134 135
        map_eval.reset(exe)
        test_map = None
D
Dang Qingqing 已提交
136
        for data in test_reader():
D
dangqingqing 已提交
137 138 139
            test_map = exe.run(test_program,
                               feed=feeder.feed(data),
                               fetch_list=[accum_map])
140
        print("Pass {0}, test map {1}".format(pass_id, test_map[0]))
D
dangqingqing 已提交
141 142

    for pass_id in range(num_passes):
X
Xingyuan Bu 已提交
143 144 145
        start_time = time.time()
        prev_start_time = start_time
        end_time = 0
D
dangqingqing 已提交
146
        for batch_id, data in enumerate(train_reader()):
X
Xingyuan Bu 已提交
147 148
            prev_start_time = start_time
            start_time = time.time()
149 150 151
            loss_v = exe.run(fluid.default_main_program(),
                             feed=feeder.feed(data),
                             fetch_list=[loss])
X
Xingyuan Bu 已提交
152
            end_time = time.time()
G
gaoyuan 已提交
153
            if batch_id % 20 == 0:
X
Xingyuan Bu 已提交
154 155
                print("Pass {0}, batch {1}, loss {2}, time {3}".format(
                    pass_id, batch_id, loss_v[0], start_time - prev_start_time))
D
dangqingqing 已提交
156 157
        test(pass_id)

X
Xingyuan Bu 已提交
158
        if pass_id % 10 == 0 or pass_id == num_passes - 1:
D
dangqingqing 已提交
159 160
            model_path = os.path.join(model_save_dir, str(pass_id))
            print 'save models to %s' % (model_path)
161 162 163 164 165 166 167 168 169 170
            fluid.io.save_persistables(exe, model_path)


def parallel_exe(args,
                 train_file_list,
                 val_file_list,
                 data_args,
                 learning_rate,
                 batch_size,
                 num_passes,
171
                 model_save_dir,
172 173
                 pretrained_model=None):
    image_shape = [3, data_args.resize_h, data_args.resize_w]
174 175 176
    if 'coco' in data_args.dataset:
        num_classes = 91
    elif 'pascalvoc' in data_args.dataset:
177 178
        num_classes = 21

D
Dang Qingqing 已提交
179 180 181
    devices = os.getenv("CUDA_VISIBLE_DEVICES") or ""
    devices_num = len(devices.split(","))

182 183 184 185 186 187 188
    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    gt_box = fluid.layers.data(
        name='gt_box', shape=[4], dtype='float32', lod_level=1)
    gt_label = fluid.layers.data(
        name='gt_label', shape=[1], dtype='int32', lod_level=1)
    difficult = fluid.layers.data(
        name='gt_difficult', shape=[1], dtype='int32', lod_level=1)
189 190 191 192
    gt_iscrowd = fluid.layers.data(
        name='gt_iscrowd', shape=[1], dtype='int32', lod_level=1)
    gt_image_info = fluid.layers.data(
        name='gt_image_id', shape=[3], dtype='int32', lod_level=1)
193 194 195

    locs, confs, box, box_var = mobile_net(num_classes, image, image_shape)
    nmsed_out = fluid.layers.detection_output(
196 197 198
        locs, confs, box, box_var, nms_threshold=args.nms_threshold)
    loss = fluid.layers.ssd_loss(locs, confs, gt_box, gt_label, box,
                                 box_var)
199 200 201 202 203 204 205 206 207 208 209 210
    loss = fluid.layers.reduce_sum(loss)

    test_program = fluid.default_main_program().clone(for_test=True)
    with fluid.program_guard(test_program):
        map_eval = fluid.evaluator.DetectionMAP(
            nmsed_out,
            gt_label,
            gt_box,
            difficult,
            num_classes,
            overlap_threshold=0.5,
            evaluate_difficult=False,
D
Dang Qingqing 已提交
211
            ap_version=args.ap_version)
212

213
    if 'coco' in data_args.dataset:
214 215
        # learning rate decay in 12, 19 pass, respectively
        if '2014' in train_file_list:
D
Dang Qingqing 已提交
216 217
            epocs = 82783 / batch_size
            boundaries = [epocs * 12, epocs * 19]
218
        elif '2017' in train_file_list:
D
Dang Qingqing 已提交
219 220
            epocs = 118287 / batch_size
            boundaries = [epcos * 12, epocs * 19]
221 222 223
        values = [
            learning_rate, learning_rate * 0.5, learning_rate * 0.25
        ]
224
    elif data_args.dataset == 'pascalvoc':
D
Dang Qingqing 已提交
225 226
        epocs = 19200 / batch_size
        boundaries = [epocs * 40, epocs * 60, epocs * 80, epocs * 100]
227 228 229 230
        values = [
            learning_rate, learning_rate * 0.5, learning_rate * 0.25,
            learning_rate * 0.1, learning_rate * 0.01
        ]
231 232 233 234 235
    optimizer = fluid.optimizer.RMSProp(
        learning_rate=fluid.layers.piecewise_decay(boundaries, values),
        regularization=fluid.regularizer.L2Decay(0.00005), )

    optimizer.minimize(loss)
D
dangqingqing 已提交
236

237 238 239 240 241 242 243 244 245
    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if pretrained_model:
        def if_exist(var):
            return os.path.exists(os.path.join(pretrained_model, var.name))
        fluid.io.load_vars(exe, pretrained_model, predicate=if_exist)

246 247 248
    if args.parallel:
        train_exe = fluid.ParallelExecutor(
            use_cuda=args.use_gpu, loss_name=loss.name)
249 250 251 252 253 254 255 256

    train_reader = paddle.batch(
        reader.train(data_args, train_file_list), batch_size=batch_size)
    test_reader = paddle.batch(
        reader.test(data_args, val_file_list), batch_size=batch_size)
    feeder = fluid.DataFeeder(
        place=place, feed_list=[image, gt_box, gt_label, difficult])

D
Dang Qingqing 已提交
257 258 259 260 261 262 263 264
    def save_model(postfix):
        model_path = os.path.join(model_save_dir, postfix)
        if os.path.isdir(model_path):
            shutil.rmtree(model_path)
        print 'save models to %s' % (model_path)
        fluid.io.save_persistables(exe, model_path)

    best_map = 0.
D
Dang Qingqing 已提交
265

D
Dang Qingqing 已提交
266
    def test(pass_id, best_map):
267 268
        _, accum_map = map_eval.get_map_var()
        map_eval.reset(exe)
269
        for batch_id, data in enumerate(test_reader()):
270 271 272
            test_map = exe.run(test_program,
                               feed=feeder.feed(data),
                               fetch_list=[accum_map])
273 274
            if batch_id % 20 == 0:
                print("Batch {0}, map {1}".format(batch_id, test_map[0]))
D
Dang Qingqing 已提交
275 276 277
        if test_map[0] > best_map:
            best_map = test_map[0]
            save_model('best_model')
278
        print("Pass {0}, test map {1}".format(pass_id, test_map[0]))
X
Xingyuan Bu 已提交
279
        return best_map
280 281 282 283 284 285 286 287

    for pass_id in range(num_passes):
        start_time = time.time()
        prev_start_time = start_time
        end_time = 0
        for batch_id, data in enumerate(train_reader()):
            prev_start_time = start_time
            start_time = time.time()
D
Dang Qingqing 已提交
288
            if len(data) < devices_num: continue
289 290 291 292 293 294 295
            if args.parallel:
                loss_v, = train_exe.run(fetch_list=[loss.name],
                                        feed_dict=feeder.feed(data))
            else:
                loss_v, = exe.run(fluid.default_main_program(),
                                  feed=feeder.feed(data),
                                  fetch_list=[loss])
296 297 298 299 300
            end_time = time.time()
            loss_v = np.mean(np.array(loss_v))
            if batch_id % 20 == 0:
                print("Pass {0}, batch {1}, loss {2}, time {3}".format(
                    pass_id, batch_id, loss_v, start_time - prev_start_time))
X
Xingyuan Bu 已提交
301
        best_map = test(pass_id, best_map)
302
        if pass_id % 10 == 0 or pass_id == num_passes - 1:
D
Dang Qingqing 已提交
303 304
            save_model(str(pass_id))
    print("Best test map {0}".format(best_map))
D
dangqingqing 已提交
305

D
Dang Qingqing 已提交
306

D
dangqingqing 已提交
307
if __name__ == '__main__':
308 309
    args = parser.parse_args()
    print_arguments(args)
310 311 312 313 314 315

    data_dir = 'data/pascalvoc'
    train_file_list = 'trainval.txt'
    val_file_list = 'test.txt'
    label_file = 'label_list'
    model_save_dir = args.model_save_dir
316 317 318 319 320 321 322 323
    if 'coco' in args.dataset:
        data_dir = './data/coco'
        if '2014' in args.dataset:
            train_file_list = 'annotations/instances_train2014.json'
            val_file_list = 'annotations/instances_minival2014.json'
        elif '2017' in args.dataset:
            train_file_list = 'annotations/instances_train2017.json'
            val_file_list = 'annotations/instances_val2017.json'
324

D
dangqingqing 已提交
325
    data_args = reader.Settings(
326 327 328
        dataset=args.dataset,
        data_dir=data_dir,
        label_file=label_file,
X
Xingyuan Bu 已提交
329 330
        resize_h=args.resize_h,
        resize_w=args.resize_w,
D
Dang Qingqing 已提交
331
        mean_value=[args.mean_value_B, args.mean_value_G, args.mean_value_R],
332 333 334
        apply_distort=args.apply_distort,
        apply_expand=args.apply_expand,
        ap_version = args.ap_version,
D
Dang Qingqing 已提交
335
        toy=args.is_toy)
336
    method = parallel_exe
D
Dang Qingqing 已提交
337 338 339 340 341 342 343 344 345 346
    method(
        args,
        train_file_list=train_file_list,
        val_file_list=val_file_list,
        data_args=data_args,
        learning_rate=args.learning_rate,
        batch_size=args.batch_size,
        num_passes=args.num_passes,
        model_save_dir=model_save_dir,
        pretrained_model=args.pretrained_model)