Replies: 1 comment
|
Yes — for overlapping labels, each output channel is an independent binary target, so the CE branch of You can compose the two supported losses explicitly: import torch
from monai.losses import DiceLoss
dice = DiceLoss(
sigmoid=True,
softmax=False,
to_onehot_y=False,
include_background=True,
)
bce = torch.nn.BCEWithLogitsLoss()
def multilabel_loss(logits: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
# Both tensors: [B, C, *spatial]; target is multi-hot (0/1), overlaps allowed.
target = target.to(dtype=logits.dtype)
return dice(logits, target) + bce(logits, target)Pass raw logits to both terms: I checked this with a synthetic voxel belonging to two labels on MONAI 1.6.0 / PyTorch 2.9.0: the composed loss was finite and gradients propagated. That verifies the API path, not performance on a real segmentation dataset. If you need imbalance handling, add |
Uh oh!
There was an error while loading. Please reload this page.
Hello,
I have encountered multiple projects in which I need to output multiple labels for a single voxel. In this case, I'd like to use the
DiceCELoss, but forcing it to use the binary cross entropy loss instead of the cross entropy one (since each channel predicted is either on or off independently of the other channels).In the current state of
DiceCELoss, it automatically choose to use either ce/bce based on the number of channels:MONAI/monai/losses/dice.py
Line 804 in e72145c
I wanted to see if there is an officially supported way to achieve the behavior described above before contributing.
Thank you
All reactions