Skip to content

Commit

Permalink
[Bug Fix] fix cross_entropy bug when 255 in label (#1499)
Browse files Browse the repository at this point in the history
  • Loading branch information
LutaoChu authored Nov 5, 2021
1 parent 39addba commit 11d7807
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions paddleseg/models/losses/cross_entropy_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ def __init__(self,
self.data_format = data_format
if weight is not None:
self.weight = paddle.to_tensor(weight, dtype='float32')
long_weight = weight + [0] * (256 - len(weight))
self.long_weight = paddle.to_tensor(long_weight, dtype='float32')
else:
self.weight = None
self.long_weight = None

def forward(self, logit, label, semantic_weights=None):
"""
Expand Down Expand Up @@ -82,12 +79,13 @@ def forward(self, logit, label, semantic_weights=None):
label = label.astype('int64')

# In F.cross_entropy, the ignore_index is invalid, which needs to be fixed.
# When there is 255 in the label and paddle version <= 2.1.3, the cross_entropy OP will report an error, which is fixed in paddle develop version.
loss = F.cross_entropy(
logit,
label,
ignore_index=self.ignore_index,
reduction='none',
weight=self.long_weight)
weight=self.weight)

return self._post_process_loss(logit, label, semantic_weights, loss)

Expand Down

0 comments on commit 11d7807

Please sign in to comment.