U
    tPÓh¸  ã                   @  s®   d dl mZ d dlZd dlm  mZ d dlmZ dddœdd„Z	dddœdd	„Z
dddœd
d„Zddddœdd„Zddddddœdd„ZG dd„ deƒZG dd„ deƒZdS )é    )ÚannotationsN)Ú_Lossútorch.Tensor)ÚimgÚreturnc                 C  s¤   t | jƒdkrBt |  ddd¡ }t |  ddd¡ }t ||¡S t | jƒdkr t |  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   úH/home/dell461/cl/sdc2/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   Úeroded_imageZdilated_imager   r   r   Ú	soft_open8   s    
r   Úint)r   Úiter_r   c                 C  s\   t | ƒ}t | | ¡}t|ƒD ]8}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%   ç      ð?Úfloat)Úy_trueÚy_predÚsmoothr   c                 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dddœ‡ fdd	„Zd
d
d
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'   ÚNone)r   r*   r   c                   s   t ƒ  ¡  || _|| _dS )z{
        Args:
            iter_: Number of iterations for skeletonization
            smooth: Smoothing parameter
        N)ÚsuperÚ__init__Úiterr*   )Úselfr   r*   ©Ú	__class__r   r   r3   ~   s    
zSoftclDiceLoss.__init__r   ©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%   r4   r   r,   Úmultiplyr*   )r5   r(   r)   Ú	skel_predÚ	skel_trueÚtprecÚtsensÚcl_dicer   r   r   Úforwardˆ   s    ( ÿ( ÿzSoftclDiceLoss.forward)r   r&   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r@   Ú__classcell__r   r   r6   r   r0   s   s   

r0   c                      s@   e Zd ZdZddddddœ‡ fd	d
„Zddd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'   r1   )r   Úalphar*   r   c                   s    t ƒ  ¡  || _|| _|| _dS )z©
        Args:
            iter_: Number of iterations for skeletonization
            smooth: Smoothing parameter
            alpha: Weighing factor for cldice
        N)r2   r3   r4   r*   rI   )r5   r   rI   r*   r6   r   r   r3       s    
zSoftDiceclDiceLoss.__init__r   r8   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 r9   )r/   r*   r%   r4   r   r,   r:   rI   )
r5   r(   r)   Údicer;   r<   r=   r>   r?   Z
total_lossr   r   r   r@   ¬   s    ( ÿ( ÿzSoftDiceclDiceLoss.forward)r   rH   r&   rA   r   r   r6   r   rG   •   s   
rG   )r&   )Ú
__future__r   r   Útorch.nn.functionalÚnnÚ
functionalr   Útorch.nn.modules.lossr   r   r   r   r%   r/   r0   rG   r   r   r   r   Ú<module>   s   "