Repository navigation
Perf: single-pass remap_instance_id (unique + bincount + LUT gather) #9009
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
ericspod
merged 7 commits into
Project-MONAI:dev
from
aymuos15:perf/remap-instance-id-quadratic
Oct 2, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c0ed16f
Perf: single-pass remap_instance_id via unique + bincount + LUT gather
aymuos15 e459cff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cd74ae2
Address review: strict zip, docstrings, Returns section
aymuos15 459152e
Merge branch 'dev' into perf/remap-instance-id-quadratic
aymuos15 75bf8a3
tests: drop redundant dtype assertion in remap passthrough test
aymuos15 e28842f
Merge branch 'dev' into perf/remap-instance-id-quadratic
aymuos15 5165cf0
Merge branch 'dev' into perf/remap-instance-id-quadratic
ericspod 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,93 @@ | ||
| # 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 parameterized import parameterized | ||
|
|
||
| from monai.metrics.utils import remap_instance_id | ||
|
|
||
| _device = "cuda:0" if torch.cuda.is_available() else "cpu" | ||
|
|
||
| TEST_CASES = [ | ||
| # (description, input, by_size, expected) | ||
| ["non_contiguous_ids", [[0, 2, 2], [5, 5, 0]], False, [[0, 1, 1], [2, 2, 0]]], | ||
| ["already_contiguous", [[0, 1], [2, 2]], False, [[0, 1], [2, 2]]], | ||
| # id 7 covers 3 pixels, id 3 covers 2, id 9 covers 1 -> sizes decide new ids | ||
| ["by_size_largest_first", [[7, 7, 7, 0], [3, 3, 9, 0]], True, [[1, 1, 1, 0], [2, 2, 3, 0]]], | ||
| # equal sizes: ascending original id order wins (stable tie-breaking) | ||
| ["by_size_ties_stable", [[5, 5, 0], [2, 2, 0]], True, [[2, 2, 0], [1, 1, 0]]], | ||
| ["no_background", [[4, 4], [6, 6]], False, [[1, 1], [2, 2]]], | ||
| ["single_instance", [[0, 0], [3, 3]], True, [[0, 0], [1, 1]]], | ||
| ] | ||
|
|
||
|
|
||
| def _reference_remap(pred: torch.Tensor, by_size: bool = False) -> torch.Tensor: | ||
| """The original per-instance-loop implementation, kept as the behavioral reference.""" | ||
| pred_id = [i for i in pred.unique() if i != 0] | ||
| if not pred_id: | ||
| return pred | ||
| if by_size: | ||
| instance_size = [(pred == instance_id).sum() for instance_id in pred_id] | ||
| pair_list = sorted(zip(pred_id, instance_size, strict=True), key=lambda x: x[1], reverse=True) | ||
| pred_id = [p[0] for p in pair_list] | ||
| new_pred = torch.zeros_like(pred, dtype=torch.int) | ||
| for idx, instance_id in enumerate(pred_id): | ||
| new_pred[pred == instance_id] = idx + 1 | ||
| return new_pred | ||
|
|
||
|
|
||
| class TestRemapInstanceId(unittest.TestCase): | ||
| """Tests for `remap_instance_id` covering expected values, pass-through cases, and reference equivalence.""" | ||
|
|
||
| @parameterized.expand(TEST_CASES) | ||
| def test_expected_value(self, _, pred, by_size, expected): | ||
| """Remapping produces the hand-computed contiguous ids, including `by_size` ordering and ties.""" | ||
| result = remap_instance_id(torch.as_tensor(pred, device=_device), by_size=by_size) | ||
| torch.testing.assert_close(result.cpu(), torch.as_tensor(expected, dtype=torch.int), check_dtype=False) | ||
|
|
||
| @parameterized.expand([["all_background_2d", (4, 4)], ["all_background_3d", (2, 3, 4)], ["empty", (0,)]]) | ||
| def test_passthrough(self, _, shape): | ||
| """Inputs without foreground ids are returned unchanged, keeping their original dtype.""" | ||
| pred = torch.zeros(shape, dtype=torch.int64, device=_device) | ||
| result = remap_instance_id(pred, by_size=True) | ||
| # assert_close checks dtype as well as values, so the original dtype is verified here | ||
| torch.testing.assert_close(result, pred) | ||
|
|
||
| def test_output_dtype(self): | ||
| """Remapped outputs use the `torch.int` dtype regardless of input dtype.""" | ||
| pred = torch.as_tensor([[0, 9]], dtype=torch.int64, device=_device) | ||
| self.assertEqual(remap_instance_id(pred).dtype, torch.int) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ["2d", (64, 64), 20, False], | ||
| ["2d_by_size", (64, 64), 20, True], | ||
| ["3d_by_size", (16, 16, 16), 12, True], | ||
| ["sparse_ids_by_size", (48, 48), 7, True], | ||
| ] | ||
| ) | ||
| def test_matches_reference(self, name, shape, n_inst, by_size): | ||
| """Randomized inputs produce output identical to the previous per-instance-loop implementation.""" | ||
| generator = torch.Generator().manual_seed(0) | ||
| pred = torch.randint(0, n_inst + 1, shape, generator=generator).to(_device) | ||
| if name.startswith("sparse"): | ||
| pred = pred * 1000 + 17 # large, non-contiguous, no-background ids | ||
| result = remap_instance_id(pred, by_size=by_size) | ||
| expected = _reference_remap(pred, by_size=by_size) | ||
| torch.testing.assert_close(result, expected) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
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.