Loss functions for binary semantic segmentation #2689
-
Hi, I saw that the binary semantic segmentation task will be available in next release (0.7.0). I saw the configure_losses function and wonder is it possible to set mode='binary' for JaccardLoss and FocalLoss if task='binary'? Or are there any other reasons why you don't specially specified mode='binary' for them in case of binary semantic segmentation? def configure_losses(self) -> None:
"""Initialize the loss criterion."""
ignore_index: int | None = self.hparams['ignore_index']
match self.hparams['loss']:
case 'ce':
ignore_value = -1000 if ignore_index is None else ignore_index
self.criterion: nn.Module = nn.CrossEntropyLoss(
ignore_index=ignore_value, weight=self.hparams['class_weights']
)
case 'bce':
self.criterion = nn.BCEWithLogitsLoss()
case 'jaccard':
# JaccardLoss requires a list of classes to use instead of a class
# index to ignore.
classes = [
i for i in range(self.hparams['num_classes']) if i != ignore_index
]
self.criterion = smp.losses.JaccardLoss(
mode='multiclass', classes=classes
)
case 'focal':
self.criterion = smp.losses.FocalLoss(
'multiclass', ignore_index=ignore_index, normalized=True
) Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
adamjstewart
Mar 31, 2025
Replies: 1 comment 1 reply
-
Good catch, I think I completely missed that! Would you like to open a PR to fix this? Just replace |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
adamjstewart
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good catch, I think I completely missed that! Would you like to open a PR to fix this? Just replace
mode='multiclass'
withmode=self.hparams['task']
like I did inClassificationTask
.