SAM 2: Segment Anything in Images and Videos
Paper • 2408.00714 • Published • 123
How to use gumka/sam2.1-small-coreml with sam2:
# Use SAM2 with images
import torch
from sam2.sam2_image_predictor import SAM2ImagePredictor
predictor = SAM2ImagePredictor.from_pretrained("gumka/sam2.1-small-coreml")
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-coreml")
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):
...Apple's float16 Core ML conversion of SAM 2.1 Small, in three packages, as used by the Select object tool of Gumka on macOS: a click on an object gives its mask, and more clicks add to it or take parts away.
| Package | Input | Output |
|---|---|---|
SAM2_1SmallImageEncoderFLOAT16.mlpackage |
image 1024 × 1024 BGRA (the picture stretched) |
image_embedding [1, 256, 64, 64], feats_s0 [1, 32, 256, 256], feats_s1 [1, 64, 128, 128] |
SAM2_1SmallPromptEncoderFLOAT16.mlpackage |
points [1, N, 2] in 1024 × 1024 coordinates, labels [1, N] (1 includes, 0 excludes) |
sparse_embeddings, dense_embeddings |
SAM2_1SmallMaskDecoderFLOAT16.mlpackage |
the five arrays above | low_res_masks [1, 3, 256, 256] (scores; above 0 is selected), scores [1, 3] |
883f5787eb0b, unchanged; 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