Instructions to use diffusionstudio/sam2.1-small-512-coreml with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use diffusionstudio/sam2.1-small-512-coreml with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained("diffusionstudio/sam2.1-small-512-coreml") 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("diffusionstudio/sam2.1-small-512-coreml") 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): ... - Notebooks
- Google Colab
- Kaggle
Add the Core ML programs
Browse files- README.md +38 -0
- constants.json +11 -0
- mask_decoder.mlpackage/Data/com.apple.CoreML/model.mlmodel +3 -0
- mask_decoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin +3 -0
- mask_decoder.mlpackage/Manifest.json +18 -0
- memory_attention.mlpackage/Data/com.apple.CoreML/model.mlmodel +3 -0
- memory_attention.mlpackage/Data/com.apple.CoreML/weights/weight.bin +3 -0
- memory_attention.mlpackage/Manifest.json +18 -0
- memory_encoder.mlpackage/Data/com.apple.CoreML/model.mlmodel +3 -0
- memory_encoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin +3 -0
- memory_encoder.mlpackage/Manifest.json +18 -0
- pointer_position.mlpackage/Data/com.apple.CoreML/model.mlmodel +3 -0
- pointer_position.mlpackage/Data/com.apple.CoreML/weights/weight.bin +3 -0
- pointer_position.mlpackage/Manifest.json +18 -0
- vision_encoder.mlpackage/Data/com.apple.CoreML/model.mlmodel +3 -0
- vision_encoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin +3 -0
- vision_encoder.mlpackage/Manifest.json +18 -0
README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: facebook/sam2.1-hiera-small
|
| 4 |
+
library_name: coreml
|
| 5 |
+
tags:
|
| 6 |
+
- sam2
|
| 7 |
+
- coreml
|
| 8 |
+
- video
|
| 9 |
+
- fp16
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# SAM 2.1 small, 512 px, Core ML
|
| 13 |
+
|
| 14 |
+
[SAM 2.1](https://huggingface.co/facebook/sam2.1-hiera-small) video tracking of one object, as five Core ML
|
| 15 |
+
programs with fixed shapes for [Diffusion Studio](https://diffusion.studio). The programs compute in fp16 and take
|
| 16 |
+
and give fp32. They need macOS 15.
|
| 17 |
+
|
| 18 |
+
| Program | Input | Output |
|
| 19 |
+
| ------------------ | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
|
| 20 |
+
| `vision_encoder` | `image` | `features_stride4`, `features_stride8`, `features_stride16`, `features_seed` |
|
| 21 |
+
| `mask_decoder` | `features_stride4`, `features_stride8`, `features`, `points` | `field`, `score`, `iou`, `object_pointer` |
|
| 22 |
+
| `memory_encoder` | `features_stride16`, `field`, `score`, `prompted` | `memory` |
|
| 23 |
+
| `memory_attention` | `features_stride16`, `memories`, `memory_rows`, `object_pointers`, `pointer_positions` | `features` |
|
| 24 |
+
| `pointer_position` | `distances` | `pointer_positions` |
|
| 25 |
+
|
| 26 |
+
- `image` is the frame, stretched to 512 × 512. The program normalizes it.
|
| 27 |
+
- `points` holds 16 rows of x, y and a label. x and y are in 0..1 of the frame. Label 1 is on the object,
|
| 28 |
+
0 is not, 2 and 3 are the corners of a box, and −1 is a row without a point.
|
| 29 |
+
- `features` of the mask decoder is `features_seed` for a frame with points, and the output of `memory_attention`
|
| 30 |
+
for a tracked frame.
|
| 31 |
+
- `field` is the mask as logits, 128 × 128.
|
| 32 |
+
- `memories` holds 7 outputs of `memory_encoder`. `memory_rows` gives each one its row of the model's
|
| 33 |
+
temporal table: row 6 for the frame with the points, and row k − 1 for the k-th newest tracked
|
| 34 |
+
frame.
|
| 35 |
+
- `distances` are the frame distances of the 16 object pointers, divided by the largest distance
|
| 36 |
+
that the pointers can have.
|
| 37 |
+
|
| 38 |
+
`constants.json` holds the sizes. `convert.py` in the `crates/segmenter` folder of Diffusion Studio makes these files.
|
constants.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"checkpoint": "facebook/sam2.1-hiera-small",
|
| 3 |
+
"image_size": 512,
|
| 4 |
+
"mask_grid": 128,
|
| 5 |
+
"feature_size": 32,
|
| 6 |
+
"hidden_size": 256,
|
| 7 |
+
"memory_size": 64,
|
| 8 |
+
"memory_frames": 7,
|
| 9 |
+
"max_object_pointers": 16,
|
| 10 |
+
"max_points": 16
|
| 11 |
+
}
|
mask_decoder.mlpackage/Data/com.apple.CoreML/model.mlmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3384336f6dc26b9014e8b514412d0563ffe62a43d382b7f07ca3f036744678f9
|
| 3 |
+
size 143255
|
mask_decoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:974601664dcb6baf1a3b9c40eafe7d280021148f940c32b0e2ade78bbd58a2d9
|
| 3 |
+
size 9853440
|
mask_decoder.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"C89564DB-2E58-4D17-87F6-C216EBB6E4A8": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Weights",
|
| 7 |
+
"name": "weights",
|
| 8 |
+
"path": "com.apple.CoreML/weights"
|
| 9 |
+
},
|
| 10 |
+
"D58EB0CC-7BF3-49E7-9402-3014657B703E": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Specification",
|
| 13 |
+
"name": "model.mlmodel",
|
| 14 |
+
"path": "com.apple.CoreML/model.mlmodel"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"rootModelIdentifier": "D58EB0CC-7BF3-49E7-9402-3014657B703E"
|
| 18 |
+
}
|
memory_attention.mlpackage/Data/com.apple.CoreML/model.mlmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:83ffd584f22f691aaae4541c6ea6d1e4a51c01e5d5395295c67fd1148555d475
|
| 3 |
+
size 135669
|
memory_attention.mlpackage/Data/com.apple.CoreML/weights/weight.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:29314f315966b74421e774b429d231890b54e0ca74155d7370122c7b13a2eac6
|
| 3 |
+
size 20896768
|
memory_attention.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"7E409F38-479C-4C06-855B-E8F05C89F089": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Weights",
|
| 7 |
+
"name": "weights",
|
| 8 |
+
"path": "com.apple.CoreML/weights"
|
| 9 |
+
},
|
| 10 |
+
"E5CA1378-CA90-43DF-877E-C4E1DBFC71EB": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Specification",
|
| 13 |
+
"name": "model.mlmodel",
|
| 14 |
+
"path": "com.apple.CoreML/model.mlmodel"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"rootModelIdentifier": "E5CA1378-CA90-43DF-877E-C4E1DBFC71EB"
|
| 18 |
+
}
|
memory_encoder.mlpackage/Data/com.apple.CoreML/model.mlmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c665810b15cfd02fcdf0bd4f312ece7d032f83b1bba8e7a8d153c420b8c8a2d1
|
| 3 |
+
size 36599
|
memory_encoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c20e51cc3a389f544e773a4516afe160e60a71aae465001dc7a3823c42a6299f
|
| 3 |
+
size 2771968
|
memory_encoder.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"1FFD2FB0-7ECD-4597-A063-B0320684B5C7": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Specification",
|
| 7 |
+
"name": "model.mlmodel",
|
| 8 |
+
"path": "com.apple.CoreML/model.mlmodel"
|
| 9 |
+
},
|
| 10 |
+
"BAB4CB79-03F9-46D0-92D7-40EA90F33DED": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Weights",
|
| 13 |
+
"name": "weights",
|
| 14 |
+
"path": "com.apple.CoreML/weights"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"rootModelIdentifier": "1FFD2FB0-7ECD-4597-A063-B0320684B5C7"
|
| 18 |
+
}
|
pointer_position.mlpackage/Data/com.apple.CoreML/model.mlmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b266b201cb7de97052aa68b21b69d6ba1535d02b897938c908f1f0db5e25c6ea
|
| 3 |
+
size 2651
|
pointer_position.mlpackage/Data/com.apple.CoreML/weights/weight.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:84d05a0f66ea1f214c89837a3a80c64dfea5514af6c7516276404be8499730c4
|
| 3 |
+
size 33408
|
pointer_position.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"BEEC54D3-60A0-46E7-857C-A21EF46F4ED3": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Specification",
|
| 7 |
+
"name": "model.mlmodel",
|
| 8 |
+
"path": "com.apple.CoreML/model.mlmodel"
|
| 9 |
+
},
|
| 10 |
+
"E700B1BD-6046-4FB6-9A05-17DC6FC06EBD": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Weights",
|
| 13 |
+
"name": "weights",
|
| 14 |
+
"path": "com.apple.CoreML/weights"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"rootModelIdentifier": "BEEC54D3-60A0-46E7-857C-A21EF46F4ED3"
|
| 18 |
+
}
|
vision_encoder.mlpackage/Data/com.apple.CoreML/model.mlmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d65f65a030fe529c64990000bf072f6cb2e0a0660f1a363f2cef30460dc47513
|
| 3 |
+
size 231816
|
vision_encoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:593a2481b36f06e0f04dc4861b4175b33ef114c79b5511c7f983d4a0187d4bb6
|
| 3 |
+
size 71821632
|
vision_encoder.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"0C6A1647-4C82-4910-AEF1-A97C3E066EC3": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Specification",
|
| 7 |
+
"name": "model.mlmodel",
|
| 8 |
+
"path": "com.apple.CoreML/model.mlmodel"
|
| 9 |
+
},
|
| 10 |
+
"548997F8-19F9-4618-BB94-04EAA914BAF3": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Weights",
|
| 13 |
+
"name": "weights",
|
| 14 |
+
"path": "com.apple.CoreML/weights"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"rootModelIdentifier": "0C6A1647-4C82-4910-AEF1-A97C3E066EC3"
|
| 18 |
+
}
|