-
Notifications
You must be signed in to change notification settings - Fork 1.6k
4856 resampling cupy backend for cuda-based spline interpolation #4919
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
18 commits
Select commit
Hold shift + click to select a range
ef5b55d
optional grid for base class api consistency
wyli 69d9590
update docs
wyli f35fdc4
update docs
wyli 8448543
docs update
wyli 493e0bb
initial pytorch tests
wyli e935d6e
update tests
wyli 21fffec
update min tests
wyli 4888361
fixes tests
wyli cba2491
update dtype
wyli fbe844f
tested compiled
wyli bf078d5
extend type annotations
wyli df7b3d9
Merge branch 'dev' into map-coord
wyli 5f3983c
Merge branch 'dev' into map-coord
wyli 1befe23
Merge branch 'new' into map-coord
wyli 5cae8bf
Merge branch 'dev' into map-coord
wyli 61f952d
Merge branch 'dev' into map-coord
wyli f8fdeac
update based on comments
wyli a5f0953
fixes a typo
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,62 @@ | ||
| # 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 unittest | ||
|
|
||
| import numpy as np | ||
| import torch | ||
| from parameterized import parameterized | ||
|
|
||
| from monai.config import USE_COMPILED | ||
| from monai.data import MetaTensor | ||
| from monai.transforms import Resample | ||
| from monai.transforms.utils import create_grid | ||
| from monai.utils import GridSampleMode, GridSamplePadMode, NdimageMode, SplineMode, convert_to_numpy | ||
| from tests.utils import assert_allclose, is_tf32_env | ||
|
|
||
| _rtol = 1e-3 if is_tf32_env() else 1e-4 | ||
|
|
||
| TEST_IDENTITY = [] | ||
| for interp in GridSampleMode if not USE_COMPILED else ("nearest", "bilinear"): # type: ignore | ||
| for pad in GridSamplePadMode: | ||
| for p in (np.float32, np.float64): | ||
| for device in [None, "cpu", "cuda"] if torch.cuda.is_available() else [None, "cpu"]: | ||
| TEST_IDENTITY.append([dict(device=device), p, interp, pad, (1, 3, 4)]) | ||
| if interp != "bicubic": | ||
| TEST_IDENTITY.append([dict(device=device), p, interp, pad, (1, 3, 5, 8)]) | ||
| for interp_s in SplineMode if not USE_COMPILED else []: # type: ignore | ||
| for pad_s in NdimageMode: | ||
| for p_s in (int, float, np.float32, np.float64): | ||
| for device in [None, "cpu", "cuda"] if torch.cuda.is_available() else [None, "cpu"]: | ||
| TEST_IDENTITY.append([dict(device=device), p_s, interp_s, pad_s, (1, 20, 21)]) | ||
| TEST_IDENTITY.append([dict(device=device), p_s, interp_s, pad_s, (1, 21, 23, 24)]) | ||
|
|
||
|
|
||
| class TestResampleBackends(unittest.TestCase): | ||
| @parameterized.expand(TEST_IDENTITY) | ||
| def test_resample_identity(self, input_param, im_type, interp, padding, input_shape): | ||
| """test resampling of an identity grid with padding 2, im_type, interp, padding, input_shape""" | ||
| xform = Resample(dtype=im_type, **input_param) | ||
| n_elem = np.prod(input_shape) | ||
| img = convert_to_numpy(np.arange(n_elem).reshape(input_shape), dtype=im_type) | ||
| grid = create_grid(input_shape[1:], homogeneous=True, backend="numpy") | ||
| grid_p = np.stack([np.pad(g, 2, "constant") for g in grid]) # testing pad | ||
| output = xform(img=img, grid=grid_p, mode=interp, padding_mode=padding) | ||
| self.assertTrue(not torch.any(torch.isinf(output) | torch.isnan(output))) | ||
| self.assertIsInstance(output, MetaTensor) | ||
| slices = [slice(None)] | ||
| slices.extend([slice(2, -2) for _ in img.shape[1:]]) | ||
| output_c = output[slices] | ||
| assert_allclose(output_c, img, rtol=_rtol, atol=1e-3, type_test="tensor") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.