U tPÓh¸ã@s®ddlmZddlZddlmmZddlmZdddœdd„Z dddœdd „Z dddœd d „Z dd dd œdd„Z ddddddœdd„Z Gdd„deƒZGdd„deƒZdS)é)Ú annotationsN)Ú_Lossú torch.Tensor)ÚimgÚreturncCs¤t|jƒdkrBt | ddd¡ }t | ddd¡ }t ||¡St|jƒdkr t | dd d ¡ }t | d d d ¡ }t | d d d¡ }t t ||¡|¡SdS)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)rr é)rr r ©r r r )r rr)r rr )rr r)r r r)rrr 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_erodes  rcCs@t|jƒdkrt |ddd¡St|jƒdkr 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 )rrÚreluÚranger)rrÚimg1ÚskelÚ_ÚdeltarrrÚ soft_skelGs r%çð?Úfloat)Úy_trueÚy_predÚsmoothrcCstt ||dd…dd…df¡}d||t |dd…dd…df¡t |dd…dd…df¡|}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_dicerrrr/_s Hr/cs>eZdZdZdddddœ‡fdd „ Zd d d d œd d „Z‡ZS)Ú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 rr&rr'ÚNone)rr*rcstƒ ¡||_||_dS)z{ Args: iter_: Number of iterations for skeletonization smooth: Smoothing parameter N)ÚsuperÚ__init__Úiterr*)Úselfrr*©Ú __class__rrr3~s zSoftclDiceLoss.__init__r©r(r)rcCsÌt||jƒ}t||jƒ}t t ||¡dd…dd…df¡|jt |dd…dd…df¡|j}t t ||¡dd…dd…df¡|jt |dd…dd…df¡|j}dd||||}|S©Nr .r&r+)r%r4rr,Úmultiplyr*)r5r(r)Ú skel_predÚ skel_trueÚtprecÚtsensÚcl_dicerrrÚforwardˆs  ( ÿ( ÿzSoftclDiceLoss.forward)rr&©Ú__name__Ú __module__Ú __qualname__Ú__doc__r3r@Ú __classcell__rrr6rr0ss  r0cs@eZdZdZddddddœ‡fd d „ Zd d d d œd d„Z‡ZS)Ú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&rr'r1)rÚalphar*rcs tƒ ¡||_||_||_dS)z© Args: iter_: Number of iterations for skeletonization smooth: Smoothing parameter alpha: Weighing factor for cldice N)r2r3r4r*rI)r5rrIr*r6rrr3 s zSoftDiceclDiceLoss.__init__rr8c Csòt|||jƒ}t||jƒ}t||jƒ}t t ||¡dd…dd…df¡|jt |dd…dd…df¡|j}t t ||¡dd…dd…df¡|jt |dd…dd…df¡|j}dd||||}d|j||j|} | Sr9)r/r*r%r4rr,r:rI) r5r(r)Údicer;r<r=r>r?Z total_lossrrrr@¬s  ( ÿ( ÿzSoftDiceclDiceLoss.forward)rrHr&rArrr6rrG•s  rG)r&)Ú __future__rrÚtorch.nn.functionalÚnnÚ functionalrÚtorch.nn.modules.lossrrrrr%r/r0rGrrrrÚ s  "