-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add generic kernel transform with support for multiple kernels #5317
Add generic kernel transform with support for multiple kernels #5317
Conversation
thanks, this looks good to me, could you please address the minor issues reported here? https://deepsource.io/gh/Project-MONAI/MONAI/run/76df1e85-7eb3-4b7e-8073-088d5f5ee6ee/python/ ? more specifically it might be useful to make the kernel functions classmethods: class KernelTransform:
...
@classmethod
def laplacian_kernel(cls, size, ndim) -> torch.Tensor:
...
return kernel then we could reuse the kernel elsewhere (without creating a KernelTransform instance):
|
Thanks @wyli. I will replace the assertions with proper errors and also convert all kernel functions to classmethods. One question though, I have not implemented the Gaussian kernel as it takes additional arguments ( Also, when adding the new transforms to |
thank you, for the first issue I think it'll be good to use also precompute the kernel func would save a few cycles during def __init__(self, kernel, spatial_dim, k_size, dtype, **kernel_args):
if isinstance(kernel, str):
func = self._create_kernel_from_string(name)
self.kernel = func(spatial_dim, k_size, **kernel_args)
self.dtype = dtype
def __call__(self, ...):
self.kernel.to(device=input.device, dtype=self.dtype or input.dtype) for the 2nd issue, would be great to have a second issue/PR... |
The reason this is not done, is that I do not know if the image will be 2D or 3D until the function is called (see line 1564 in the array.py, where I extract the However, I could precompute 2D and 3D kernels and just use the appropriate kernel at runtime?
Ok, then I will use kwargs and add the information to the main docstring as well as the |
sure, i think that's a nice idea |
I would suggest putting the kernel creation functions into a utilities file in transforms or networks as regular functions so that they are accessible to other if we wanted to use them for other purposes. |
Signed-off-by: kbressem <[email protected]>
- rename class to `ImageFilter` - `ImageFilter` now accepts strings (preset filters), `torch.Tensor` or `np.ndarray`, `nn.Module` and `monai.transforms.Transform` - The filter is created just once, to make `__call__` faster - Additional `nn.Modules` for simple filters have been created to make them easier available - add tests for `ImageFilter` and `MeanFilter` - Extend tests for ImageFilter - Maybe extend ImageFilter to accept multiple arguments? - Add Tests for other nn.Modules Signed-off-by: kbressem <[email protected]>
…com/kbressem/MONAI into 3178-generic-filterkernel-transform
for more information, see https://pre-commit.ci
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
for more information, see https://pre-commit.ci
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
… 1.8 or 1.9 Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
Signed-off-by: kbressem <[email protected]>
/build |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it looks good to me.
Signed-off-by: kbressem [email protected]
Fixes #3178
Description
As discussed in #3178, this PR adds a more general interface to apply simple convolutions to images. This PR (re-)implements 2D and 3D versions for mean filtering, edge detection, outline detection, mask dilatation and image sharpening.
Detailed description
The main transform is
ImageFilter
. It comes with some preset filters that can be initialized using a string. Available presets are["mean", "laplace", "elliptical", "sobel", "sharpen", "median", "gauss", "savitzky_golay"]
. For example, a transformation for mean filtering can be created with:ImageFilter
automatically detects whether the input is 2D or 3D and creates the appropriate filter on the first call. This filter will then be used in future calls of the transformation, so switching between 2D and 3D images is not supported.The initialization form string is only a convenience function. All preset filters are also available in
monai.networks.layers.simplelayers
asnn.Module
subclasses. ImageFiltercan also be created directly from an
nn.Module`. For example, to create a transformation for mean filtering, the following is also possible:In this example, however, the filter size is predefined and is not automatically derived from the input.
In addition, it is also possible to use custom filters with
ImageFilter
using atorch.Tensor
ornumpy.ndarray
. For example:RandImageFilter
is a randomizable variant ofImageFilter
that executes with probabilityp
on each call. BothImageFilter
andRandImageFilter
also have dictionary versions.Types of changes
./runtests.sh -f -u --net --coverage
../runtests.sh --quick --unittests --disttests
.make html
command in thedocs/
folder.