@@ -45,12 +45,17 @@ def conv_block(input, groups, filters, ksizes, strides=None, with_pool=True):
4545
4646
4747class PyramidBox (object ):
48- def __init__ (self , data_shape , is_infer = False , sub_network = False ):
48+ def __init__ (self ,
49+ data_shape ,
50+ num_classes ,
51+ is_infer = False ,
52+ sub_network = False ):
4953 self .data_shape = data_shape
5054 self .min_sizes = [16. , 32. , 64. , 128. , 256. , 512. ]
5155 self .steps = [4. , 8. , 16. , 32. , 64. , 128. ]
5256 self .is_infer = is_infer
5357 self .sub_network = sub_network
58+ self .num_classes = num_classes
5459
5560 # the base network is VGG with atrous layers
5661 self ._input ()
@@ -59,6 +64,8 @@ def __init__(self, data_shape, is_infer=False, sub_network=False):
5964 self ._low_level_fpn ()
6065 self ._cpm_module ()
6166 self ._pyramidbox ()
67+ else :
68+ self ._vgg_ssd ()
6269
6370 def feeds (self ):
6471 if self .is_infer :
@@ -188,9 +195,10 @@ def _pyramidbox(self):
188195 """
189196 Get prior-boxes and pyramid-box
190197 """
191- self .ssh_conv3_norm = self ._l2_norm_scale (self .ssh_conv3 )
192- self .ssh_conv4_norm = self ._l2_norm_scale (self .ssh_conv4 )
193- self .ssh_conv5_norm = self ._l2_norm_scale (self .ssh_conv5 )
198+ self .ssh_conv3_norm = self ._l2_norm_scale (
199+ self .ssh_conv3 , init_scale = 10. )
200+ self .ssh_conv4_norm = self ._l2_norm_scale (self .ssh_conv4 , init_scale = 8. )
201+ self .ssh_conv5_norm = self ._l2_norm_scale (self .ssh_conv5 , init_scale = 5. )
194202
195203 def permute_and_reshape (input , last_dim ):
196204 trans = fluid .layers .transpose (input , perm = [0 , 2 , 3 , 1 ])
@@ -253,34 +261,41 @@ def permute_and_reshape(input, last_dim):
253261 self .prior_boxes = fluid .layers .concat (boxes )
254262 self .box_vars = fluid .layers .concat (vars )
255263
256- def vgg_ssd (self , num_classes , image_shape ):
257- self .conv3_norm = self ._l2_norm_scale (self .conv3 )
258- self .conv4_norm = self ._l2_norm_scale (self .conv4 )
259- self .conv5_norm = self ._l2_norm_scale (self .conv5 )
264+ def _vgg_ssd (self ):
265+ self .conv3_norm = self ._l2_norm_scale (self .conv3 , init_scale = 10. )
266+ self .conv4_norm = self ._l2_norm_scale (self .conv4 , init_scale = 8. )
267+ self .conv5_norm = self ._l2_norm_scale (self .conv5 , init_scale = 5. )
260268
261269 mbox_locs , mbox_confs , box , box_var = fluid .layers .multi_box_head (
262270 inputs = [
263271 self .conv3_norm , self .conv4_norm , self .conv5_norm , self .conv6 ,
264272 self .conv7 , self .conv8
265273 ],
266274 image = self .image ,
267- num_classes = num_classes ,
268- # min_ratio=20,
269- # max_ratio=90,
275+ num_classes = self .num_classes ,
270276 min_sizes = [16.0 , 32.0 , 64.0 , 128.0 , 256.0 , 512.0 ],
271277 max_sizes = [[], [], [], [], [], []],
272- # max_sizes=[[], 150.0, 195.0, 240.0, 285.0, 300.0],
273278 aspect_ratios = [[1. ], [1. ], [1. ], [1. ], [1. ], [1. ]],
274279 steps = [4.0 , 8.0 , 16.0 , 32.0 , 64.0 , 128.0 ],
275- base_size = image_shape [2 ],
280+ base_size = self . data_shape [2 ],
276281 offset = 0.5 ,
277282 flip = False )
278283
279- # locs, confs, box, box_var = vgg_extra_net(num_classes, image, image_shape)
280- # nmsed_out = fluid.layers.detection_output(
281- # locs, confs, box, box_var, nms_threshold=args.nms_threshold)
282- loss = fluid .layers .ssd_loss (mbox_locs , mbox_confs , self .face_box ,
283- self .gt_label , box , box_var )
284+ self .face_mbox_loc = mbox_locs
285+ self .face_mbox_conf = mbox_confs
286+ self .prior_boxes = box
287+ self .box_vars = box_var
288+
289+ def vgg_ssd_loss (self ):
290+ loss = fluid .layers .ssd_loss (
291+ self .face_mbox_loc ,
292+ self .face_mbox_conf ,
293+ self .face_box ,
294+ self .gt_label ,
295+ self .prior_boxes ,
296+ self .box_vars ,
297+ overlap_threshold = 0.35 ,
298+ neg_overlap = 0.35 )
284299 loss = fluid .layers .reduce_sum (loss )
285300
286301 return loss
@@ -297,7 +312,7 @@ def train(self):
297312 total_loss = face_loss + head_loss
298313 return face_loss , head_loss , total_loss
299314
300- def test (self ):
315+ def infer (self ):
301316 test_program = fluid .default_main_program ().clone (for_test = True )
302317 with fluid .program_guard (test_program ):
303318 face_nmsed_out = fluid .layers .detection_output (
@@ -306,24 +321,4 @@ def test(self):
306321 self .prior_boxes ,
307322 self .box_vars ,
308323 nms_threshold = 0.45 )
309- head_nmsed_out = fluid .layers .detection_output (
310- self .head_mbox_loc ,
311- self .head_mbox_conf ,
312- self .prior_boxes ,
313- self .box_vars ,
314- nms_threshold = 0.45 )
315- face_map_eval = fluid .evaluator .DetectionMAP (
316- face_nmsed_out ,
317- self .gt_label ,
318- self .face_box ,
319- class_num = 2 ,
320- overlap_threshold = 0.5 ,
321- ap_version = '11point' )
322- head_map_eval = fluid .evaluator .DetectionMAP (
323- head_nmsed_out ,
324- self .gt_label ,
325- self .head_box ,
326- class_num = 2 ,
327- overlap_threshold = 0.5 ,
328- ap_version = '11point' )
329- return test_program , face_map_eval , head_map_eval
324+ return test_program , face_nmsed_out
0 commit comments