MLX-native ports of Meta/Facebook SAM 2.1 models for Apple Silicon.
1import numpy as np
2from mlx_sam import SAM2VideoPredictor
3
4predictor = SAM2VideoPredictor.from_pretrained(
5 "avbiswas/sam2.1-hiera-small-mlx" # replace with this model repo id
6)
7
8state = predictor.init_state("path/to/video_or_frames")
9
10predictor.add_new_points_or_box(
11 state,
12 frame_idx=0,
13 obj_id=1,
14 points=np.array([[625.0, 429.0]], dtype=np.float32),
15 labels=np.array([1], dtype=np.int32),
16)
17
18for frame_idx, obj_ids, masks in predictor.propagate_in_video(state):
19 # masks: NumPy float32 array shaped [objects, 1, height, width]
20 pass
Benchmarks were run on an Apple M2 Max with 32 GB unified memory. Video tests
use the SAM2 dog demo clip: 1280x720, 289 frames, 29.97 FPS, 9.64 s.
Quantized models reduce download size and memory footprint. On current MLX
kernels, quantization should not be assumed to speed up video tracking; it
primarily helps memory and distribution size.
This MLX port is released under the Apache 2.0 license.
The original SAM 2 repository and source models are from Meta/Facebook and are
also Apache 2.0 licensed.