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
)
| 154 | |
| 155 | |
| 156 | def 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 | |
| 179 | def build_point_grid(n_per_side: int) -> np.ndarray: |
no outgoing calls
no test coverage detected