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

Heterogeneous resolution yields non-zero boundary. #45

Open
ramonemiliani93 opened this issue Aug 30, 2021 · 5 comments
Open

Heterogeneous resolution yields non-zero boundary. #45

ramonemiliani93 opened this issue Aug 30, 2021 · 5 comments

Comments

@ramonemiliani93
Copy link

ramonemiliani93 commented Aug 30, 2021

Hi, when the resolution for the distance maps is heterogeneous the formula in one_hot2dist will yield non-zero values in the boundaries. For example:

import numpy as np
from scipy.ndimage import distance_transform_edt as eedt

array = np.zeros((5, 5))
array[1:4, 1:4] = 1
resolution = (1, 0.5)

mask = array.astype(np.bool)
eedt(~mask, resolution) * ~mask - (eedt(mask, resolution) - 1) * mask

Here the boundaries will be 0.5 instead of 0. Maybe eroding one of the masks could help generalize:

import numpy as np
from scipy.ndimage import distance_transform_edt as eedt
from scipy.ndimage import binary_erosion

def signed_eedt(array: np.ndarray, resolution: Sequence[float]) -> np.ndarray:
    if not array.any():
        return np.zeros_like(array)

    mask = array.astype(np.bool)
    positive = eedt(~mask, resolution) * ~mask
    negative = eedt(binary_erosion(mask), resolution) * mask

    return positive - negative
@ramonemiliani93 ramonemiliani93 changed the title Non homogeneous resolution yields non-zero boundary. Heterogenous resolution yields non-zero boundary. Aug 30, 2021
@ramonemiliani93 ramonemiliani93 changed the title Heterogenous resolution yields non-zero boundary. Heterogeneous resolution yields non-zero boundary. Aug 30, 2021
@HKervadec
Copy link
Member

Hi Ramone,

Thanks for the interest, this is really a corner case. Are you allowed to share any of such examples, with real-world data?

Did you try to train in-spite with the boundary at 0.5? Though the distance would be slightly off, I would not necessarily affect the minimization process much.

I will try to keep thinking about this in the coming days. Your suggested erosion might be enough.

Best,

Hoel

@ramonemiliani93
Copy link
Author

Hi Hoel,

Unfortunately I have no data to share, any other synthetic example will have the same problem. Using skimage.morphology.ball and slicing it through the middle may yield a better toy problem.

I agree the optimization shouldn't be affected by this; with the current implementation I did not train for long so I am unable to tell you about the results, on the other hand, the binary erosion is working. I have to train for longer and compare with only regional loss.

If you come up with something keep me posted.

Best,

Ramón

@HKervadec
Copy link
Member

Hi Ramón,

Thank you for your useful feedback. I suppose I could (should) add some unit test for that. (I have some partial unit-testing file somewhere locally, that I haven't included in the repo.) That might help to decide how to deal with this corner case.

I have some deadlines coming up and I will not have time to handle that right now, so I will snooze this until late October.

Best,

Hoel

@ramonemiliani93
Copy link
Author

Hi Hoel,

Wish you the best with your deadlines! Yes, if at some point you add the unit test files, I could help you resolve the corner case.

Best,

Ramón

@HKervadec
Copy link
Member

I just added a basic test.py in 8f44574

Feel free to play around and add new test-cases, this would help to define the issue.

It seems that now the codebase triggers some deprecation warnings (I have updated my machine, so brand-new python, numpy and pytorch), I will investigate those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants