-
-
Notifications
You must be signed in to change notification settings - Fork 16.6k
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
update mask2segments and saving results #9785
update mask2segments and saving results #9785
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Hello @vladoossss, thank you for submitting a YOLOv5 π PR! To allow your work to be integrated as seamlessly as possible, we advise you to:
- β
Verify your PR is up-to-date with
ultralytics/yolov5
master
branch. If your PR is behind you can update your code by clicking the 'Update branch' button or by runninggit pull
andgit merge master
locally. - β Verify all YOLOv5 Continuous Integration (CI) checks are passing.
- β Reduce changes to the absolute minimum required for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." β Bruce Lee
@vladoossss I'd like to minimize the code changes here. Are you saying that some detections produce no masks? Is there a way I can reproduce the issue myself? I don't think it makes sense to add integers to a list of segment masks. Can you simply update masks2segments with an def masks2segments(masks, strategy='largest'):
# Convert masks(n,160,160) into segments(n,xy)
segments = []
for x in masks.int().cpu().numpy().astype('uint8'):
c = cv2.findContours(x, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
if c:
if strategy == 'concat': # concatenate all segments
c = np.concatenate([x.reshape(-1, 2) for x in c])
elif strategy == 'largest': # select largest segment
c = np.array(c[np.array([len(x) for x in c]).argmax()]).reshape(-1, 2)
segments.append(c.astype('float32'))
return segments |
@vladoossss ok took another look. I see we need to keep the output array the same length as the input masks, how about creating an empty nparray when there is no segment of the same 2 columns. Can you try this new function with no other changes anywhere (i.e. use master segments/predict.py etc.) def masks2segments(masks, strategy='largest'):
# Convert masks(n,160,160) into segments(n,xy)
segments = []
for x in masks.int().cpu().numpy().astype('uint8'):
c = cv2.findContours(x, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
if c:
if strategy == 'concat': # concatenate all segments
c = np.concatenate([x.reshape(-1, 2) for x in c])
elif strategy == 'largest': # select largest segment
c = np.array(c[np.array([len(x) for x in c]).argmax()]).reshape(-1, 2)
else:
c = np.zeros((0, 2)) # no segments found
segments.append(c.astype('float32'))
return segments |
Signed-off-by: Glenn Jocher <[email protected]>
Signed-off-by: Glenn Jocher <[email protected]>
Yes, modifying just this one function also works fine! |
@vladoossss great! PR is merged. Thank you for your contributions to YOLOv5 π and Vision AI β |
PR for this issue: #9784
π οΈ PR Summary
Made with β€οΈ by Ultralytics Actions
π Summary
Enhances segmentation strategies in YOLOv5 by ensuring robust handling of images with no detectable segments.
π Key Changes
π― Purpose & Impact