From b6591cea8fee6cd66fae4a75575b8ed7001a879a Mon Sep 17 00:00:00 2001 From: yancong Date: Tue, 13 Sep 2022 17:47:33 +0800 Subject: [PATCH] dev(MeanIoU): store confusion matries of each batch --- mmeval/segmentation/mean_iou.py | 45 +++------------------------------ 1 file changed, 4 insertions(+), 41 deletions(-) diff --git a/mmeval/segmentation/mean_iou.py b/mmeval/segmentation/mean_iou.py index b434312c..0a32e1bc 100644 --- a/mmeval/segmentation/mean_iou.py +++ b/mmeval/segmentation/mean_iou.py @@ -91,7 +91,7 @@ def num_classes(self) -> int: The number of classes should be set during initialization, otherwise it will be obtained from the 'classes' or 'num_classes' field in - `self.dataset_meta`. + ``self.dataset_meta``. Raises: RuntimeError: If the num_classes is not set. @@ -114,8 +114,8 @@ def num_classes(self) -> int: def add(self, predictions: Sequence, labels: Sequence) -> None: # type: ignore # yapf: disable # noqa: E501 """Process one batch of data and predictions. - Calculate the confusion matrix from the inputs and update the total - confusion matrix stored in `self._results[0]`. + Calculate the confusion matrix from the inputs and store in + ``self._results``. Args: data_batch (Sequence): A batch of data from the dataloader. @@ -123,13 +123,7 @@ def add(self, predictions: Sequence, labels: Sequence) -> None: # type: ignore """ cm = self.compute_confusion_matrix(predictions, labels, self.num_classes) - # update the total confusion matrix stored in `self._results[0]` - if len(self._results) == 0: - self._results.append(cm) - else: - # Cumulative the confusion matrix. - total_cm = self._results[0] - self._results[0] = total_cm + cm + self._results.append(cm) @overload # type: ignore @dispatch @@ -315,34 +309,3 @@ def compute_metric( - Keys from `self._compute_core` if in verbose results mode. """ return self._compute_metric(results) - - -# The code below is temporary for test, will be removed. - -if __name__ == '__main__': - - from _miou import IoUMetric as _IoUMetric - torch.manual_seed(0) - - num_classes = 2 - high, width = (224, 224) - batch_size = 48 - np_miou = MeanIoU(num_classes=num_classes, verbose_results=True) - miou = MeanIoU( - dataset_meta={'classes': [i for i in range(num_classes)]}, - verbose_results=True) - _miou = _IoUMetric( - iou_metrics=['mIoU', 'mDice', 'mFscore'], - dataset_meta={'classes': [i for i in range(num_classes)]}) - - for i in range(10): - labels = torch.randint(0, num_classes, size=(batch_size, high, width)) - predicts = torch.randint( - 0, num_classes, size=(batch_size, high, width)) - np_miou.add(predicts.numpy(), labels.numpy()) - miou.add(predicts, labels) - _miou.add(predicts, labels) - - print(miou.compute()) - print(np_miou.compute()) - print(_miou.compute())