Skip to content
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
2 changes: 1 addition & 1 deletion monai/engines/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def get_stats(self, *vars):
stats = {
ESKeys.RANK: self.state.rank,
ESKeys.BEST_VALIDATION_EPOCH: self.state.best_metric_epoch,
ESKeys.BEST_VALIDATION_METRTC: self.state.best_metric,
ESKeys.BEST_VALIDATION_METRIC: self.state.best_metric,
}
for k in vars:
stats[k] = getattr(self.state, k, None)
Expand Down
373 changes: 252 additions & 121 deletions monai/transforms/spatial/array.py

Large diffs are not rendered by default.

120 changes: 87 additions & 33 deletions monai/transforms/spatial/dictionary.py

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ def create_grid(
"""
compute a `spatial_size` mesh.

- when ``homogeneous=True``, the output shape is (N+1, dim_size_1, dim_size_2, ..., dim_size_N)
- when ``homogeneous=False``, the output shape is (N, dim_size_1, dim_size_2, ..., dim_size_N)

Args:
spatial_size: spatial size of the grid.
spacing: same len as ``spatial_size``, defaults to 1.0 (dense grid).
Expand Down
2 changes: 2 additions & 0 deletions monai/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
MetaKeys,
Method,
MetricReduction,
NdimageMode,
NumpyPadMode,
PostFix,
ProbMapKeys,
PytorchPadMode,
SkipMode,
SpaceKeys,
SplineMode,
StrEnum,
TraceKeys,
TransformBackends,
Expand Down
35 changes: 34 additions & 1 deletion monai/utils/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
"StrEnum",
"NumpyPadMode",
"GridSampleMode",
"SplineMode",
"InterpolateMode",
"UpsampleMode",
"BlendMode",
"PytorchPadMode",
"NdimageMode",
"GridSamplePadMode",
"Average",
"MetricReduction",
Expand Down Expand Up @@ -92,6 +94,22 @@ class NumpyPadMode(StrEnum):
EMPTY = "empty"


class NdimageMode(StrEnum):
"""
Comment thread
wyli marked this conversation as resolved.
The available options determine how the input array is extended beyond its boundaries when interpolating.
See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
"""

REFLECT = "reflect"
GRID_MIRROR = "grid-mirror"
CONSTANT = "constant"
GRID_CONSTANT = "grid-constant"
NEAREST = "nearest"
MIRROR = "mirror"
GRID_WRAP = "grid-wrap"
WRAP = "wrap"


class GridSampleMode(StrEnum):
"""
See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
Expand All @@ -110,6 +128,21 @@ class GridSampleMode(StrEnum):
BICUBIC = "bicubic"


class SplineMode(StrEnum):
"""
Order of spline interpolation.

See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
"""

ZERO = 0
ONE = 1
TWO = 2
THREE = 3
FOUR = 4
FIVE = 5


class InterpolateMode(StrEnum):
"""
See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
Expand Down Expand Up @@ -492,4 +525,4 @@ class EngineStatsKeys(StrEnum):
TOTAL_EPOCHS = "total_epochs"
TOTAL_ITERATIONS = "total_iterations"
BEST_VALIDATION_EPOCH = "best_validation_epoch"
BEST_VALIDATION_METRTC = "best_validation_metric"
BEST_VALIDATION_METRIC = "best_validation_metric"
1 change: 1 addition & 0 deletions tests/min_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def run_testsuit():
"test_rand_zoom",
"test_rand_zoomd",
"test_randtorchvisiond",
"test_resample_backends",
"test_resize",
"test_resized",
"test_resample_to_match",
Expand Down
62 changes: 62 additions & 0 deletions tests/test_resample_backends.py
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()
3 changes: 1 addition & 2 deletions tests/test_spatial_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import torch
from parameterized import parameterized

from monai.config import USE_COMPILED
from monai.data.meta_obj import set_track_meta
from monai.data.meta_tensor import MetaTensor
from monai.data.utils import to_affine_nd
Expand All @@ -37,7 +36,7 @@
for dst, expct in zip(destinations_3d, expected_3d):
for device in TEST_DEVICES:
for align in (False, True):
interp = ("nearest", "bilinear", 0, 1) if align and USE_COMPILED else ("nearest", "bilinear")
interp = ("nearest", "bilinear")
for interp_mode in interp:
for padding_mode in ("zeros", "border", "reflection"):
TESTS.append(
Expand Down
3 changes: 1 addition & 2 deletions tests/test_spatial_resampled.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import torch
from parameterized import parameterized

from monai.config import USE_COMPILED
from monai.data.meta_tensor import MetaTensor
from monai.data.utils import to_affine_nd
from monai.transforms.spatial.dictionary import SpatialResampled
Expand All @@ -37,7 +36,7 @@
for device in TEST_DEVICES:
for align in (True, False):
for dtype in (torch.float32, torch.float64):
interp = ("nearest", "bilinear", 0, 1) if align and USE_COMPILED else ("nearest", "bilinear")
interp = ("nearest", "bilinear")
for interp_mode in interp:
for padding_mode in ("zeros", "border", "reflection"):
TESTS.append(
Expand Down