2525from monai .losses .spatial_mask import MaskedLoss
2626from monai .losses .utils import compute_tp_fp_fn
2727from monai .networks import one_hot
28- from monai .utils import DiceCEReduction , LossReduction , Weight , look_up_option , pytorch_after
28+ from monai .utils import DiceCEReduction , LossReduction , Weight , look_up_option
2929
3030
3131class DiceLoss (_Loss ):
@@ -738,20 +738,14 @@ def __init__(
738738 batch = batch ,
739739 weight = dice_weight ,
740740 )
741- if pytorch_after (1 , 10 ):
742- self .cross_entropy = nn .CrossEntropyLoss (
743- weight = weight , reduction = reduction , label_smoothing = label_smoothing
744- )
745- else :
746- self .cross_entropy = nn .CrossEntropyLoss (weight = weight , reduction = reduction )
741+ self .cross_entropy = nn .CrossEntropyLoss (weight = weight , reduction = reduction , label_smoothing = label_smoothing )
747742 self .binary_cross_entropy = nn .BCEWithLogitsLoss (pos_weight = weight , reduction = reduction )
748743 if lambda_dice < 0.0 :
749744 raise ValueError ("lambda_dice should be no less than 0.0." )
750745 if lambda_ce < 0.0 :
751746 raise ValueError ("lambda_ce should be no less than 0.0." )
752747 self .lambda_dice = lambda_dice
753748 self .lambda_ce = lambda_ce
754- self .old_pt_ver = not pytorch_after (1 , 10 )
755749
756750 def ce (self , input : torch .Tensor , target : torch .Tensor ) -> torch .Tensor :
757751 """
@@ -764,12 +758,6 @@ def ce(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
764758 if n_pred_ch != n_target_ch and n_target_ch == 1 :
765759 target = torch .squeeze (target , dim = 1 )
766760 target = target .long ()
767- elif self .old_pt_ver :
768- warnings .warn (
769- f"Multichannel targets are not supported in this older Pytorch version { torch .__version__ } . "
770- "Using argmax (as a workaround) to convert target to a single channel."
771- )
772- target = torch .argmax (target , dim = 1 )
773761 elif not torch .is_floating_point (target ):
774762 target = target .to (dtype = input .dtype )
775763
0 commit comments