Skip to content

447 change Zoom default to keep_shape=True #448

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 4 commits into from
May 29, 2020
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
5 changes: 2 additions & 3 deletions examples/notebooks/mednist_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"from torch.utils.data import Dataset, DataLoader\n",
"\n",
"from monai.transforms import \\\n",
" Compose, LoadPNG, AddChannel, ScaleIntensity, ToTensor, RandRotate, RandFlip, RandZoom, Resize\n",
" Compose, LoadPNG, AddChannel, ScaleIntensity, ToTensor, RandRotate, RandFlip, RandZoom\n",
"from monai.networks.nets import densenet121\n",
"from monai.metrics import compute_roc_auc\n",
"\n",
Expand Down Expand Up @@ -235,10 +235,9 @@
" LoadPNG(image_only=True),\n",
" AddChannel(),\n",
" ScaleIntensity(),\n",
" RandRotate(degrees=15, prob=0.5),\n",
" RandRotate(degrees=15, prob=0.5, reshape=False),\n",
" RandFlip(spatial_axis=0, prob=0.5),\n",
" RandZoom(min_zoom=0.9, max_zoom=1.1, prob=0.5),\n",
" Resize(spatial_size=(64, 64), mode='constant'),\n",
" ToTensor()\n",
"])\n",
"\n",
Expand Down
8 changes: 4 additions & 4 deletions monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class Zoom(Transform):
prefilter (bool): Apply spline_filter before interpolation. Default: True.
use_gpu (bool): Should use cpu or gpu. Uses cupyx which doesn't support order > 1 and modes
'wrap' and 'reflect'. Defaults to cpu for these cases or if cupyx not found.
keep_size (bool): Should keep original size (pad if needed).
keep_size (bool): Should keep original size (pad if needed), default is True.
"""

def __init__(
Expand All @@ -344,7 +344,7 @@ def __init__(
cval=0,
prefilter=True,
use_gpu=False,
keep_size=False,
keep_size=True,
):
self.zoom = zoom
self.interp_order = interp_order
Expand Down Expand Up @@ -585,7 +585,7 @@ class RandZoom(Randomizable, Transform):
prefilter (bool): Apply spline_filter before interpolation. Default: True.
use_gpu (bool): Should use cpu or gpu. Uses cupyx which doesn't support order > 1 and modes
'wrap' and 'reflect'. Defaults to cpu for these cases or if cupyx not found.
keep_size (bool): Should keep original size (pad if needed).
keep_size (bool): Should keep original size (pad if needed), default is True.
"""

def __init__(
Expand All @@ -598,7 +598,7 @@ def __init__(
cval=0,
prefilter=True,
use_gpu=False,
keep_size=False,
keep_size=True,
):
if hasattr(min_zoom, "__iter__") and hasattr(max_zoom, "__iter__"):
assert len(min_zoom) == len(max_zoom), "min_zoom and max_zoom must have same length."
Expand Down
8 changes: 4 additions & 4 deletions monai/transforms/spatial/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ class Zoomd(MapTransform):
prefilter (bool or sequence of bool): Apply spline_filter before interpolation. Default: True.
use_gpu (bool): Should use cpu or gpu. Uses cupyx which doesn't support order > 1 and modes
'wrap' and 'reflect'. Defaults to cpu for these cases or if cupyx not found.
keep_size (bool): Should keep original size (pad if needed).
keep_size (bool): Should keep original size (pad if needed), default is True.
"""

def __init__(
Expand All @@ -726,7 +726,7 @@ def __init__(
cval=0,
prefilter=True,
use_gpu=False,
keep_size=False,
keep_size=True,
):
super().__init__(keys)
self.zoomer = Zoom(zoom=zoom, use_gpu=use_gpu, keep_size=keep_size)
Expand Down Expand Up @@ -768,7 +768,7 @@ class RandZoomd(Randomizable, MapTransform):
prefilter (bool or sequence of bool): Apply spline_filter before interpolation. Default: True.
use_gpu (bool): Should use cpu or gpu. Uses cupyx which doesn't support order > 1 and modes
'wrap' and 'reflect'. Defaults to cpu for these cases or if cupyx not found.
keep_size (bool): Should keep original size (pad if needed).
keep_size (bool): Should keep original size (pad if needed), default is True.
"""

def __init__(
Expand All @@ -782,7 +782,7 @@ def __init__(
cval=0,
prefilter=True,
use_gpu=False,
keep_size=False,
keep_size=True,
):
super().__init__(keys)
if hasattr(min_zoom, "__iter__") and hasattr(max_zoom, "__iter__"):
Expand Down
10 changes: 4 additions & 6 deletions tests/test_integration_classification_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
RandFlip,
RandRotate,
RandZoom,
Resize,
ScaleIntensity,
ToTensor,
)
Expand Down Expand Up @@ -62,10 +61,9 @@ def run_training_test(root_dir, train_x, train_y, val_x, val_y, device=torch.dev
LoadPNG(image_only=True),
AddChannel(),
ScaleIntensity(),
RandRotate(degrees=15, prob=0.5),
RandRotate(degrees=15, prob=0.5, reshape=False),
RandFlip(spatial_axis=0, prob=0.5),
RandZoom(min_zoom=0.9, max_zoom=1.1, prob=0.5),
Resize(spatial_size=(64, 64), mode="constant"),
ToTensor(),
]
)
Expand Down Expand Up @@ -223,11 +221,11 @@ def test_training(self):

# check training properties
np.testing.assert_allclose(
losses, [0.8501208358129878, 0.18469145818121113, 0.08108749352158255, 0.04965383692342005], rtol=1e-3
losses, [0.8009556981788319, 0.16794362251356149, 0.07708252014912617, 0.04769148252856959], rtol=1e-3
)
repeated[i].extend(losses)
print("best metric", best_metric)
np.testing.assert_allclose(best_metric, 0.9999480167572079, rtol=1e-4)
np.testing.assert_allclose(best_metric, 0.9999460507942981, rtol=1e-4)
repeated[i].append(best_metric)
np.testing.assert_allclose(best_metric_epoch, 4)
model_file = os.path.join(self.data_dir, "best_metric_model.pth")
Expand All @@ -236,7 +234,7 @@ def test_training(self):
infer_metric = run_inference_test(self.data_dir, self.test_x, self.test_y, device=self.device)

# check inference properties
np.testing.assert_allclose(np.asarray(infer_metric), [1036, 895, 982, 1033, 958, 1047], atol=1)
np.testing.assert_allclose(np.asarray(infer_metric), [1034, 895, 982, 1033, 961, 1047], atol=1)
repeated[i].extend(infer_metric)

np.testing.assert_allclose(repeated[0], repeated[1])
Expand Down