U uPh'@stddlmZddlZddlZddlmZddlmZddlm Z GdddeZ Gdd d eZ Gd d d eZ dS) ) annotationsN)_Loss)one_hot) LossReductioncsPeZdZdZddddejfdddddd d fd d Zd d d dddZZS)AsymmetricFocalTverskyLossa AsymmetricFocalTverskyLoss is a variant of FocalTverskyLoss, which attentions to the foreground class. Actually, it's only supported for binary image segmentation now. Reimplementation of the Asymmetric Focal Tversky Loss described in: - "Unified Focal Loss: Generalising Dice and Cross Entropy-based Losses to Handle Class Imbalanced Medical Image Segmentation", Michael Yeung, Computerized Medical Imaging and Graphics Fffffff?g?Hz>boolfloatLossReduction | strNone) to_onehot_ydeltagammaepsilon reductionreturncs0tjt|jd||_||_||_||_dS)a Args: to_onehot_y: whether to convert `y` into the one-hot format. Defaults to False. delta : weight of the background. Defaults to 0.7. gamma : value of the exponent gamma in the definition of the Focal loss . Defaults to 0.75. epsilon : it defines a very small number each time. simmily smooth value. Defaults to 1e-7. rNsuper__init__rvaluer rrrselfr rrrr __class__T/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/losses/unified_focal_loss.pyr#s z#AsymmetricFocalTverskyLoss.__init__ torch.Tensory_predy_truerc CsN|jd}|jr0|dkr$tdn t||d}|j|jkrVtd|jd|jdt||jd|j}t t dt |j}tj |||d }tj |d||d }tj d|||d }||j||j |d|j ||j}d|ddd f} d|dddftd|dddf|j } ttj| | gd d } | S) N6single channel prediction, `to_onehot_y=True` ignored. num_classes"ground truth has different shape () from input ()?dimr)shaper warningswarnr ValueErrortorchclamprlistrangelensumrpowrmeanstack) rr r! n_pred_chaxistpfnfpZ dice_classZ back_diceZ fore_dicelossrrrforward8s"    ,4z"AsymmetricFocalTverskyLoss.forward __name__ __module__ __qualname____doc__rMEANrrA __classcell__rrrrrs rcsNeZdZdZddddejfdddddd fd d Zd d d d ddZZS)AsymmetricFocalLossa AsymmetricFocalLoss is a variant of FocalTverskyLoss, which attentions to the foreground class. Actually, it's only supported for binary image segmentation now. Reimplementation of the Asymmetric Focal Loss described in: - "Unified Focal Loss: Generalising Dice and Cross Entropy-based Losses to Handle Class Imbalanced Medical Image Segmentation", Michael Yeung, Computerized Medical Imaging and Graphics Frr*rr r r )r rrrrcs0tjt|jd||_||_||_||_dS)a Args: to_onehot_y : whether to convert `y` into the one-hot format. Defaults to False. delta : weight of the background. Defaults to 0.7. gamma : value of the exponent gamma in the definition of the Focal loss . Defaults to 0.75. epsilon : it defines a very small number each time. simmily smooth value. Defaults to 1e-7. rNrrrrrrcs zAsymmetricFocalLoss.__init__rrcCs|jd}|jr0|dkr$tdn t||d}|j|jkrVtd|jd|jdt||jd|j}| t |}t d|dddf|j |dddf}d|j |}|dddf}|j |}t tjtj||gdd dd }|S) Nr"r#r$r&r'r(r)rr+)r.r r/r0rr1r2r3rlogr8rrr9r7r:)rr r!r; cross_entropyZback_ceZfore_cer@rrrrAxs    . "zAsymmetricFocalLoss.forwardrBrrrrrIWs rIcsReZdZdZdddddejfdddddd d fd d Zd d d dddZZS)AsymmetricUnifiedFocalLossa AsymmetricUnifiedFocalLoss is a variant of Focal Loss. Actually, it's only supported for binary image segmentation now Reimplementation of the Asymmetric Unified Focal Tversky Loss described in: - "Unified Focal Loss: Generalising Dice and Cross Entropy-based Losses to Handle Class Imbalanced Medical Image Segmentation", Michael Yeung, Computerized Medical Imaging and Graphics Fr*g?rr intr r )r r%weightrrrcsZtjt|jd||_||_||_||_||_t |j|jd|_ t |j|jd|_ dS)a Args: to_onehot_y : whether to convert `y` into the one-hot format. Defaults to False. num_classes : number of classes, it only supports 2 now. Defaults to 2. delta : weight of the background. Defaults to 0.7. gamma : value of the exponent gamma in the definition of the Focal loss. Defaults to 0.75. epsilon : it defines a very small number each time. simmily smooth value. Defaults to 1e-7. weight : weight for each loss function, if it's none it's 0.5. Defaults to None. Example: >>> import torch >>> from monai.losses import AsymmetricUnifiedFocalLoss >>> pred = torch.ones((1,1,32,32), dtype=torch.float32) >>> grnd = torch.ones((1,1,32,32), dtype=torch.int64) >>> fl = AsymmetricUnifiedFocalLoss(to_onehot_y=True) >>> fl(pred, grnd) r)rrN) rrrrr r%rrrNrIasy_focal_lossrasy_focal_tversky_loss)rr r%rNrrrrrrrsz#AsymmetricUnifiedFocalLoss.__init__rrcCsb|j|jkr&td|jd|jdt|jdkrRt|jdkrRtd|j|jddkr|t||jd}t||jd}t||jdkrtd |jd|jd}|jr|dkrt d n t||d}| ||}| ||}|j |d|j |}|j tjjkrt|S|j tjjkr2|S|j tjjkrLt|Std |j d d S)a Args: y_pred : the shape should be BNH[WD], where N is the number of classes. It only supports binary segmentation. The input should be the original logits since it will be transformed by a sigmoid in the forward function. y_true : the shape should be BNH[WD], where N is the number of classes. It only supports binary segmentation. Raises: ValueError: When input and target are different shape ValueError: When len(y_pred.shape) != 4 and len(y_pred.shape) != 5 ValueError: When num_classes ValueError: When the number of classes entered does not match the expected number r&r'r(z$input shape must be 4 or 5, but got r"r$z*Please make sure the number of classes is r#zUnsupported reduction: z0, available options are ["mean", "sum", "none"].N)r.r1r6rr%r2maxr r/r0rOrPrNrrSUMrr7NONErGr9)rr r!r;rOrPr@rrrrAs0        z"AsymmetricUnifiedFocalLoss.forwardrBrrrrrLs $rL) __future__rr/r2torch.nn.modules.lossrmonai.networksr monai.utilsrrrIrLrrrr s    @: