o
    i¸  ã                   @  sŠ   d dl mZ d dlZd dlm  mZ d dlmZ ddd„Z	dd	d
„Z
ddd„Zddd„Zdddd„ZG dd„ deƒZG dd„ deƒZdS ) é    )ÚannotationsN)Ú_LossÚimgútorch.TensorÚreturnc                 C  s¤   t | jƒdkr!t |  ddd¡ }t |  ddd¡ }t ||¡S t | jƒdkrPt |  dd	d
¡ }t |  dd	d¡ }t |  dd	d¡ }t t ||¡|¡S dS )zÕ
    Perform soft erosion on the input image

    Args:
        img: the shape should be BCH(WD)

    Adapted from:
        https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L6
    é   )é   é   ©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	   N)ÚlenÚshapeÚFÚ
max_pool2dÚtorchÚminÚ
max_pool3d)r   Úp1Úp2Úp3© r   úU/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/cldice.pyÚ
soft_erode   s   
ür   c                 C  s@   t | jƒdkrt | ddd¡S t | jƒdkrt | ddd¡S dS )z×
    Perform soft dilation on the input image

    Args:
        img: the shape should be BCH(WD)

    Adapted from:
        https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L18
    r   )r   r   r
   r   )r   r   r   r   N)r   r   r   r   r   )r   r   r   r   Úsoft_dilate(   s
   
ÿr   c                 C  s   t | ƒ}t|ƒ}|S )zê
    Wrapper function to perform soft opening on the input image

    Args:
        img: the shape should be BCH(WD)

    Adapted from:
        https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L25
    )r   r   )r   Zeroded_imageZdilated_imager   r   r   Ú	soft_open8   s   
r   Úiter_Úintc                 C  s\   t | ƒ}t | | ¡}t|ƒD ]}t| ƒ} t | ƒ}t | | ¡}|t |||  ¡ }q|S )a>  
    Perform soft skeletonization on the input image

    Adapted from:
       https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L29

    Args:
        img: the shape should be BCH(WD)
        iter_: number of iterations for skeletonization

    Returns:
        skeletonized image
    )r   r   ÚreluÚranger   )r   r   Úimg1ÚskelÚ_Údeltar   r   r   Ú	soft_skelG   s   r$   ç      ð?Úy_trueÚy_predÚsmoothÚfloatc                 C  st   t  | | dd…dd…df ¡}d| | t  | dd…dd…df ¡t  |dd…dd…df ¡ |  }d| }|S )a  
    Function to compute soft dice loss

    Adapted from:
        https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/cldice.py#L22

    Args:
        y_true: the shape should be BCH(WD)
        y_pred: the shape should be BCH(WD)

    Returns:
        dice loss
    Nr	   .ç       @r%   )r   Úsum)r&   r'   r(   ÚintersectionÚcoeffÚ	soft_dicer   r   r   r.   _   s    Hr.   c                      s.   e Zd ZdZdd‡ fd
d„Zddd„Z‡  ZS )ÚSoftclDiceLossa:  
    Compute the Soft clDice loss defined in:

        Shit et al. (2021) clDice -- A Novel Topology-Preserving Loss Function
        for Tubular Structure Segmentation. (https://arxiv.org/abs/2003.07311)

    Adapted from:
        https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/cldice.py#L7
    r   r%   r   r   r(   r)   r   ÚNonec                   s   t ƒ  ¡  || _|| _dS )z{
        Args:
            iter_: Number of iterations for skeletonization
            smooth: Smoothing parameter
        N)ÚsuperÚ__init__Úiterr(   )Úselfr   r(   ©Ú	__class__r   r   r2   ~   s   

zSoftclDiceLoss.__init__r&   r   r'   c                 C  sÌ   t || jƒ}t || jƒ}t t ||¡d d …dd …df ¡| j t |d d …dd …df ¡| j  }t t ||¡d d …dd …df ¡| j t |d d …dd …df ¡| j  }dd||  ||   }|S ©Nr	   .r%   r*   )r$   r3   r   r+   Úmultiplyr(   )r4   r&   r'   Ú	skel_predÚ	skel_trueÚtprecÚtsensÚcl_dicer   r   r   Úforwardˆ   s   ( ÿ( ÿzSoftclDiceLoss.forward)r   r%   )r   r   r(   r)   r   r0   ©r&   r   r'   r   r   r   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r>   Ú__classcell__r   r   r5   r   r/   s   s    

r/   c                      s.   e Zd ZdZdd‡ fdd„Zddd„Z‡  ZS )ÚSoftDiceclDiceLossa;  
    Compute the Soft clDice loss defined in:

        Shit et al. (2021) clDice -- A Novel Topology-Preserving Loss Function
        for Tubular Structure Segmentation. (https://arxiv.org/abs/2003.07311)

    Adapted from:
        https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/cldice.py#L38
    r   ç      à?r%   r   r   Úalphar)   r(   r   r0   c                   s    t ƒ  ¡  || _|| _|| _dS )z©
        Args:
            iter_: Number of iterations for skeletonization
            smooth: Smoothing parameter
            alpha: Weighing factor for cldice
        N)r1   r2   r3   r(   rH   )r4   r   rH   r(   r5   r   r   r2       s   

zSoftDiceclDiceLoss.__init__r&   r   r'   c           
      C  sò   t ||| jƒ}t|| jƒ}t|| jƒ}t t ||¡d d …dd …df ¡| j t |d d …dd …df ¡| j  }t t ||¡d d …dd …df ¡| j t |d d …dd …df ¡| j  }dd||  ||   }d| j | | j|  }	|	S r7   )r.   r(   r$   r3   r   r+   r8   rH   )
r4   r&   r'   Údicer9   r:   r;   r<   r=   Z
total_lossr   r   r   r>   ¬   s   ( ÿ( ÿzSoftDiceclDiceLoss.forward)r   rG   r%   )r   r   rH   r)   r(   r)   r   r0   r?   r@   r   r   r5   r   rF   •   s    
rF   )r   r   r   r   )r   r   r   r   r   r   )r%   )r&   r   r'   r   r(   r)   r   r   )Ú
__future__r   r   Útorch.nn.functionalÚnnÚ
functionalr   Útorch.nn.modules.lossr   r   r   r   r$   r.   r/   rF   r   r   r   r   Ú<module>   s   



"