Skip to content

Commit

Permalink
Merge branch 'dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
KumoLiu authored Aug 9, 2024
2 parents 8226d4c + 0bb05d7 commit 0223751
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions monai/data/wsi_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from monai.data.utils import iter_patch_position
from monai.data.wsi_reader import BaseWSIReader, WSIReader
from monai.transforms import ForegroundMask, Randomizable, apply_transform
from monai.utils import convert_to_dst_type, ensure_tuple_rep
from monai.utils import convert_to_dst_type, ensure_tuple, ensure_tuple_rep
from monai.utils.enums import CommonKeys, ProbMapKeys, WSIPatchKeys

__all__ = ["PatchWSIDataset", "SlidingPatchWSIDataset", "MaskedPatchWSIDataset"]
Expand Down Expand Up @@ -123,9 +123,9 @@ def _get_label(self, sample: dict):
def _get_location(self, sample: dict):
if self.center_location:
size = self._get_size(sample)
return [sample[WSIPatchKeys.LOCATION][i] - size[i] // 2 for i in range(len(size))]
return ensure_tuple(sample[WSIPatchKeys.LOCATION][i] - size[i] // 2 for i in range(len(size)))
else:
return sample[WSIPatchKeys.LOCATION]
return ensure_tuple(sample[WSIPatchKeys.LOCATION])

def _get_level(self, sample: dict):
if self.patch_level is None:
Expand Down
11 changes: 10 additions & 1 deletion monai/losses/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ def __init__(
weight: torch.Tensor | None = None,
lambda_dice: float = 1.0,
lambda_ce: float = 1.0,
label_smoothing: float = 0.0,
) -> None:
"""
Args:
Expand Down Expand Up @@ -704,6 +705,9 @@ def __init__(
Defaults to 1.0.
lambda_ce: the trade-off weight value for cross entropy loss. The value should be no less than 0.0.
Defaults to 1.0.
label_smoothing: a value in [0, 1] range. If > 0, the labels are smoothed
by the given factor to reduce overfitting.
Defaults to 0.0.
"""
super().__init__()
Expand All @@ -728,7 +732,12 @@ def __init__(
batch=batch,
weight=dice_weight,
)
self.cross_entropy = nn.CrossEntropyLoss(weight=weight, reduction=reduction)
if pytorch_after(1, 10):
self.cross_entropy = nn.CrossEntropyLoss(
weight=weight, reduction=reduction, label_smoothing=label_smoothing
)
else:
self.cross_entropy = nn.CrossEntropyLoss(weight=weight, reduction=reduction)
self.binary_cross_entropy = nn.BCEWithLogitsLoss(pos_weight=weight, reduction=reduction)
if lambda_dice < 0.0:
raise ValueError("lambda_dice should be no less than 0.0.")
Expand Down

0 comments on commit 0223751

Please sign in to comment.