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

3387 Fix 0.0 or None value in AsDiscrete #3393

Merged
merged 7 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions monai/transforms/post/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ def __call__(
if argmax or self.argmax:
img_t = torch.argmax(img_t, dim=0, keepdim=True)

to_onehot = to_onehot or self.to_onehot
to_onehot = self.to_onehot if to_onehot is None else to_onehot
if to_onehot is not None:
if not isinstance(to_onehot, int):
raise AssertionError("the number of classes for One-Hot must be an integer.")
img_t = one_hot(img_t, num_classes=to_onehot, dim=0)

threshold = threshold or self.threshold
threshold = self.threshold if threshold is None else threshold
if threshold is not None:
img_t = img_t >= threshold

Expand Down
10 changes: 10 additions & 0 deletions tests/test_as_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
]
)

# test threshold = 0.0
TEST_CASES.append(
wyli marked this conversation as resolved.
Show resolved Hide resolved
[
{"argmax": False, "to_onehot": None, "threshold": 0.0},
p([[[0.0, -1.0], [-2.0, 3.0]]]),
p([[[1.0, 0.0], [0.0, 1.0]]]),
(1, 2, 2),
]
)

TEST_CASES.append([{"argmax": False, "to_onehot": 3}, p(1), p([0.0, 1.0, 0.0]), (3,)])

TEST_CASES.append(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_as_discreted.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
]
)

# test threshold = 0.0
TEST_CASES.append(
[
{"keys": ["pred", "label"], "argmax": False, "to_onehot": None, "threshold": [0.0, None]},
{"pred": p([[[0.0, -1.0], [-2.0, 3.0]]]), "label": p([[[0, 1], [1, 1]]])},
{"pred": p([[[1.0, 0.0], [0.0, 1.0]]]), "label": p([[[0.0, 1.0], [1.0, 1.0]]])},
(1, 2, 2),
]
)


class TestAsDiscreted(unittest.TestCase):
@parameterized.expand(TEST_CASES)
Expand Down