MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / calculate_stability_score

Function calculate_stability_score

utils/sam_utils/amg.py:156–176  ·  view source on GitHub ↗

Computes the stability score for a batch of masks. The stability score is the IoU between the binary masks obtained by thresholding the predicted mask logits at high and low values.

(
    masks: torch.Tensor, mask_threshold: float, threshold_offset: float
)

Source from the content-addressed store, hash-verified

154
155
156def calculate_stability_score(
157 masks: torch.Tensor, mask_threshold: float, threshold_offset: float
158) -> torch.Tensor:
159 """
160 Computes the stability score for a batch of masks. The stability
161 score is the IoU between the binary masks obtained by thresholding
162 the predicted mask logits at high and low values.
163 """
164 # One mask is always contained inside the other.
165 # Save memory by preventing unnecesary cast to torch.int64
166 intersections = (
167 (masks > (mask_threshold + threshold_offset))
168 .sum(-1, dtype=torch.int16)
169 .sum(-1, dtype=torch.int32)
170 )
171 unions = (
172 (masks > (mask_threshold - threshold_offset))
173 .sum(-1, dtype=torch.int16)
174 .sum(-1, dtype=torch.int32)
175 )
176 return intersections / unions
177
178
179def build_point_grid(n_per_side: int) -> np.ndarray:

Callers 2

_process_batchMethod · 0.90
forwardMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected