Skip to content
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

3879 Fix TestKeepLargestConnectedComponent issue #3895

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tests/test_keep_largest_connected_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# limitations under the License.

import unittest
from copy import deepcopy

import torch
import torch.nn.functional as F
Expand All @@ -18,7 +19,7 @@
from monai.transforms import KeepLargestConnectedComponent
from monai.transforms.utils_pytorch_numpy_unification import moveaxis
from monai.utils.type_conversion import convert_to_dst_type
from tests.utils import TEST_NDARRAYS, assert_allclose
from tests.utils import TEST_NDARRAYS, SkipIfBeforePyTorchVersion, assert_allclose


def to_onehot(x):
Expand Down Expand Up @@ -350,6 +351,21 @@ def test_correct_results(self, _, args, input_image, expected):
converter = KeepLargestConnectedComponent(**args)
result = converter(input_image)
assert_allclose(result, expected, type_test=False)

@parameterized.expand(TESTS)
@SkipIfBeforePyTorchVersion((1, 7))
def test_correct_results_before_after_onehot(self, _, args, input_image, expected):
"""
From torch==1.7, torch.argmax changes its mechanism that if there are multiple maximal values then the
indices of the first maximal value are returned (before this version, the indices of the last maximal value
are returned).
Therefore, we can may use of this changes to convert the onehotted labels into un-onehot format directly
and then check if the result stays the same.

"""
converter = KeepLargestConnectedComponent(**args)
result = converter(deepcopy(input_image))

if "is_onehot" in args:
args["is_onehot"] = not args["is_onehot"]
# if not onehotted, onehot it and make sure result stays the same
Expand Down