From 6f94de33ec30ad929ec336ebd2e2c4011f9fdbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Wed, 5 Oct 2022 16:50:57 -0400 Subject: [PATCH 01/10] Adding a slightly modified Ebrahim's code It is adapted to MONAI style. Co-authored-by: Ebrahim Ebrahim ebrahim.ebrahim@kitware.com --- monai/networks/layers/simplelayers.py | 138 ++++++++++++++++++++++---- 1 file changed, 118 insertions(+), 20 deletions(-) diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index 8c421719a7c..d06a3875a54 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -32,10 +32,12 @@ "GaussianFilter", "HilbertTransform", "LLTM", + "MedianBlur", "Reshape", "SavitzkyGolayFilter", "SkipConnection", "apply_filter", + "median_blur", "separable_filtering", ] @@ -47,11 +49,11 @@ class ChannelPad(nn.Module): """ def __init__( - self, - spatial_dims: int, - in_channels: int, - out_channels: int, - mode: Union[ChannelMatching, str] = ChannelMatching.PAD, + self, + spatial_dims: int, + in_channels: int, + out_channels: int, + mode: Union[ChannelMatching, str] = ChannelMatching.PAD, ): """ @@ -160,15 +162,14 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: def _separable_filtering_conv( - input_: torch.Tensor, - kernels: List[torch.Tensor], - pad_mode: str, - d: int, - spatial_dims: int, - paddings: List[int], - num_channels: int, + input_: torch.Tensor, + kernels: List[torch.Tensor], + pad_mode: str, + d: int, + spatial_dims: int, + paddings: List[int], + num_channels: int, ) -> torch.Tensor: - if d < 0: return input_ @@ -280,7 +281,7 @@ def apply_filter(x: torch.Tensor, kernel: torch.Tensor, **kwargs) -> torch.Tenso ) kernel = kernel.to(x) # broadcast kernel size to (batch chns, spatial_kernel_size) - kernel = kernel.expand(batch, chns, *kernel.shape[(k_size - n_spatial) :]) + kernel = kernel.expand(batch, chns, *kernel.shape[(k_size - n_spatial):]) kernel = kernel.reshape(-1, 1, *kernel.shape[2:]) # group=1 x = x.view(1, kernel.shape[0], *spatials) conv = [F.conv1d, F.conv2d, F.conv3d][n_spatial - 1] @@ -434,14 +435,111 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: return torch.as_tensor(ht, device=ht.device, dtype=ht.dtype) +def get_binary_kernel3d(window_size: Sequence[int]) -> torch.Tensor: + r"""Create a binary kernel to extract the patches. + If the window size is HxWxD will create a (H*W*D)xHxWxD kernel. + """ + window_range: int = window_size[0] * window_size[1] * window_size[2] + kernel: torch.Tensor = torch.zeros(window_range, 1, window_size[0], window_size[1], window_size[2]) + for i in range(window_size[0]): + for j in range(window_size[1]): + for k in range(window_size[2]): + l: int = i * window_size[2] * window_size[1] + j * window_size[2] + k + kernel[l, 0, i, j, k] += 1.0 + return kernel + + +def _compute_zero_padding(kernel_size: Sequence[int]) -> List[int]: + r"""Utility function that computes zero padding tuple.""" + computed: List[int] = [(k - 1) // 2 for k in kernel_size] + return computed + + +def median_blur(in_tensor: torch.Tensor, kernel_size: Sequence[int], + kernel: torch.Tensor = None) -> torch.Tensor: + r"""Blur an image using the median filter. + Args: + in_tensor: the input image with shape :math:`(B,C,H,W,D)`. + kernel_size: the blurring kernel size. + Returns: + the blurred input tensor with shape :math:`(B,C,H,W,D)`. + Example: + >>> x = torch.rand(2, 4, 5, 7, 6) + >>> output = median_blur(x, (3, 3, 3)) + >>> output.shape + torch.Size([2, 4, 5, 7, 6]) + """ + if not isinstance(in_tensor, torch.Tensor): + raise TypeError(f"Input type is not a torch.Tensor. Got {type(in_tensor)}") + + if not len(in_tensor.shape) == 5: + raise ValueError(f"Invalid in_tensor shape, we expect BxCxHxWxD. Got: {in_tensor.shape}") + + padding: Sequence[int] = _compute_zero_padding(kernel_size) + + # prepare kernel + if kernel is None: + kernel: torch.Tensor = get_binary_kernel3d(kernel_size).to(in_tensor) + b, c, h, w, d = in_tensor.shape + + # map the local window to single vector + features: torch.Tensor = F.conv3d(in_tensor.reshape(b * c, 1, h, w, d), kernel, padding=padding, stride=1) + features = features.view(b, c, -1, h, w, d) # BxCx(K_h * K_w * K_d)xHxWxD + + # compute the median along the feature axis + median: torch.Tensor = torch.median(features, dim=2)[0] + + return median + + +class MedianBlur(nn.Module): + r"""Blur an image using the median filter. + Args: + radius: the blurring kernel radius (radius of 1 corresponds to 3x3x3 kernel). + Returns: + the blurred in_tensor tensor. + Shape: + - Input: :math:`(B, C, H, W, D)` + - Output: :math:`(B, C, H, W, D)` + Example: + >>> in_tensor = torch.rand(2, 4, 5, 7, 6) + >>> blur = MedianBlur([1, 1, 1]) # 3x3x3 kernel + >>> output = blur(in_tensor) + >>> output.shape + torch.Size([2, 4, 5, 7, 6]) + """ + + def __init__(self, radius: Union[Sequence[int], int], device='cpu') -> None: + super().__init__() + self.radius: Sequence[int] = radius + if issequenceiterable(radius): + if len(radius) != 3: + raise ValueError(f"Only 3 dimensional images are supported by {str(self.__class__)}") + else: + sigma = [deepcopy(radius) for _ in range(3)] + self.radius: Sequence[int] = radius + self.radius = get_binary_kernel3d(1 + 2 * radius).to(device) + + def forward(self, in_tensor: torch.Tensor, number_of_passes=1) -> torch.Tensor: + """ + Args: + in_tensor: in shape [batch, channels, H, W, D]. + number_of_passes: median filtering will be repeated this many times + """ + x = in_tensor + for _ in range(number_of_passes): + x = median_blur(x, self.radius, self.kernel) + return x + + class GaussianFilter(nn.Module): def __init__( - self, - spatial_dims: int, - sigma: Union[Sequence[float], float, Sequence[torch.Tensor], torch.Tensor], - truncated: float = 4.0, - approx: str = "erf", - requires_grad: bool = False, + self, + spatial_dims: int, + sigma: Union[Sequence[float], float, Sequence[torch.Tensor], torch.Tensor], + truncated: float = 4.0, + approx: str = "erf", + requires_grad: bool = False, ) -> None: """ Args: From 88d089a393b921538239020287951e7a525a1d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Mon, 10 Oct 2022 10:33:10 -0400 Subject: [PATCH 02/10] Add MedianSmooth transform based on GaussianSmooth --- docs/source/transforms.rst | 16 ++++ monai/networks/layers/__init__.py | 2 + monai/networks/layers/simplelayers.py | 106 ++++++++++++----------- monai/transforms/__init__.py | 4 + monai/transforms/intensity/array.py | 34 +++++++- monai/transforms/intensity/dictionary.py | 34 ++++++++ tests/test_median_filter.py | 69 +++++++++++++++ tests/test_median_smooth.py | 39 +++++++++ tests/test_median_smoothd.py | 69 +++++++++++++++ 9 files changed, 322 insertions(+), 51 deletions(-) create mode 100644 tests/test_median_filter.py create mode 100644 tests/test_median_smooth.py create mode 100644 tests/test_median_smoothd.py diff --git a/docs/source/transforms.rst b/docs/source/transforms.rst index ccd050534d4..d12ad8197a8 100644 --- a/docs/source/transforms.rst +++ b/docs/source/transforms.rst @@ -336,6 +336,14 @@ Intensity :members: :special-members: __call__ +`MedianSmooth` +"""""""""""""""" +.. image:: https://github.com/Project-MONAI/DocImages/raw/main/transforms/MedianSmooth.png + :alt: example of MedianSmooth +.. autoclass:: MedianSmooth + :members: + :special-members: __call__ + `GaussianSmooth` """""""""""""""" .. image:: https://github.com/Project-MONAI/DocImages/raw/main/transforms/GaussianSmooth.png @@ -1415,6 +1423,14 @@ Intensity (Dict) :members: :special-members: __call__ +`MedianSmoothd` +""""""""""""""""" +.. image:: https://github.com/Project-MONAI/DocImages/raw/main/transforms/MedianSmoothd.png + :alt: example of MedianSmoothd +.. autoclass:: MedianSmoothd + :members: + :special-members: __call__ + `GaussianSmoothd` """"""""""""""""" .. image:: https://github.com/Project-MONAI/DocImages/raw/main/transforms/GaussianSmoothd.png diff --git a/monai/networks/layers/__init__.py b/monai/networks/layers/__init__.py index f122dccee66..31bd36dd8fb 100644 --- a/monai/networks/layers/__init__.py +++ b/monai/networks/layers/__init__.py @@ -20,10 +20,12 @@ Flatten, GaussianFilter, HilbertTransform, + MedianFilter, Reshape, SavitzkyGolayFilter, SkipConnection, apply_filter, + median_filter, separable_filtering, ) from .spatial_transforms import AffineTransform, grid_count, grid_grad, grid_pull, grid_push diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index d06a3875a54..85909e05f8c 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -32,12 +32,12 @@ "GaussianFilter", "HilbertTransform", "LLTM", - "MedianBlur", + "MedianFilter", "Reshape", "SavitzkyGolayFilter", "SkipConnection", "apply_filter", - "median_blur", + "median_filter", "separable_filtering", ] @@ -49,11 +49,11 @@ class ChannelPad(nn.Module): """ def __init__( - self, - spatial_dims: int, - in_channels: int, - out_channels: int, - mode: Union[ChannelMatching, str] = ChannelMatching.PAD, + self, + spatial_dims: int, + in_channels: int, + out_channels: int, + mode: Union[ChannelMatching, str] = ChannelMatching.PAD, ): """ @@ -162,13 +162,13 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: def _separable_filtering_conv( - input_: torch.Tensor, - kernels: List[torch.Tensor], - pad_mode: str, - d: int, - spatial_dims: int, - paddings: List[int], - num_channels: int, + input_: torch.Tensor, + kernels: List[torch.Tensor], + pad_mode: str, + d: int, + spatial_dims: int, + paddings: List[int], + num_channels: int, ) -> torch.Tensor: if d < 0: return input_ @@ -281,7 +281,7 @@ def apply_filter(x: torch.Tensor, kernel: torch.Tensor, **kwargs) -> torch.Tenso ) kernel = kernel.to(x) # broadcast kernel size to (batch chns, spatial_kernel_size) - kernel = kernel.expand(batch, chns, *kernel.shape[(k_size - n_spatial):]) + kernel = kernel.expand(batch, chns, *kernel.shape[(k_size - n_spatial) :]) kernel = kernel.reshape(-1, 1, *kernel.shape[2:]) # group=1 x = x.view(1, kernel.shape[0], *spatials) conv = [F.conv1d, F.conv2d, F.conv3d][n_spatial - 1] @@ -455,91 +455,97 @@ def _compute_zero_padding(kernel_size: Sequence[int]) -> List[int]: return computed -def median_blur(in_tensor: torch.Tensor, kernel_size: Sequence[int], - kernel: torch.Tensor = None) -> torch.Tensor: - r"""Blur an image using the median filter. +def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: torch.Tensor = None) -> torch.Tensor: + r"""Apply median filter to an image. Args: - in_tensor: the input image with shape :math:`(B,C,H,W,D)`. - kernel_size: the blurring kernel size. + in_tensor: the input image with shape :math:`(C,H,W,D)`. + kernel_size: the convolution kernel size. Returns: - the blurred input tensor with shape :math:`(B,C,H,W,D)`. + the filtered input tensor with shape :math:`(C,H,W,D)`. Example: - >>> x = torch.rand(2, 4, 5, 7, 6) - >>> output = median_blur(x, (3, 3, 3)) + >>> x = torch.rand(4, 5, 7, 6) + >>> output = median_filter(x, (3, 3, 3)) >>> output.shape - torch.Size([2, 4, 5, 7, 6]) + torch.Size([4, 5, 7, 6]) """ if not isinstance(in_tensor, torch.Tensor): raise TypeError(f"Input type is not a torch.Tensor. Got {type(in_tensor)}") - if not len(in_tensor.shape) == 5: - raise ValueError(f"Invalid in_tensor shape, we expect BxCxHxWxD. Got: {in_tensor.shape}") + original_shape = in_tensor.shape + if len(original_shape) == 5 and original_shape[0] == 1: + in_tensor = in_tensor.squeeze(0) + if not len(in_tensor.shape) == 4: + raise ValueError(f"Invalid in_tensor shape, we expect CxHxWxD. Got: {in_tensor.shape}") padding: Sequence[int] = _compute_zero_padding(kernel_size) # prepare kernel if kernel is None: kernel: torch.Tensor = get_binary_kernel3d(kernel_size).to(in_tensor) - b, c, h, w, d = in_tensor.shape + else: + kernel: torch.Tensor = kernel.to(in_tensor) + c, h, w, d = in_tensor.shape # map the local window to single vector - features: torch.Tensor = F.conv3d(in_tensor.reshape(b * c, 1, h, w, d), kernel, padding=padding, stride=1) - features = features.view(b, c, -1, h, w, d) # BxCx(K_h * K_w * K_d)xHxWxD + features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, h, w, d), kernel, padding=padding, stride=1) + features = features.view(c, -1, h, w, d) # Cx(K_h * K_w * K_d)xHxWxD # compute the median along the feature axis - median: torch.Tensor = torch.median(features, dim=2)[0] + median: torch.Tensor = torch.median(features, dim=1)[0] + median = median.reshape(original_shape) return median -class MedianBlur(nn.Module): - r"""Blur an image using the median filter. +class MedianFilter(nn.Module): + r"""Apply median filter to an image. Args: radius: the blurring kernel radius (radius of 1 corresponds to 3x3x3 kernel). Returns: - the blurred in_tensor tensor. + filtered input tensor. Shape: - - Input: :math:`(B, C, H, W, D)` - - Output: :math:`(B, C, H, W, D)` + - Input: :math:`(C, H, W, D)` + - Output: :math:`(C, H, W, D)` Example: - >>> in_tensor = torch.rand(2, 4, 5, 7, 6) - >>> blur = MedianBlur([1, 1, 1]) # 3x3x3 kernel + >>> in_tensor = torch.rand(4, 5, 7, 6) + >>> blur = MedianFilter([1, 1, 1]) # 3x3x3 kernel >>> output = blur(in_tensor) >>> output.shape - torch.Size([2, 4, 5, 7, 6]) + torch.Size([4, 5, 7, 6]) """ - def __init__(self, radius: Union[Sequence[int], int], device='cpu') -> None: + def __init__(self, radius: Union[Sequence[int], int], device="cpu") -> None: super().__init__() self.radius: Sequence[int] = radius if issequenceiterable(radius): if len(radius) != 3: raise ValueError(f"Only 3 dimensional images are supported by {str(self.__class__)}") + else: + self.window: Sequence[int] = [1 + 2 * deepcopy(r) for r in radius] else: - sigma = [deepcopy(radius) for _ in range(3)] - self.radius: Sequence[int] = radius - self.radius = get_binary_kernel3d(1 + 2 * radius).to(device) + self.window: Sequence[int] = [1 + 2 * deepcopy(radius) for _ in range(3)] + self.kernel = get_binary_kernel3d(self.window).to(device) def forward(self, in_tensor: torch.Tensor, number_of_passes=1) -> torch.Tensor: """ Args: - in_tensor: in shape [batch, channels, H, W, D]. + in_tensor: in shape [channels, H, W, D]. number_of_passes: median filtering will be repeated this many times """ x = in_tensor for _ in range(number_of_passes): - x = median_blur(x, self.radius, self.kernel) + x = median_filter(x, self.window, self.kernel) return x class GaussianFilter(nn.Module): def __init__( - self, - spatial_dims: int, - sigma: Union[Sequence[float], float, Sequence[torch.Tensor], torch.Tensor], - truncated: float = 4.0, - approx: str = "erf", - requires_grad: bool = False, + self, + spatial_dims: int, + sigma: Union[Sequence[float], float, Sequence[torch.Tensor], torch.Tensor], + truncated: float = 4.0, + approx: str = "erf", + requires_grad: bool = False, ) -> None: """ Args: diff --git a/monai/transforms/__init__.py b/monai/transforms/__init__.py index 7df5c4f075c..389571d16fc 100644 --- a/monai/transforms/__init__.py +++ b/monai/transforms/__init__.py @@ -99,6 +99,7 @@ IntensityRemap, KSpaceSpikeNoise, MaskIntensity, + MedianSmooth, NormalizeIntensity, RandAdjustContrast, RandBiasField, @@ -152,6 +153,9 @@ MaskIntensityd, MaskIntensityD, MaskIntensityDict, + MedianSmoothd, + MedianSmoothD, + MedianSmoothDict, NormalizeIntensityd, NormalizeIntensityD, NormalizeIntensityDict, diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 25110ac0dde..80562c08d86 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -26,7 +26,7 @@ from monai.config.type_definitions import NdarrayOrTensor, NdarrayTensor from monai.data.meta_obj import get_track_meta from monai.data.utils import get_random_patch, get_valid_patch_size -from monai.networks.layers import GaussianFilter, HilbertTransform, SavitzkyGolayFilter +from monai.networks.layers import GaussianFilter, HilbertTransform, MedianFilter, SavitzkyGolayFilter from monai.transforms.transform import RandomizableTransform, Transform from monai.transforms.utils import Fourier, equalize_hist, is_positive, rescale_array from monai.transforms.utils_pytorch_numpy_unification import clip, percentile, where @@ -56,6 +56,7 @@ "MaskIntensity", "DetectEnvelope", "SavitzkyGolaySmooth", + "MedianSmooth", "GaussianSmooth", "RandGaussianSmooth", "GaussianSharpen", @@ -1136,6 +1137,37 @@ def __call__(self, img: NdarrayOrTensor): return out +class MedianSmooth(Transform): + """ + Apply median filter to the input data based on specified `radius` parameter. + A default value `radius=1` is provided for reference. + + Args: + radius: if a list of values, must match the count of spatial dimensions of input data, + and apply every value in the list to 1 spatial dimension. if only 1 value provided, + use it for all spatial dimensions. + """ + + backend = [TransformBackends.TORCH] + + def __init__(self, radius: Union[Sequence[int], int] = 1) -> None: + self.radius = radius + + def __call__(self, img: NdarrayTensor) -> NdarrayTensor: + img = convert_to_tensor(img, track_meta=get_track_meta()) + img_t, *_ = convert_data_type(img, torch.Tensor, dtype=torch.float) + radius: Union[Sequence[torch.Tensor], torch.Tensor] + if isinstance(self.radius, Sequence): + radius = [torch.as_tensor(s, device=img_t.device) for s in self.radius] + else: + radius = torch.as_tensor(self.radius, device=img_t.device) + median_filter_instance = MedianFilter(radius) # dim=img_t.ndim - 1 + out_t: torch.Tensor = median_filter_instance(img_t.unsqueeze(0)).squeeze(0) + out, *_ = convert_to_dst_type(out_t, dst=img, dtype=out_t.dtype) + + return out + + class GaussianSmooth(Transform): """ Apply Gaussian smooth to the input data based on specified `sigma` parameter. diff --git a/monai/transforms/intensity/dictionary.py b/monai/transforms/intensity/dictionary.py index d3847eb4b60..efdfd502ca9 100644 --- a/monai/transforms/intensity/dictionary.py +++ b/monai/transforms/intensity/dictionary.py @@ -32,6 +32,7 @@ HistogramNormalize, KSpaceSpikeNoise, MaskIntensity, + MedianSmooth, NormalizeIntensity, RandAdjustContrast, RandBiasField, @@ -78,6 +79,7 @@ "ScaleIntensityRangePercentilesd", "MaskIntensityd", "SavitzkyGolaySmoothd", + "MedianSmoothd", "GaussianSmoothd", "RandGaussianSmoothd", "GaussianSharpend", @@ -124,6 +126,8 @@ "MaskIntensityDict", "SavitzkyGolaySmoothD", "SavitzkyGolaySmoothDict", + "MedianSmoothD", + "MedianSmoothDict", "GaussianSmoothD", "GaussianSmoothDict", "RandGaussianSmoothD", @@ -988,6 +992,35 @@ def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, N return d +class MedianSmoothd(MapTransform): + """ + Dictionary-based wrapper of :py:class:`monai.transforms.MedianSmooth`. + + Args: + keys: keys of the corresponding items to be transformed. + See also: :py:class:`monai.transforms.compose.MapTransform` + radius: if a list of values, must match the count of spatial dimensions of input data, + and apply every value in the list to 1 spatial dimension. if only 1 value provided, + use it for all spatial dimensions. + allow_missing_keys: don't raise exception if key is missing. + + """ + + backend = MedianSmooth.backend + + def __init__( + self, keys: KeysCollection, radius: Union[Sequence[int], int], allow_missing_keys: bool = False + ) -> None: + super().__init__(keys, allow_missing_keys) + self.converter = MedianSmooth(radius) + + def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]: + d = dict(data) + for key in self.key_iterator(d): + d[key] = self.converter(d[key]) + return d + + class GaussianSmoothd(MapTransform): """ Dictionary-based wrapper of :py:class:`monai.transforms.GaussianSmooth`. @@ -1780,6 +1813,7 @@ def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, N ScaleIntensityRangePercentilesD = ScaleIntensityRangePercentilesDict = ScaleIntensityRangePercentilesd MaskIntensityD = MaskIntensityDict = MaskIntensityd SavitzkyGolaySmoothD = SavitzkyGolaySmoothDict = SavitzkyGolaySmoothd +MedianSmoothD = MedianSmoothDict = MedianSmoothd GaussianSmoothD = GaussianSmoothDict = GaussianSmoothd RandGaussianSmoothD = RandGaussianSmoothDict = RandGaussianSmoothd GaussianSharpenD = GaussianSharpenDict = GaussianSharpend diff --git a/tests/test_median_filter.py b/tests/test_median_filter.py new file mode 100644 index 00000000000..d3d4ce5a393 --- /dev/null +++ b/tests/test_median_filter.py @@ -0,0 +1,69 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np +import torch + +from monai.networks.layers import MedianFilter + + +class MedianFilterTestCase(unittest.TestCase): + def test_3d(self): + a = torch.ones(1, 1, 4, 3, 4) + g = MedianFilter(1).to(torch.device("cpu:0")) + + expected = np.array( + [ + [ + [ + [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], + [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], + [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], + [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], + ] + ] + ] + ) + out = g(a).cpu().numpy() + np.testing.assert_allclose(out, expected, rtol=1e-5) + + def test_3d_radii(self): + a = torch.ones(1, 1, 4, 3, 2) + g = MedianFilter([3, 2, 1]).to(torch.device("cpu:0")) + + expected = np.array( + [ + [ + [ + [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], + [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], + [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], + [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], + ] + ] + ] + ) + out = g(a).cpu().numpy() + np.testing.assert_allclose(out, expected, rtol=1e-5) + if torch.cuda.is_available(): + g = MedianFilter([3, 2, 1]).to(torch.device("cuda:0")) + np.testing.assert_allclose(g(a.cuda()).cpu().numpy(), expected, rtol=1e-2) + + def test_wrong_args(self): + with self.assertRaisesRegex(ValueError, ""): + MedianFilter([3, 2]).to(torch.device("cpu:0")) + MedianFilter([3, 2, 1]).to(torch.device("cpu:0")) # test init + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_median_smooth.py b/tests/test_median_smooth.py new file mode 100644 index 00000000000..e270fbeca95 --- /dev/null +++ b/tests/test_median_smooth.py @@ -0,0 +1,39 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +from parameterized import parameterized + +from monai.transforms import MedianSmooth +from tests.utils import TEST_NDARRAYS, assert_allclose + +TESTS = [] + +for p in TEST_NDARRAYS: + TESTS.append( + [ + {"radius": 1}, + p([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]), + p([[[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]]]), + ] + ) + + +class TestMedianSmooth(unittest.TestCase): + @parameterized.expand(TESTS) + def test_value(self, argments, image, expected_data): + result = MedianSmooth(**argments)(image) + assert_allclose(result, expected_data, atol=1e-4, rtol=1e-4, type_test="tensor") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_median_smoothd.py b/tests/test_median_smoothd.py new file mode 100644 index 00000000000..338ef160700 --- /dev/null +++ b/tests/test_median_smoothd.py @@ -0,0 +1,69 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np +from parameterized import parameterized + +from monai.transforms import MedianSmoothd +from tests.utils import TEST_NDARRAYS, assert_allclose + +TESTS = [] +for p in TEST_NDARRAYS: + TESTS.append( + [ + {"keys": "img", "radius": 1}, + {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, + np.array( + [[[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]]] + ), + ] + ) + + TESTS.append( + [ + {"keys": "img", "radius": [1, 1, 1]}, + {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, + np.array( + [[[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]]] + ), + ] + ) + + TESTS.append( + [ + {"keys": "img", "radius": [1, 1, 2]}, + {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, + np.array( + [[[0.0, 0.0, 0.0], [0.0, 0, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 0, 0.0], [0.0, 0.0, 0.0]]] + ), + ] + ) + + TESTS.append( + [ + {"keys": "img", "radius": [1, 0, 1]}, + {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, + np.array([[[0.0, 1, 0.0], [0.0, 2, 0.0], [0.0, 3, 0.0]], [[0.0, 1, 0.0], [0.0, 2, 0.0], [0.0, 3, 0.0]]]), + ] + ) + + +class TestMedianSmoothd(unittest.TestCase): + @parameterized.expand(TESTS) + def test_value(self, argments, image, expected_data): + result = MedianSmoothd(**argments)(image) + assert_allclose(result["img"], expected_data, rtol=1e-4, type_test="tensor") + + +if __name__ == "__main__": + unittest.main() From 79abfd1e4d116ef3235d5f0a21f9861eea11aad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Tue, 11 Oct 2022 11:02:54 -0400 Subject: [PATCH 03/10] Fix the last unit test regression failure --- tests/test_median_filter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_median_filter.py b/tests/test_median_filter.py index d3d4ce5a393..d79ccb7520c 100644 --- a/tests/test_median_filter.py +++ b/tests/test_median_filter.py @@ -27,8 +27,8 @@ def test_3d(self): [ [ [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], + [[0.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 0.0]], + [[0.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 0.0]], [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], ] ] From 5ec512ad60c186a4b8bf3d9454afe6bbfaac29b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Tue, 11 Oct 2022 11:11:53 -0400 Subject: [PATCH 04/10] Eric's review feedback --- monai/networks/layers/simplelayers.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index 85909e05f8c..2aca7a60bb7 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -435,18 +435,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: return torch.as_tensor(ht, device=ht.device, dtype=ht.dtype) -def get_binary_kernel3d(window_size: Sequence[int]) -> torch.Tensor: +def get_binary_kernel(window_size: Sequence[int]) -> torch.Tensor: r"""Create a binary kernel to extract the patches. - If the window size is HxWxD will create a (H*W*D)xHxWxD kernel. + The window size HxWxD will create a (H*W*D)xHxWxD kernel. """ - window_range: int = window_size[0] * window_size[1] * window_size[2] - kernel: torch.Tensor = torch.zeros(window_range, 1, window_size[0], window_size[1], window_size[2]) - for i in range(window_size[0]): - for j in range(window_size[1]): - for k in range(window_size[2]): - l: int = i * window_size[2] * window_size[1] + j * window_size[2] + k - kernel[l, 0, i, j, k] += 1.0 - return kernel + prod=torch.prod(torch.tensor(window_size)) + return torch.diag(torch.ones(prod)).reshape(prod,1,*window_size) def _compute_zero_padding(kernel_size: Sequence[int]) -> List[int]: @@ -481,14 +475,13 @@ def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: t # prepare kernel if kernel is None: - kernel: torch.Tensor = get_binary_kernel3d(kernel_size).to(in_tensor) + kernel: torch.Tensor = get_binary_kernel(kernel_size).to(in_tensor) else: kernel: torch.Tensor = kernel.to(in_tensor) - c, h, w, d = in_tensor.shape - + c, *sshape = in_tensor.shape # map the local window to single vector - features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, h, w, d), kernel, padding=padding, stride=1) - features = features.view(c, -1, h, w, d) # Cx(K_h * K_w * K_d)xHxWxD + features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding=padding, stride=1) + features = features.view(c, -1, *sshape) # Cx(K_h * K_w * K_d)xHxWxD # compute the median along the feature axis median: torch.Tensor = torch.median(features, dim=1)[0] @@ -524,7 +517,7 @@ def __init__(self, radius: Union[Sequence[int], int], device="cpu") -> None: self.window: Sequence[int] = [1 + 2 * deepcopy(r) for r in radius] else: self.window: Sequence[int] = [1 + 2 * deepcopy(radius) for _ in range(3)] - self.kernel = get_binary_kernel3d(self.window).to(device) + self.kernel = get_binary_kernel(self.window).to(device) def forward(self, in_tensor: torch.Tensor, number_of_passes=1) -> torch.Tensor: """ From 7b04b02d3ccb4d4468a6652a2871111bb30e93a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Tue, 11 Oct 2022 11:31:15 -0400 Subject: [PATCH 05/10] Rely on same padding type instead of computing it --- monai/networks/layers/simplelayers.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index 2aca7a60bb7..b40f26bcc3e 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -443,12 +443,6 @@ def get_binary_kernel(window_size: Sequence[int]) -> torch.Tensor: return torch.diag(torch.ones(prod)).reshape(prod,1,*window_size) -def _compute_zero_padding(kernel_size: Sequence[int]) -> List[int]: - r"""Utility function that computes zero padding tuple.""" - computed: List[int] = [(k - 1) // 2 for k in kernel_size] - return computed - - def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: torch.Tensor = None) -> torch.Tensor: r"""Apply median filter to an image. Args: @@ -471,8 +465,6 @@ def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: t if not len(in_tensor.shape) == 4: raise ValueError(f"Invalid in_tensor shape, we expect CxHxWxD. Got: {in_tensor.shape}") - padding: Sequence[int] = _compute_zero_padding(kernel_size) - # prepare kernel if kernel is None: kernel: torch.Tensor = get_binary_kernel(kernel_size).to(in_tensor) @@ -480,7 +472,7 @@ def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: t kernel: torch.Tensor = kernel.to(in_tensor) c, *sshape = in_tensor.shape # map the local window to single vector - features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding=padding, stride=1) + features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding='same', stride=1) features = features.view(c, -1, *sshape) # Cx(K_h * K_w * K_d)xHxWxD # compute the median along the feature axis From 613f5563d1a13975e33ec5b98cabf9c43f33e9d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Tue, 11 Oct 2022 11:54:21 -0400 Subject: [PATCH 06/10] WIP: 2D support ERROR: test_value_15 (__main__.TestMedianSmoothd) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/local/lib/python3.10/dist-packages/parameterized/parameterized.py", line 533, in standalone_func return func(*(a + p.args), **p.kwargs) File "/home/dzenan/MONAI/tests/test_median_smoothd.py", line 72, in test_value result = MedianSmoothd(**argments)(image) File "/home/dzenan/MONAI/monai/transforms/intensity/dictionary.py", line 1020, in __call__ d[key] = self.converter(d[key]) File "/home/dzenan/MONAI/monai/transforms/intensity/array.py", line 1165, in __call__ out_t: torch.Tensor = median_filter_instance(img_t.unsqueeze(0)).squeeze(0) File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1130, in _call_impl return forward_call(*input, **kwargs) File "/home/dzenan/MONAI/monai/networks/layers/simplelayers.py", line 519, in forward x = median_filter(x, self.window, self.kernel) File "/home/dzenan/MONAI/monai/networks/layers/simplelayers.py", line 475, in median_filter features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding="same", stride=1) RuntimeError: Expected 4-dimensional input for 4-dimensional weight[3, 1, 1, 3], but got 5-dimensional input of size [1, 1, 1, 3, 3] instead ---------------------------------------------------------------------- Ran 20 tests in 0.828s --- monai/networks/layers/simplelayers.py | 15 ++++++--------- tests/test_median_smoothd.py | 8 ++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index b40f26bcc3e..946cdea33da 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -439,8 +439,8 @@ def get_binary_kernel(window_size: Sequence[int]) -> torch.Tensor: r"""Create a binary kernel to extract the patches. The window size HxWxD will create a (H*W*D)xHxWxD kernel. """ - prod=torch.prod(torch.tensor(window_size)) - return torch.diag(torch.ones(prod)).reshape(prod,1,*window_size) + prod = torch.prod(torch.tensor(window_size)) + return torch.diag(torch.ones(prod)).reshape(prod, 1, *window_size) def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: torch.Tensor = None) -> torch.Tensor: @@ -462,8 +462,8 @@ def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: t original_shape = in_tensor.shape if len(original_shape) == 5 and original_shape[0] == 1: in_tensor = in_tensor.squeeze(0) - if not len(in_tensor.shape) == 4: - raise ValueError(f"Invalid in_tensor shape, we expect CxHxWxD. Got: {in_tensor.shape}") + if not len(in_tensor.shape) != len(kernel_size): + raise ValueError(f"in_tensor has dimension {len(in_tensor.shape)}, kernel dimension is {len(kernel_size)}") # prepare kernel if kernel is None: @@ -472,7 +472,7 @@ def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: t kernel: torch.Tensor = kernel.to(in_tensor) c, *sshape = in_tensor.shape # map the local window to single vector - features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding='same', stride=1) + features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding="same", stride=1) features = features.view(c, -1, *sshape) # Cx(K_h * K_w * K_d)xHxWxD # compute the median along the feature axis @@ -503,10 +503,7 @@ def __init__(self, radius: Union[Sequence[int], int], device="cpu") -> None: super().__init__() self.radius: Sequence[int] = radius if issequenceiterable(radius): - if len(radius) != 3: - raise ValueError(f"Only 3 dimensional images are supported by {str(self.__class__)}") - else: - self.window: Sequence[int] = [1 + 2 * deepcopy(r) for r in radius] + self.window: Sequence[int] = [1 + 2 * deepcopy(r) for r in radius] else: self.window: Sequence[int] = [1 + 2 * deepcopy(radius) for _ in range(3)] self.kernel = get_binary_kernel(self.window).to(device) diff --git a/tests/test_median_smoothd.py b/tests/test_median_smoothd.py index 338ef160700..b30ebe99b62 100644 --- a/tests/test_median_smoothd.py +++ b/tests/test_median_smoothd.py @@ -19,6 +19,14 @@ TESTS = [] for p in TEST_NDARRAYS: + TESTS.append( + [ + {"keys": "img", "radius": [0, 1]}, + {"img": p(np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))}, + np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]), + ] + ) + TESTS.append( [ {"keys": "img", "radius": 1}, From 27aad65a2ca92501acaaa0fc2190d2bbe9ef4d21 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 11 Oct 2022 21:35:06 +0100 Subject: [PATCH 07/10] wip filtering the last n dimensions Signed-off-by: Wenqi Li --- docs/source/transforms.rst | 4 +- monai/networks/layers/simplelayers.py | 101 ++++++++++++++++---------- monai/transforms/intensity/array.py | 14 ++-- tests/test_median_smooth.py | 2 +- tests/test_median_smoothd.py | 30 ++++---- 5 files changed, 89 insertions(+), 62 deletions(-) diff --git a/docs/source/transforms.rst b/docs/source/transforms.rst index d12ad8197a8..874f01a9459 100644 --- a/docs/source/transforms.rst +++ b/docs/source/transforms.rst @@ -337,7 +337,7 @@ Intensity :special-members: __call__ `MedianSmooth` -"""""""""""""""" +"""""""""""""" .. image:: https://github.com/Project-MONAI/DocImages/raw/main/transforms/MedianSmooth.png :alt: example of MedianSmooth .. autoclass:: MedianSmooth @@ -1424,7 +1424,7 @@ Intensity (Dict) :special-members: __call__ `MedianSmoothd` -""""""""""""""""" +""""""""""""""" .. image:: https://github.com/Project-MONAI/DocImages/raw/main/transforms/MedianSmoothd.png :alt: example of MedianSmoothd .. autoclass:: MedianSmoothd diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index 946cdea33da..0cb5b1936eb 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -11,7 +11,7 @@ import math from copy import deepcopy -from typing import List, Sequence, Union +from typing import List, Optional, Sequence, Union import torch import torch.nn.functional as F @@ -20,8 +20,16 @@ from monai.networks.layers.convutils import gaussian_1d from monai.networks.layers.factories import Conv -from monai.utils import ChannelMatching, SkipMode, look_up_option, optional_import, pytorch_after -from monai.utils.misc import issequenceiterable +from monai.utils import ( + ChannelMatching, + SkipMode, + convert_to_tensor, + ensure_tuple_rep, + issequenceiterable, + look_up_option, + optional_import, + pytorch_after, +) _C, _ = optional_import("monai._C") fft, _ = optional_import("torch.fft") @@ -435,45 +443,61 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: return torch.as_tensor(ht, device=ht.device, dtype=ht.dtype) -def get_binary_kernel(window_size: Sequence[int]) -> torch.Tensor: - r"""Create a binary kernel to extract the patches. +def get_binary_kernel(window_size: Sequence[int], dtype=torch.float, device=None) -> torch.Tensor: + """ + Create a binary kernel to extract the patches. The window size HxWxD will create a (H*W*D)xHxWxD kernel. """ - prod = torch.prod(torch.tensor(window_size)) - return torch.diag(torch.ones(prod)).reshape(prod, 1, *window_size) + win_size = convert_to_tensor(window_size, int, wrap_sequence=True) + prod = torch.prod(win_size) + s = [prod, 1, *win_size] + return torch.diag(torch.ones(prod, dtype=dtype, device=device)).view(s) # type: ignore -def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: torch.Tensor = None) -> torch.Tensor: - r"""Apply median filter to an image. +def median_filter( + in_tensor: torch.Tensor, + kernel_size: Sequence[int] = (3, 3, 3), + spatial_dims: int = 3, + kernel: Optional[torch.Tensor] = None, +) -> torch.Tensor: + """ + Apply median filter to an image. + Args: - in_tensor: the input image with shape :math:`(C,H,W,D)`. + in_tensor: input tensor; median filtering will be applied to the last `spatial_dims` dimensions. kernel_size: the convolution kernel size. + spatial_dims: number of spatial dimensions to apply median filtering. + kernel: an optional customized kernel. + Returns: - the filtered input tensor with shape :math:`(C,H,W,D)`. - Example: + the filtered input tensor, shape remains the same as ``in_tensor`` + + Example:: + + >>> from monai.networks.layers import median_filter + >>> import torch >>> x = torch.rand(4, 5, 7, 6) >>> output = median_filter(x, (3, 3, 3)) >>> output.shape torch.Size([4, 5, 7, 6]) + """ if not isinstance(in_tensor, torch.Tensor): raise TypeError(f"Input type is not a torch.Tensor. Got {type(in_tensor)}") original_shape = in_tensor.shape - if len(original_shape) == 5 and original_shape[0] == 1: - in_tensor = in_tensor.squeeze(0) - if not len(in_tensor.shape) != len(kernel_size): - raise ValueError(f"in_tensor has dimension {len(in_tensor.shape)}, kernel dimension is {len(kernel_size)}") - + oshape, sshape = original_shape[: len(original_shape) - spatial_dims], original_shape[-spatial_dims:] + oprod = torch.prod(convert_to_tensor(oshape, int, wrap_sequence=True)) # prepare kernel if kernel is None: - kernel: torch.Tensor = get_binary_kernel(kernel_size).to(in_tensor) + kernel_size = ensure_tuple_rep(kernel_size, spatial_dims) + kernel = get_binary_kernel(kernel_size, in_tensor.dtype, in_tensor.device) else: - kernel: torch.Tensor = kernel.to(in_tensor) - c, *sshape = in_tensor.shape + kernel = kernel.to(in_tensor) # map the local window to single vector - features: torch.Tensor = F.conv3d(in_tensor.reshape(c, 1, *sshape), kernel, padding="same", stride=1) - features = features.view(c, -1, *sshape) # Cx(K_h * K_w * K_d)xHxWxD + conv = [F.conv1d, F.conv2d, F.conv3d][spatial_dims - 1] # type: ignore + features: torch.Tensor = conv(in_tensor.reshape(oprod, 1, *sshape), kernel, padding="same", stride=1) # type: ignore + features = features.view(oprod, -1, *sshape) # type: ignore # compute the median along the feature axis median: torch.Tensor = torch.median(features, dim=1)[0] @@ -483,40 +507,43 @@ def median_filter(in_tensor: torch.Tensor, kernel_size: Sequence[int], kernel: t class MedianFilter(nn.Module): - r"""Apply median filter to an image. + """ + Apply median filter to an image. + Args: - radius: the blurring kernel radius (radius of 1 corresponds to 3x3x3 kernel). + radius: the blurring kernel radius (radius of 1 corresponds to 3x3x3 kernel when spatial_dims=3). + Returns: filtered input tensor. - Shape: - - Input: :math:`(C, H, W, D)` - - Output: :math:`(C, H, W, D)` - Example: + + Example:: + + >>> from monai.networks.layers import MedianFilter + >>> import torch >>> in_tensor = torch.rand(4, 5, 7, 6) >>> blur = MedianFilter([1, 1, 1]) # 3x3x3 kernel >>> output = blur(in_tensor) >>> output.shape torch.Size([4, 5, 7, 6]) + """ - def __init__(self, radius: Union[Sequence[int], int], device="cpu") -> None: + def __init__(self, radius: Union[Sequence[int], int], spatial_dims: int = 3, device="cpu") -> None: super().__init__() - self.radius: Sequence[int] = radius - if issequenceiterable(radius): - self.window: Sequence[int] = [1 + 2 * deepcopy(r) for r in radius] - else: - self.window: Sequence[int] = [1 + 2 * deepcopy(radius) for _ in range(3)] - self.kernel = get_binary_kernel(self.window).to(device) + self.spatial_dims = spatial_dims + self.radius: Sequence[int] = ensure_tuple_rep(radius, spatial_dims) + self.window: Sequence[int] = [1 + 2 * deepcopy(r) for r in self.radius] + self.kernel = get_binary_kernel(self.window, device=device) def forward(self, in_tensor: torch.Tensor, number_of_passes=1) -> torch.Tensor: """ Args: - in_tensor: in shape [channels, H, W, D]. + in_tensor: input tensor, median filtering will be applied to the last `spatial_dims` dimensions. number_of_passes: median filtering will be repeated this many times """ x = in_tensor for _ in range(number_of_passes): - x = median_filter(x, self.window, self.kernel) + x = median_filter(x, kernel=self.kernel, spatial_dims=self.spatial_dims) return x diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 80562c08d86..8b6fa40bbe0 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -1142,6 +1142,8 @@ class MedianSmooth(Transform): Apply median filter to the input data based on specified `radius` parameter. A default value `radius=1` is provided for reference. + See also: :py:func:`monai.networks.layers.median_filter` + Args: radius: if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, @@ -1156,15 +1158,11 @@ def __init__(self, radius: Union[Sequence[int], int] = 1) -> None: def __call__(self, img: NdarrayTensor) -> NdarrayTensor: img = convert_to_tensor(img, track_meta=get_track_meta()) img_t, *_ = convert_data_type(img, torch.Tensor, dtype=torch.float) - radius: Union[Sequence[torch.Tensor], torch.Tensor] - if isinstance(self.radius, Sequence): - radius = [torch.as_tensor(s, device=img_t.device) for s in self.radius] - else: - radius = torch.as_tensor(self.radius, device=img_t.device) - median_filter_instance = MedianFilter(radius) # dim=img_t.ndim - 1 - out_t: torch.Tensor = median_filter_instance(img_t.unsqueeze(0)).squeeze(0) + spatial_dims = img_t.ndim - 1 + r = ensure_tuple_rep(self.radius, spatial_dims) + median_filter_instance = MedianFilter(r, spatial_dims=img_t.ndim - 1) + out_t: torch.Tensor = median_filter_instance(img_t) out, *_ = convert_to_dst_type(out_t, dst=img, dtype=out_t.dtype) - return out diff --git a/tests/test_median_smooth.py b/tests/test_median_smooth.py index e270fbeca95..57802f821d0 100644 --- a/tests/test_median_smooth.py +++ b/tests/test_median_smooth.py @@ -23,7 +23,7 @@ [ {"radius": 1}, p([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]), - p([[[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]]]), + p([[[0.0, 1.0, 0.0], [1.0, 2, 1.0], [0.0, 2.0, 0.0]], [[0.0, 4.0, 0.0], [4.0, 5, 4.0], [0.0, 5.0, 0.0]]]), ] ) diff --git a/tests/test_median_smoothd.py b/tests/test_median_smoothd.py index b30ebe99b62..5eb41c05bd2 100644 --- a/tests/test_median_smoothd.py +++ b/tests/test_median_smoothd.py @@ -18,12 +18,12 @@ from tests.utils import TEST_NDARRAYS, assert_allclose TESTS = [] -for p in TEST_NDARRAYS: +for p in TEST_NDARRAYS[0:1]: TESTS.append( [ {"keys": "img", "radius": [0, 1]}, - {"img": p(np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))}, - np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]), + {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]]]))}, + np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]]]), ] ) @@ -32,44 +32,46 @@ {"keys": "img", "radius": 1}, {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, np.array( - [[[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]]] + [[[0.0, 1.0, 0.0], [1.0, 2, 1.0], [0.0, 2.0, 0.0]], [[0.0, 4.0, 0.0], [4.0, 5, 4.0], [0.0, 5.0, 0.0]]] ), ] ) TESTS.append( [ - {"keys": "img", "radius": [1, 1, 1]}, + {"keys": "img", "radius": [1, 1]}, {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, np.array( - [[[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0.0, 0.0]]] + [[[0.0, 1.0, 0.0], [1.0, 2, 1.0], [0.0, 2.0, 0.0]], [[0.0, 4.0, 0.0], [4.0, 5, 4.0], [0.0, 5.0, 0.0]]] ), ] ) TESTS.append( [ - {"keys": "img", "radius": [1, 1, 2]}, - {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, + {"keys": "img", "radius": [1, 1]}, + {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 0, 5], [6, 6, 6]]]))}, np.array( - [[[0.0, 0.0, 0.0], [0.0, 0, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 0, 0.0], [0.0, 0.0, 0.0]]] + [[[0.0, 1.0, 0.0], [1.0, 2, 1.0], [0.0, 2.0, 0.0]], [[0.0, 4.0, 0.0], [4.0, 5, 4.0], [0.0, 5.0, 0.0]]] ), ] ) TESTS.append( [ - {"keys": "img", "radius": [1, 0, 1]}, - {"img": p(np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]))}, - np.array([[[0.0, 1, 0.0], [0.0, 2, 0.0], [0.0, 3, 0.0]], [[0.0, 1, 0.0], [0.0, 2, 0.0], [0.0, 3, 0.0]]]), + {"keys": "img", "radius": [1, 1, 1]}, + {"img": p(np.array([[[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]]))}, + np.array( + [[[[0.0, 0.0, 0.0], [0.0, 2, 0.0], [0.0, 0, 0.0]], [[0.0, 0, 0.0], [0.0, 2, 0.0], [0.0, 0, 0.0]]]] + ), ] ) class TestMedianSmoothd(unittest.TestCase): @parameterized.expand(TESTS) - def test_value(self, argments, image, expected_data): - result = MedianSmoothd(**argments)(image) + def test_value(self, arguments, image, expected_data): + result = MedianSmoothd(**arguments)(image) assert_allclose(result["img"], expected_data, rtol=1e-4, type_test="tensor") From 3b3136caba6718b4ee992e46dbe3b6211db9c2a1 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 11 Oct 2022 22:04:07 +0100 Subject: [PATCH 08/10] update docs Signed-off-by: Wenqi Li --- docs/source/networks.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/source/networks.rst b/docs/source/networks.rst index cc0dfb0be56..a4c225de292 100644 --- a/docs/source/networks.rst +++ b/docs/source/networks.rst @@ -334,6 +334,16 @@ Layers .. autoclass:: GaussianFilter :members: +`MedianFilter` +~~~~~~~~~~~~~~ +.. autoclass:: MedianFilter + :members: + +`median_filter` +~~~~~~~~~~~~~~~ +.. autoclass:: median_filter + :members: + `BilateralFilter` ~~~~~~~~~~~~~~~~~ .. autoclass:: BilateralFilter From e1ebaa1c4e09754e99411322256ef346dabf6d6f Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 11 Oct 2022 22:39:21 +0100 Subject: [PATCH 09/10] fixes tests Signed-off-by: Wenqi Li --- monai/networks/layers/simplelayers.py | 13 ++++++++++++- monai/transforms/utils_create_transform_ims.py | 12 ++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index 0cb5b1936eb..2064c3d386c 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -459,6 +459,7 @@ def median_filter( kernel_size: Sequence[int] = (3, 3, 3), spatial_dims: int = 3, kernel: Optional[torch.Tensor] = None, + **kwargs, ) -> torch.Tensor: """ Apply median filter to an image. @@ -468,6 +469,7 @@ def median_filter( kernel_size: the convolution kernel size. spatial_dims: number of spatial dimensions to apply median filtering. kernel: an optional customized kernel. + kwargs: additional parameters to the `conv`. Returns: the filtered input tensor, shape remains the same as ``in_tensor`` @@ -496,7 +498,16 @@ def median_filter( kernel = kernel.to(in_tensor) # map the local window to single vector conv = [F.conv1d, F.conv2d, F.conv3d][spatial_dims - 1] # type: ignore - features: torch.Tensor = conv(in_tensor.reshape(oprod, 1, *sshape), kernel, padding="same", stride=1) # type: ignore + if "padding" not in kwargs: + if pytorch_after(1, 10): + kwargs["padding"] = "same" + else: + # even-sized kernels are not supported + kwargs["padding"] = [(k - 1) // 2 for k in kernel.shape[2:]] + elif kwargs["padding"] == "same" and not pytorch_after(1, 10): + # even-sized kernels are not supported + kwargs["padding"] = [(k - 1) // 2 for k in kernel.shape[2:]] + features: torch.Tensor = conv(in_tensor.reshape(oprod, 1, *sshape), kernel, stride=1, **kwargs) # type: ignore features = features.view(oprod, -1, *sshape) # type: ignore # compute the median along the feature axis diff --git a/monai/transforms/utils_create_transform_ims.py b/monai/transforms/utils_create_transform_ims.py index 68ecd25128c..448d69ccba9 100644 --- a/monai/transforms/utils_create_transform_ims.py +++ b/monai/transforms/utils_create_transform_ims.py @@ -92,6 +92,7 @@ HistogramNormalize, KSpaceSpikeNoise, MaskIntensity, + MedianSmooth, NormalizeIntensity, RandAdjustContrast, RandBiasField, @@ -123,6 +124,7 @@ HistogramNormalized, KSpaceSpikeNoised, MaskIntensityd, + MedianSmoothD, NormalizeIntensityd, RandAdjustContrastd, RandBiasFieldd, @@ -472,7 +474,7 @@ def create_transform_im( create_transform_im(RandFlipd, dict(keys=keys, prob=1, spatial_axis=2), data) create_transform_im(Flip, dict(spatial_axis=1), data) create_transform_im(Flipd, dict(keys=keys, spatial_axis=2), data) - create_transform_im(Orientation, dict(axcodes="RPI", image_only=True), data) + create_transform_im(Orientation, dict(axcodes="RPI"), data) create_transform_im(Orientationd, dict(keys=keys, axcodes="RPI"), data) create_transform_im( Rand3DElastic, dict(prob=1.0, sigma_range=(1, 2), magnitude_range=(0.5, 0.5), shear_range=(1, 1, 1)), data @@ -545,11 +547,7 @@ def create_transform_im( create_transform_im(KSpaceSpikeNoise, dict(loc=(100, 100, 100), k_intensity=13), data) create_transform_im(KSpaceSpikeNoised, dict(keys=CommonKeys.IMAGE, loc=(100, 100, 100), k_intensity=13), data) create_transform_im(RandKSpaceSpikeNoise, dict(prob=1, intensity_range=(10, 13)), data) - create_transform_im( - RandKSpaceSpikeNoised, - dict(keys=CommonKeys.IMAGE, global_prob=1, prob=1, common_sampling=True, intensity_range=(13, 15)), - data, - ) + create_transform_im(RandKSpaceSpikeNoised, dict(keys=CommonKeys.IMAGE, prob=1, intensity_range=(13, 15)), data) create_transform_im(RandRicianNoise, dict(prob=1.0, mean=1, std=0.5), data) create_transform_im(RandRicianNoised, dict(keys=CommonKeys.IMAGE, prob=1.0, mean=1, std=0.5), data) create_transform_im(SavitzkyGolaySmooth, dict(window_length=5, order=1), data) @@ -601,6 +599,8 @@ def create_transform_im( create_transform_im(ForegroundMaskd, dict(keys=CommonKeys.IMAGE, invert=True), data) create_transform_im(GaussianSmooth, dict(sigma=2), data) create_transform_im(GaussianSmoothd, dict(keys=CommonKeys.IMAGE, sigma=2), data) + create_transform_im(MedianSmooth, dict(radius=3), data) + create_transform_im(MedianSmoothD, dict(keys=keys, radius=5), data) create_transform_im(RandGaussianSmooth, dict(prob=1.0, sigma_x=(1, 2)), data) create_transform_im(RandGaussianSmoothd, dict(keys=CommonKeys.IMAGE, prob=1.0, sigma_x=(1, 2)), data) create_transform_im(GaussianSharpen, dict(), GaussianSmoothd(CommonKeys.IMAGE, 2)(data)) From 925f69a38bba6667447ef98d0ec9aa34edaf8d77 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 11 Oct 2022 23:00:33 +0100 Subject: [PATCH 10/10] make demo images Signed-off-by: Wenqi Li --- monai/transforms/utils_create_transform_ims.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/transforms/utils_create_transform_ims.py b/monai/transforms/utils_create_transform_ims.py index 448d69ccba9..c0d45d55627 100644 --- a/monai/transforms/utils_create_transform_ims.py +++ b/monai/transforms/utils_create_transform_ims.py @@ -600,7 +600,7 @@ def create_transform_im( create_transform_im(GaussianSmooth, dict(sigma=2), data) create_transform_im(GaussianSmoothd, dict(keys=CommonKeys.IMAGE, sigma=2), data) create_transform_im(MedianSmooth, dict(radius=3), data) - create_transform_im(MedianSmoothD, dict(keys=keys, radius=5), data) + create_transform_im(MedianSmoothD, dict(keys=keys, radius=1), data) create_transform_im(RandGaussianSmooth, dict(prob=1.0, sigma_x=(1, 2)), data) create_transform_im(RandGaussianSmoothd, dict(keys=CommonKeys.IMAGE, prob=1.0, sigma_x=(1, 2)), data) create_transform_im(GaussianSharpen, dict(), GaussianSmoothd(CommonKeys.IMAGE, 2)(data)) @@ -684,7 +684,7 @@ def create_transform_im( ) create_transform_im(LabelToContour, dict(), data, is_post=True) create_transform_im(LabelToContourd, dict(keys=CommonKeys.LABEL), data, is_post=True) - create_transform_im(Spacing, dict(pixdim=(5, 5, 5), image_only=True), data) + create_transform_im(Spacing, dict(pixdim=(5, 5, 5)), data) create_transform_im(Spacingd, dict(keys=keys, pixdim=(5, 5, 5), mode=["bilinear", "nearest"]), data) create_transform_im(RandAxisFlip, dict(prob=1), data) create_transform_im(RandAxisFlipd, dict(keys=keys, prob=1), data)