MONAI icon indicating copy to clipboard operation
MONAI copied to clipboard

Refactor morphological functions as transform (or transform utils)

Open mingxin-zheng opened this issue 1 year ago • 12 comments

Describe the bug

The original discussion was in PR comment: https://github.com/Project-MONAI/MONAI/pull/7893#issuecomment-2200581510

Will a refactor of the erodeand dilate functions make it easier in different applications/networks/pre-processing?

https://github.com/Project-MONAI/MONAI/blob/dev/monai/apps/generation/maisi/utils/morphological_ops.py

Expected behavior

class Erode3D(Transform):
   ...

# erode with a 3D filter when needed
input_img = torch.rand(b, l, h, d)
eroder = Erode3D(...)
output = eroder(input_img) 

mingxin-zheng avatar Aug 08 '24 07:08 mingxin-zheng

Since this operation is used by MAISI and VISTA3D both, do you think it will help? @heyufan1995 @Can-Zhao @guopengf ?

mingxin-zheng avatar Aug 08 '24 07:08 mingxin-zheng

Also cc @yiheng-wang-nv @ericspod

KumoLiu avatar Aug 08 '24 07:08 KumoLiu

We should look at what other requirements there are but I think it would make sense to have a morphological operations transform to encapsulate them.

ericspod avatar Aug 13 '24 12:08 ericspod

+1 for making them transforms.

Should MONAI use ITK's / SimpleITK's implementations? They offer a multitude of options: binary, grayscale, open, close, generalized - and all are multi-threaded, I believe some contain logic to specifically to only perform convolution only at an object's edges to significantly speed results compared to full convolution, and other developers have created GPU/CUDA accelerated versions but they aren't in ITK by default.

aylward avatar Aug 13 '24 12:08 aylward

We should look into what the implementation would look like, it's definitely possible to at least support using ITK implementations, though the virtue of what was added with MAISI was that it is only Pytorch. We could support using ITK if present but then default to Pytorch implementations if not.

ericspod avatar Aug 13 '24 13:08 ericspod

I meet the error ModuleNotFoundError: No module named 'monai.transforms.morphological_ops'

qic999 avatar Aug 13 '24 14:08 qic999

I meet the error ModuleNotFoundError: No module named 'monai.transforms.morphological_ops'

Hi @qic999, it should be monai.transforms.utils_morphological_ops.

KumoLiu avatar Aug 13 '24 14:08 KumoLiu

I meet the error ModuleNotFoundError: No module named 'monai.transforms.morphological_ops'

Hi @qic999, it should be monai.transforms.utils_morphological_ops.

OK. Thanks. This error occurs on line 24 of https://github.com/Project-MONAI/tutorials/tree/main/generation/maisi/scripts/utils.py.

qic999 avatar Aug 13 '24 14:08 qic999

OK. Thanks. This error occurs on line 24 of https://github.com/Project-MONAI/tutorials/tree/main/generation/maisi/scripts/utils.py.

Thanks, will fix it in the following prs.

KumoLiu avatar Aug 13 '24 14:08 KumoLiu

OK. Thanks. This error occurs on line 24 of https://github.com/Project-MONAI/tutorials/tree/main/generation/maisi/scripts/utils.py.

Thanks, will fix it in the following prs.

new error: ModuleNotFoundError: No module named 'monai.transforms.utils_morphological_ops' my env is image

qic999 avatar Aug 13 '24 15:08 qic999

Hi @aylward , the current implementations of erode/dilate are basically padding and conv2d/3d with torch

https://github.com/Project-MONAI/MONAI/blob/4877767cf92649a38ffda0fc590f2b92ba59f019/monai/transforms/utils_morphological_ops.py#L90

Currently the usage of erode/dilate needs to be on GPUs, and it will be more work to do if we deviate from the torch implementation.

In my opinion, the role of itk is more like another backend on top of numpy and torch. So my gut feeling of ITK integration is that a wrapper that can bridge torch and itk/simple-itk, but I could be wrong here.

mingxin-zheng avatar Aug 14 '24 03:08 mingxin-zheng

OK. Thanks. This error occurs on line 24 of https://github.com/Project-MONAI/tutorials/tree/main/generation/maisi/scripts/utils.py.

Thanks, will fix it in the following prs.

new error: ModuleNotFoundError: No module named 'monai.transforms.utils_morphological_ops' my env is image

Hi @qic999 , you'll need the dev version or the weekly build. The morphological operations are added recently and they are not available in monai 1.3.2, which is the version you are using. Check this page for installation guide:

https://docs.monai.io/en/stable/installation.html

If my comment above can't resolve your issue, please open a new issue so that the team can better support you. I would like to keep this thread simply focus the implementation plan. Thank you!

mingxin-zheng avatar Aug 14 '24 03:08 mingxin-zheng

Closing this ticket as there are no similar requests, and I am using the function calls for the morph operation right now in my work.

mingxin-zheng avatar Apr 04 '25 14:04 mingxin-zheng