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

Raise error messages for invalid prompts #234

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jhj0517
Copy link
Contributor

@jhj0517 jhj0517 commented Aug 17, 2024

This PR raises the error message for invalid prompts.

As I understand it (please correct me if I'm wrong)
These prompts cannot be used:

  1. Multiple box and points combinataions in SAM2ImagePredictor.predict() ( Only single combination is supported )

  2. Multiple boxes in SAM2VideoPredictor.add_new_points_or_box() ( Only single box is supported )

This is sample code to reproduce errors with these invalid prompts:

import os
import numpy as np
from PIL import Image
from sam2.build_sam import build_sam2, build_sam2_video_predictor
from sam2.sam2_image_predictor import SAM2ImagePredictor

# Set paths
SAM2_CONFIGS_DIR = "path\\to\\configs_directory"
MODELS_DIR = "path\\to\\models_directory"
IMAGE_PATH = "path\\to\\image.jpg"
VIDEO_FRAMES_DIR = "path\\to\\video_frames_directory"

config_path = os.path.join(SAM2_CONFIGS_DIR, "sam2_hiera_l.yaml")
model_path = os.path.join(MODELS_DIR, "sam2_hiera_large.pt")

model = build_sam2(
    config_file=config_path,
    ckpt_path=model_path,
    device="cuda"
)
image_predictor = SAM2ImagePredictor(sam_model=model)
image = Image.open(IMAGE_PATH)
image_predictor.set_image(image)

double_box = np.array([
    [210, 609, 276, 698],
    [308, 425, 357, 501]
])
double_point_coords = np.array([
    [245, 655],
    [324, 456]
])
double_point_labels = np.array([1, 1])

# Using multiple combinations and getting errors
image_predictor.predict(point_coords=double_point_coords,
                        point_labels=double_point_labels,
                        box=double_box)

# Video Predictor case
model = build_sam2_video_predictor(
    config_file=config_path,
    ckpt_path=model_path,
    device="cuda"
)
inference_state = model.init_state(video_path=VIDEO_FRAMES_DIR)

# Using multiple boxes and getting errors
idx, scores, logits = model.add_new_points_or_box(
    frame_idx=0,
    obj_id=0,
    inference_state=inference_state,
    points=None,
    labels=None,
    box=double_box
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants