diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index 96d443c193..39d2f0d33f 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -1483,12 +1483,13 @@ def __call__( See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample """ self.randomize() - # if not doing transform and spatial size not defined, nothing to do + # if not doing transform and spatial size doesn't change, nothing to do # except convert to float and convert numpy/torch - if not self._do_transform and not spatial_size and not self.spatial_size: + sp_size = fall_back_tuple(spatial_size or self.spatial_size, img.shape[1:]) + do_resampling = self._do_transform or (sp_size != ensure_tuple(img.shape[1:])) + if not do_resampling: img = img.float() if isinstance(img, torch.Tensor) else img.astype("float32") return torch.Tensor(img) if self.resampler.as_tensor_output else np.array(img) - sp_size = fall_back_tuple(spatial_size or self.spatial_size, img.shape[1:]) grid = self.get_identity_grid(sp_size) if self._do_transform: grid = self.rand_affine_grid(grid=grid) diff --git a/monai/transforms/spatial/dictionary.py b/monai/transforms/spatial/dictionary.py index 4a4f039d28..31c8293ecf 100644 --- a/monai/transforms/spatial/dictionary.py +++ b/monai/transforms/spatial/dictionary.py @@ -759,7 +759,7 @@ def __call__( sp_size = fall_back_tuple(self.rand_affine.spatial_size, data[self.keys[0]].shape[1:]) # change image size or do random transform - do_resampling = self.rand_affine.spatial_size is not None or self._do_transform + do_resampling = self._do_transform or (sp_size != ensure_tuple(data[self.keys[0]].shape[1:])) # to be consistent with the self._do_transform case (dtype and device) affine = torch.as_tensor(np.eye(len(sp_size) + 1), device=self.rand_affine.rand_affine_grid.device)