Update README with usage examples
Browse files
README.md
CHANGED
|
@@ -1,50 +1,110 @@
|
|
| 1 |
---
|
| 2 |
-
license: other
|
| 3 |
-
extra_gated_fields:
|
| 4 |
-
First Name: text
|
| 5 |
-
Last Name: text
|
| 6 |
-
Date of birth: date_picker
|
| 7 |
-
Country: country
|
| 8 |
-
Affiliation: text
|
| 9 |
-
Job title:
|
| 10 |
-
type: select
|
| 11 |
-
options:
|
| 12 |
-
- Student
|
| 13 |
-
- Research Graduate
|
| 14 |
-
- AI researcher
|
| 15 |
-
- AI developer/engineer
|
| 16 |
-
- Reporter
|
| 17 |
-
- Other
|
| 18 |
-
geo: ip_location
|
| 19 |
-
? By clicking Submit below I accept the terms of the license and acknowledge that
|
| 20 |
-
the information I provide will be collected stored processed and shared in accordance
|
| 21 |
-
with the Meta Privacy Policy
|
| 22 |
-
: checkbox
|
| 23 |
-
extra_gated_description: The information you provide will be collected, stored, processed
|
| 24 |
-
and shared in accordance with the [Meta Privacy Policy](https://www.facebook.com/privacy/policy/).
|
| 25 |
-
extra_gated_button_content: Submit
|
| 26 |
-
language:
|
| 27 |
-
- en
|
| 28 |
-
pipeline_tag: mask-generation
|
| 29 |
library_name: mlx
|
|
|
|
| 30 |
tags:
|
| 31 |
-
- sam3
|
| 32 |
- mlx
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
---
|
| 35 |
|
| 36 |
-
#
|
| 37 |
|
| 38 |
-
|
| 39 |
-
using mlx-vlm version **0.4.2**.
|
| 40 |
-
Refer to the [original model card](https://huggingface.co/facebook/sam3) for more details on the model.
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
```bash
|
| 45 |
-
pip install
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
```
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
```bash
|
| 49 |
-
python -m mlx_vlm.
|
| 50 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
library_name: mlx
|
| 3 |
+
base_model: facebook/sam3
|
| 4 |
tags:
|
|
|
|
| 5 |
- mlx
|
| 6 |
+
- sam3
|
| 7 |
+
- segmentation
|
| 8 |
+
- detection
|
| 9 |
+
- tracking
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# sam3-5bit
|
| 13 |
|
| 14 |
+
[facebook/sam3](https://huggingface.co/facebook/sam3) converted to MLX (5-bit quantized, 0.72 GB).
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
Open-vocabulary **object detection**, **instance segmentation**, and **video tracking** on Apple Silicon (~860M parameters).
|
| 17 |
+
|
| 18 |
+
## Quick Start
|
| 19 |
|
| 20 |
```bash
|
| 21 |
+
pip install mlx-vlm
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
from PIL import Image
|
| 26 |
+
from mlx_vlm.utils import load_model, get_model_path
|
| 27 |
+
from mlx_vlm.models.sam3.generate import Sam3Predictor
|
| 28 |
+
from mlx_vlm.models.sam3.processing_sam3 import Sam3Processor
|
| 29 |
+
|
| 30 |
+
model_path = get_model_path("mlx-community/sam3-5bit")
|
| 31 |
+
model = load_model(model_path)
|
| 32 |
+
processor = Sam3Processor.from_pretrained(str(model_path))
|
| 33 |
+
predictor = Sam3Predictor(model, processor, score_threshold=0.3)
|
| 34 |
```
|
| 35 |
|
| 36 |
+
## Object Detection
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
image = Image.open("photo.jpg")
|
| 40 |
+
result = predictor.predict(image, text_prompt="a dog")
|
| 41 |
+
|
| 42 |
+
for i in range(len(result.scores)):
|
| 43 |
+
x1, y1, x2, y2 = result.boxes[i]
|
| 44 |
+
print(f"[{result.scores[i]:.2f}] box=({x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f})")
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
## Instance Segmentation
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
result = predictor.predict(image, text_prompt="a person")
|
| 51 |
+
|
| 52 |
+
# result.boxes -> (N, 4) xyxy bounding boxes
|
| 53 |
+
# result.masks -> (N, H, W) binary segmentation masks
|
| 54 |
+
# result.scores -> (N,) confidence scores
|
| 55 |
+
|
| 56 |
+
import numpy as np
|
| 57 |
+
overlay = np.array(image).copy()
|
| 58 |
+
W, H = image.size
|
| 59 |
+
for i in range(len(result.scores)):
|
| 60 |
+
mask = result.masks[i]
|
| 61 |
+
if mask.shape != (H, W):
|
| 62 |
+
mask = np.array(Image.fromarray(mask.astype(np.float32)).resize((W, H)))
|
| 63 |
+
binary = mask > 0
|
| 64 |
+
overlay[binary] = (overlay[binary] * 0.5 + np.array([255, 0, 0]) * 0.5).astype(np.uint8)
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
## Box-Guided Detection
|
| 68 |
+
|
| 69 |
+
```python
|
| 70 |
+
import numpy as np
|
| 71 |
+
boxes = np.array([[100, 50, 400, 350]]) # xyxy pixel coords
|
| 72 |
+
result = predictor.predict(image, text_prompt="a cat", boxes=boxes)
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
## Semantic Segmentation
|
| 76 |
+
|
| 77 |
+
```python
|
| 78 |
+
import mlx.core as mx
|
| 79 |
+
|
| 80 |
+
inputs = processor.preprocess_image(image)
|
| 81 |
+
text_inputs = processor.preprocess_text("a cat")
|
| 82 |
+
outputs = model.detect(
|
| 83 |
+
mx.array(inputs["pixel_values"]),
|
| 84 |
+
mx.array(text_inputs["input_ids"]),
|
| 85 |
+
mx.array(text_inputs["attention_mask"]),
|
| 86 |
+
)
|
| 87 |
+
mx.eval(outputs)
|
| 88 |
+
|
| 89 |
+
pred_masks = outputs["pred_masks"] # (B, 200, 288, 288) instance masks
|
| 90 |
+
semantic_seg = outputs["semantic_seg"] # (B, 1, 288, 288) semantic segmentation
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
## Video Tracking (CLI)
|
| 94 |
+
|
| 95 |
```bash
|
| 96 |
+
python -m mlx_vlm.models.sam3.track_video --video input.mp4 --prompt "a car" --model mlx-community/sam3-5bit
|
| 97 |
```
|
| 98 |
+
|
| 99 |
+
| Flag | Default | Description |
|
| 100 |
+
|------|---------|-------------|
|
| 101 |
+
| `--video` | *(required)* | Input video path |
|
| 102 |
+
| `--prompt` | *(required)* | Text prompt |
|
| 103 |
+
| `--output` | `<input>_tracked.mp4` | Output video path |
|
| 104 |
+
| `--model` | `facebook/sam3` | Model path or HF repo |
|
| 105 |
+
| `--threshold` | `0.15` | Score threshold |
|
| 106 |
+
| `--every` | `2` | Detect every N frames |
|
| 107 |
+
|
| 108 |
+
## Original Model
|
| 109 |
+
|
| 110 |
+
[facebook/sam3](https://huggingface.co/facebook/sam3) 路 [Paper](https://ai.meta.com/blog/segment-anything-model-3/) 路 [Code](https://github.com/facebookresearch/sam3)
|