o
    i(                     @  s>  d dl mZ d dlZd dlmZmZ d dlmZ d dlZ	d dl
Z
d dlmZ d dlm  mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZ G dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$G dd deZ%eZ&e#Z'e$Z(e!Z)e%Z*e"Z+dS )    )annotationsN)CallableSequence)Any)_Loss)	FocalLoss)
MaskedLoss)compute_tp_fp_fn)one_hot)DiceCEReductionLossReductionWeightlook_up_optionc                      sJ   e Zd ZdZdddddddejdddddfd! fddZd"dd Z  ZS )#DiceLossaS  
    Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks.
    The data `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]).

    Note that axis N of `input` is expected to be logits or probabilities for each class, if passing logits as input,
    must set `sigmoid=True` or `softmax=True`, or specifying `other_act`. And the same axis of `target`
    can be 1 or N (one-hot format).

    The `smooth_nr` and `smooth_dr` parameters are values added to the intersection and union components of
    the inter-over-union calculation to smooth results respectively, these values should be small.

    The original papers:

        Milletari, F. et. al. (2016) V-Net: Fully Convolutional Neural Networks for Volumetric
        Medical Image Segmentation. 3DV 2016.

        Wang, Z. et. al. (2023) Jaccard Metric Losses: Optimizing the Jaccard Index with
        Soft Labels. NeurIPS 2023.

        Wang, Z. et. al. (2023) Dice Semimetric Losses: Optimizing the Dice Score with
        Soft Labels. MICCAI 2023.

    TFNh㈵>include_backgroundboolto_onehot_ysigmoidsoftmax	other_actCallable | Nonesquared_predjaccard	reductionLossReduction | str	smooth_nrfloat	smooth_drbatchweight3Sequence[float] | float | int | torch.Tensor | None
soft_labelreturnNonec                   s   t  jt|jd |durt|stdt|j dt|t| t|du dkr1t	d|| _
|| _|| _|| _|| _|| _|| _t|	| _t|
| _|| _|dur\t|nd}| d| |  || _dS )a
  
        Args:
            include_background: if False, channel index 0 (background category) is excluded from the calculation.
                if the non-background segmentations are small compared to the total image size they can get overwhelmed
                by the signal from the background so excluding it in such cases helps convergence.
            to_onehot_y: whether to convert the ``target`` into the one-hot format,
                using the number of classes inferred from `input` (``input.shape[1]``). 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: callable function to execute other activation layers, Defaults to ``None``. for example:
                ``other_act = torch.tanh``.
            squared_pred: use squared versions of targets and predictions in the denominator or not.
            jaccard: compute Jaccard Index (soft IoU) instead of dice or not.
            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`.
            weight: weights to apply to the voxels of each class. If None no weights are applied.
                The input can be a single value (same weight for all classes), a sequence of values (the length
                of the sequence should be the same as the number of classes. If not ``include_background``,
                the number of classes should not include the background category class 0).
                The value/values should be no less than 0. Defaults to None.
            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.

        r   N*other_act must be None or callable but is .   XIncompatible values: more than 1 of [sigmoid=True, softmax=True, other_act is not None].class_weight)super__init__r   valuecallable	TypeErrortype__name__int
ValueErrorr   r   r   r   r   r   r   r   r   r   r   torch	as_tensorregister_bufferr"   )selfr   r   r   r   r   r   r   r   r   r   r   r    r"   	__class__ S/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/dice.pyr,   8   s&   7 


zDiceLoss.__init__inputtorch.Tensortargetc                 C  s  | j rt |}|jd }| jr |dkrtd nt|d}| jdur*| |}| jr=|dkr7td nt||d}| j	s^|dkrJtd n|ddddf }|ddddf }|j|jkrqt
d|j d|j d	td
t|j }| jrdg| }| jrd
nd}t||||| j\}}}| js|d9 }|d9 }d
| | j }	d
|| |  | j }
d|	|
  }|jd }| jdur|dkr| jjdkrt| jg| | _n| jjd |krtd| j dk rtd|| j| }| jtjjkrt|}|S | jtj jkrt!|}|S | jtj"jkr8t#|jdd
 dgt|jd
   }|$|}|S td| j d)a  
        Args:
            input: the shape should be BNH[WD], where N is the number of classes.
            target: the shape should be BNH[WD] or B1H[WD], where N is the number of classes.

        Raises:
            AssertionError: When input and target (after one hot transform if set)
                have different shapes.
            ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].

        Example:
            >>> from monai.losses.dice import *  # NOQA
            >>> import torch
            >>> from monai.losses.dice import DiceLoss
            >>> B, C, H, W = 7, 5, 3, 2
            >>> input = torch.rand(B, C, H, W)
            >>> target_idx = torch.randint(low=0, high=C - 1, size=(B, H, W)).long()
            >>> target = one_hot(target_idx[:, None, ...], num_classes=C)
            >>> self = DiceLoss(reduction='none')
            >>> loss = self(input, target)
            >>> assert np.broadcast_shapes(loss.shape, input.shape) == input.shape
        r(   2single channel prediction, `softmax=True` ignored.N6single channel prediction, `to_onehot_y=True` ignored.num_classes>single channel prediction, `include_background=False` ignored.z"ground truth has different shape () from input ()   r         ?zthe length of the `weight` sequence should be the same as the number of classes.
                        If `include_background=False`, the weight should not include
                        the background category class 0.z:the value/values of the `weight` should be no less than 0.Unsupported reduction: 0, available options are ["mean", "sum", "none"].)%r   r4   shaper   warningswarnr   r   r
   r   AssertionErrorarangelentolistr   r   r	   r"   r   r   r   r*   ndimr5   r3   mintor   r   MEANr-   meanSUMsumNONElistview)r7   r<   r>   	n_pred_chreduce_axisordtpfpfn	numeratordenominatorfZnum_of_classesbroadcast_shaper:   r:   r;   forward   sj   







	&
zDiceLoss.forward)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r   r#   r$   r<   r=   r>   r=   r#   r=   )	r1   
__module____qualname____doc__r   rT   r,   re   __classcell__r:   r:   r8   r;   r      s"    Kr   c                      s.   e Zd ZdZd fddZddddZ  ZS )MaskedDiceLossa  
    Add an additional `masking` process before `DiceLoss`, accept a binary mask ([0, 1]) indicating a region,
    `input` and `target` will be masked by the region: region with mask `1` will keep the original value,
    region with `0` mask will be converted to `0`. Then feed `input` and `target` to normal `DiceLoss` computation.
    This has the effect of ensuring only the masked region contributes to the loss computation and
    hence gradient calculation.

    argsr   kwargsr#   r$   c                   s&   t  j|i | tt  jd| _dS )z@
        Args follow :py:class:`monai.losses.DiceLoss`.
        )lossN)r+   r,   r   re   spatial_weighted)r7   rl   rm   r8   r:   r;   r,      s   zMaskedDiceLoss.__init__Nr<   r=   r>   masktorch.Tensor | Nonec                 C  s   | j |||dS )z
        Args:
            input: the shape should be BNH[WD].
            target: the shape should be BNH[WD].
            mask: the shape should B1H[WD] or 11H[WD].
        )r<   r>   rp   )ro   )r7   r<   r>   rp   r:   r:   r;   re      s   zMaskedDiceLoss.forward)rl   r   rm   r   r#   r$   N)r<   r=   r>   r=   rp   rq   r#   r=   r1   rg   rh   ri   r,   re   rj   r:   r:   r8   r;   rk      s    	rk   c                      sP   e Zd ZdZdddddejejddddfd! fddZdd Z	d"dd Z
  ZS )#GeneralizedDiceLossa>  
    Compute the generalised Dice loss defined in:

        Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning
        loss function for highly unbalanced segmentations. DLMIA 2017.

    Adapted from:
        https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L279
    TFNr   r   r   r   r   r   r   r   w_typeWeight | strr   r   r   r   r   r   r"   r#   r$   c                   s   t  jt|jd |durt|stdt|j dt|t| t|du dkr1t	d|| _
|| _|| _|| _|| _t|t| _t|| _t|	| _|
| _|| _dS )a  
        Args:
            include_background: If False channel index 0 (background category) is excluded from the calculation.
            to_onehot_y: whether to convert the ``target`` into the one-hot format,
                using the number of classes inferred from `input` (``input.shape[1]``). 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: callable function to execute other activation layers, Defaults to ``None``. for example:
                ``other_act = torch.tanh``.
            w_type: {``"square"``, ``"simple"``, ``"uniform"``}
                Type of function to transform ground truth volume to a weight factor. Defaults to ``"square"``.
            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, intersection over union is computed from each item in the batch.
                If True, the class-weighted intersection and union areas are first summed across the batches.
            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.

        r%   Nr&   r'   r(   r)   )r+   r,   r   r-   r.   r/   r0   r1   r2   r3   r   r   r   r   r   r   r   ru   r   r   r   r   r"   )r7   r   r   r   r   r   ru   r   r   r   r   r"   r8   r:   r;   r,     s   - 


zGeneralizedDiceLoss.__init__c                 C  sB   | j ttjkrt|S | j ttjkrt|| S t|S rr   )ru   strr   SIMPLEr4   
reciprocalSQUARE	ones_like)r7   Zgrndr:   r:   r;   w_funcN  s
   

zGeneralizedDiceLoss.w_funcr<   r=   r>   c                 C  s  | j rt |}|jd }| jr |dkrtd nt|d}| jdur*| |}| jr=|dkr7td nt||d}| j	s^|dkrJtd n|ddddf }|ddddf }|j|jkrqt
d|j d|j d	td
t|j }| jrdg| }t|||d| j\}}}|d9 }|d9 }d
|| |  }t||}	| |	 }
t|
}| jrd|
|< |
|t|
  }
nd|
|< tj|
ddd jdd}|
||  }
| jrdnd}d||
 j|dd | j }||
 j|dd| j }d||  }| jtjjkrt|}|S | jtjjkrt|}|S | jtjjkrAt |jdd
 dgt|jd
   }|!|}|S t"d| j d)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"].

        r(   r?   Nr@   rA   rC   z"ground truth has differing shape (rD   rE   rF   r   rG           dim       @T)keepdim      ?rH   rI   )#r   r4   rJ   r   rK   rL   r   r   r
   r   rM   rN   rO   rP   r   r	   r"   rW   r|   r   isinfmax	unsqueezer   r   r   r   rT   r-   rU   rV   rX   rY   rZ   r3   )r7   r<   r>   r[   r\   r^   r_   r`   rb   Zground_owinfsZ
max_valuesZfinal_reduce_dimnumerdenomrc   rd   r:   r:   r;   re   U  sf   








	&
zGeneralizedDiceLoss.forward)r   r   r   r   r   r   r   r   r   r   ru   rv   r   r   r   r   r   r   r   r   r"   r   r#   r$   rf   )r1   rg   rh   ri   r   rz   r   rT   r,   r|   re   rj   r:   r:   r8   r;   rt     s     @rt   c                      s`   e Zd ZdZdejddfd" fddZd#ddZd$ddZd%ddZ	d%ddZ
d&d d!Z  ZS )'GeneralizedWassersteinDiceLossa{  
    Compute the generalized Wasserstein Dice Loss defined in:

        Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class
        Segmentation using Holistic Convolutional Networks. BrainLes 2017.

    Or its variant (use the option weighting_mode="GDL") defined in the Appendix of:

        Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic
        segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients.
        arXiv preprint arXiv:2007.15546

    Adapted from:
        https://github.com/LucasFidon/GeneralizedWassersteinDiceLoss
    defaultr   dist_matrixnp.ndarray | torch.Tensorweighting_moderw   r   r   r   r   r   r#   r$   c                   s   t  jt|jd |jd |jd kr%td|jd  d|jd  d|dvr/td| || _t| jtj	r@t
| j| _t
| jdkrR| jt
| j | _|| _| jd| _t|| _t|| _d	S )
a	  
        Args:
            dist_matrix: 2d tensor or 2d numpy array; matrix of distances between the classes.
            It must have dimension C x C where C is the number of classes.
            weighting_mode: {``"default"``, ``"GDL"``}
                Specifies how to weight the class-specific sum of errors.
                Default to ``"default"``.

                - ``"default"``: (recommended) use the original weighting method as in:
                    Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class
                    Segmentation using Holistic Convolutional Networks. BrainLes 2017.
                - ``"GDL"``: use a GDL-like weighting method as in the Appendix of:
                    Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic
                    segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients.
                    arXiv preprint arXiv:2007.15546
            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.

        Raises:
            ValueError: When ``dist_matrix`` is not a square matrix.

        Example:
            .. code-block:: python

                import torch
                import numpy as np
                from monai.losses import GeneralizedWassersteinDiceLoss

                # Example with 3 classes (including the background: label 0).
                # The distance between the background class (label 0) and the other classes is the maximum, equal to 1.
                # The distance between class 1 and class 2 is 0.5.
                dist_mat = np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 0.5], [1.0, 0.5, 0.0]], dtype=np.float32)
                wass_loss = GeneralizedWassersteinDiceLoss(dist_matrix=dist_mat)

                pred_score = torch.tensor([[1000, 0, 0], [0, 1000, 0], [0, 0, 1000]], dtype=torch.float32)
                grnd = torch.tensor([0, 1, 2], dtype=torch.int64)
                wass_loss(pred_score, grnd)  # 0

        r%   r   r(   zdist_matrix must be C x C, got z x r'   )r   GDLz8weighting_mode must be either 'default' or 'GDL, got %s.N)r+   r,   r   r-   rJ   r3   m
isinstancenpndarrayr4   
from_numpyr   
alpha_modesizerB   r   r   r   )r7   r   r   r   r   r   r8   r:   r;   r,     s   5"
z'GeneralizedWassersteinDiceLoss.__init__r<   r=   r>   c                 C  sJ  | |d|dd}| |dd }tj|dd}| ||}| |}| jdkr>| |||}| 	|||}	n| |||}t
j|dd}
d| |
 }	d| | j |	| j  }d| }| jtjjkrpt
|}|S | jtjjkr~t
|}|S | jtjjkr|jdd d	t|jd   }||}|S td
| j d)zy
        Args:
            input: the shape should be BNH[WD].
            target: the shape should be BNH[WD].

        r   r(   r~   r   rF   r   r   )r(   rH   rI   )reshaper   longFr   wasserstein_distance_map)_compute_alpha_generalized_true_positivesr   "_compute_generalized_true_positive_compute_denominatorr4   rW   r   r   r   r   rT   r-   rU   rV   rX   rJ   rO   rZ   r3   )r7   r<   r>   
flat_inputflat_targetprobsZwass_dist_mapalphaZtrue_posr   Z	all_errorZ	wass_diceZwass_dice_lossrd   r:   r:   r;   re     s0   



	 
z&GeneralizedWassersteinDiceLoss.forward
flat_probar   c                 C  s   t t | j|j}t j|dd}t j|dd}||d|d|d|df}t j|dd}||d|d|df}t j|dd}t j	|d|d}t j
|dd}|| }t j|dd}|S )a(  
        Compute the voxel-wise Wasserstein distance between the
        flattened prediction and the flattened labels (ground_truth) with respect
        to the distance matrix on the label space M.
        This corresponds to eq. 6 in:

            Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class
            Segmentation using Holistic Convolutional Networks. BrainLes 2017.

        Args:
            flat_proba: the probabilities of input(predicted) tensor.
            flat_target: the target tensor.
        r   r~      r(   rF   )r   index)r4   cloner5   r   rS   devicer   expandr   gathersqueezerW   )r7   r   r   r   Z
m_extendedflat_target_extendedZwasserstein_mapr:   r:   r;   r   0  s   *z7GeneralizedWassersteinDiceLoss.wasserstein_distance_mapr   r   c                 C  d   t j|dd}||d| j|df}t j|dd}t j||dd}t j|d|  ddgdS )
        Args:
            alpha: generalised number of true positives of target class.
            flat_target: the target tensor.
            wasserstein_distance_map: the map obtained from the above function.
        rF   r~   r   r(   r   r   r   r4   r   r   r   rB   r   rW   r7   r   r   r   Zalpha_extendedr   r:   r:   r;   r   V  
   
zAGeneralizedWassersteinDiceLoss._compute_generalized_true_positivec                 C  r   )r   rF   r~   r   r(   r   r   r   r   r:   r:   r;   r   g  r   z3GeneralizedWassersteinDiceLoss._compute_denominatorc                 C  s|   t |d| jf |j}| jdkr4tj	|| jd
ddd }t j|dd}d|d  }|S d|d	d	df< |S )
zC
        Args:
            flat_target: the target tensor.
        r   r   rA   rF   r(   r~   r   r}   N)r4   onesr   rB   r   rS   r   r   r   r
   permuterW   )r7   r   r   Z	one_hot_fZvolumesr:   r:   r;   r   x  s   "
zHGeneralizedWassersteinDiceLoss._compute_alpha_generalized_true_positives)r   r   r   rw   r   r   r   r   r   r   r#   r$   rf   )r   r=   r   r=   r#   r=   )r   r=   r   r=   r   r=   r#   r=   )r   r=   r#   r=   )r1   rg   rh   ri   r   rT   r,   re   r   r   r   r   rj   r:   r:   r8   r;   r     s    
G
3
&
r   c                      s`   e Zd ZdZ															d*d+ fdd Zd,d$d%Zd,d&d'Zd,d(d)Z  ZS )-
DiceCELossa  
    Compute both Dice loss and Cross Entropy Loss, and return the weighted sum of these two losses.
    The details of Dice loss is shown in ``monai.losses.DiceLoss``.
    The details of Cross Entropy Loss is shown in ``torch.nn.CrossEntropyLoss`` and ``torch.nn.BCEWithLogitsLoss()``.
    In this implementation, two deprecated parameters ``size_average`` and ``reduce``, and the parameter ``ignore_index`` are
    not supported.

    TFNrU   r   r   r}   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r   r    rq   lambda_dice	lambda_celabel_smoothingr#   r$   c                   s   t    t|tj}|dur|s|dd }n|}t|||||||||	|
||d| _tj|||d| _	tj
||d| _|dk rDtd|dk rLtd|| _|| _dS )	a!  
        Args:
            ``lambda_ce`` are only used for cross entropy loss.
            ``reduction`` and ``weight`` is used for both losses and other parameters are only used for dice loss.

            include_background: if False channel index 0 (background category) is excluded from the calculation.
            to_onehot_y: whether to convert the ``target`` into the one-hot format,
                using the number of classes inferred from `input` (``input.shape[1]``). Defaults to False.
            sigmoid: if True, apply a sigmoid function to the prediction, only used by the `DiceLoss`,
                don't need to specify activation function for `CrossEntropyLoss` and `BCEWithLogitsLoss`.
            softmax: if True, apply a softmax function to the prediction, only used by the `DiceLoss`,
                don't need to specify activation function for `CrossEntropyLoss` and `BCEWithLogitsLoss`.
            other_act: callable function to execute other activation layers, Defaults to ``None``. for example:
                ``other_act = torch.tanh``. only used by the `DiceLoss`, not for the `CrossEntropyLoss` and `BCEWithLogitsLoss`.
            squared_pred: use squared versions of targets and predictions in the denominator or not.
            jaccard: compute Jaccard Index (soft IoU) instead of dice or not.
            reduction: {``"mean"``, ``"sum"``}
                Specifies the reduction to apply to the output. Defaults to ``"mean"``. The dice loss should
                as least reduce the spatial dimensions, which is different from cross entropy loss, thus here
                the ``none`` option cannot be used.

                - ``"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`.
            weight: a rescaling weight given to each class for cross entropy loss for `CrossEntropyLoss`.
                or a weight of positive examples to be broadcasted with target used as `pos_weight` for `BCEWithLogitsLoss`.
                See ``torch.nn.CrossEntropyLoss()`` or ``torch.nn.BCEWithLogitsLoss()`` for more information.
                The weight is also used in `DiceLoss`.
            lambda_dice: the trade-off weight value for dice loss. The value should be no less than 0.0.
                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.

        Nr(   r   r   r   r   r   r   r   r   r   r   r   r    )r    r   r   )
pos_weightr   r}   'lambda_dice should be no less than 0.0.z%lambda_ce should be no less than 0.0.)r+   r,   r   r   r-   r   dicennCrossEntropyLosscross_entropyBCEWithLogitsLossbinary_cross_entropyr3   r   r   )r7   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   Zdice_weightr8   r:   r;   r,     s6   
<
zDiceCELoss.__init__r<   r=   r>   c                 C  sb   |j d |j d }}||kr|dkrtj|dd}| }nt|s+|j|jd}| ||S )a  
        Compute CrossEntropy loss for the input logits and target.
        Will remove the channel dim according to PyTorch CrossEntropyLoss:
        https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?#torch.nn.CrossEntropyLoss.

        r(   r~   dtype)rJ   r4   r   r   is_floating_pointrS   r   r   )r7   r<   r>   r[   Zn_target_chr:   r:   r;   ce  s   

zDiceCELoss.cec                 C  s$   t |s|j|jd}| ||S )zh
        Compute Binary CrossEntropy loss for the input logits and target in one single class.

        r   )r4   r   rS   r   r   )r7   r<   r>   r:   r:   r;   bce  s   
zDiceCELoss.bcec              
   C  s   |  |  kr!td|j dt|j d|j dt|j d	|jd dkr?|jd |jd kr?td|j d|j d| ||}|jd dkrR| ||n| ||}| j| | j|  }|S )	a  
        Args:
            input: the shape should be BNH[WD].
            target: the shape should be BNH[WD] or B1H[WD].

        Raises:
            ValueError: When number of dimensions for input and target are different.
            ValueError: When number of channels for target is neither 1 (without one-hot encoding) nor the same as input.

        Returns:
            torch.Tensor: value of the loss.

        Lthe number of dimensions for input and target should be the same, got shape  (nb dims: ) and P). if target is not one-hot encoded, please provide a tensor with shape B1H[WD].r(   gnumber of channels for target is neither 1 (without one-hot encoding) nor the same as input, got shape  and r'   )	r   r3   rJ   rO   r   r   r   r   r   )r7   r<   r>   	dice_lossce_loss
total_lossr:   r:   r;   re     s0   "&zDiceCELoss.forward)TFFFNFFrU   r   r   FNr   r   r}   ) r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r   r   r   r    rq   r   r   r   r   r   r   r#   r$   rf   )	r1   rg   rh   ri   r,   r   r   re   rj   r:   r:   r8   r;   r     s*    
Z

r   c                      sN   e Zd ZdZ																d(d) fd!d"Zd*d&d'Z  ZS )+DiceFocalLossa  
    Compute both Dice loss and Focal Loss, and return the weighted sum of these two losses.
    The details of Dice loss is shown in ``monai.losses.DiceLoss``.
    The details of Focal Loss is shown in ``monai.losses.FocalLoss``.

    ``gamma`` and ``lambda_focal`` are only used for the focal loss.
    ``include_background``, ``weight``, ``reduction``, and ``alpha`` are used for both losses,
    and other parameters are only used for dice loss.

    TFNrU   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r   gammar    r!   r   lambda_focalr   float | Noner#   r$   c                   sx   t    t|d|||||||	|
||d| _t|d||||d| _|dk r)td|dk r1td|| _|| _|| _	dS )af
  
        Args:
            include_background: if False channel index 0 (background category) is excluded from the calculation.
            to_onehot_y: whether to convert the ``target`` into the one-hot format,
                using the number of classes inferred from `input` (``input.shape[1]``). Defaults to False.
            sigmoid: if True, apply a sigmoid function to the prediction, only used by the `DiceLoss`,
                don't need to specify activation function for `FocalLoss`.
            softmax: if True, apply a softmax function to the prediction, only used by the `DiceLoss`,
                don't need to specify activation function for `FocalLoss`.
            other_act: callable function to execute other activation layers, Defaults to ``None``.
                for example: `other_act = torch.tanh`. only used by the `DiceLoss`, not for `FocalLoss`.
            squared_pred: use squared versions of targets and predictions in the denominator or not.
            jaccard: compute Jaccard Index (soft IoU) instead of dice or not.
            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`.
            gamma: value of the exponent gamma in the definition of the Focal loss.
            weight: weights to apply to the voxels of each class. If None no weights are applied.
                The input can be a single value (same weight for all classes), a sequence of values (the length
                of the sequence should be the same as the number of classes).
            lambda_dice: the trade-off weight value for dice loss. The value should be no less than 0.0.
                Defaults to 1.0.
            lambda_focal: the trade-off weight value for focal loss. The value should be no less than 0.0.
                Defaults to 1.0.
            alpha: value of the alpha in the definition of the alpha-balanced Focal loss. The value should be in
                [0, 1]. Defaults to None.
        Fr   )r   r   r   r    r   r   r}   r   (lambda_focal should be no less than 0.0.N)
r+   r,   r   r   r   focalr3   r   r   r   )r7   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r8   r:   r;   r,   6  s<   
7
zDiceFocalLoss.__init__r<   r=   r>   c              
   C  s   |  |  kr!td|j dt|j d|j dt|j d	|jd dkr?|jd |jd kr?td|j d|j d| jrW|jd }|dkrQtd	 nt||d
}| ||}| 	||}| j
| | j|  }|S )a  
        Args:
            input: the shape should be BNH[WD]. The input should be the original logits
                due to the restriction of ``monai.losses.FocalLoss``.
            target: the shape should be BNH[WD] or B1H[WD].

        Raises:
            ValueError: When number of dimensions for input and target are different.
            ValueError: When number of channels for target is neither 1 (without one-hot encoding) nor the same as input.

        Returns:
            torch.Tensor: value of the loss.
        r   r   r   r   r(   r   r   r'   r@   rA   )r   r3   rJ   rO   r   rK   rL   r
   r   r   r   r   )r7   r<   r>   r[   r   
focal_lossr   r:   r:   r;   re     s:   "
zDiceFocalLoss.forward)TFFFNFFrU   r   r   Fr   Nr   r   N)"r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r   r   r   r   r   r    r!   r   r   r   r   r   r   r#   r$   rf   rs   r:   r:   r8   r;   r   *  s(    Vr   c                      sN   e Zd ZdZdddddejejdddddddfd% fddZd&d#d$Z	  Z
S )'GeneralizedDiceFocalLossa  Compute both Generalized Dice Loss and Focal Loss, and return their weighted average. The details of Generalized Dice Loss
    and Focal Loss are available at ``monai.losses.GeneralizedDiceLoss`` and ``monai.losses.FocalLoss``.

    Args:
        include_background (bool, optional): if False channel index 0 (background category) is excluded from the calculation.
            Defaults to True.
        to_onehot_y: whether to convert the ``target`` into the one-hot format,
            using the number of classes inferred from `input` (``input.shape[1]``). Defaults to False.
        sigmoid (bool, optional): if True, apply a sigmoid function to the prediction. Defaults to False.
        softmax (bool, optional): if True, apply a softmax function to the prediction. Defaults to False.
        other_act (Optional[Callable], optional): callable function to execute other activation layers,
            Defaults to ``None``. for example: `other_act = torch.tanh`.
            only used by the `GeneralizedDiceLoss`, not for the `FocalLoss`.
        w_type (Union[Weight, str], optional): {``"square"``, ``"simple"``, ``"uniform"``}. Type of function to transform
            ground-truth volume to a weight factor. Defaults to ``"square"``.
        reduction (Union[LossReduction, str], optional): {``"none"``, ``"mean"``, ``"sum"``}. Specified 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 (float, optional): a small constant added to the numerator to avoid zero. Defaults to 1e-5.
        smooth_dr (float, optional): a small constant added to the denominator to avoid nan. Defaults to 1e-5.
        batch (bool, optional): whether to sum the intersection and union areas over the batch dimension before the dividing.
            Defaults to False, i.e., the areas are computed for each item in the batch.
        gamma (float, optional): value of the exponent gamma in the definition of the Focal loss. Defaults to 2.0.
        weight (Optional[Union[Sequence[float], float, int, torch.Tensor]], optional): weights to apply to
            the voxels of each class. If None no weights are applied. The input can be a single value
            (same weight for all classes), a sequence of values (the length of the sequence hould be the same as
            the number of classes). Defaults to None.
        lambda_gdl (float, optional): the trade-off weight value for Generalized Dice Loss. The value should be
            no less than 0.0. Defaults to 1.0.
        lambda_focal (float, optional): the trade-off weight value for Focal Loss. The value should be no less
            than 0.0. Defaults to 1.0.

    Raises:
        ValueError: if either `lambda_gdl` or `lambda_focal` is less than 0.
    TFNr   r   r   r   r   r   r   r   r   r   ru   rv   r   r   r   r   r   r   r   r    r!   
lambda_gdlr   r#   r$   c                   sl   t    t|||||||||	|
d
| _t|||||d| _|dk r&td|dk r.td|| _|| _d S )N)
r   r   r   r   r   ru   r   r   r   r   )r   r   r   r    r   r}   z&lambda_gdl should be no less than 0.0.r   )	r+   r,   rt   generalized_dicer   r   r3   r   r   )r7   r   r   r   r   r   ru   r   r   r   r   r   r    r   r   r8   r:   r;   r,     s4   

z!GeneralizedDiceFocalLoss.__init__r<   r=   r>   c              
   C  s   |  |  kr!td|j dt|j d|j dt|j d	|jd dkr?|jd |jd kr?td|j d|j d| ||}| ||}| j| | j|  }|S )	a/  
        Args:
            input (torch.Tensor): the shape should be BNH[WD]. The input should be the original logits
                due to the restriction of ``monai.losses.FocalLoss``.
            target (torch.Tensor): the shape should be BNH[WD] or B1H[WD].

        Raises:
            ValueError: When number of dimensions for input and target are different.
            ValueError: When number of channels for target is neither 1 (without one-hot encoding) nor the same as input.

        Returns:
            torch.Tensor: value of the loss.
        r   r   r   r   r(   r   r   r'   )r   r3   rJ   rO   r   r   r   r   )r7   r<   r>   Zgdl_lossr   r   r:   r:   r;   re     s0   "z GeneralizedDiceFocalLoss.forward)r   r   r   r   r   r   r   r   r   r   ru   rv   r   r   r   r   r   r   r   r   r   r   r    r!   r   r   r   r   r#   r$   rf   )r1   rg   rh   ri   r   rz   r   rT   r,   re   rj   r:   r:   r8   r;   r     s$    (,r   ),
__future__r   rK   collections.abcr   r   typingr   numpyr   r4   torch.nnr   torch.nn.functional
functionalr   torch.nn.modules.lossr   Zmonai.losses.focal_lossr   Zmonai.losses.spatial_maskr   Zmonai.losses.utilsr	   monai.networksr
   monai.utilsr   r   r   r   r   rk   rt   r   r   r   r   Dicedice_ce
dice_focalr   generalized_dice_focalgeneralized_wasserstein_dicer:   r:   r:   r;   <module>   s@    J # f ! 
t