o  i+ã@spddlmZddlmZddlZddlmZddlmZddl m Z ddl m Z d d l mZGd d „d eƒZdS) é)Ú annotations)ÚAnyN)Ú_Loss)Údo_metric_reduction)ÚMetricReductioné)Ú TensorOrListé)ÚCumulativeIterationMetriccsDeZdZdZejdfd‡fd d „ Z dddd„Zdddd„Z‡Z S)Ú LossMetrica¯ A wrapper to make ``loss_fn`` available as a cumulative metric. That is, the loss values computed from mini-batches can be combined in the ``reduction`` mode across multiple iterations, as a quantitative measurement of a model. Example: .. code-block:: python import torch from monai.losses import DiceLoss from monai.metrics import LossMetric dice_loss = DiceLoss(include_background=True) loss_metric = LossMetric(loss_fn=dice_loss) # first iteration y_pred = torch.tensor([[[[1.0, 0.0], [0.0, 1.0]]]]) # shape [batch=1, channel=1, 2, 2] y = torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]) # shape [batch=1, channel=1, 2, 2] loss_metric(y_pred, y) # second iteration y_pred = torch.tensor([[[[1.0, 0.0], [0.0, 0.0]]]]) # shape [batch=1, channel=1, 2, 2] y = torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]) # shape [batch=1, channel=1, 2, 2] loss_metric(y_pred, y) # aggregate print(loss_metric.aggregate(reduction="none")) # tensor([[0.2000], [0.5000]]) (shape [batch=2, channel=1]) # reset loss_metric.reset() print(loss_metric.aggregate()) Args: loss_fn: a callable function that takes ``y_pred`` and optionally ``y`` as input (in the "batch-first" format), returns a "batch-first" tensor of loss values. reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values, available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``, ``"sum_channel"``}, default to ``"mean"``. if "none", will not do reduction. get_not_nans: whether to return the `not_nans` count, if True, aggregate() returns (metric, not_nans). Here `not_nans` count the number of not nans for the metric, thus its shape equals to the shape of the metric. FÚloss_fnrÚ reductionúMetricReduction | strÚ get_not_nansÚboolÚreturnÚNonecs tƒ ¡||_||_||_dS©N)ÚsuperÚ__init__r r r)Úselfr r r©Ú __class__©ú[/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/metrics/loss_metric.pyrHs  zLossMetric.__init__NúMetricReduction | str | Noneú0torch.Tensor | tuple[torch.Tensor, torch.Tensor]cCsZ| ¡}|dur|jrt d¡t d¡fSt d¡St||p |jƒ\}}|jr+||fS|S)a¶ Returns the aggregated loss value across multiple iterations. Args: reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values, available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``, ``"sum_channel"``}, default to `self.reduction`. if "none", will not do reduction. Ng)Ú get_bufferrÚtorchÚtensorrr )rr ÚdataÚfÚnot_nansrrrÚ aggregatePs  $zLossMetric.aggregateÚy_predú torch.TensorÚyútorch.Tensor | NoneÚkwargsrrcKsN|dur | |¡n| ||¡}t|tjƒr%| ¡dkr%|d}| ¡dks|S)a  Input `y_pred` is compared with ground truth `y`. Both `y_pred` and `y` are expected to be a batch-first Tensor (BC[HWD]). Returns: a tensor with shape (BC[HWD]), or a list of tensors, each tensor with shape (C[HWD]). Nr)r Ú isinstancerÚTensorÚdim)rr$r&r(Z iter_lossrrrÚ_compute_tensoras    ÿzLossMetric._compute_tensor)r rr rrrrrr)r rrr)r$r%r&r'r(rrr) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚMEANrr#r,Ú __classcell__rrrrr s.ÿ ÿr )Ú __future__rÚtypingrrÚtorch.nn.modules.lossrÚmonai.metrics.utilsrÚ monai.utilsrÚconfigrÚmetricr r rrrrÚs