train.py 12.0 KB
Newer Older
X
Xingyuan Bu 已提交
1
import paddle
D
dangqingqing 已提交
2 3 4 5
import paddle.fluid as fluid
import reader
import load_model as load_model
from mobilenet_ssd import mobile_net
6 7
from utility import add_arguments, print_arguments
import os
X
Xingyuan Bu 已提交
8
import time
9 10 11 12 13 14
import numpy as np
import argparse
import functools

parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
15 16 17 18 19 20 21 22
# yapf: disable
add_arg('learning_rate',    float, 0.001,     "Learning rate.")
add_arg('batch_size',       int,   32,        "Minibatch size.")
add_arg('num_passes',       int,   25,        "Epoch number.")
add_arg('parallel',         bool,  True,      "Whether use parallel training.")
add_arg('use_gpu',          bool,  True,      "Whether use GPU.")
add_arg('dataset',          str, 'pascalvoc', "coco or pascalvoc.")
add_arg('model_save_dir',   str, 'model',     "The path to save model.")
23
add_arg('pretrained_model', str, 'pretrained/ssd_mobilenet_v1_coco/', "The init model path.")
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
add_arg('apply_distort',    bool, True,   "Whether apply distort")
add_arg('apply_expand',     bool, False,  "Whether appley expand")
add_arg('resize_h',         int,  300,    "resize image size")
add_arg('resize_w',         int,  300,    "resize image size")
add_arg('mean_value_B',     float, 127.5, "mean value which will be subtracted")  #123.68
add_arg('mean_value_G',     float, 127.5, "mean value which will be subtracted")  #116.78
add_arg('mean_value_R',     float, 127.5, "mean value 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: disable


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 已提交
44
    image_shape = [3, data_args.resize_h, data_args.resize_w]
X
Xingyuan Bu 已提交
45 46 47 48
    if data_args.dataset == 'coco':
        num_classes = 81
    elif data_args.dataset == 'pascalvoc':
        num_classes = 21
D
dangqingqing 已提交
49 50 51 52 53 54 55 56 57

    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)

58 59 60 61 62 63 64 65
    if args.parallel:
        places = fluid.layers.get_places()
        pd = fluid.layers.ParallelDo(places)
        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 已提交
66 67 68 69
            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)
70 71
            nmsed_out = fluid.layers.detection_output(
                locs, confs, box, box_var, nms_threshold=0.45)
72
            loss = fluid.layers.reduce_sum(loss)
73
            pd.write_output(loss)
74
            pd.write_output(nmsed_out)
75

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

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

    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 已提交
107 108
    optimizer = fluid.optimizer.RMSProp(
        learning_rate=fluid.layers.piecewise_decay(boundaries, values),
G
gaoyuan 已提交
109
        regularization=fluid.regularizer.L2Decay(0.00005), )
D
dangqingqing 已提交
110

111
    optimizer.minimize(loss)
D
dangqingqing 已提交
112

113
    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
D
dangqingqing 已提交
114 115 116
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

117 118 119 120 121
    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 已提交
122 123 124 125 126 127 128 129
    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):
130
        _, accum_map = map_eval.get_map_var()
D
dangqingqing 已提交
131 132 133 134 135 136 137 138 139
        map_eval.reset(exe)
        test_map = None
        for _, data in enumerate(test_reader()):
            test_map = exe.run(test_program,
                               feed=feeder.feed(data),
                               fetch_list=[accum_map])
        print("Test {0}, map {1}".format(pass_id, test_map[0]))

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

X
Xingyuan Bu 已提交
155
        if pass_id % 10 == 0 or pass_id == num_passes - 1:
D
dangqingqing 已提交
156 157
            model_path = os.path.join(model_save_dir, str(pass_id))
            print 'save models to %s' % (model_path)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
            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,
                 model_save_dir='model',
                 pretrained_model=None):
    image_shape = [3, data_args.resize_h, data_args.resize_w]
    if data_args.dataset == 'coco':
        num_classes = 81
    elif data_args.dataset == 'pascalvoc':
        num_classes = 21

    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)

    locs, confs, box, box_var = mobile_net(num_classes, image, image_shape)
    nmsed_out = fluid.layers.detection_output(
        locs, confs, box, box_var, nms_threshold=0.45)
    loss = fluid.layers.ssd_loss(locs, confs, gt_box, gt_label, box,
                                 box_var)
    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,
            ap_version='integral')

    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]
    optimizer = fluid.optimizer.RMSProp(
        learning_rate=fluid.layers.piecewise_decay(boundaries, values),
        regularization=fluid.regularizer.L2Decay(0.00005), )

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

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    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)

    train_exe = fluid.ParallelExecutor(use_cuda=True, loss_name=loss.name)

    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):
        _, accum_map = map_eval.get_map_var()
        map_eval.reset(exe)
        test_map = None
        for _, data in enumerate(test_reader()):
            test_map = exe.run(test_program,
                               feed=feeder.feed(data),
                               fetch_list=[accum_map])
        print("Test {0}, map {1}".format(pass_id, test_map[0]))

    for pass_id in range(num_passes):
        start_time = time.time()
        prev_start_time = start_time
        end_time = 0
        test(pass_id)
        for batch_id, data in enumerate(train_reader()):
            prev_start_time = start_time
            start_time = time.time()
            loss_v, = train_exe.run(fetch_list=[loss.name],
                                   feed_dict=feeder.feed(data))
            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))

        if pass_id % 10 == 0 or pass_id == num_passes - 1:
            model_path = os.path.join(model_save_dir, str(pass_id))
            print 'save models to %s' % (model_path)
            fluid.io.save_persistables(exe, model_path)
D
dangqingqing 已提交
266 267

if __name__ == '__main__':
268 269
    args = parser.parse_args()
    print_arguments(args)
270 271 272 273 274 275 276 277 278 279 280 281

    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
    if args.dataset == 'coco':
        data_dir = './data/COCO17'
        train_file_list = 'annotations/instances_train2017.json'
        val_file_list = 'annotations/instances_val2017.json'
        label_file = 'label_list'

D
dangqingqing 已提交
282
    data_args = reader.Settings(
283
        dataset=args.dataset,
X
Xingyuan Bu 已提交
284
        toy=args.is_toy,
285 286
        data_dir=data_dir,
        label_file=label_file,
X
Xingyuan Bu 已提交
287 288 289 290 291
        apply_distort=args.apply_distort,
        apply_expand=args.apply_expand,
        resize_h=args.resize_h,
        resize_w=args.resize_w,
        mean_value=[args.mean_value_B, args.mean_value_G, args.mean_value_R])
292 293 294 295 296 297 298 299 300 301 302
    #method = parallel_do
    method = parallel_exe
    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)