From d44317dc69a322adad9b041220672cc4800ee813 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Fri, 15 Oct 2021 23:16:47 +0800 Subject: [PATCH 1/2] [DLMED] add metric example Signed-off-by: Nic Ma --- monai/metrics/metric.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/monai/metrics/metric.py b/monai/metrics/metric.py index bb4aa7c343..a4c5fe8ee2 100644 --- a/monai/metrics/metric.py +++ b/monai/metrics/metric.py @@ -120,7 +120,7 @@ class Cumulative(ABC): cum.add(a, b) cum.add(c, d) cum.aggregate() - result = cum.get_buffer() + result = cum.get_buffer() # optional cum.reset() """ @@ -197,6 +197,24 @@ class CumulativeIterationMetric(Cumulative, IterationMetric): Typically, it computes some intermediate results for every iteration, cumulates in buffers, then syncs across all the distributed ranks and aggregates for the final result when epoch completed. + For example, `MeanDice` inherits this class and the usage: + + .. code-block:: python + + dice_metric = DiceMetric(include_background=True, reduction="mean") + + for val_data in val_loader: + val_outputs = model(val_data["img"]) + val_outputs = [postprocessing_transform(i) for i in decollate_batch(val_outputs)] + # compute metric for current iteration + dice_metric(y_pred=val_outputs, y=val_data["seg"]) + + # aggregate the final mean dice result + metric = dice_metric.aggregate().item() + + # reset the status for next computation round + dice_metric.reset() + """ def __call__(self, y_pred: TensorOrList, y: Optional[TensorOrList] = None): # type: ignore From c8be13e2617062ee8a2b831a2a1b2c3d6d390740 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Mon, 18 Oct 2021 16:35:19 +0800 Subject: [PATCH 2/2] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/metrics/metric.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/monai/metrics/metric.py b/monai/metrics/metric.py index a4c5fe8ee2..f54bb984f0 100644 --- a/monai/metrics/metric.py +++ b/monai/metrics/metric.py @@ -215,6 +215,9 @@ class CumulativeIterationMetric(Cumulative, IterationMetric): # reset the status for next computation round dice_metric.reset() + And to load `predictions` and `labels` from files, then compute metrics with multi-processing, please refer to: + https://github.com/Project-MONAI/tutorials/blob/master/modules/compute_metric.py. + """ def __call__(self, y_pred: TensorOrList, y: Optional[TensorOrList] = None): # type: ignore