-
Notifications
You must be signed in to change notification settings - Fork 1.5k
CheckLabelShaped to check and correct label shapes #5606
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c98f898
adding CheckLabelShaped transform
myron 35716e1
import
myron 30ca1d7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 86e0018
flake
myron 1b5758c
update according to comments
myron 45b292c
showing warning on shape mismatch
myron cd1064d
doc strings, extra_params
myron 8da8c17
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bb2fc53
Merge branch 'dev' into analyzer
myron 3203764
warning update
myron 70daebe
Merge branch 'analyzer' of github.com:myron/MONAI into analyzer
myron d4f9e65
Update monai/apps/auto3dseg/data_analyzer.py
wyli 9c5593f
fixes tests
wyli 29a34ad
skip build
wyli 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
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,71 @@ | ||
| # 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. | ||
|
|
||
| import warnings | ||
| from typing import Dict, Hashable, Mapping | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| from monai.config import KeysCollection | ||
| from monai.networks.utils import pytorch_after | ||
| from monai.transforms import MapTransform | ||
|
|
||
|
|
||
| class EnsureSameShaped(MapTransform): | ||
| """ | ||
| Checks if segmentation label images (in keys) have the same spatial shape as the main image (in source_key), | ||
| and raise an error if the shapes are significantly different. | ||
| If the shapes are only slightly different (within an allowed_shape_difference in each dim), then resize the label using | ||
| nearest interpolation. This transform is designed to correct datasets with slight label shape mismatches. | ||
| Generally image and segmentation label must have the same spatial shape, however some public datasets are having slight | ||
| shape mismatches, which will cause potential crashes when calculating loss or metric functions. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| keys: KeysCollection = "label", | ||
| allow_missing_keys: bool = False, | ||
| source_key: str = "image", | ||
| allowed_shape_difference: int = 5, | ||
| ) -> None: | ||
| """ | ||
| Args: | ||
| keys: keys of the corresponding items to be compared to the source_key item shape. | ||
| allow_missing_keys: do not raise exception if key is missing. | ||
| source_key: key of the item with the reference shape. | ||
| allowed_shape_difference: raises error if shapes are different more than this value in any dimension, | ||
| otherwise corrects for the shape mismatch using nearest interpolation. | ||
|
|
||
| """ | ||
| super().__init__(keys=keys, allow_missing_keys=allow_missing_keys) | ||
| self.source_key = source_key | ||
| self.allowed_shape_difference = allowed_shape_difference | ||
|
|
||
| def __call__(self, data: Mapping[Hashable, torch.Tensor]) -> Dict[Hashable, torch.Tensor]: | ||
| d = dict(data) | ||
| image_shape = d[self.source_key].shape[1:] | ||
| for key in self.key_iterator(d): | ||
| label_shape = d[key].shape[1:] | ||
| if label_shape != image_shape: | ||
| if np.allclose(list(label_shape), list(image_shape), atol=self.allowed_shape_difference): | ||
| warnings.warn( | ||
| f"The {key} with shape {label_shape} was resized to match the source shape {image_shape}," | ||
| f"the meta-data was not updated." | ||
| ) | ||
| d[key] = torch.nn.functional.interpolate( | ||
| input=d[key].unsqueeze(0), | ||
| size=image_shape, | ||
| mode="nearest-exact" if pytorch_after(1, 11) else "nearest", | ||
| ).squeeze(0) | ||
| else: | ||
| raise ValueError(f"The {key} shape {label_shape} is different from the source shape {image_shape}.") | ||
| return d | ||
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
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.