Mask Generation
sam2
swift-object-cutout
segment-anything
interactive-segmentation
background-removal
core-ai
apple-silicon
macos
Instructions to use arraypress/cutout-sam2-tiny with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use arraypress/cutout-sam2-tiny with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained("arraypress/cutout-sam2-tiny") 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("arraypress/cutout-sam2-tiny") 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
File size: 4,809 Bytes
a0b4f4d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | ---
license: apache-2.0
base_model: facebook/sam2.1-hiera-tiny
tags:
- segment-anything
- sam2
- interactive-segmentation
- background-removal
- core-ai
- apple-silicon
- macos
library_name: swift-object-cutout
pipeline_tag: mask-generation
---
# cutout-sam2-tiny: SAM 2.1 tiny for Core AI (macOS 27)
[SAM 2.1](https://github.com/facebookresearch/sam2) hiera tiny (Ravi et al., Meta, *SAM 2: Segment
Anything in Images and Videos*, 2024; code and weights Apache-2.0), exported to an Apple Core AI
`.aimodel`. Click a point on a picture and it returns the object under it: three candidate masks
and the network's own estimate of how good each one is.
[`cutout`](https://github.com/arraypress/swift-cutout-cli) uses it for `--at` (click to cut), through
[swift-object-cutout](https://github.com/arraypress/swift-object-cutout). It downloads this
repository by itself the first time it is needed, pinned to one commit, with every file checked
against its published hash.
## Files
| Path | What |
|---|---|
| `cutout-sam2-tiny-float16.aimodel/` | Two entry points (below). Float16 weights and arithmetic, float32 inputs and outputs, 63 MB. |
| `export_sam2.py` | The export script (`SAM2_BUILD_CUDA=0 uv run export_sam2.py --dtype float16`). |
| `LICENSE`, `NOTICE` | The authors' Apache-2.0 licence, and what the export changed. |
**`encode`**: `image` `[1,3,1024,1024]` float32, RGB in 0…1 → `image_embedding` `[1,256,64,64]`,
`feats_s0` `[1,32,256,256]`, `feats_s1` `[1,64,128,128]`. The host stretches the picture to
1024×1024 without keeping its aspect ratio, as SAM 2's own predictor does; the ImageNet
normalisation and SAM 2's no-memory embedding are inside.
**`decode`**: the three encoder outputs, `point` `[1,1,2]` (x then y, in pixels of the 1024² square)
and `label` `[1,1]` (1 = the object, 0 = not it) → `masks` `[1,3,256,256]` logits and `scores`
`[1,3]`, the predicted IoU of each. Take the best-scoring mask, threshold it at 0 and stretch it back
over the picture. One encoding serves any number of clicks.
Nothing in the network is rewritten: the two entry points are the official
`SAM2ImagePredictor.set_image` and `_predict` steps, traced as they are. In float32 on the CPU the
traced steps match the predictor to 8 × 10⁻⁵ in the logits.
## Fidelity
60 clicks (two per picture) on 30 generated pictures: 20 AI-generated room and house scenes at
1024², and 10 synthetic shape scenes at sizes from 640×480 to 1600×900. The same 1024² pixels went
to every runtime. The reference is the PyTorch original in float32 on the CPU; "chosen" is the
best-scoring mask, which is the one a click returns.
| Runtime | Chosen-mask IoU, mean | worst | Clicks ≥ 0.99 | Same mask chosen | Score error, worst |
|---|---|---|---|---|---|
| this export, float16 (this repo) | 0.9996 | 0.9959 | 60 of 60 | 60 of 60 | 0.006 |
| this export, float32 (not published) | 1.0000 | 1.0000 | 60 of 60 | 60 of 60 | < 10⁻⁴ |
| Apple's Core ML conversion (`apple/coreml-sam2.1-tiny`, FLOAT16) | 0.968 | 0.500 | 34 of 60 | 56 of 60 | 0.22 |
Apple's Core ML conversion is what `cutout` ran before. Its masks drift from the original on
textured scenes, where it sometimes picks a different mask (IoU 0.50 on one click). This export
agrees with the original on every click. On the synthetic scenes the two runtimes agree to
0.99 or better, end to end; on the generated rooms they differ by as much as Apple's conversion differs
from the original.
## Speed and memory
Apple M3 Max, macOS 27, GPU, measured through `cutout`:
- **First load:** compiles the model for the Mac in well under a second and keeps a 60 MB compiled
copy in `~/Library/Caches/coreai-cache`.
- **Per click:** `encode` takes 0.15 s and `decode` 5 ms. One `cutout --at` run takes 0.6–1.0 s from
start to finish, against 4.5 s for the Core ML conversion, most of which went on loading.
- **Batches:** a folder of 30 pictures took 0.51 s per picture (decode, model, edge, PNG), against
0.42 s for the Core ML conversion, which ran on the Neural Engine.
- **Memory:** the folder run peaked at 1.2 GB, against 1.4 GB for the Core ML conversion.
## Use
```sh
cutout photo.jpg --at 0.32,0.6 # fetches this model on first use
cutout ./shots --at 0.5,0.45 --out cut # a folder, the model loaded once
cutout model remove click # the model and its compiled copy
```
Or by hand: `hf download arraypress/cutout-sam2-tiny --local-dir models`, then point
`$CUTOUT_CLICK_MODEL` at the `.aimodel`. Requires macOS 27 (Core AI) on Apple silicon. Converted
with coreai-torch 0.4.3, coreai-core 1.0.0b3, torch 2.9.0, hydra-core 1.3.2 and sam2 at 2b90b9f.
## Licence
Apache-2.0, as the original: see `LICENSE` (the authors' licence) and `NOTICE` (the copyright, and
what the export changed).
|