From ec83c5b4162d3eb02026d7afc741f28708a49958 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 25 May 2022 10:14:04 +0100 Subject: [PATCH 1/2] adds interp mode Signed-off-by: Wenqi Li --- monai/utils/enums.py | 1 + tests/test_resize.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/monai/utils/enums.py b/monai/utils/enums.py index 0871e04c1f..af044f30fe 100644 --- a/monai/utils/enums.py +++ b/monai/utils/enums.py @@ -82,6 +82,7 @@ class InterpolateMode(Enum): """ NEAREST = "nearest" + NEAREST_EXACT = "nearest-exact" LINEAR = "linear" BILINEAR = "bilinear" BICUBIC = "bicubic" diff --git a/tests/test_resize.py b/tests/test_resize.py index 9214c44cf0..2bdf003afa 100644 --- a/tests/test_resize.py +++ b/tests/test_resize.py @@ -46,9 +46,11 @@ def test_invalid_inputs(self): ((32, 32), "area", False), ((32, 32, 32), "trilinear", True), ((256, 256), "bilinear", False), + ((256, 256), "nearest-exact", False), ] ) def test_correct_results(self, spatial_size, mode, anti_aliasing): + """resize 'spatial_size' and 'mode'""" resize = Resize(spatial_size, mode=mode, anti_aliasing=anti_aliasing) _order = 0 if mode.endswith("linear"): From 30152f4e86ac98befd07b4aa85c05a2fe0531197 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 25 May 2022 17:36:41 +0100 Subject: [PATCH 2/2] update docstrings Signed-off-by: Wenqi Li --- monai/apps/detection/transforms/dictionary.py | 4 ++-- monai/data/png_writer.py | 2 +- monai/transforms/spatial/array.py | 14 ++++++++------ monai/transforms/spatial/dictionary.py | 6 +++--- tests/test_resize.py | 4 ++-- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/monai/apps/detection/transforms/dictionary.py b/monai/apps/detection/transforms/dictionary.py index 7f57de3943..f2b73e75a0 100644 --- a/monai/apps/detection/transforms/dictionary.py +++ b/monai/apps/detection/transforms/dictionary.py @@ -264,7 +264,7 @@ class ZoomBoxd(MapTransform, InvertibleTransform): zoom: The zoom factor along the spatial axes. If a float, zoom is the same for each spatial axis. If a sequence, zoom should contain one value for each spatial axis. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html It also can be a sequence of string, each element corresponds to a key in ``keys``. @@ -401,7 +401,7 @@ class RandZoomBoxd(RandomizableTransform, MapTransform, InvertibleTransform): to keep the original spatial shape ratio. If a sequence, max_zoom should contain one value for each spatial axis. If 2 values provided for 3D data, use the first value for both H & W dims to keep the same zoom ratio. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html It also can be a sequence of string, each element corresponds to a key in ``keys``. diff --git a/monai/data/png_writer.py b/monai/data/png_writer.py index 5d05536923..b1fe7eb327 100644 --- a/monai/data/png_writer.py +++ b/monai/data/png_writer.py @@ -38,7 +38,7 @@ def write_png( data: input data to write to file. file_name: expected file name that saved on disk. output_spatial_shape: spatial shape of the output image. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"bicubic"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html scale: {``255``, ``65535``} postprocess data by clipping to [0, 1] and scaling to diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index 69f98aa2a0..e2176b4e7d 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -617,7 +617,7 @@ class Resize(Transform): which must be an int number in this case, keeping the aspect ratio of the initial image, refer to: https://albumentations.ai/docs/api_reference/augmentations/geometric/resize/ #albumentations.augmentations.geometric.resize.LongestMaxSize. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html align_corners: This only has an effect when mode is @@ -663,7 +663,8 @@ def __call__( """ Args: img: channel first array, must have shape: (num_channels, H[, W, ..., ]). - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, + ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``self.mode``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html align_corners: This only has an effect when mode is @@ -856,7 +857,7 @@ class Zoom(Transform): zoom: The zoom factor along the spatial axes. If a float, zoom is the same for each spatial axis. If a sequence, zoom should contain one value for each spatial axis. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html padding_mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, @@ -903,7 +904,8 @@ def __call__( """ Args: img: channel first array, must have shape: (num_channels, H[, W, ..., ]). - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, + ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``self.mode``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html padding_mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, @@ -1231,7 +1233,7 @@ class RandZoom(RandomizableTransform): to keep the original spatial shape ratio. If a sequence, max_zoom should contain one value for each spatial axis. If 2 values provided for 3D data, use the first value for both H & W dims to keep the same zoom ratio. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html padding_mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, @@ -1299,7 +1301,7 @@ def __call__( """ Args: img: channel first array, must have shape 2D: (nchannels, H, W), or 3D: (nchannels, H, W, D). - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``self.mode``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html padding_mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, diff --git a/monai/transforms/spatial/dictionary.py b/monai/transforms/spatial/dictionary.py index d00dd4463c..28d9c66448 100644 --- a/monai/transforms/spatial/dictionary.py +++ b/monai/transforms/spatial/dictionary.py @@ -806,7 +806,7 @@ class Resized(MapTransform, InvertibleTransform): which must be an int number in this case, keeping the aspect ratio of the initial image, refer to: https://albumentations.ai/docs/api_reference/augmentations/geometric/resize/ #albumentations.augmentations.geometric.resize.LongestMaxSize. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html It also can be a sequence of string, each element corresponds to a key in ``keys``. @@ -1829,7 +1829,7 @@ class Zoomd(MapTransform, InvertibleTransform): zoom: The zoom factor along the spatial axes. If a float, zoom is the same for each spatial axis. If a sequence, zoom should contain one value for each spatial axis. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html It also can be a sequence of string, each element corresponds to a key in ``keys``. @@ -1929,7 +1929,7 @@ class RandZoomd(RandomizableTransform, MapTransform, InvertibleTransform): to keep the original spatial shape ratio. If a sequence, max_zoom should contain one value for each spatial axis. If 2 values provided for 3D data, use the first value for both H & W dims to keep the same zoom ratio. - mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} + mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``} The interpolation mode. Defaults to ``"area"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html It also can be a sequence of string, each element corresponds to a key in ``keys``. diff --git a/tests/test_resize.py b/tests/test_resize.py index 2bdf003afa..0c6e53135b 100644 --- a/tests/test_resize.py +++ b/tests/test_resize.py @@ -17,7 +17,7 @@ from parameterized import parameterized from monai.transforms import Resize -from tests.utils import TEST_NDARRAYS, NumpyImageTestCase2D, assert_allclose +from tests.utils import TEST_NDARRAYS, NumpyImageTestCase2D, assert_allclose, pytorch_after TEST_CASE_0 = [{"spatial_size": 15}, (6, 10, 15)] @@ -46,7 +46,7 @@ def test_invalid_inputs(self): ((32, 32), "area", False), ((32, 32, 32), "trilinear", True), ((256, 256), "bilinear", False), - ((256, 256), "nearest-exact", False), + ((256, 256), "nearest-exact" if pytorch_after(1, 11) else "neareast", False), ] ) def test_correct_results(self, spatial_size, mode, anti_aliasing):