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

find_contours function error - endless loop and RAM out of memory #14

Open
fgraffitti-cyberhawk opened this issue Jun 6, 2023 · 4 comments

Comments

@fgraffitti-cyberhawk
Copy link

fgraffitti-cyberhawk commented Jun 6, 2023

I am having some problems with the upolygon.find_contours function.

I have some input masks that make the function run endlessly, while my computer RAM starts increasing until it reaches 100% and python kernel crashes and/or I terminate the find_contours execution.

If I try to run the opencv cv2.findContours function I don't have this problem, however the hierarchical structure of the output is quite different from the one of upolygon.find_contours.
Unfortunately I am not able to map the output of cv2.findContours to a format similar to the upolygon.find_contours output, as the upolygon code has no docstring/comments explaining the output format.

Any idea of how either to fix the find_contours problem, or how to map cv2.findContours output to upolygon.find_contours output?

Here some details:
Windows 11
python 3.10.8
upolygon 0.1.10 (updated this morning to the latest release)

I link a json file containing a few examples of masks (in RLE format) that give the error : https://cyberhawk-my.sharepoint.com/:u:/g/personal/francesco_graffitti_thecyberhawk_com/EQLKLbPehtRJpoxPoGiGnHgB-u4WuqsD6fFj4zWyfRHxaA?e=rZclfs

And here is the plot of those same masks:
image

Code to reproduce the error:

import json
from upolygon import find_contours, rle_decode
from darwin.importer.formats.coco import decode_binary_rle

with open('data.json') as f:
  data = eval(f.read())

mask_index = 0 # select any of the masks from the data.json file-all of them lead to an error
segmentation = data[mask_index]
counts = decode_binary_rle(segmentation["counts"])
mask = rle_decode(counts, segmentation["size"][::-1])

# this is the upolygon function that runs endlessly
_labels, external, _internal = find_contours(mask)

# this is the opencv find contours function that works as expected on the same mask
import cv2
contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

What's weird, for example, is that the first top-left mask gives this error, but if I crop the image tighter around the annotation (leaving some padding around it, I am using as coordinates [2100:4100,1500:6600]) the find_contours function runs smoothly, even though what I cut out was just pixels having value=0.

@owencjones
Copy link
Contributor

Thanks for the thorough steps to recreate, we have a ticket in our backlog now to look into this, and you can speak to your CX reps for updates if you need them. The code you've submitted will be very helpful in recreating the issue.

Many thanks

Owen

@Nathanjp91
Copy link
Contributor

Just to note we are working on this issue and testing a fix here
#15

@fgraffitti-cyberhawk
Copy link
Author

@Nathanjp91 @owencjones
Hey guys, I have seen from the logs that you have found the problem in the function and fixed it. Do you have an ETA on when the changes will be merged into main?
Thanks for having worked on this!

@Nathanjp91
Copy link
Contributor

@fgraffitti-cyberhawk We have it in QA at the moment but because of the nature of the issue and the code involved was taking longer than expected. The fix is currently in a branch here of darwin-py. There's some issue getting it deployed because upolygon needs to be rebuilt with a new version for pypi to accept it in darwin-py, but you could install that branch of darwin-py and test if it resolves your issues.

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

3 participants