SA-1B, the dataset released with the Segment Anything Model (SAM), was produced by prompting SAM with a grid of points and filtering what came back. Each candidate mask has a logit map and the model's own predicted IoU. Implement the stability score and the filter chain.
stability_score(logits, offset=1.0)
Threshold the logit map at and at . Return the IoU of the two binary masks. Every pixel above is also above , so this is the number of pixels with logit divided by the number with logit . Return 0.0 if no pixel is above .
filter_masks(logits, pred_iou, iou_thresh=0.88, stability_thresh=0.95, max_area=0.95, nms_thresh=0.7)
logits is a list of arrays of shape and pred_iou a list of floats. The binary mask of a candidate is logit > 0. Check each candidate in this order and record the first test it fails:
"low_iou": its predicted IoU is below iou_thresh;"unstable": its stability score is below stability_thresh;"too_large": its binary mask covers a fraction of the image greater than or equal to max_area.The survivors go through greedy box non-maximum suppression. A mask's box is (x0, y0, x1, y1), the smallest and largest column and row of its binary mask, and it covers pixels. Visit the survivors from the highest predicted IoU to the lowest (ties: lower index first). Keep a mask unless the IoU of its box with the box of an already kept mask is greater than nms_thresh; otherwise record it as "duplicate".
Return a dict with "kept" (indices in the order they were kept), "boxes" (the kept masks' boxes as lists of ints, same order) and "dropped", a dict with the four keys above, each a list of indices in increasing order.
nms_thresh is not suppressed.