You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Low resolution labels are often too jagged.
Describe the solution you'd like
Originally I hoped that the Spacingd transform would just have some antialiasing.
But a separate transform for this might be a better solution anyway.
I implemented a simple transform using a gaussian filter combined with a threshold on every label.
@wyli I did see #178 but I did not recognize it as duplicate in that my suggestion does not do any downsampling, right?
Also, could you elaborate on the multiscale loss wrapper. I'm unfamiliar with that.
Any idea how this affects the model training?
Not yet. I guess we will find out 🙂
We will need to make sure to tune the parameters so that there is no significant information loss but smoother shapes actually seem more natural and learning smooth shapes compared to jagged ones, does seem like a good idea.
I'm positive that this will have a positive effect on the training.
I'm also planning on extending the implementation in that the labels can be provided as parameter.
That would allow to set different parameters for different label classes. This will allow to better fine tune the parameters for every label class.
Here the version with the option to define the labels the edge smoothing is applied to and an optional pre-dilation. The pre-dilation seems to help to preserve smaller shapes (just an experimental addition).
defisin_tensor(ar1: Tensor, ar2: Iterable[Any]):
""" This is a placeholder for torch.isin which will be released with torch 1.10.0. https://pytorch.org/docs/1.10.0/generated/torch.isin.html?highlight=isin https://github.com/pytorch/pytorch/issues/3025 """return (ar1[..., None] ==torch.tensor(list(ar2))).any(-1)
defdilation3d(binary_image: torch.Tensor) ->torch.Tensor:
kernel=torch.ones(size=[1, 1, 3, 3, 3], dtype=torch.float32)
returntorch.clamp(conv3d(binary_image, kernel, padding=(1, 1, 1)), 0, 1)
classAntialiasing(MapTransform):
def__init__(
self,
keys: KeysCollection,
applied_labels: Optional[Union[Iterable[int], int]] =None,
sigma: Union[Sequence[float], float] =1.0,
approx: str="erf",
threshold: float=0.5,
pre_dilation: bool=False,
allow_missing_keys: bool=False,
) ->None:
super().__init__(keys, allow_missing_keys)
self.applied_labels=ensure_tuple(applied_labels) ifapplied_labelselseNoneself.sigma=sigmaself.approx=approxself.threshold=thresholdself.pre_dilation=pre_dilationdef__call__(self, data: Mapping[Hashable, NdarrayTensor]) ->Dict[Hashable, NdarrayTensor]:
d=dict(data)
forkeyinself.key_iterator(d):
img: torch.Tensorimg, *_=convert_data_type(d[key], torch.Tensor) # type: ignoregaussian_filter=GaussianFilter(img.ndim-1, self.sigma, approx=self.approx)
# Get labels if not provided. Exclude background label.applied_labels=set(self.applied_labelsortorch.unique(img))
background_label=0applied_labels.discard(background_label)
# Ignore labels not in `applied_labels`new_img=img* (isin_tensor(img, applied_labels) ==0)
forlabelinapplied_labels:
label_mask= (img==label).to(torch.float32).unsqueeze(0)
ifself.pre_dilation:
label_mask=dilation3d(label_mask)
blurred=gaussian_filter(label_mask).squeeze(0)
new_img[blurred>self.threshold] =labeld[key] =new_img# type: ignorereturnd
@Spenhouet Hi Spenhouet, I am very interested in this smooth method. I also implementated a similar method [#3333 ].
However I found that in my task, I can not get good results using Gaussian filger (I tried different sigma values from 1 to 10). So I applied Mean filter and then got very good results. My threshold is still 0.4.
I do not know how you choose the correct sigma value in your method? Have you tried Mean filger? Because if you use Mean filter, you do not need to set sigma value. You just need to set the filter size which should be the zoom factor I think.
I can open a PR and contribute the mean filtering transform if you want, also supporting multiple kernels (but I might need some help with creating the kernels).
Hi @dzenanz I'm currently working on a more general FilterKernelTransform, which provides mean filtering, laplacian filter, elliptical filter and sobel filter. Maybe we can work together on this?
Maybe we can collaborate. I see you have a fork, but I was unable to find any progress there. But overhead of combining our efforts might be high, as I am starting from existing working code which I only need to plug into MONAI form with accompanying tests, docs etc.
Activity
wyli commentedon Oct 23, 2021
Duplicate of #178? Any idea how this affects the model training?
wyli commentedon Oct 23, 2021
There is also a multiscale loss wrapper https://github.com/Project-MONAI/MONAI/blob/dev/monai/losses/multi_scale.py
Spenhouet commentedon Oct 23, 2021
@wyli I did see #178 but I did not recognize it as duplicate in that my suggestion does not do any downsampling, right?
Also, could you elaborate on the multiscale loss wrapper. I'm unfamiliar with that.
Not yet. I guess we will find out 🙂
We will need to make sure to tune the parameters so that there is no significant information loss but smoother shapes actually seem more natural and learning smooth shapes compared to jagged ones, does seem like a good idea.
I'm positive that this will have a positive effect on the training.
I'm also planning on extending the implementation in that the labels can be provided as parameter.
That would allow to set different parameters for different label classes. This will allow to better fine tune the parameters for every label class.
Spenhouet commentedon Oct 24, 2021
Here the version with the option to define the labels the edge smoothing is applied to and an optional pre-dilation. The pre-dilation seems to help to preserve smaller shapes (just an experimental addition).
Jingnan-Jia commentedon Nov 19, 2021
@Spenhouet Hi Spenhouet, I am very interested in this smooth method. I also implementated a similar method [#3333 ].
However I found that in my task, I can not get good results using Gaussian filger (I tried different sigma values from 1 to 10). So I applied Mean filter and then got very good results. My threshold is still 0.4.
I do not know how you choose the correct sigma value in your method? Have you tried Mean filger? Because if you use Mean filter, you do not need to set sigma value. You just need to set the filter size which should be the zoom factor I think.
Spenhouet commentedon Nov 19, 2021
@Jingnan-Jia I just finetuned the parameters for our data (trial and error).
What is the issue of "not good results"?
Could you share your transform code?
jlvahldiek commentedon Sep 30, 2022
Another option could be median smoothing. We achieved very good and fast results with the below transform.
cc @kbressem
wyli commentedon Sep 30, 2022
that looks like mean filtering instead of median,
perhaps you can use https://github.com/Project-MONAI/MONAI/blob/dev/monai/transforms/post/array.py(I reread, and realised no good transform currently in monai for it)...and a few transforms should be unified to have a generic filtering API:
MONAI/monai/transforms/post/array.py
Line 517 in ef68f16
MONAI/monai/transforms/post/array.py
Line 812 in ef68f16
MONAI/monai/transforms/intensity/array.py
Line 1139 in ef68f16
cc @Nic-Ma
kbressem commentedon Sep 30, 2022
I can open a PR and contribute the mean filtering transform if you want, also supporting multiple kernels (but I might need some help with creating the kernels).
wyli commentedon Sep 30, 2022
thanks @kbressem, please go ahead, I haven't looked into the details, but in my opinion some MONAI transform to achieve similar functions like https://github.com/kornia/kornia/blob/master/kornia/filters/filter.py would be very useful, using the utilities:
MONAI/monai/networks/layers/simplelayers.py
Lines 38 to 39 in 607123b
dzenanz commentedon Oct 3, 2022
Is there interest in adding a median filter based on @ebrahimebrahim's medianblur_with_gpu? If so, I would be interested in contributing it with a PR.
dzenanz commentedon Oct 4, 2022
I created #5264, as a related but separate feature request.
kbressem commentedon Oct 4, 2022
Hi @dzenanz I'm currently working on a more general FilterKernelTransform, which provides mean filtering, laplacian filter, elliptical filter and sobel filter. Maybe we can work together on this?
dzenanz commentedon Oct 4, 2022
Maybe we can collaborate. I see you have a fork, but I was unable to find any progress there. But overhead of combining our efforts might be high, as I am starting from existing working code which I only need to plug into MONAI form with accompanying tests, docs etc.
Add generic kernel transform with support for multiple kernels (#5317)