o  i'@stddlmZddlZddlZddlmZddlmZddlm Z GdddeZ Gdd d eZ Gd d d eZ dS) ) annotationsN)_Loss)one_hot) LossReductioncs:eZdZdZddddejfdfdd ZdddZZS)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> to_onehot_ybooldeltafloatgammaepsilon reductionLossReduction | strreturnNonec0tjt|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 r r rselfr r r rr __class__a/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/unified_focal_loss.pyr#  z#AsymmetricFocalTverskyLoss.__init__y_pred torch.Tensory_truec CsN|jd}|jr|dkrtdnt||d}|j|jkr+td|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 ValueErrortorchclamprlistrangelensumr powr meanstack) rr r" n_pred_chaxistpfnfpZ dice_classZ back_diceZ fore_dicelossrrrforward8s"    ,4z"AsymmetricFocalTverskyLoss.forward) r r r r r r rr rrrrr r!r"r!rr! __name__ __module__ __qualname____doc__rMEANrrB __classcell__rrrrr rcs:eZdZdZddddejfdfdd ZdddZZS)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 r rrrcr)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. rNrrrrrrcrzAsymmetricFocalLoss.__init__r r!r"rcCs|jd}|jr|dkrtdnt||d}|j|jkr+td|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 r0r1rr2r3r4rlogr9r r r:r8r;)rr r"r< cross_entropyZback_ceZfore_cerArrrrBxs    . "zAsymmetricFocalLoss.forward) r r r r r r rr rrrCrDrrrrrLWrKrLcs<eZdZdZdddddejfdfdd ZdddZZS)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 r r&intweightr r r rrcsZtjt|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)r r N) rrrrr r&r r rQrLasy_focal_lossrasy_focal_tversky_loss)rr r&rQr r rrrrrsz#AsymmetricUnifiedFocalLoss.__init__r r!r"rcCsX|j|jkrtd|jd|jdt|jdkr)t|jdkr)td|j|jddkr>t||jd}t||jd}t||jdkrRtd |jd|jd}|jrj|dkrdt d nt||d}| ||}| ||}|j |d|j |}|j tjjkrt|S|j tjjkr|S|j tjjkrt|Std |j d ) 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"].)r/r2r7rr&r3maxr r0r1rRrSrQrrSUMrr8NONErIr:)rr r"r<rRrSrArrrrBs0        z"AsymmetricUnifiedFocalLoss.forward) r r r&rPrQr r r r r rrrCrDrrrrrOs $rO) __future__rr0r3torch.nn.modules.lossrmonai.networksr monai.utilsrrrLrOrrrrs    @: