Instructions to use diffusionstudio/sam2.1-base-512-coreml with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use diffusionstudio/sam2.1-base-512-coreml with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained("diffusionstudio/sam2.1-base-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-base-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-base-plus
|
| 4 |
+
library_name: coreml
|
| 5 |
+
tags:
|
| 6 |
+
- sam2
|
| 7 |
+
- coreml
|
| 8 |
+
- video
|
| 9 |
+
- fp16
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# SAM 2.1 base-plus, 512 px, Core ML
|
| 13 |
+
|
| 14 |
+
[SAM 2.1](https://huggingface.co/facebook/sam2.1-hiera-base-plus) 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-base-plus",
|
| 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:148374f2add3ac80a116e1e59664da37c7e8d3c1cbc757ada6eaebe9ab5f9b96
|
| 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:1ec346853c570845bd3d222c7442df600692da69351f4e43c99e47f29003c1f1
|
| 3 |
+
size 9853440
|
mask_decoder.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"23CAC55D-C829-4FAD-ABA5-A02AD3D4DF3F": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Weights",
|
| 7 |
+
"name": "weights",
|
| 8 |
+
"path": "com.apple.CoreML/weights"
|
| 9 |
+
},
|
| 10 |
+
"B5BB3434-CAD8-4C8E-A3E9-A474EFEB2E8B": {
|
| 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": "B5BB3434-CAD8-4C8E-A3E9-A474EFEB2E8B"
|
| 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:a70878bd13fb0992c00245d97608f2bdf3c6ac5370b68e83db555ff7881b31b7
|
| 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:49e28815b476755658845d782b2cb75fcb60285c1071f86a1f04101e65a5efc6
|
| 3 |
+
size 20896768
|
memory_attention.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"884268DC-3202-430A-AE5A-6B2A9FE58DF9": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Weights",
|
| 7 |
+
"name": "weights",
|
| 8 |
+
"path": "com.apple.CoreML/weights"
|
| 9 |
+
},
|
| 10 |
+
"D35CDCFB-B4A7-42C3-ADC2-DA86BF84E41E": {
|
| 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": "D35CDCFB-B4A7-42C3-ADC2-DA86BF84E41E"
|
| 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:20680a80d849684c6a8769e18fb8376594effb1345526c2bcaf09d9236f121b3
|
| 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:c107bbd3c42af893d4cd95112980ab8375ed95b307bc206660bcad493c03703f
|
| 3 |
+
size 2771968
|
memory_encoder.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"8C5BD031-BC1F-45D0-A1A3-8B38F01A046E": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Weights",
|
| 7 |
+
"name": "weights",
|
| 8 |
+
"path": "com.apple.CoreML/weights"
|
| 9 |
+
},
|
| 10 |
+
"DD6788D7-4127-4CDE-B9CE-6DCBC04779E1": {
|
| 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": "DD6788D7-4127-4CDE-B9CE-6DCBC04779E1"
|
| 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:fb0884c4f8c4bf84d0b73cde73036ec77ef4adb35582d3969c18537dce5c2118
|
| 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:c67a77d21056e035517294656ba9de8d6b27921496ba22625a47ec12724a2cd0
|
| 3 |
+
size 33408
|
pointer_position.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"5D0009B4-E0A3-48CD-B004-AF6D1B3094E0": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Specification",
|
| 7 |
+
"name": "model.mlmodel",
|
| 8 |
+
"path": "com.apple.CoreML/model.mlmodel"
|
| 9 |
+
},
|
| 10 |
+
"BE0FC02B-5AE0-44EC-B1FB-3BBB0193EDA5": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Weights",
|
| 13 |
+
"name": "weights",
|
| 14 |
+
"path": "com.apple.CoreML/weights"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"rootModelIdentifier": "5D0009B4-E0A3-48CD-B004-AF6D1B3094E0"
|
| 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:7278280217173642f0bfbee04862d61ce28f458018d57b42474c6fdf5540bdb6
|
| 3 |
+
size 340632
|
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:fe633c972a33d5631b61217e032283b025c1f668d5c557f48f6fe236044e5499
|
| 3 |
+
size 141895744
|
vision_encoder.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"64A75D7A-9D86-4D57-B91B-1F978CFE63F2": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Weights",
|
| 7 |
+
"name": "weights",
|
| 8 |
+
"path": "com.apple.CoreML/weights"
|
| 9 |
+
},
|
| 10 |
+
"A7DD3143-9BC7-4C7B-B2E8-F853F5AFA74B": {
|
| 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": "A7DD3143-9BC7-4C7B-B2E8-F853F5AFA74B"
|
| 18 |
+
}
|