U žPÓh+ã@spddlmZddlmZddlZddlmZddlmZddl m Z ddl m Z d d l mZGd d „d eƒZdS) é)Ú annotations)ÚAnyN)Ú_Loss)Údo_metric_reduction)ÚMetricReductioné)Ú TensorOrListé)ÚCumulativeIterationMetriccs\eZdZdZejdfdddddœ‡fdd „ Zdd d d œdd„Zddddddœdd„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. FrzMetricReduction | strÚboolÚNone)Úloss_fnÚ reductionÚ get_not_nansÚreturncs tƒ ¡||_||_||_dS)N)ÚsuperÚ__init__rrr)Úselfrrr©Ú __class__©úN/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/metrics/loss_metric.pyrHs zLossMetric.__init__NzMetricReduction | str | Nonez0torch.Tensor | tuple[torch.Tensor, torch.Tensor])rrcCsZ| ¡}|dkr4|jr*t d¡t d¡fSt d¡St||p@|jƒ\}}|jrV||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.aggregatez torch.Tensorztorch.Tensor | Nonerr)Úy_predÚyÚkwargsrcKsD|dkr| |¡n | ||¡}t|tjƒr@| ¡dkr@|d}q*|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)N)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚMEANrrr&Ú __classcell__rrrrr s.ÿ ÿr )Ú __future__rÚtypingrrÚtorch.nn.modules.lossrÚmonai.metrics.utilsrÚ monai.utilsrÚconfigrÚmetricr r rrrrÚ s