Skip to content

2695 Fix Resize shape issue #2696

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 5 commits into from
Aug 5, 2021
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
3 changes: 1 addition & 2 deletions monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""

import warnings
from math import ceil
from typing import Any, List, Optional, Sequence, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -403,7 +402,7 @@ def __call__(
if not isinstance(self.spatial_size, int):
raise ValueError("spatial_size must be an int number if size_mode is 'longest'.")
scale = self.spatial_size / max(img_size)
spatial_size_ = tuple(ceil(s * scale) for s in img_size)
spatial_size_ = tuple(int(round(s * scale)) for s in img_size)
resized = torch.nn.functional.interpolate( # type: ignore
input=torch.as_tensor(np.ascontiguousarray(img), dtype=torch.float).unsqueeze(0),
size=spatial_size_,
Expand Down
11 changes: 8 additions & 3 deletions tests/test_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
from monai.transforms import Resize
from tests.utils import NumpyImageTestCase2D

TEST_CASE_0 = [{"spatial_size": 15}, (6, 11, 15)]
TEST_CASE_0 = [{"spatial_size": 15}, (6, 10, 15)]

TEST_CASE_1 = [{"spatial_size": 15, "mode": "area"}, (6, 11, 15)]
TEST_CASE_1 = [{"spatial_size": 15, "mode": "area"}, (6, 10, 15)]

TEST_CASE_2 = [{"spatial_size": 6, "mode": "trilinear", "align_corners": True}, (3, 5, 6)]
TEST_CASE_2 = [{"spatial_size": 6, "mode": "trilinear", "align_corners": True}, (2, 4, 6)]


class TestResize(NumpyImageTestCase2D):
Expand Down Expand Up @@ -63,6 +63,11 @@ def test_longest_shape(self, input_param, expected_shape):
result = Resize(**input_param)(input_data)
np.testing.assert_allclose(result.shape[1:], expected_shape)

def test_longest_infinite_decimals(self):
resize = Resize(spatial_size=1008, size_mode="longest", mode="bilinear", align_corners=False)
ret = resize(np.random.randint(0, 2, size=[1, 2544, 3032]))
self.assertTupleEqual(ret.shape, (1, 846, 1008))


if __name__ == "__main__":
unittest.main()
8 changes: 4 additions & 4 deletions tests/test_resized.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
from monai.transforms import Resized
from tests.utils import NumpyImageTestCase2D

TEST_CASE_0 = [{"keys": "img", "spatial_size": 15}, (6, 11, 15)]
TEST_CASE_0 = [{"keys": "img", "spatial_size": 15}, (6, 10, 15)]

TEST_CASE_1 = [{"keys": "img", "spatial_size": 15, "mode": "area"}, (6, 11, 15)]
TEST_CASE_1 = [{"keys": "img", "spatial_size": 15, "mode": "area"}, (6, 10, 15)]

TEST_CASE_2 = [{"keys": "img", "spatial_size": 6, "mode": "trilinear", "align_corners": True}, (3, 5, 6)]
TEST_CASE_2 = [{"keys": "img", "spatial_size": 6, "mode": "trilinear", "align_corners": True}, (2, 4, 6)]

TEST_CASE_3 = [
{"keys": ["img", "label"], "spatial_size": 6, "mode": ["trilinear", "nearest"], "align_corners": [True, None]},
(3, 5, 6),
(2, 4, 6),
]


Expand Down