-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Batch inverse #1795
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
Batch inverse #1795
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6dfd7b6
batch inverse
rijobro c4ff072
add batch_inverse tests
rijobro 81c9aa8
Merge remote-tracking branch 'MONAI/master' into batch_inverse
rijobro 0682344
update
rijobro b7aec08
autofix
rijobro de51114
Merge remote-tracking branch 'MONAI/master' into batch_inverse
rijobro 77ff3cd
Merge remote-tracking branch 'MONAI/master' into batch_inverse
rijobro b78ee22
cant pickle lambda
rijobro dac89ba
No ID check for windows
rijobro 2d1ca33
win32 change
rijobro 0a8d321
autofix
rijobro 385b9ce
code format
rijobro a03ce5f
more changes
rijobro ba6ecbb
changes
rijobro a7cf63e
Merge remote-tracking branch 'MONAI/master' into batch_inverse
rijobro f63ae66
Merge branch 'master' into batch_inverse
wyli e69f25a
create PadListDataCollate transform
rijobro 79c7097
rst
rijobro 4aa837f
change default collation of batch inversion
rijobro 3975a86
Merge remote-tracking branch 'MONAI/master' into batch_inverse
rijobro 3f8c630
Merge remote-tracking branch 'MONAI/master' into batch_inverse
rijobro d1bb7df
Merge branch 'master' into batch_inverse
rijobro a1c9952
Merge branch 'master' into batch_inverse
rijobro 924901b
update docs
rijobro 56d57c8
Merge branch 'batch_inverse' of https://github.com/rijobro/MONAI into…
rijobro 708bf39
Merge branch 'batch_inverse' into rijobro/batch_inverse
rijobro 6e3ca06
update docs
rijobro 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,84 @@ | ||
| # Copyright 2020 - 2021 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 typing import Any, Callable, Dict, Hashable, Optional, Sequence | ||
|
|
||
| import numpy as np | ||
| from torch.utils.data.dataloader import DataLoader as TorchDataLoader | ||
|
|
||
| from monai.data.dataloader import DataLoader | ||
| from monai.data.dataset import Dataset | ||
| from monai.data.utils import decollate_batch, pad_list_data_collate | ||
| from monai.transforms.croppad.batch import PadListDataCollate | ||
| from monai.transforms.inverse import InvertibleTransform | ||
| from monai.transforms.transform import Transform | ||
| from monai.utils import first | ||
|
|
||
| __all__ = ["BatchInverseTransform"] | ||
|
|
||
|
|
||
| class _BatchInverseDataset(Dataset): | ||
| def __init__( | ||
| self, | ||
| data: Sequence[Any], | ||
| transform: InvertibleTransform, | ||
| pad_collation_used: bool, | ||
| ) -> None: | ||
| super().__init__(data, transform) | ||
| self.invertible_transform = transform | ||
| self.pad_collation_used = pad_collation_used | ||
|
|
||
| def __getitem__(self, index: int) -> Dict[Hashable, np.ndarray]: | ||
| data = dict(self.data[index]) | ||
| # If pad collation was used, then we need to undo this first | ||
| if self.pad_collation_used: | ||
| data = PadListDataCollate.inverse(data) | ||
|
|
||
| return self.invertible_transform.inverse(data) | ||
|
|
||
|
|
||
| def no_collation(x): | ||
| return x | ||
|
|
||
|
|
||
| class BatchInverseTransform(Transform): | ||
| """Perform inverse on a batch of data. This is useful if you have inferred a batch of images and want to invert them all.""" | ||
|
|
||
| def __init__( | ||
| self, transform: InvertibleTransform, loader: TorchDataLoader, collate_fn: Optional[Callable] = no_collation | ||
| ) -> None: | ||
| """ | ||
| Args: | ||
| transform: a callable data transform on input data. | ||
| loader: data loader used to generate the batch of data. | ||
| collate_fn: how to collate data after inverse transformations. Default won't do any collation, so the output will be a | ||
| list of size batch size. | ||
| """ | ||
| self.transform = transform | ||
| self.batch_size = loader.batch_size | ||
| self.num_workers = loader.num_workers | ||
| self.collate_fn = collate_fn | ||
| self.pad_collation_used = loader.collate_fn == pad_list_data_collate | ||
|
|
||
| def __call__(self, data: Dict[str, Any]) -> Any: | ||
|
|
||
| decollated_data = decollate_batch(data) | ||
| inv_ds = _BatchInverseDataset(decollated_data, self.transform, self.pad_collation_used) | ||
| inv_loader = DataLoader( | ||
rijobro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| inv_ds, batch_size=self.batch_size, num_workers=self.num_workers, collate_fn=self.collate_fn | ||
| ) | ||
| try: | ||
| return first(inv_loader) | ||
| except RuntimeError as re: | ||
| re_str = str(re) | ||
| if "equal size" in re_str: | ||
| re_str += "\nMONAI hint: try creating `BatchInverseTransform` with `collate_fn=lambda x: x`." | ||
| raise RuntimeError(re_str) | ||
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.