avbiswas commited on
Commit
cacbc5f
·
verified ·
1 Parent(s): ef99ec1

Update model card

Browse files
Files changed (1) hide show
  1. README.md +128 -5
README.md CHANGED
@@ -1,9 +1,132 @@
1
  ---
2
  license: apache-2.0
 
3
  pipeline_tag: image-segmentation
4
  tags:
5
- - sam2
6
- - sam
7
- - segment-anything
8
- - image-segmentation
9
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ library_name: mlx
4
  pipeline_tag: image-segmentation
5
  tags:
6
+ - mlx
7
+ - sam2
8
+ - segment-anything
9
+ - image-segmentation
10
+ - video-segmentation
11
+ - video-object-tracking
12
+ - apple-silicon
13
+ base_model:
14
+ - facebook/sam2.1-hiera-tiny
15
+ - facebook/sam2.1-hiera-small
16
+ - facebook/sam2.1-hiera-base-plus
17
+ - facebook/sam2.1-hiera-large
18
+ ---
19
+
20
+ # SAM 2.1 MLX
21
+
22
+ MLX-native ports of Meta/Facebook SAM 2.1 models for Apple Silicon.
23
+
24
+ This model is converted from Meta's SAM 2.1 checkpoints and the official
25
+ `facebookresearch/sam2` implementation. It is intended for local image
26
+ segmentation and video object tracking with MLX, without requiring PyTorch at
27
+ runtime.
28
+
29
+ - Project repo: https://github.com/avbiswas/sam2-mlx
30
+ - Model collection: https://huggingface.co/collections/avbiswas/sam2-mlx-6a0a0dcfbbbcb089d13d23cd
31
+ - Original SAM2 repo: https://github.com/facebookresearch/sam2
32
+ - Original models: https://huggingface.co/facebook
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ pip install mlx-sam
38
+ ```
39
+
40
+ or with uv:
41
+
42
+ ```bash
43
+ uv pip install mlx-sam
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ ```python
49
+ import numpy as np
50
+ from mlx_sam import SAM2VideoPredictor
51
+
52
+ predictor = SAM2VideoPredictor.from_pretrained(
53
+ "avbiswas/sam2.1-hiera-small-mlx" # replace with this model repo id
54
+ )
55
+
56
+ state = predictor.init_state("path/to/video_or_frames")
57
+
58
+ predictor.add_new_points_or_box(
59
+ state,
60
+ frame_idx=0,
61
+ obj_id=1,
62
+ points=np.array([[625.0, 429.0]], dtype=np.float32),
63
+ labels=np.array([1], dtype=np.int32),
64
+ )
65
+
66
+ for frame_idx, obj_ids, masks in predictor.propagate_in_video(state):
67
+ # masks: NumPy float32 array shaped [objects, 1, height, width]
68
+ pass
69
+ ```
70
+
71
+ ## Benchmarks
72
+
73
+ Benchmarks were run on an Apple M2 Max with 32 GB unified memory. Video tests
74
+ use the SAM2 dog demo clip: `1280x720`, 289 frames, 29.97 FPS, `9.64 s`.
75
+
76
+ ### FP32 MLX vs Torch/MPS
77
+
78
+ Prompted first-frame fixture at `1024x1024` internal resolution.
79
+
80
+ | Model | Size | Torch/MPS | MLX | Speedup | Parity vs Torch |
81
+ | --- | ---: | ---: | ---: | ---: | --- |
82
+ | `sam2.1-hiera-tiny-mlx` | `172.6 MiB` | `96.6 ms` | `71.3 ms` | `1.36x` | mask mean abs `1.17e-05` |
83
+ | `sam2.1-hiera-small-mlx` | `199.7 MiB` | `112.5 ms` | `84.5 ms` | `1.33x` | mask mean abs `8.14e-06` |
84
+ | `sam2.1-hiera-base-plus-mlx` | `336.4 MiB` | `203.5 ms` | `144.7 ms` | `1.41x` | mask mean abs `5.04e-06` |
85
+ | `sam2.1-hiera-large-mlx` | `892.2 MiB` | `433.0 ms` | `341.1 ms` | `1.27x` | mask mean abs `7.84e-06` |
86
+
87
+ ### Video Tracking
88
+
89
+ For `sam2.1-hiera-small-mlx` on the 9.64 second dog clip:
90
+
91
+ | Workload | Torch/MPS | MLX | Result |
92
+ | --- | ---: | ---: | --- |
93
+ | Full video, post-prompt propagation | `331 ms/frame` | `189 ms/frame` | MLX `1.75x` faster |
94
+ | Full video, total run | `100.5 s` | `94.8 s` | MLX faster end to end |
95
+ | Raw propagation, no save/overlay/final resize | `407 ms/frame` | `287 ms/frame` | MLX `1.42x` faster |
96
+
97
+ Experimental preview mode at `768x768` internal resolution:
98
+
99
+ | Setting | Propagation | Quality vs 1024 |
100
+ | --- | ---: | --- |
101
+ | `1024x1024` baseline | `268.5 ms/frame` | reference |
102
+ | `768x768`, fp16 memory attention | `52.9 ms/frame` | mean IoU `0.949`, presence `80 / 80` on 80-frame dog clip |
103
+
104
+ ### Quantized Variants
105
+
106
+ Quantized models reduce download size and memory footprint. On current MLX
107
+ kernels, quantization should not be assumed to speed up video tracking; it
108
+ primarily helps memory and distribution size.
109
+
110
+ | Variant | Typical Size Reduction | Notes |
111
+ | --- | ---: | --- |
112
+ | `*-mlx-16bit` | about `2x` smaller | fp16 weights, closest quantized parity |
113
+ | `*-mlx-8bit` | about `2.5x-3x` smaller | int8 linear quantization |
114
+ | `*-mlx-4bit` | about `3.5x` smaller | mixed recipe: int8 trunk/mask decoder, int4 memory/object-pointer layers |
115
+
116
+ Example small model parity vs fp32 MLX:
117
+
118
+ | Model | Size | Parity vs fp32 MLX |
119
+ | --- | ---: | --- |
120
+ | `sam2.1-hiera-small-mlx-16bit` | `99.9 MiB` | mask mean abs `8.24e-03` |
121
+ | `sam2.1-hiera-small-mlx-8bit` | `76.7 MiB` | mask mean abs `2.99e-02` |
122
+ | `sam2.1-hiera-small-mlx-4bit` | `56.4 MiB` | mask mean abs `2.87e-02` |
123
+
124
+ ## License
125
+
126
+ This MLX port is released under the Apache 2.0 license.
127
+
128
+ The original SAM 2 repository and source models are from Meta/Facebook and are
129
+ also Apache 2.0 licensed.
130
+
131
+ - Original SAM2 license: https://github.com/facebookresearch/sam2/blob/main/LICENSE
132
+ - Original SAM2 repo: https://github.com/facebookresearch/sam2