Image Segmentation
sam2
MLX
English
segment-anything
apple-silicon
video-segmentation
object-tracking
Instructions to use eisneim/sam2.1_mlx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use eisneim/sam2.1_mlx with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained(eisneim/sam2.1_mlx) 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(eisneim/sam2.1_mlx) 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): ... - MLX
How to use eisneim/sam2.1_mlx with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] huggingface-cli download --local-dir sam2.1_mlx eisneim/sam2.1_mlx
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- README.md +79 -3
- sam2.1_hiera_base_plus.safetensors +3 -0
- sam2.1_hiera_small.safetensors +3 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
README.md
CHANGED
|
@@ -1,3 +1,79 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
pipeline_tag: image-segmentation
|
| 5 |
+
tags:
|
| 6 |
+
- sam2
|
| 7 |
+
- segment-anything
|
| 8 |
+
- mlx
|
| 9 |
+
- apple-silicon
|
| 10 |
+
- video-segmentation
|
| 11 |
+
- object-tracking
|
| 12 |
+
license: apache-2.0
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# SAM2.1 MLX Weights
|
| 16 |
+
|
| 17 |
+
MLX-format weights for [SAM2.1 (Segment Anything Model 2.1)](https://github.com/facebookresearch/sam2) ported to Apple MLX.
|
| 18 |
+
|
| 19 |
+
## Quick Start
|
| 20 |
+
|
| 21 |
+
**1. Clone the code:**
|
| 22 |
+
```bash
|
| 23 |
+
git clone https://github.com/eisneim/sam2.1_mlx.git
|
| 24 |
+
cd sam2.1_mlx
|
| 25 |
+
pip install mlx opencv-python safetensors numpy
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
**2. Download weights from this repo:**
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
# Base Plus (recommended, best quality/speed balance)
|
| 32 |
+
huggingface-cli download eisneim/sam2.1_mlx_weights sam2.1_hiera_base_plus.safetensors --local-dir weights/
|
| 33 |
+
|
| 34 |
+
# Small (faster, slightly lower quality)
|
| 35 |
+
huggingface-cli download eisneim/sam2.1_mlx_weights sam2.1_hiera_small.safetensors --local-dir weights/
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
Or manually download the `.safetensors` files and place them in `weights/`.
|
| 39 |
+
|
| 40 |
+
**3. Run:**
|
| 41 |
+
|
| 42 |
+
```bash
|
| 43 |
+
# Video tracking — click on an object in the first frame
|
| 44 |
+
python inference_video.py -i your_video.mp4
|
| 45 |
+
|
| 46 |
+
# Image segmentation — click on an object
|
| 47 |
+
python inference_image.py -i your_image.jpg
|
| 48 |
+
|
| 49 |
+
# Use the small model
|
| 50 |
+
python inference_video.py -i your_video.mp4 --model small
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
## Available Models
|
| 54 |
+
|
| 55 |
+
| Model | File | Size | Quality | Speed |
|
| 56 |
+
|-------|------|------|---------|-------|
|
| 57 |
+
| base_plus | `sam2.1_hiera_base_plus.safetensors` | ~300MB | Best | ~130 fps |
|
| 58 |
+
| small | `sam2.1_hiera_small.safetensors` | ~150MB | Good | ~200 fps |
|
| 59 |
+
|
| 60 |
+
## Converting Weights Yourself
|
| 61 |
+
|
| 62 |
+
If you prefer to convert from the original PyTorch checkpoints:
|
| 63 |
+
|
| 64 |
+
```bash
|
| 65 |
+
# Download PyTorch weights from Meta
|
| 66 |
+
wget https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_base_plus.pt -P weights/
|
| 67 |
+
|
| 68 |
+
# Convert to MLX safetensors
|
| 69 |
+
python -m src.sam2.convert --src weights/sam2.1_hiera_base_plus.pt --dst weights/sam2.1_hiera_base_plus.safetensors
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
## Links
|
| 73 |
+
|
| 74 |
+
- **Code:** https://github.com/eisneim/sam2.1_mlx
|
| 75 |
+
- **Original SAM2:** https://github.com/facebookresearch/sam2
|
| 76 |
+
|
| 77 |
+
## License
|
| 78 |
+
|
| 79 |
+
Apache 2.0 (same as the original SAM2).
|
sam2.1_hiera_base_plus.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:80d5c62aa0a608017ba0e664667580ddc2ea8c52a2fccc86b4f1040b3f9fa232
|
| 3 |
+
size 323473304
|
sam2.1_hiera_small.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0b3a0198309acc9d1879d0a78aedc4adea10cf2fa9ff6ab173872fc6f4ae3eef
|
| 3 |
+
size 184302864
|