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

Batch prediction don't squeeze outputs when using batched points or boxes #102

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions sam2/sam2_image_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,19 @@ def predict_batch(
return_logits=return_logits,
img_idx=img_idx,
)
masks_np = masks.squeeze(0).float().detach().cpu().numpy()
iou_predictions_np = (
iou_predictions.squeeze(0).float().detach().cpu().numpy()
)
low_res_masks_np = low_res_masks.squeeze(0).float().detach().cpu().numpy()

using_batch_points = point_coords is not None and len(point_coords.shape) == 3
using_batch_box = box is not None and len(box.shape) == 3

if not using_batch_points and not using_batch_box:
masks = masks.squeeze(0)
iou_predictions = iou_predictions.squeeze(0)
low_res_masks = low_res_masks.squeeze(0)

masks_np = masks.float().detach().cpu().numpy()
iou_predictions_np = iou_predictions.float().detach().cpu().numpy()
low_res_masks_np = low_res_masks.float().detach().cpu().numpy()

all_masks.append(masks_np)
all_ious.append(iou_predictions_np)
all_low_res_masks.append(low_res_masks_np)
Expand Down Expand Up @@ -277,9 +285,17 @@ def predict(
return_logits=return_logits,
)

masks_np = masks.squeeze(0).float().detach().cpu().numpy()
iou_predictions_np = iou_predictions.squeeze(0).float().detach().cpu().numpy()
low_res_masks_np = low_res_masks.squeeze(0).float().detach().cpu().numpy()
using_batch_points = point_coords is not None and len(point_coords.shape) == 3
using_batch_box = box is not None and len(box.shape) == 3

if not using_batch_points and not using_batch_box:
masks = masks.squeeze(0)
iou_predictions = iou_predictions.squeeze(0)
low_res_masks = low_res_masks.squeeze(0)

masks_np = masks.float().detach().cpu().numpy()
iou_predictions_np = iou_predictions.float().detach().cpu().numpy()
low_res_masks_np = low_res_masks.float().detach().cpu().numpy()
return masks_np, iou_predictions_np, low_res_masks_np

def _prep_prompts(
Expand Down