Skip to content
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

All transforms support torch and numpy #2949

Merged
merged 20 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 18 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
9 changes: 6 additions & 3 deletions monai/networks/layers/spatial_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def backward(ctx, grad):
return None, grads[0], None, None, None


def grid_pull(input: torch.Tensor, grid: torch.Tensor, interpolation="linear", bound="zero", extrapolate: bool = True):
def grid_pull(
input: torch.Tensor, grid: torch.Tensor, interpolation="linear", bound="zero", extrapolate: bool = True
) -> torch.Tensor:
"""
Sample an image with respect to a deformation field.

Expand Down Expand Up @@ -112,8 +114,9 @@ def grid_pull(input: torch.Tensor, grid: torch.Tensor, interpolation="linear", b
_C.InterpolationType.__members__[i] if isinstance(i, str) else _C.InterpolationType(i)
for i in ensure_tuple(interpolation)
]

return _GridPull.apply(input, grid, interpolation, bound, extrapolate)
out: torch.Tensor
out = _GridPull.apply(input, grid, interpolation, bound, extrapolate) # type: ignore
return out


class _GridPush(torch.autograd.Function):
Expand Down
20 changes: 20 additions & 0 deletions monai/transforms/croppad/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ def __call__(self, img: Union[np.ndarray, torch.Tensor]):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray)
sd = min(len(self.slices), len(img.shape[1:])) # spatial dims
slices = [slice(None)] + self.slices[:sd]
return img[tuple(slices)]
Expand Down Expand Up @@ -449,6 +450,7 @@ def __call__(self, img: np.ndarray):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
roi_size = fall_back_tuple(self.roi_size, img.shape[1:])
center = [i // 2 for i in img.shape[1:]]
cropper = SpatialCrop(roi_center=center, roi_size=roi_size)
Expand All @@ -469,6 +471,7 @@ def __init__(self, roi_scale: Union[Sequence[float], float]):
self.roi_scale = roi_scale

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
img_size = img.shape[1:]
ndim = len(img_size)
roi_size = [ceil(r * s) for r, s in zip(ensure_tuple_rep(self.roi_scale, ndim), img_size)]
Expand Down Expand Up @@ -530,6 +533,7 @@ def __call__(self, img: np.ndarray):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize(img.shape[1:])
if self._size is None:
raise AssertionError
Expand Down Expand Up @@ -576,6 +580,7 @@ def __call__(self, img: np.ndarray):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
img_size = img.shape[1:]
ndim = len(img_size)
self.roi_size = [ceil(r * s) for r, s in zip(ensure_tuple_rep(self.roi_scale, ndim), img_size)]
Expand Down Expand Up @@ -645,6 +650,7 @@ def __call__(self, img: np.ndarray) -> List[np.ndarray]:
Apply the transform to `img`, assuming `img` is channel-first and
cropping doesn't change the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
return [self.cropper(img) for _ in range(self.num_samples)]


Expand Down Expand Up @@ -754,6 +760,8 @@ def __call__(self, img: np.ndarray, mode: Optional[Union[NumpyPadMode, str]] = N
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't change the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore

box_start, box_end = self.compute_bounding_box(img)
cropped = self.crop_pad(img, box_start, box_end, mode)

Expand Down Expand Up @@ -799,12 +807,16 @@ def __call__(self, img: np.ndarray, weight_map: Optional[np.ndarray] = None) ->
Returns:
A list of image patches
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
if weight_map is None:
weight_map = self.weight_map
if weight_map is None:
raise ValueError("weight map must be provided for weighted patch sampling.")
if img.shape[1:] != weight_map.shape[1:]:
raise ValueError(f"image and weight map spatial shape mismatch: {img.shape[1:]} vs {weight_map.shape[1:]}.")

weight_map, *_ = convert_data_type(weight_map, np.ndarray) # type: ignore

self.randomize(weight_map)
_spatial_size = fall_back_tuple(self.spatial_size, weight_map.shape[1:])
results = []
Expand Down Expand Up @@ -940,6 +952,9 @@ def __call__(
if image is None:
image = self.image

image, *_ = convert_data_type(image, np.ndarray) # type: ignore
label, *_ = convert_data_type(label, np.ndarray) # type: ignore

self.randomize(label, fg_indices, bg_indices, image)
results: List[np.ndarray] = []
if self.centers is not None:
Expand Down Expand Up @@ -1073,6 +1088,9 @@ def __call__(
if image is None:
image = self.image

image, *_ = convert_data_type(image, np.ndarray) # type: ignore
label, *_ = convert_data_type(label, np.ndarray) # type: ignore

self.randomize(label, indices, image)
results: List[np.ndarray] = []
if self.centers is not None:
Expand Down Expand Up @@ -1125,6 +1143,7 @@ def __call__(self, img: np.ndarray, mode: Optional[Union[NumpyPadMode, str]] = N
If None, defaults to the ``mode`` in construction.
See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
return self.padder(self.cropper(img), mode=mode)


Expand Down Expand Up @@ -1159,6 +1178,7 @@ def __call__(self, img: np.ndarray) -> np.ndarray:
"""
See also: :py:class:`monai.transforms.utils.generate_spatial_bounding_box`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
bbox = []

for channel in range(img.shape[0]):
Expand Down
5 changes: 4 additions & 1 deletion monai/transforms/croppad/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from monai.utils import ImageMetaKey as Key
from monai.utils import Method, NumpyPadMode, PytorchPadMode, ensure_tuple, ensure_tuple_rep, fall_back_tuple
from monai.utils.enums import InverseKeys
from monai.utils.type_conversion import convert_data_type

__all__ = [
"PadModeSequence",
Expand Down Expand Up @@ -848,7 +849,9 @@ def __init__(

def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
box_start, box_end = self.cropper.compute_bounding_box(img=d[self.source_key])
img: np.ndarray
img, *_ = convert_data_type(d[self.source_key], np.ndarray) # type: ignore
box_start, box_end = self.cropper.compute_bounding_box(img=img)
d[self.start_coord_key] = box_start
d[self.end_coord_key] = box_end
for key, m in self.key_iterator(d, self.mode):
Expand Down
16 changes: 15 additions & 1 deletion monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ def __call__(self, img: np.ndarray):
"""
Apply the transform to `img`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize(data=img)
if not self._do_transform:
return img
Expand Down Expand Up @@ -731,6 +732,7 @@ def __call__(self, img: np.ndarray):
"""
Apply the transform to `img`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
epsilon = 1e-7
img_min = img.min()
img_range = img.max() - img_min
Expand Down Expand Up @@ -773,6 +775,7 @@ def __call__(self, img: np.ndarray):
"""
Apply the transform to `img`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if self.gamma_value is None:
raise ValueError("gamma_value is not set.")
Expand Down Expand Up @@ -910,10 +913,13 @@ def __call__(self, img: np.ndarray, mask_data: Optional[np.ndarray] = None) -> n
- ValueError: When ``mask_data`` and ``img`` channels differ and ``mask_data`` is not single channel.

"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
mask_data = self.mask_data if mask_data is None else mask_data
if mask_data is None:
raise ValueError("must provide the mask_data when initializing the transform or at runtime.")

mask_data, *_ = convert_data_type(mask_data, np.ndarray) # type: ignore

mask_data = np.asarray(self.select_fn(mask_data))
if mask_data.shape[0] != 1 and mask_data.shape[0] != img.shape[0]:
raise ValueError(
Expand All @@ -936,7 +942,7 @@ class SavitzkyGolaySmooth(Transform):
or ``'circular'``. Default: ``'zeros'``. See ``torch.nn.Conv1d()`` for more information.
"""

backend = [TransformBackends.NUMPY]
backend = [TransformBackends.TORCH]

def __init__(self, window_length: int, order: int, axis: int = 1, mode: str = "zeros"):

Expand Down Expand Up @@ -1000,6 +1006,7 @@ def __call__(self, img: np.ndarray):
np.ndarray containing envelope of data in img along the specified axis.

"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
# add one to transform axis because a batch axis will be added at dimension 0
hilbert_transform = HilbertTransform(self.axis + 1, self.n)
# convert to Tensor and add Batch axis expected by HilbertTransform
Expand All @@ -1026,6 +1033,7 @@ def __init__(self, sigma: Union[Sequence[float], float] = 1.0, approx: str = "er
self.approx = approx

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
gaussian_filter = GaussianFilter(img.ndim - 1, self.sigma, approx=self.approx)
input_data = torch.as_tensor(np.ascontiguousarray(img), dtype=torch.float).unsqueeze(0)
return gaussian_filter(input_data).squeeze(0).detach().numpy()
Expand Down Expand Up @@ -1070,6 +1078,7 @@ def randomize(self, data: Optional[Any] = None) -> None:
self.z = self.R.uniform(low=self.sigma_z[0], high=self.sigma_z[1])

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if not self._do_transform:
return img
Expand Down Expand Up @@ -1117,6 +1126,7 @@ def __init__(
self.approx = approx

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
gaussian_filter1 = GaussianFilter(img.ndim - 1, self.sigma1, approx=self.approx)
gaussian_filter2 = GaussianFilter(img.ndim - 1, self.sigma2, approx=self.approx)
input_data = torch.as_tensor(np.ascontiguousarray(img), dtype=torch.float).unsqueeze(0)
Expand Down Expand Up @@ -1183,6 +1193,7 @@ def randomize(self, data: Optional[Any] = None) -> None:
self.a = self.R.uniform(low=self.alpha[0], high=self.alpha[1])

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if not self._do_transform:
return img
Expand Down Expand Up @@ -1227,6 +1238,7 @@ def randomize(self, data: Optional[Any] = None) -> None:
)

def __call__(self, img: np.ndarray) -> np.ndarray:
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if not self._do_transform:
return img
Expand Down Expand Up @@ -1713,6 +1725,7 @@ def _transform_holes(self, img: np.ndarray) -> np.ndarray:
raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize(img.shape[1:])
if self._do_transform:
img = self._transform_holes(img=img)
Expand Down Expand Up @@ -1871,6 +1884,7 @@ def __init__(
self.dtype = dtype

def __call__(self, img: np.ndarray, mask: Optional[np.ndarray] = None) -> np.ndarray:
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
return equalize_hist(
img=img,
mask=mask if mask is not None else self.mask,
Expand Down
Loading