U Ph\@shddlmZddlZddlmZmZddlmZddlm Z Gddde Z dd d d d d d d dZ dS)) annotationsN)do_metric_reductionignore_background)MetricReduction)CumulativeIterationMetriccs^eZdZdZdejddfddddddfdd Zd d d d d d ZddddddZZ S)MeanIoUag Compute average Intersection over Union (IoU) score between two tensors. It supports both multi-classes and multi-labels tasks. Input `y_pred` is compared with ground truth `y`. `y_pred` is expected to have binarized predictions and `y` should be in one-hot format. You can use suitable transforms in ``monai.transforms.post`` first to achieve binarized values. The `include_background` parameter can be set to ``False`` to exclude the first category (channel index 0) which is by convention assumed to be background. If the non-background segmentations are small compared to the total image size they can get overwhelmed by the signal from the background. `y_pred` and `y` can be a list of channel-first Tensor (CHW[D]) or a batch-first Tensor (BCHW[D]). Example of the typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`. Args: include_background: whether to include IoU computation on the first channel of the predicted output. Defaults to ``True``. 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. ignore_empty: whether to ignore empty ground truth cases during calculation. If `True`, NaN value will be set for empty ground truth cases. If `False`, 1 will be set if the predictions of empty ground truth cases are also empty. TFboolzMetricReduction | strNone)include_background reduction get_not_nans ignore_emptyreturncs&t||_||_||_||_dS)N)super__init__r r r r)selfr r r r __class__J/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/metrics/meaniou.pyr3s  zMeanIoU.__init__ torch.Tensor)y_predyrcCs4|}|dkr td|dt|||j|jdS)a Args: y_pred: input data to compute, typical segmentation model output. It must be one-hot format and first dim is batch, example shape: [16, 3, 32, 32]. The values should be binarized. y: ground truth to compute mean IoU metric. It must be one-hot format and first dim is batch. The values should be binarized. Raises: ValueError: when `y_pred` has less than three dimensions. zHy_pred should have at least 3 dimensions (batch, channel, spatial), got .)rrr r) ndimension ValueError compute_iour r)rrrdimsrrr_compute_tensor@s zMeanIoU._compute_tensorNzMetricReduction | str | Nonez0torch.Tensor | tuple[torch.Tensor, torch.Tensor])r rcCsB|}t|tjstdt||p(|j\}}|jr>||fS|S)a Execute reduction logic for the output of `compute_iou`. 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. z-the data to aggregate must be PyTorch Tensor.) get_buffer isinstancetorchTensorrrr r )rr datafnot_nansrrr aggregateTs  zMeanIoU.aggregate)N) __name__ __module__ __qualname____doc__rMEANrr r( __classcell__rrrrrs rTrr )rrr rrc Cs|st||d\}}|j|jkr:td|jd|jdt|j}ttd|}tj|||d}t||}tj||d}|||} |rt|dk|| tj t d|j d St| dk|| tj d |j d S) aComputes Intersection over Union (IoU) score metric from a batch of predictions. Args: y_pred: input data to compute, typical segmentation model output. It must be one-hot format and first dim is batch, example shape: [16, 3, 32, 32]. The values should be binarized. y: ground truth to compute mean IoU metric. It must be one-hot format and first dim is batch. The values should be binarized. include_background: whether to include IoU computation on the first channel of the predicted output. Defaults to True. ignore_empty: whether to ignore empty ground truth cases during calculation. If `True`, NaN value will be set for empty ground truth cases. If `False`, 1 will be set if the predictions of empty ground truth cases are also empty. Returns: IoU scores per batch and per class, (shape [batch_size, num_classes]). Raises: ValueError: when `y_pred` and `y` have different shapes. )rrz*y_pred and y should have same shapes, got z and r)dimrnan)deviceg?) rshaperlenlistranger#sumwheretensorfloatr2) rrr rn_len reduce_axis intersectiony_oy_pred_ounionrrrris    &r)TT) __future__rr#monai.metrics.utilsrr monai.utilsrmetricrrrrrrr s   T