o  i+@slddlmZddlZddlmZddlZddlmZddlm Z ddl m Z ddl m Z Gdd d eZdS) ) annotationsN)Callable)_Loss)compute_tp_fp_fn)one_hot) LossReductionc sHeZdZdZdddddddejddddf d fdd Zd!ddZZS)" TverskyLossa Compute the Tversky loss defined in: Sadegh et al. (2017) Tversky loss function for image segmentation using 3D fully convolutional deep networks. (https://arxiv.org/abs/1706.05721) Wang, Z. et. al. (2023) Dice Semimetric Losses: Optimizing the Dice Score with Soft Labels. MICCAI 2023. Adapted from: https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L631 TFNg?gh㈵>include_backgroundbool to_onehot_ysigmoidsoftmax other_actCallable | Nonealphafloatbeta reductionLossReduction | str smooth_nr smooth_drbatch soft_labelreturnNonec stjt|jd|durt|stdt|jdt|t|t|dudkr1t d||_ ||_ ||_ ||_ ||_||_||_t| |_t| |_| |_| |_dS)a` Args: include_background: If False channel index 0 (background category) is excluded from the calculation. to_onehot_y: whether to convert `y` into the one-hot format. Defaults to False. sigmoid: If True, apply a sigmoid function to the prediction. softmax: If True, apply a softmax function to the prediction. other_act: if don't want to use `sigmoid` or `softmax`, use other callable function to execute other activation layers, Defaults to ``None``. for example: `other_act = torch.tanh`. alpha: weight of false positives beta: weight of false negatives reduction: {``"none"``, ``"mean"``, ``"sum"``} Specifies the reduction to apply to the output. Defaults to ``"mean"``. - ``"none"``: no reduction will be applied. - ``"mean"``: the sum of the output will be divided by the number of elements in the output. - ``"sum"``: the output will be summed. smooth_nr: a small constant added to the numerator to avoid zero. smooth_dr: a small constant added to the denominator to avoid nan. batch: whether to sum the intersection and union areas over the batch dimension before the dividing. Defaults to False, a Dice loss value is computed independently from each item in the batch before any `reduction`. soft_label: whether the target contains non-binary values (soft labels) or not. If True a soft label formulation of the loss will be used. Raises: TypeError: When ``other_act`` is not an ``Optional[Callable]``. ValueError: When more than 1 of [``sigmoid=True``, ``softmax=True``, ``other_act is not None``]. Incompatible values. )rNz*other_act must be None or callable but is .zXIncompatible values: more than 1 of [sigmoid=True, softmax=True, other_act is not None].)super__init__rvaluecallable TypeErrortype__name__int ValueErrorr r r r rrrrrrrr) selfr r r r rrrrrrrr __class__V/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/tversky.pyr(s 0    zTverskyLoss.__init__input torch.Tensortargetc Cs|jrt|}|jd}|jr |dkrtdnt|d}|jdur*||}|jr=|dkr7tdnt||d}|j s^|dkrJtdn|ddddf}|ddddf}|j|jkrqt d|jd|jd t d t |j }|jrd g|}t|||d|jd \}}}||j9}||j9}||j}||||j} d || } |jtjjkrt| S|jtjjkr| S|jtjjkrt| Std|jd)z Args: input: the shape should be BNH[WD]. target: the shape should be BNH[WD]. Raises: ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"]. rz2single channel prediction, `softmax=True` ignored.Nz6single channel prediction, `to_onehot_y=True` ignored.) num_classesz>single channel prediction, `include_background=False` ignored.z"ground truth has differing shape (z) from input ()rFg?zUnsupported reduction: z0, available options are ["mean", "sum", "none"].)r torchshaper warningswarnrr rr AssertionErrorarangelentolistrrrrrrrrrSUMrsumNONEMEANmeanr%) r&r+r- n_pred_ch reduce_axistpfpfn numerator denominatorscorer)r)r*forwardisH                 zTverskyLoss.forward)r r r r r r r r rrrrrrrrrrrrrr rr rr)r+r,r-r,rr,) r# __module__ __qualname____doc__rr<rrF __classcell__r)r)r'r*rs Ar) __future__rr3collections.abcrr1torch.nn.modules.lossrmonai.losses.utilsrmonai.networksr monai.utilsrrr)r)r)r*s