Skip to content

Commit bcd1483

Browse files
committed
Re-order GroundedSAMInvocation._to_numpy_masks(...) to do slightly more work on the GPU.
1 parent e206890 commit bcd1483

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

invokeai/app/invocations/grounded_sam.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ def _segment(
156156

157157
def _to_numpy_masks(self, masks: torch.Tensor) -> list[npt.NDArray[np.uint8]]:
158158
"""Convert the tensor output from the Segment Anything model to a list of numpy masks."""
159-
masks = masks.cpu().float()
160-
masks = masks.permute(0, 2, 3, 1)
161-
masks = masks.mean(dim=-1)
162-
masks = (masks > 0).int()
163-
np_masks = masks.numpy().astype(np.uint8)
159+
eps = 0.0001
160+
# [num_masks, channels, height, width] -> [num_masks, height, width]
161+
masks = masks.permute(0, 2, 3, 1).float().mean(dim=-1)
162+
masks = masks > eps
163+
np_masks = masks.cpu().numpy().astype(np.uint8)
164164
return list(np_masks)
165165

166166
def _apply_polygon_refinement(self, masks: list[npt.NDArray[np.uint8]]) -> list[npt.NDArray[np.uint8]]:

0 commit comments

Comments
 (0)