SAM 2: Segment Anything in Images and Videos
Paper • 2408.00714 • Published • 123
How to use gumka/sam2.1-small-onnx with sam2:
# Use SAM2 with images
import torch
from sam2.sam2_image_predictor import SAM2ImagePredictor
predictor = SAM2ImagePredictor.from_pretrained("gumka/sam2.1-small-onnx")
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
predictor.set_image(<your_image>)
masks, _, _ = predictor.predict(<input_prompts>) # Use SAM2 with videos
import torch
from sam2.sam2_video_predictor import SAM2VideoPredictor
predictor = SAM2VideoPredictor.from_pretrained("gumka/sam2.1-small-onnx")
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
state = predictor.init_state(<your_video>)
# add new prompts and instantly get the output on the same frame
frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>)
# propagate the prompts to get masklets throughout the video
for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
...A float32 ONNX export of SAM 2.1 Small, in two graphs, as used by the Select object tool of Gumka on Linux: a click on an object gives its mask, and more clicks add to it or take parts away.
| Files | Input | Output |
|---|---|---|
vision_encoder.onnx, vision_encoder.onnx_data (162 MB) |
pixel_values float32 [1, 3, 1024, 1024]: the picture stretched, RGB from 0 to 1, normalized with ImageNet's mean (0.485, 0.456, 0.406) and deviation (0.229, 0.224, 0.225) |
image_embeddings.0 [1, 32, 256, 256], image_embeddings.1 [1, 64, 128, 128], image_embeddings.2 [1, 256, 64, 64] |
prompt_encoder_mask_decoder.onnx, prompt_encoder_mask_decoder.onnx_data (21 MB) |
the three embeddings; input_points float32 [1, 1, N, 2] in 1024 × 1024 coordinates; input_labels int64 [1, 1, N] (1 includes, 0 excludes); input_boxes float32 [1, B, 4] (B may be 0) |
pred_masks [1, 1, 3, 256, 256] (scores; above 0 is selected), iou_scores [1, 1, 3], object_score_logits [1, 1, 1] |
.onnx_data file beside it: keep the two together.a7df49d8de14, unchanged (its quantized variants are left out); Gumka's
models.toml pins each
file's size and SHA-256. License: Apache-2.0, see LICENSE.Base model
facebook/sam2.1-hiera-small