Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion monai/losses/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from monai.losses.focal_loss import FocalLoss
from monai.losses.spatial_mask import MaskedLoss
from monai.networks import one_hot
from monai.utils import LossReduction, Weight, look_up_option
from monai.utils import DiceCEReduction, LossReduction, Weight, look_up_option


class DiceLoss(_Loss):
Expand Down Expand Up @@ -671,6 +671,7 @@ def __init__(

"""
super().__init__()
reduction = look_up_option(reduction, DiceCEReduction).value
self.dice = DiceLoss(
include_background=include_background,
to_onehot_y=to_onehot_y,
Expand Down
1 change: 1 addition & 0 deletions monai/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BlendMode,
ChannelMatching,
CommonKeys,
DiceCEReduction,
ForwardMode,
GridSampleMode,
GridSamplePadMode,
Expand Down
11 changes: 11 additions & 0 deletions monai/utils/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"Average",
"MetricReduction",
"LossReduction",
"DiceCEReduction",
"Weight",
"ChannelMatching",
"SkipMode",
Expand Down Expand Up @@ -166,6 +167,16 @@ class LossReduction(Enum):
SUM = "sum"


class DiceCEReduction(Enum):
"""
See also:
- :py:class:`monai.losses.dice.DiceCELoss`
"""

MEAN = "mean"
SUM = "sum"


class Weight(Enum):
"""
See also: :py:class:`monai.losses.dice.GeneralizedDiceLoss`
Expand Down
5 changes: 5 additions & 0 deletions tests/test_dice_ce_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def test_ill_shape(self):
with self.assertRaisesRegex(ValueError, ""):
loss(torch.ones((1, 2, 3)), torch.ones((1, 1, 2, 3)))

def test_ill_reduction(self):
with self.assertRaisesRegex(ValueError, ""):
loss = DiceCELoss(reduction="none")
loss(torch.ones((1, 2, 3)), torch.ones((1, 1, 2, 3)))

@SkipIfBeforePyTorchVersion((1, 7, 0))
def test_script(self):
loss = DiceCELoss()
Expand Down