Skip to content

Commit 69643b5

Browse files
authored
Enable the SSD loss to support normalization by the total number of output locations. (#8630)
* Register more data type for reshape operator. * Enable the SSD loss to support normalization by the total number of output locations. * Fix the doc format.
1 parent f054867 commit 69643b5

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

python/paddle/fluid/layers/detection.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ def ssd_loss(location,
328328
conf_loss_weight=1.0,
329329
match_type='per_prediction',
330330
mining_type='max_negative',
331+
normalize=True,
331332
sample_size=None):
332333
"""
333334
**Multi-box loss layer for object dection algorithm of SSD**
@@ -376,18 +377,20 @@ def ssd_loss(location,
376377
`overlap_threshold` to determine the extra matching bboxes when
377378
finding matched boxes. 0.5 by default.
378379
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.
380381
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',
382383
0.5 by default.
383-
sample_size (int): The max sample size of negative box, used only when
384-
mining_type is hard_example.
385384
loc_loss_weight (float): Weight for localization loss, 1.0 by default.
386385
conf_loss_weight (float): Weight for confidence loss, 1.0 by default.
387386
match_type (str): The type of matching method during training, should
388387
be 'bipartite' or 'per_prediction', 'per_prediction' by defalut.
389388
mining_type (str): The hard example mining type, should be 'hard_example'
390389
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'.
391394
392395
Returns:
393396
Variable: The weighted sum of the localization loss and confidence loss,
@@ -507,6 +510,13 @@ def __reshape_to_2d(var):
507510

508511
# 5.3 Compute overall weighted loss.
509512
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+
510520
return loss
511521

512522

0 commit comments

Comments
 (0)