Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit d4ab8e8

Browse files
authored
Fix MMD Metric (#368)
* Fix MMD Metric Signed-off-by: Walter Hugo Lopez Pinaya <ianonimato@hotmail.com> * Fix MMD Metric Signed-off-by: Walter Hugo Lopez Pinaya <ianonimato@hotmail.com> --------- Signed-off-by: Walter Hugo Lopez Pinaya <ianonimato@hotmail.com>
1 parent 5e3fe02 commit d4ab8e8

File tree

3 files changed

+10
-24
lines changed

3 files changed

+10
-24
lines changed

generative/metrics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
from __future__ import annotations
1313

1414
from .fid import FIDMetric
15-
from .mmd import MMD
15+
from .mmd import MMDMetric
1616
from .ms_ssim import MultiScaleSSIMMetric
1717
from .ssim import SSIMMetric

generative/metrics/mmd.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
from collections.abc import Callable
1515

1616
import torch
17-
from monai.metrics.regression import RegressionMetric
18-
from monai.utils import MetricReduction
17+
from monai.metrics.metric import Metric
1918

2019

21-
class MMD(RegressionMetric):
20+
class MMDMetric(Metric):
2221
"""
2322
Unbiased Maximum Mean Discrepancy (MMD) is a kernel-based method for measuring the similarity between two
2423
distributions. It is a non-negative metric where a smaller value indicates a closer match between the two
@@ -31,29 +30,15 @@ class MMD(RegressionMetric):
3130
filter, but it can be any function that takes a tensor as input and returns a tensor as output such as a
3231
feature extractor or an Identity function.
3332
y_pred_transform: Callable to transform the y_pred tensor before computing the metric.
34-
reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values, available
35-
reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``,
36-
`"sum_channel"``}, default to ``"mean"``. if "none", will not do reduction. This parameter is ignored due to
37-
the mathematical formulation of MMD.
38-
get_not_nans: whether to return the `not_nans` count, if True, aggregate() returns (metric, not_nans). Here
39-
`not_nans` count the number of not nans for the metric, thus its shape equals to the shape of the metric.
40-
This parameter is ignored due to the mathematical formulation of MMD.
41-
4233
"""
4334

44-
def __init__(
45-
self,
46-
y_transform: Callable | None = None,
47-
y_pred_transform: Callable | None = None,
48-
reduction: MetricReduction | str = MetricReduction.MEAN,
49-
get_not_nans: bool = False,
50-
) -> None:
51-
super().__init__(reduction=reduction, get_not_nans=get_not_nans)
35+
def __init__(self, y_transform: Callable | None = None, y_pred_transform: Callable | None = None) -> None:
36+
super().__init__()
5237

5338
self.y_transform = y_transform
5439
self.y_pred_transform = y_pred_transform
5540

56-
def _compute_metric(self, y: torch.Tensor, y_pred: torch.Tensor) -> torch.Tensor:
41+
def __call__(self, y: torch.Tensor, y_pred: torch.Tensor) -> torch.Tensor:
5742
"""
5843
Args:
5944
y: first sample (e.g., the reference image). Its shape is (B,C,W,H) for 2D data and (B,C,W,H,D) for 3D.

tests/test_compute_mmd_metric.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import torch
1818
from parameterized import parameterized
1919

20-
from generative.metrics import MMD
20+
from generative.metrics import MMDMetric
2121

2222
TEST_CASES = [
2323
[
@@ -36,12 +36,13 @@
3636
class TestMMDMetric(unittest.TestCase):
3737
@parameterized.expand(TEST_CASES)
3838
def test_results(self, input_param, input_data, expected_val):
39-
results = MMD(**input_param)._compute_metric(**input_data)
39+
metric = MMDMetric(**input_param)
40+
results = metric(**input_data)
4041
np.testing.assert_allclose(results.detach().cpu().numpy(), expected_val, rtol=1e-4)
4142

4243
def test_if_inputs_different_shapes(self):
4344
with self.assertRaises(ValueError):
44-
MMD()(torch.ones([3, 3, 144, 144]), torch.ones([3, 3, 145, 145]))
45+
MMDMetric()(torch.ones([3, 3, 144, 144]), torch.ones([3, 3, 145, 145]))
4546

4647

4748
if __name__ == "__main__":

0 commit comments

Comments
 (0)