-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add loss_fn to IgniteMetric and rename to IgniteMetricHandler #6695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
324e53f
Add DiceCEMetric
matt3o 4384f4c
WiP: Add unittest for DiceCEMetric
matt3o 94cb06f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 48c6ef9
Remove DiceCEMetric
matt3o 6815816
Add IgniteLossMetric
matt3o 2d77048
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 84ff449
Merge branch 'dev' into Add_dice_ce_metric
matt3o 2524e0d
Undo previous commits as discussed
matt3o 7c57f00
Add loss_fn support to IgniteMetric
matt3o 14b0748
Delete previously created files
matt3o a3154ee
Modify IgniteMetric to also support loss_fn
matt3o 0392f60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a51bd4d
Add tests for IgniteMetric(Handler)
matt3o d76f0ed
Fix formatting
matt3o 9d93967
Update test cases for IgniteMetric(Handler)
matt3o 974a3eb
Rename IgniteMetric to IgniteMetricHandler
matt3o acceeab
Rename test_handler_ignite_metric_handler to test_handler_ignite_metric
matt3o e77e97b
Remove warning
matt3o 9ef3dac
Fix ignite ImportError
matt3o 1388b46
Fix typing
matt3o 91f85ae
Add deprecation warning for IgniteMetric
matt3o 2c5d188
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c8027b2
Add test_handler_ignite_metric to the min_tests list
matt3o f54a1bf
Fix code formatting
matt3o 966c99a
Fix code formatting and remove debug prints
matt3o 1a4b133
Merge branch 'dev' into Add_dice_ce_metric
wyli f92824a
Remove commented code
matt3o File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
|
||
from monai.handlers.ignite_metric import IgniteMetric | ||
from monai.utils import MetricReduction | ||
from monai.losses import DiceCELoss | ||
from monai.metrics import LossMetric | ||
|
||
class DiceCEMetric(IgniteMetric): | ||
""" | ||
Computes DiceCE score metric from full size Tensor and collects average over batch, class-channels, iterations. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
# include_background: bool = True, | ||
reduction: MetricReduction | str = MetricReduction.MEAN, | ||
# num_classes: int | None = None, | ||
output_transform: Callable = lambda x: x, | ||
save_details: bool = True, | ||
*args, | ||
**kwargs | ||
) -> None: | ||
""" | ||
|
||
Args: | ||
include_background: whether to include dice computation on the first channel of the predicted output. | ||
Defaults to True. | ||
reduction: define the mode to reduce metrics, will only execute 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. | ||
num_classes: number of input channels (always including the background). When this is None, | ||
``y_pred.shape[1]`` will be used. This option is useful when both ``y_pred`` and ``y`` are | ||
single-channel class indices and the number of classes is not automatically inferred from data. | ||
output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then | ||
construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or | ||
lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. | ||
`engine.state` and `output_transform` inherit from the ignite concept: | ||
https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: | ||
https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. | ||
save_details: whether to save metric computation details per image, for example: mean dice of every image. | ||
default to True, will save to `engine.state.metric_details` dict with the metric name as key. | ||
args: Arguments for the DiceCELoss | ||
|
||
See also: | ||
:py:meth:`monai.metrics.meandice.compute_dice` | ||
""" | ||
# metric_fn = DiceMetric(include_background=include_background, reduction=reduction, num_classes=num_classes) | ||
loss_function = DiceCELoss(*args, **kwargs) | ||
metric_fn = LossMetric(loss_fn=loss_function, reduction=reduction, get_not_nans=False) | ||
wyli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super().__init__(metric_fn=metric_fn, output_transform=output_transform, save_details=save_details) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import unittest | ||
|
||
import torch | ||
from ignite.engine import Engine, Events | ||
from parameterized import parameterized | ||
|
||
from monai.handlers import DiceCEMetric, from_engine | ||
from tests.utils import assert_allclose | ||
|
||
TEST_CASE_1 = [{"include_background": True, "output_transform": from_engine(["pred", "label"])}, 0.813259, (4, 2)] | ||
TEST_CASE_2 = [{"include_background": False, "output_transform": from_engine(["pred", "label"])}, 0.813259, (4, 1)] | ||
TEST_CASE_3 = [ | ||
{"reduction": "mean_channel", "output_transform": from_engine(["pred", "label"])}, | ||
torch.Tensor([0.313262, 2.313251, 0.313262, 0.313262]), | ||
(4, 2), | ||
] | ||
|
||
|
||
class TestHandlerDiceCEMetric(unittest.TestCase): | ||
# TODO test multi node averaged dice | ||
|
||
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) | ||
def test_compute(self, input_params, expected_avg, details_shape): | ||
dice_metric = DiceCEMetric(**input_params) | ||
|
||
def _val_func(engine, batch): | ||
pass | ||
|
||
engine = Engine(_val_func) | ||
dice_metric.attach(engine=engine, name="dice_ce_metric") | ||
# test input a list of channel-first tensor | ||
y_pred = [torch.Tensor([[0], [1]]), torch.Tensor([[1], [0]])] | ||
y = torch.Tensor([[[0], [1]], [[0], [1]]]) | ||
engine.state.output = {"pred": y_pred, "label": y} | ||
engine.fire_event(Events.ITERATION_COMPLETED) | ||
|
||
y_pred = [torch.Tensor([[0], [1]]), torch.Tensor([[1], [0]])] | ||
y = torch.Tensor([[[0], [1]], [[1], [0]]]) | ||
engine.state.output = {"pred": y_pred, "label": y} | ||
engine.fire_event(Events.ITERATION_COMPLETED) | ||
|
||
engine.fire_event(Events.EPOCH_COMPLETED) | ||
assert_allclose(engine.state.metrics["dice_ce_metric"], expected_avg, atol=1e-4, rtol=1e-4, type_test=False) | ||
self.assertTupleEqual(tuple(engine.state.metric_details["dice_ce_metric"].shape), details_shape) | ||
|
||
@parameterized.expand([TEST_CASE_1, TEST_CASE_2]) | ||
def test_shape_mismatch(self, input_params, _expected_avg, _details_shape): | ||
dice_metric = DiceCEMetric(**input_params) | ||
with self.assertRaises((AssertionError, ValueError)): | ||
y_pred = torch.Tensor([[0, 1], [1, 0]]) | ||
y = torch.ones((2, 3)) | ||
dice_metric.update([y_pred, y]) | ||
|
||
with self.assertRaises((AssertionError, ValueError)): | ||
y_pred = torch.Tensor([[0, 1], [1, 0]]) | ||
y = torch.ones((3, 2)) | ||
dice_metric.update([y_pred, y]) | ||
|
||
# @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) | ||
# def test_compute_n_class(self, input_params, expected_avg, details_shape): | ||
# dice_metric = DiceCEMetric(num_classes=2, **input_params) | ||
|
||
# def _val_func(engine, batch): | ||
# pass | ||
|
||
# engine = Engine(_val_func) | ||
# dice_metric.attach(engine=engine, name="dice_ce_metric") | ||
# # test input a list of channel-first tensor | ||
# y_pred = [torch.Tensor([[1]]), torch.Tensor([[0]])] | ||
# y = torch.Tensor([[[0], [1]], [[0], [1]]]) | ||
# engine.state.output = {"pred": y_pred, "label": y} | ||
# engine.fire_event(Events.ITERATION_COMPLETED) | ||
|
||
# y_pred = [torch.Tensor([[1]]), torch.Tensor([[0]])] # class indices y_pred | ||
# y = torch.Tensor([[[1]], [[0]]]) # class indices y | ||
# engine.state.output = {"pred": y_pred, "label": y} | ||
# engine.fire_event(Events.ITERATION_COMPLETED) | ||
|
||
# engine.fire_event(Events.EPOCH_COMPLETED) | ||
# assert_allclose(engine.state.metrics["dice_ce_metric"], expected_avg, atol=1e-4, rtol=1e-4, type_test=False) | ||
# self.assertTupleEqual(tuple(engine.state.metric_details["dice_ce_metric"].shape), details_shape) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.