未验证 提交 489571b4 编写于 作者: Q qingqing01 提交者: GitHub

Support groups in de-conv and fix bug. (#939)

* Support groups in de-conv and fix bug.

* Clean code.
上级 36e3fcd5
......@@ -75,11 +75,8 @@ def satisfy_sample_constraint(sampler, sample_bbox, bbox_labels):
if sampler.min_jaccard_overlap == 0 and sampler.max_jaccard_overlap == 0:
return True
for i in range(len(bbox_labels)):
object_bbox = bbox(
bbox_labels[i][0],
bbox_labels[i][1], # tangxu @ 2018-05-17
bbox_labels[i][2],
bbox_labels[i][3])
object_bbox = bbox(bbox_labels[i][0], bbox_labels[i][1],
bbox_labels[i][2], bbox_labels[i][3])
overlap = jaccard_overlap(sample_bbox, object_bbox)
if sampler.min_jaccard_overlap != 0 and \
overlap < sampler.min_jaccard_overlap:
......
......@@ -82,8 +82,7 @@ class PyramidBox(object):
self.conv5 = conv_block(self.conv4, 3, [512] * 3, [3] * 3)
# fc6 and fc7 in paper, priorbox min_size is 128
self.conv6 = conv_block(
self.conv5, 2, [1024, 1024], [3, 1], with_pool=False)
self.conv6 = conv_block(self.conv5, 2, [1024, 1024], [3, 1])
# conv6_1 and conv6_2 in paper, priorbox min_size is 256
self.conv7 = conv_block(
self.conv6, 2, [256, 512], [1, 3], [1, 2], with_pool=False)
......@@ -101,9 +100,15 @@ class PyramidBox(object):
b_attr = ParamAttr(learning_rate=2., regularizer=L2Decay(0.))
conv1 = fluid.layers.conv2d(
up_from, ch, 1, act='relu', bias_attr=b_attr)
# TODO: add group
conv_trans = fluid.layers.conv2d_transpose(
conv1, ch, None, 4, 1, 2, bias_attr=False)
conv1,
ch,
output_size=None,
filter_size=4,
padding=1,
stride=2,
groups=ch,
bias_attr=False)
b_attr = ParamAttr(learning_rate=2., regularizer=L2Decay(0.))
conv2 = fluid.layers.conv2d(
up_to, ch, 1, act='relu', bias_attr=b_attr)
......@@ -235,8 +240,7 @@ class PyramidBox(object):
self.prior_boxes = fluid.layers.concat(boxes)
self.box_vars = fluid.layers.concat(vars)
def vgg_ssd(self, num_classes, image_shape): # tangxu
def vgg_ssd(self, num_classes, image_shape):
self.conv3_norm = self._l2_norm_scale(self.conv3)
self.conv4_norm = self._l2_norm_scale(self.conv4)
self.conv5_norm = self._l2_norm_scale(self.conv5)
......@@ -275,7 +279,10 @@ class PyramidBox(object):
head_loss = fluid.layers.ssd_loss(
self.head_mbox_loc, self.head_mbox_conf, self.gt_box, self.gt_label,
self.prior_boxes, self.box_vars)
return face_loss, head_loss
face_loss = fluid.layers.reduce_sum(face_loss)
head_loss = fluid.layers.reduce_sum(head_loss)
total_loss = face_loss + head_loss
return face_loss, head_loss, total_loss
def test(self):
test_program = fluid.default_main_program().clone(for_test=True)
......
......@@ -19,6 +19,7 @@ add_arg('learning_rate', float, 0.0001, "Learning rate.")
add_arg('batch_size', int, 16, "Minibatch size.")
add_arg('num_passes', int, 120, "Epoch number.")
add_arg('use_gpu', bool, True, "Whether use GPU.")
add_arg('use_pyramidbox', bool, False, "Whether use PyramidBox model.")
add_arg('dataset', str, 'WIDERFACE', "coco2014, coco2017, and pascalvoc.")
add_arg('model_save_dir', str, 'model', "The path to save model.")
add_arg('pretrained_model', str, './vgg_model/', "The init model path.")
......@@ -37,8 +38,12 @@ def train(args, data_args, learning_rate, batch_size, pretrained_model,
image_shape = [3, data_args.resize_h, data_args.resize_w]
network = PyramidBox(image_shape)
loss = network.vgg_ssd(num_classes, image_shape)
if args.use_pyramidbox:
network = PyramidBox(image_shape, sub_network=args.use_pyramidbox)
face_loss, head_loss, loss = network.train()
else:
network = PyramidBox(image_shape, sub_network=args.use_pyramidbox)
loss = network.vgg_ssd(num_classes, image_shape)
epocs = 12880 / batch_size
boundaries = [epocs * 100, epocs * 125, epocs * 150]
......@@ -46,6 +51,7 @@ def train(args, data_args, learning_rate, batch_size, pretrained_model,
learning_rate, learning_rate * 0.1, learning_rate * 0.01,
learning_rate * 0.001
]
#print('main program ', fluid.default_main_program())
optimizer = fluid.optimizer.RMSProp(
learning_rate=fluid.layers.piecewise_decay(boundaries, values),
......@@ -60,10 +66,8 @@ def train(args, data_args, learning_rate, batch_size, pretrained_model,
# fluid.io.save_inference_model('./vgg_model/', ['image'], [loss], exe)
if pretrained_model:
def if_exist(var):
return os.path.exists(os.path.join(pretrained_model, var.name))
print('Load pre-trained model.')
fluid.io.load_vars(exe, pretrained_model, predicate=if_exist)
......@@ -108,7 +112,6 @@ def train(args, data_args, learning_rate, batch_size, pretrained_model,
if batch_id % 1 == 0:
print("Pass {0}, batch {1}, loss {2}, time {3}".format(
pass_id, batch_id, loss_v, start_time - prev_start_time))
test(pass_id, best_map)
if pass_id % 10 == 0 or pass_id == num_passes - 1:
save_model(str(pass_id))
print("Best test map {0}".format(best_map))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册