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

Race condition in ErodeObjectMorphologyImageFilter #2095

Closed
makarovNick opened this issue Apr 15, 2024 · 2 comments
Closed

Race condition in ErodeObjectMorphologyImageFilter #2095

makarovNick opened this issue Apr 15, 2024 · 2 comments

Comments

@makarovNick
Copy link

GlobalDefaultNumberOfThreads affects result
Applying ErodeObjectMorphologyImageFilter on binary image produces different result with
GlobalDefaultNumberOfThreads > 1

To Reproduce
Steps to reproduce the behavior:

  1. Operating system, version, and architecture
  • OS: [Ubuntu 18.4]
    Architecture: x86_64
    CPU(s): 16
  1. Programming language: Python 3.9.14]

  2. Version of SimpleITK:
    tested on 2.1.0, 2.2.0, 2.3.1

  3. How was SimpleITK installed?

  • binary distribution [e.g. python -m pip install SimpleITK]
  1. A minimal working example which causes the error.
    data - shape.zip
import SimpleITK as sitk
img = sitk.ReadImage("shape.nrrd")

sitk.ProcessObject.SetGlobalDefaultNumberOfThreads(1)
eroded_img = sitk.ErodeObjectMorphology(img)
result_volume = sitk.GetArrayFromImage(eroded_img).sum()
print("Expecting volume: ", result_volume)

sitk.ProcessObject.SetGlobalDefaultNumberOfThreads(16)
for _ in range(10):
    eroded_img = sitk.ErodeObjectMorphology(img)
    volume = sitk.GetArrayFromImage(eroded_img).sum()
    if result_volume != volume:
        print("Wrong result! Got", volume)

Got:

Expecting volume:  6039
Wrong result! Got 6213
Wrong result! Got 6109
Wrong result! Got 6046
Wrong result! Got 6119
Wrong result! Got 6212
Wrong result! Got 6042
Wrong result! Got 6183
Wrong result! Got 6212
Wrong result! Got 6044

Expected behavior
Expecting eroded image to be the same

Additional context
Also Is it supposed to work differently from sitk.BinaryErode ? Because sitk.BinaryErode gives different result for this image :)

@blowekamp
Copy link
Member

blowekamp commented Apr 15, 2024

I usually use the Binary Morphology filters or a Grayscale Morphology filters to perform binary morphology operations.

I was able to reproduce the race condition, and reviewed the code for the ObjectMorphology filters. It appears that there is a fundamental issue with the set of filters using the "output" image to both sample the kernel and write to it.

This is an ITK issue could you please repost the issue onto InsightSoftwareConsortium/ITK?

@blowekamp
Copy link
Member

Thank you for reporting the issue.

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

No branches or pull requests

2 participants