Skip to content

Commit f37a430

Browse files
authored
447 change Zoom default to keep_shape=True (#448)
* [DLMED] change Zoom default to keep_shape=True * [DLMED] update tests and examples * [DLMED] fix integration test
1 parent 3181e3e commit f37a430

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

examples/notebooks/mednist_tutorial.ipynb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"from torch.utils.data import Dataset, DataLoader\n",
8181
"\n",
8282
"from monai.transforms import \\\n",
83-
" Compose, LoadPNG, AddChannel, ScaleIntensity, ToTensor, RandRotate, RandFlip, RandZoom, Resize\n",
83+
" Compose, LoadPNG, AddChannel, ScaleIntensity, ToTensor, RandRotate, RandFlip, RandZoom\n",
8484
"from monai.networks.nets import densenet121\n",
8585
"from monai.metrics import compute_roc_auc\n",
8686
"\n",
@@ -235,10 +235,9 @@
235235
" LoadPNG(image_only=True),\n",
236236
" AddChannel(),\n",
237237
" ScaleIntensity(),\n",
238-
" RandRotate(degrees=15, prob=0.5),\n",
238+
" RandRotate(degrees=15, prob=0.5, reshape=False),\n",
239239
" RandFlip(spatial_axis=0, prob=0.5),\n",
240240
" RandZoom(min_zoom=0.9, max_zoom=1.1, prob=0.5),\n",
241-
" Resize(spatial_size=(64, 64), mode='constant'),\n",
242241
" ToTensor()\n",
243242
"])\n",
244243
"\n",

monai/transforms/spatial/array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class Zoom(Transform):
333333
prefilter (bool): Apply spline_filter before interpolation. Default: True.
334334
use_gpu (bool): Should use cpu or gpu. Uses cupyx which doesn't support order > 1 and modes
335335
'wrap' and 'reflect'. Defaults to cpu for these cases or if cupyx not found.
336-
keep_size (bool): Should keep original size (pad if needed).
336+
keep_size (bool): Should keep original size (pad if needed), default is True.
337337
"""
338338

339339
def __init__(
@@ -344,7 +344,7 @@ def __init__(
344344
cval=0,
345345
prefilter=True,
346346
use_gpu=False,
347-
keep_size=False,
347+
keep_size=True,
348348
):
349349
self.zoom = zoom
350350
self.interp_order = interp_order
@@ -585,7 +585,7 @@ class RandZoom(Randomizable, Transform):
585585
prefilter (bool): Apply spline_filter before interpolation. Default: True.
586586
use_gpu (bool): Should use cpu or gpu. Uses cupyx which doesn't support order > 1 and modes
587587
'wrap' and 'reflect'. Defaults to cpu for these cases or if cupyx not found.
588-
keep_size (bool): Should keep original size (pad if needed).
588+
keep_size (bool): Should keep original size (pad if needed), default is True.
589589
"""
590590

591591
def __init__(
@@ -598,7 +598,7 @@ def __init__(
598598
cval=0,
599599
prefilter=True,
600600
use_gpu=False,
601-
keep_size=False,
601+
keep_size=True,
602602
):
603603
if hasattr(min_zoom, "__iter__") and hasattr(max_zoom, "__iter__"):
604604
assert len(min_zoom) == len(max_zoom), "min_zoom and max_zoom must have same length."

monai/transforms/spatial/dictionary.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ class Zoomd(MapTransform):
714714
prefilter (bool or sequence of bool): Apply spline_filter before interpolation. Default: True.
715715
use_gpu (bool): Should use cpu or gpu. Uses cupyx which doesn't support order > 1 and modes
716716
'wrap' and 'reflect'. Defaults to cpu for these cases or if cupyx not found.
717-
keep_size (bool): Should keep original size (pad if needed).
717+
keep_size (bool): Should keep original size (pad if needed), default is True.
718718
"""
719719

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

774774
def __init__(
@@ -782,7 +782,7 @@ def __init__(
782782
cval=0,
783783
prefilter=True,
784784
use_gpu=False,
785-
keep_size=False,
785+
keep_size=True,
786786
):
787787
super().__init__(keys)
788788
if hasattr(min_zoom, "__iter__") and hasattr(max_zoom, "__iter__"):

tests/test_integration_classification_2d.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
RandFlip,
3131
RandRotate,
3232
RandZoom,
33-
Resize,
3433
ScaleIntensity,
3534
ToTensor,
3635
)
@@ -62,10 +61,9 @@ def run_training_test(root_dir, train_x, train_y, val_x, val_y, device=torch.dev
6261
LoadPNG(image_only=True),
6362
AddChannel(),
6463
ScaleIntensity(),
65-
RandRotate(degrees=15, prob=0.5),
64+
RandRotate(degrees=15, prob=0.5, reshape=False),
6665
RandFlip(spatial_axis=0, prob=0.5),
6766
RandZoom(min_zoom=0.9, max_zoom=1.1, prob=0.5),
68-
Resize(spatial_size=(64, 64), mode="constant"),
6967
ToTensor(),
7068
]
7169
)
@@ -223,11 +221,11 @@ def test_training(self):
223221

224222
# check training properties
225223
np.testing.assert_allclose(
226-
losses, [0.8501208358129878, 0.18469145818121113, 0.08108749352158255, 0.04965383692342005], rtol=1e-3
224+
losses, [0.8009556981788319, 0.16794362251356149, 0.07708252014912617, 0.04769148252856959], rtol=1e-3
227225
)
228226
repeated[i].extend(losses)
229227
print("best metric", best_metric)
230-
np.testing.assert_allclose(best_metric, 0.9999480167572079, rtol=1e-4)
228+
np.testing.assert_allclose(best_metric, 0.9999460507942981, rtol=1e-4)
231229
repeated[i].append(best_metric)
232230
np.testing.assert_allclose(best_metric_epoch, 4)
233231
model_file = os.path.join(self.data_dir, "best_metric_model.pth")
@@ -236,7 +234,7 @@ def test_training(self):
236234
infer_metric = run_inference_test(self.data_dir, self.test_x, self.test_y, device=self.device)
237235

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

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

0 commit comments

Comments
 (0)