o  i@sVddlmZddlmZddlZddlmmZddl m Z Gddde Z e Z dS)) annotations)UnionN)_LosscsDeZdZdZddfd d ZddddZd ddZd!ddZZS)"DeepSupervisionLossz Wrapper class around the main loss function to accept a list of tensors returned from a deeply supervised networks. The final loss is computed as the sum of weighted losses for each of deep supervision levels. expNlossr weight_modestrweightslist[float] | NonereturnNonecs&t||_||_||_d|_dS)a Args: loss: main loss instance, e.g DiceLoss(). weight_mode: {``"same"``, ``"exp"``, ``"two"``} Specifies the weights calculation for each image level. Defaults to ``"exp"``. - ``"same"``: all weights are equal to 1. - ``"exp"``: exponentially decreasing weights by a power of 2: 1, 0.5, 0.25, 0.125, etc . - ``"two"``: equal smaller weights for lower levels: 1, 0.5, 0.5, 0.5, 0.5, etc weights: a list of weights to apply to each deeply supervised sub-loss, if provided, this will be used regardless of the weight_mode z nearest-exactN)super__init__rrr interp_mode)selfrrr  __class__V/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/ds_loss.pyrs  zDeepSupervisionLoss.__init__levelsint list[float]cCstd|}|jdurt|j|kr|jd|}|S|jdkr&dg|}|S|jdkr6ddt|D}|S|jdkrFd dt|D}|Sdg|}|S) zG Calculates weights for a given number of scale levels rNsame?rcSsg|] }td|dqS)?g?)max.0lrrr 7sz3DeepSupervisionLoss.get_weights..twocSsg|] }|dkr dndqS)rrrrrrrrr!9s)rr lenrrange)rrr rrr get_weights-s     zDeepSupervisionLoss.get_weightsinput torch.TensortargetcCsD|jdd|jddkrtj||jdd|jd}|||S)z Calculates a loss output accounting for differences in shapes, and downsizing targets if necessary (using nearest neighbor interpolation) Generally downsizing occurs for all level, except for the first (level==0) N)sizemode)shapeF interpolaterr)rr&r(rrrget_loss?s zDeepSupervisionLoss.get_loss-Union[None, torch.Tensor, list[torch.Tensor]]cCst|ttfr4|jt|d}tjdtj|jd}t t|D]}|||| |||7}q|S|durs  A