Add README
Browse files
README.md
CHANGED
|
@@ -1,3 +1,101 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- image-segmentation
|
| 5 |
+
- segment-anything
|
| 6 |
+
- onnx
|
| 7 |
+
- onnxruntime
|
| 8 |
+
library_name: onnxruntime
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Segment Anything (SAM + MobileSAM) — ONNX Models
|
| 12 |
+
|
| 13 |
+
ONNX-exported versions of Meta's [Segment Anything Model (SAM)](https://github.com/facebookresearch/segment-anything) and [MobileSAM](https://github.com/ChaoningZhang/MobileSAM), ready for CPU/GPU inference with [ONNX Runtime](https://onnxruntime.ai/) — no PyTorch required at runtime.
|
| 14 |
+
|
| 15 |
+
These models are used by **[AnyLabeling](https://github.com/vietanhdev/anylabeling)** for AI-assisted image annotation, and exported by **[samexporter](https://github.com/vietanhdev/samexporter)**.
|
| 16 |
+
|
| 17 |
+
## Available Models
|
| 18 |
+
|
| 19 |
+
| File | Variant | Encoder size | Notes |
|
| 20 |
+
|------|---------|-------------|-------|
|
| 21 |
+
| `sam_vit_b_01ec64.zip` | SAM ViT-B | ~90 MB | Fastest, lowest accuracy |
|
| 22 |
+
| `sam_vit_b_01ec64_quant.zip` | SAM ViT-B (Quant) | ~25 MB | Quantized — smaller & faster |
|
| 23 |
+
| `sam_vit_l_0b3195.zip` | SAM ViT-L | ~330 MB | Good balance |
|
| 24 |
+
| `sam_vit_l_0b3195_quant.zip` | SAM ViT-L (Quant) | ~83 MB | Quantized — smaller & faster |
|
| 25 |
+
| `sam_vit_h_4b8939.zip` | SAM ViT-H | ~630 MB | Highest accuracy |
|
| 26 |
+
| `sam_vit_h_4b8939_quant.zip` | SAM ViT-H (Quant) | ~158 MB | Quantized — smaller & faster |
|
| 27 |
+
| `mobile_sam_20230629.zip` | MobileSAM | ~9 MB | Ultra-lightweight |
|
| 28 |
+
|
| 29 |
+
Each zip contains two ONNX files: an **encoder** (runs once per image) and a **decoder** (runs interactively for each prompt).
|
| 30 |
+
|
| 31 |
+
## Prompt Types
|
| 32 |
+
|
| 33 |
+
- **Point** (`+point` / `-point`): click to include/exclude regions
|
| 34 |
+
- **Rectangle**: draw a bounding box around the target object
|
| 35 |
+
|
| 36 |
+
## Use with AnyLabeling (Recommended)
|
| 37 |
+
|
| 38 |
+
[AnyLabeling](https://github.com/vietanhdev/anylabeling) is a desktop annotation tool with a built-in model manager that downloads, caches, and runs these models automatically — no coding required.
|
| 39 |
+
|
| 40 |
+
1. Install: `pip install anylabeling`
|
| 41 |
+
2. Launch: `anylabeling`
|
| 42 |
+
3. Click the **Brain** button → select a SAM model from the dropdown
|
| 43 |
+
4. Use point or rectangle prompts to segment objects
|
| 44 |
+
|
| 45 |
+
[](https://github.com/vietanhdev/anylabeling)
|
| 46 |
+
|
| 47 |
+
## Use Programmatically with ONNX Runtime
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
import urllib.request, zipfile, pathlib
|
| 51 |
+
# Download and extract
|
| 52 |
+
url = "https://huggingface.co/vietanhdev/segment-anything-onnx-models/resolve/main/sam_vit_b_01ec64.zip"
|
| 53 |
+
urllib.request.urlretrieve(url, "sam_vit_b_01ec64.zip")
|
| 54 |
+
with zipfile.ZipFile("sam_vit_b_01ec64.zip") as z:
|
| 55 |
+
z.extractall("sam_vit_b_01ec64")
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
Then use [samexporter](https://github.com/vietanhdev/samexporter)'s inference module:
|
| 59 |
+
|
| 60 |
+
```bash
|
| 61 |
+
pip install samexporter
|
| 62 |
+
python -m samexporter.inference \
|
| 63 |
+
--encoder_model sam_vit_b_01ec64/sam_vit_b_encoder.onnx \
|
| 64 |
+
--decoder_model sam_vit_b_01ec64/sam_vit_b_decoder.onnx \
|
| 65 |
+
--image photo.jpg \
|
| 66 |
+
--prompt prompt.json \
|
| 67 |
+
--output result.png
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
## Re-export from Source
|
| 71 |
+
|
| 72 |
+
To re-export or customize the models using [samexporter](https://github.com/vietanhdev/samexporter):
|
| 73 |
+
|
| 74 |
+
```bash
|
| 75 |
+
pip install samexporter
|
| 76 |
+
# Export SAM ViT-H encoder + decoder
|
| 77 |
+
python -m samexporter.export_encoder \
|
| 78 |
+
--checkpoint original_models/sam_vit_h_4b8939.pth \
|
| 79 |
+
--output output_models/sam_vit_h_4b8939.encoder.onnx \
|
| 80 |
+
--model-type vit_h --use-preprocess
|
| 81 |
+
python -m samexporter.export_decoder \
|
| 82 |
+
--checkpoint original_models/sam_vit_h_4b8939.pth \
|
| 83 |
+
--output output_models/sam_vit_h_4b8939.decoder.onnx \
|
| 84 |
+
--model-type vit_h --return-single-mask
|
| 85 |
+
# Or convert all SAM variants at once:
|
| 86 |
+
bash convert_all_meta_sam.sh
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
## Related Repositories
|
| 90 |
+
|
| 91 |
+
| Repo | Description |
|
| 92 |
+
|------|-------------|
|
| 93 |
+
| [vietanhdev/samexporter](https://github.com/vietanhdev/samexporter) | Export scripts, inference code, conversion tools |
|
| 94 |
+
| [vietanhdev/anylabeling](https://github.com/vietanhdev/anylabeling) | Desktop annotation app powered by these models |
|
| 95 |
+
| [facebookresearch/segment-anything](https://github.com/facebookresearch/segment-anything) | Original SAM by Meta |
|
| 96 |
+
| [ChaoningZhang/MobileSAM](https://github.com/ChaoningZhang/MobileSAM) | Original MobileSAM |
|
| 97 |
+
|
| 98 |
+
## License
|
| 99 |
+
|
| 100 |
+
The ONNX models are derived from Meta's SAM and MobileSAM, both released under the **Apache 2.0** license.
|
| 101 |
+
The export code is part of [samexporter](https://github.com/vietanhdev/samexporter), released under the **MIT** license.
|