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

Fix threshold in sam_predict #406

Open
wants to merge 2 commits into
base: Main
Choose a base branch
from
Open

Fix threshold in sam_predict #406

wants to merge 2 commits into from

Conversation

nidefawl
Copy link

@nidefawl nidefawl commented Dec 30, 2023

SAMDetectorSegmented can return incorrect masks in the batch_masks output.

This is because the threshold for the 3 masks returned by SamPredictor predict can differ.

def sam_predict(predictor, points, plabs, bbox, threshold):
point_coords = None if not points else np.array(points)
point_labels = None if not plabs else np.array(plabs)
box = np.array([bbox]) if bbox is not None else None
cur_masks, scores, _ = predictor.predict(point_coords=point_coords, point_labels=point_labels, box=box)
total_masks = []
selected = False
max_score = 0
for idx in range(len(scores)):
if scores[idx] > max_score:
max_score = scores[idx]
max_mask = cur_masks[idx]
if scores[idx] >= threshold:
selected = True
total_masks.append(cur_masks[idx])
else:
pass
if not selected:
total_masks.append(max_mask)
return total_masks

But make_sam_mask_segmented expects 3 masks per detection.

return (mask, merge_and_stack_masks(stacked_masks, group_size=3))

This commit fixes it by respecting the threshold value and returning all or none of the predicted masks.

Copy link
Owner

@ltdrdata ltdrdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check tensor shape of image.

It is b, h, w, c.

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

Successfully merging this pull request may close these issues.

2 participants