[dff9e0]: / __pycache__ / losses.cpython-37.pyc

Download this file

68 lines (67 with data), 10.0 kB

B

A–®eÃ.ã@søddlZddlmmZddlmZddlmZddlmZm	Z	m
Z
ddd„ZGdd	„d	ejƒZ
Gd
d„dejƒZGdd
„d
ejƒZGdd„deƒZGdd„deƒZGdd„dejƒZGdd„dejƒZGdd„dej	ƒZdd„Zdd„Zdd„ZdS)éN)Únn)ÚVariable)ÚMSELossÚSmoothL1LossÚL1Lossçíµ ÷ư>cCs~| ¡| ¡kstdƒ‚t|ƒ}t|ƒ}| ¡}|| d¡}|dk	rN||}|| d¡|| d¡}d||j|dS)aÓ
    Computes DiceCoefficient as defined in https://arxiv.org/abs/1606.04797 given  a multi channel input and target.
    Assumes the input is a normalized probability, e.g. a result of Sigmoid or Softmax function.

    Args:
         input (torch.Tensor): NxCxSpatial input tensor
         target (torch.Tensor): NxCxSpatial target tensor
         epsilon (float): prevents division by zero
         weight (torch.Tensor): Cx1 tensor of weight per channel/class
    z-'input' and 'target' must have the same shapeéÿÿÿÿNé)Úmin)ÚsizeÚAssertionErrorÚflattenÚfloatÚsumÚclamp)ÚinputÚtargetÚepsilonÚweightÚ	intersectÚdenominator©rúC/Users/hanxuegu/Documents/Documents/bone/bone_paper_codes/losses.pyÚcompute_per_channel_dices
rcs(eZdZdZ‡fdd„Zdd„Z‡ZS)Ú_MaskingLossWrapperzv
    Loss wrapper which prevents the gradient of the loss to be computed where target is equal to `ignore_index`.
    cs.tt|ƒ ¡|dk	stdƒ‚||_||_dS)Nzignore_index cannot be None)ÚsuperrÚ__init__rÚlossÚignore_index)Úselfrr)Ú	__class__rrr)sz_MaskingLossWrapper.__init__cCs2| ¡ |j¡}d|_||}||}| ||¡S)NF)ÚcloneÚne_rÚ
requires_gradr)rrrÚmaskrrrÚforward/s
z_MaskingLossWrapper.forward)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rr%Ú
__classcell__rr)r rr$srcs*eZdZdZd‡fdd„	Zdd„Z‡ZS)ÚSkipLastTargetChannelWrapperz>
    Loss wrapper which removes additional target channel
    Fcstt|ƒ ¡||_||_dS)N)rr+rrÚsqueeze_channel)rrr,)r rrrAsz%SkipLastTargetChannelWrapper.__init__cCsL| d¡dkstdƒ‚|dd…dd…df}|jr@tj|dd}| ||¡S)NézFTarget tensor has a singleton channel dimension, cannot remove channelr.)Údim)rrr,ÚtorchÚsqueezer)rrrrrrr%Fs
z$SkipLastTargetChannelWrapper.forward)F)r&r'r(r)rr%r*rr)r rr+<sr+cs2eZdZdZd
‡fdd„	Zdd„Zdd	„Z‡ZS)Ú_AbstractDiceLossz@
    Base class for different implementations of Dice loss.
    NÚsigmoidcs`tt|ƒ ¡| d|¡|dks&t‚|dkr:t ¡|_n"|dkrRtjdd|_n
dd„|_dS)	Nr)r2ÚsoftmaxÚnoner2r3r-)r.cSs|S)Nr)ÚxrrrÚ<lambda>eóz,_AbstractDiceLoss.__init__.<locals>.<lambda>)	rr1rÚregister_bufferrrÚSigmoidÚ
normalizationÚSoftmax)rrr:)r rrrWsz_AbstractDiceLoss.__init__cCst‚dS)N)ÚNotImplementedError)rrrrrrrÚdicegsz_AbstractDiceLoss.dicecCs*| |¡}|j|||jd}dt |¡S)N)rgð?)r:r=rr/Úmean)rrrZper_channel_dicerrrr%ks
z_AbstractDiceLoss.forward)Nr2)r&r'r(r)rr=r%r*rr)r rr1Rsr1cs*eZdZdZd‡fdd„	Zdd„Z‡ZS)	ÚDiceLossaComputes Dice Loss according to https://arxiv.org/abs/1606.04797.
    For multi-class segmentation `weight` parameter can be used to assign different weights per class.
    The input to the loss function is assumed to be a logit and will be normalized by the Sigmoid function.
    Nr2cstƒ ||¡dS)N)rr)rrr:)r rrr|szDiceLoss.__init__cCst|||jdS)N)r)rr)rrrrrrrr=sz
DiceLoss.dice)Nr2)r&r'r(r)rr=r*rr)r rr?vsr?cs*eZdZdZd‡fdd„	Zdd„Z‡ZS)	ÚGeneralizedDiceLossz_Computes Generalized Dice Loss (GDL) as described in https://arxiv.org/pdf/1707.03237.pdf.
    r2çíµ ÷ư>cstƒjd|d||_dS)N)rr:)rrr)rr:r)r rrr‡szGeneralizedDiceLoss.__init__cCsÚ| ¡| ¡kstdƒ‚t|ƒ}t|ƒ}| ¡}| d¡dkrjtj|d|fdd}tj|d|fdd}| d¡}d||j|jd}d|_	|| d¡}||}|| d¡}||j|jd}d| ¡| ¡S)	Nz-'input' and 'target' must have the same shaperr-)r.r)r
Fr	)
rrr
rr/Úcatrrrr#)rrrrZw_lrrrrrr=‹s
zGeneralizedDiceLoss.dice)r2rA)r&r'r(r)rr=r*rr)r rr@ƒsr@cs(eZdZdZ‡fdd„Zdd„Z‡ZS)ÚBCEDiceLossz)Linear combination of BCE and Dice lossescs0tt|ƒ ¡||_t ¡|_||_tƒ|_	dS)N)
rrCrÚalpharÚBCEWithLogitsLossÚbceÚbetar?r=)rrDrG)r rrr©s

zBCEDiceLoss.__init__cCs$|j| ||¡|j| ||¡S)N)rDrFrGr=)rrrrrrr%°szBCEDiceLoss.forward)r&r'r(r)rr%r*rr)r rrC¦srCcs6eZdZdZd	‡fdd„	Zdd„Zedd„ƒZ‡ZS)
ÚWeightedCrossEntropyLosszXWeightedCrossEntropyLoss (WCE) as described in https://arxiv.org/pdf/1707.03237.pdf
    rcstt|ƒ ¡||_dS)N)rrHrr)rr)r rrr¸sz!WeightedCrossEntropyLoss.__init__cCs| |¡}tj||||jdS)N)rr)Ú_class_weightsÚFÚ
cross_entropyr)rrrrrrrr%¼s
z WeightedCrossEntropyLoss.forwardcCsBtj|dd}t|ƒ}d| d¡}| d¡}t||dd}|S)Nr-)r.gð?rF)r#)rJr3r
rr)rÚ	flattenedÚ	nominatorrÚ
class_weightsrrrrIÀs
z'WeightedCrossEntropyLoss._class_weights)r)	r&r'r(r)rr%ÚstaticmethodrIr*rr)r rrH´srHcs*eZdZd‡fdd„	Z‡fdd„Z‡ZS)ÚWeightedSmoothL1LossTcs$tƒjdd||_||_||_dS)Nr4)Ú	reduction)rrÚ	thresholdÚapply_below_thresholdr)rrRÚinitial_weightrS)r rrrÌszWeightedSmoothL1Loss.__init__csDtƒ ||¡}|jr ||jk}n
||jk}|||j||<| ¡S)N)rr%rSrRrr>)rrrÚl1r$)r rrr%Òs
zWeightedSmoothL1Loss.forward)T)r&r'r(rr%r*rr)r rrPËsrPcCs:| d¡}dttd| ¡ƒƒ}| |¡}| ¡ |d¡S)z™Flattens a given tensor such that the channel axis is first.
    The shapes are transformed as follows:
       (N, C, D, H, W) -> (C, N * D * H * W)
    r-)r-rr	r)rÚtupleÚranger.ÚpermuteÚ
contiguousÚview)ÚtensorÚCÚ
axis_orderÚ
transposedrrrr
ßs

r
cCsÎd|kstdƒ‚|d}| d¡}| dd¡}| dd¡}| dd¡}|dk	rbt |¡ |d	¡}| d
d¡}|dk	rŠt |¡ |d	¡}t|||||ƒ}|dks´|dks´t||ƒ}|rÊt|| dd¡ƒ}|S)
zÆ
    Returns the loss function based on provided configuration
    :param config: (dict) a top level configuration object containing the 'loss' key
    :return: an instance of the loss function
    rz*Could not find loss function configurationÚnamerNÚskip_last_targetFrÚdeviceÚ
pos_weight)ÚCrossEntropyLossrHr,)	rÚpopr/r[ÚtoÚ_create_lossrr+Úget)ÚconfigÚloss_configr_rr`rrbrrrrÚget_loss_criterionîs"

rjcCs:|dkrtj|dS|dkr>| dd¡}| dd¡}t||ƒS|dkr`|dkrRd}tj||d	S|d
kr~|dkrtd}t|dS|dkr’t||d
S|dkr°| dd¡}t|dS|dkrÐ| dd¡}t||dS|dkrÞt	ƒS|dkrît
ƒS|dkrþtƒS|dkr&t|d|d| dd¡dSt
d|›dƒ‚dS)NrE)rbrCZalphsgð?rGrciœÿÿÿ)rrrH)rÚPixelWiseCrossEntropyLoss)rNrr@r:r2)r:r?)rr:rrrrPrRrTrST)rRrTrSzUnsupported loss function: 'ú')rrErgrCrcrHrkr@r?rrrrPÚRuntimeError)r_rirrrbrDrGr:rrrrfsB





rf)rN)r/Útorch.nn.functionalrÚ
functionalrJÚtorch.autogradrÚtorch.nnrrrrÚModulerr+r1r?r@rCrHrPr
rjrfrrrrÚ<module>s 
$
#%