U uPh@s`ddlmZddlZddlmZddlZddlmZddlm Z ddl m Z GdddeZ dS) ) annotationsN)Callable)_Loss)one_hot) LossReductioncsheZdZdZdddddddejdddf dddddd d d d d dd d fd d ZddddddZZS) TverskyLossaC 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) Adapted from: https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L631 TFNg?gh㈵>boolzCallable | NonefloatzLossReduction | strNone) include_background to_onehot_ysigmoidsoftmax other_actalphabeta reduction smooth_nr smooth_drbatchreturnc stjt|jd|dk r:t|s:tdt|jdt|t|t|dk dkrbt 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`. 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 rrrrr rrr) selfr r r rrrrrrrr __class__I/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/losses/tversky.pyr$s-   zTverskyLoss.__init__z torch.Tensor)inputtargetrcCs|jrt|}|jd}|jr@|dkr4tdn t|d}|jdk rT||}|jrz|dkrntdn t||d}|j s|dkrtdn(|ddddf}|ddddf}|j|jkrt d|jd|jd |}d|}|}d|}t d t |j }|jr"d g|}t|||} |jt|||} |jt|||} | |j} | | | |j} d | | }|jtjjkrt|S|jtjjkr|S|jtjjkrt|Std |jddS)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 ()rg?zUnsupported reduction: z0, available options are ["mean", "sum", "none"].)r torchshaperwarningswarnrr rr AssertionErrorarangelentolistrsumrrrrrrSUMrNONEMEANmeanr!)r"r'r( n_pred_chp0p1Zg0g1 reduce_axistpfpfn numerator denominatorscorer%r%r&forwardasP                zTverskyLoss.forward) r __module__ __qualname____doc__rr7rrD __classcell__r%r%r#r&rs *=r) __future__rr.collections.abcrr,torch.nn.modules.lossrmonai.networksr monai.utilsrrr%r%r%r& s