Skip to content

Commit

Permalink
dev(MeanIoU): store confusion matries of each batch
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-tong committed Sep 13, 2022
1 parent b192834 commit b6591ce
Showing 1 changed file with 4 additions and 41 deletions.
45 changes: 4 additions & 41 deletions mmeval/segmentation/mean_iou.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -114,22 +114,16 @@ 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.
predictions (Sequence): A batch of outputs from the model.
"""
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
Expand Down Expand Up @@ -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())

0 comments on commit b6591ce

Please sign in to comment.