@@ -328,6 +328,7 @@ def ssd_loss(location,
328
328
conf_loss_weight = 1.0 ,
329
329
match_type = 'per_prediction' ,
330
330
mining_type = 'max_negative' ,
331
+ normalize = True ,
331
332
sample_size = None ):
332
333
"""
333
334
**Multi-box loss layer for object dection algorithm of SSD**
@@ -376,18 +377,20 @@ def ssd_loss(location,
376
377
`overlap_threshold` to determine the extra matching bboxes when
377
378
finding matched boxes. 0.5 by default.
378
379
neg_pos_ratio (float): The ratio of the negative boxes to the positive
379
- boxes, used only when mining_type is max_negative, 3.0 by defalut.
380
+ boxes, used only when mining_type is ' max_negative' , 3.0 by defalut.
380
381
neg_overlap (float): The negative overlap upper bound for the unmatched
381
- predictions. Use only when mining_type is max_negative,
382
+ predictions. Use only when mining_type is ' max_negative' ,
382
383
0.5 by default.
383
- sample_size (int): The max sample size of negative box, used only when
384
- mining_type is hard_example.
385
384
loc_loss_weight (float): Weight for localization loss, 1.0 by default.
386
385
conf_loss_weight (float): Weight for confidence loss, 1.0 by default.
387
386
match_type (str): The type of matching method during training, should
388
387
be 'bipartite' or 'per_prediction', 'per_prediction' by defalut.
389
388
mining_type (str): The hard example mining type, should be 'hard_example'
390
389
or 'max_negative', now only support `max_negative`.
390
+ normalize (bool): Whether to normalize the SSD loss by the total number
391
+ of output locations, True by defalut.
392
+ sample_size (int): The max sample size of negative box, used only when
393
+ mining_type is 'hard_example'.
391
394
392
395
Returns:
393
396
Variable: The weighted sum of the localization loss and confidence loss,
@@ -507,6 +510,13 @@ def __reshape_to_2d(var):
507
510
508
511
# 5.3 Compute overall weighted loss.
509
512
loss = conf_loss_weight * conf_loss + loc_loss_weight * loc_loss
513
+ # reshape to [N, Np], N is the batch size and Np is the prior box number.
514
+ loss = ops .reshape (x = loss , shape = [- 1 , num_prior ])
515
+ loss = nn .reduce_sum (loss , dim = 1 , keep_dim = True )
516
+ if normalize :
517
+ normalizer = nn .reduce_sum (target_loc_weight )
518
+ loss = loss / normalizer
519
+
510
520
return loss
511
521
512
522
0 commit comments