Instructions to use mlboydaisuke/SAM2-hiera-tiny-LiteRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use mlboydaisuke/SAM2-hiera-tiny-LiteRT with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- sam2
How to use mlboydaisuke/SAM2-hiera-tiny-LiteRT with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained(mlboydaisuke/SAM2-hiera-tiny-LiteRT) 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(mlboydaisuke/SAM2-hiera-tiny-LiteRT) 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 SAM 2.1 video-tracking graphs (memory attention/encoder, 4 per-frame GPU graphs) + card section
Browse files- README.md +52 -0
- sam2v_decode.tflite +3 -0
- sam2v_encode.tflite +3 -0
- sam2v_memcond2.tflite +3 -0
- sam2v_memcond7.tflite +3 -0
- sam2v_memorize.tflite +3 -0
- sam2v_mtpe.bin +3 -0
- sam2v_no_obj_ptr.bin +3 -0
- sam2v_prompt.bin +3 -0
- sam2v_tpos_proj.bin +3 -0
- sam2v_track_sparse.bin +3 -0
README.md
CHANGED
|
@@ -79,6 +79,58 @@ enc.invoke()
|
|
| 79 |
flat = enc.get_tensor(enc.get_output_details()[0]["index"]).flatten() # image_embed | fpn0 | fpn1
|
| 80 |
```
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
## Conversion
|
| 83 |
|
| 84 |
Converted with `litert-torch` from the Hugging Face `transformers` SAM 2 model. The full
|
|
|
|
| 79 |
flat = enc.get_tensor(enc.get_output_details()[0]["index"]).flatten() # image_embed | fpn0 | fpn1
|
| 80 |
```
|
| 81 |
|
| 82 |
+
|
| 83 |
+
## Video tracking (SAM 2 video path)
|
| 84 |
+
|
| 85 |
+
The full SAM 2.1 tracking loop β memory attention, memory encoder, object pointers and the
|
| 86 |
+
prompt-conditioned mask decoder β as four fixed-shape per-frame graphs on the CompiledModel
|
| 87 |
+
GPU. The rolling memory bank and per-frame orchestration run on the host (Kotlin/Swift/Python);
|
| 88 |
+
only tensor math touches the GPU. Tap once on the first frame and the mask follows the object.
|
| 89 |
+
|
| 90 |
+
| File | Size (fp16) | In β Out |
|
| 91 |
+
|---|---|---|
|
| 92 |
+
| `sam2v_encode.tflite` | 80 MB | image `[1,3,1024,1024]` β `pix_raw \| hi0 \| hi1` |
|
| 93 |
+
| `sam2v_memcond7.tflite` / `sam2v_memcond2.tflite` | 26 MB | `pix_raw \| memory bank \| temporal pos \| pointers \| key mask` β `pix_feat` (7- / 2-slot bank) |
|
| 94 |
+
| `sam2v_decode.tflite` | 18 MB | `pix_feat \| hi0 \| hi1 \| sparse \| nomem` β `masks \| iou \| obj_ptr \| obj_score` |
|
| 95 |
+
| `sam2v_memorize.tflite` | 3 MB | `pix_raw \| mask_for_mem \| occ` β spatial memory `[4096, 64]` |
|
| 96 |
+
| `sam2v_prompt.bin`, `sam2v_track_sparse.bin`, `sam2v_mtpe.bin`, `sam2v_no_obj_ptr.bin`, `sam2v_tpos_proj.bin` | β€64 KB | host-side constants (prompt encoder, temporal PE, pointer projection) | |
|
| 97 |
+
|
| 98 |
+
**Why it works on the GPU:** SAM 2's memory attention runs its RoPE attention with the batch
|
| 99 |
+
dim collapsed (rank 3), which the ML Drift delegate silently mis-computes β the graphs here are
|
| 100 |
+
re-authored **batch-first (rank 4)**, numerically identical on the host and correct on the GPU
|
| 101 |
+
(exact under fp32 GPU compute). The residual fp16 accumulation over the memory keys does not
|
| 102 |
+
reach the mask.
|
| 103 |
+
|
| 104 |
+
**Fidelity:** the assembled loop matches the PyTorch `Sam2VideoModel` reference at min
|
| 105 |
+
mask-IoU **0.9999** over a 10-frame clip (7- and 2-slot banks). All four graphs are fully
|
| 106 |
+
GPU-resident with no CPU fallback β Pixel 8a (Mali): encode 828/828, memcond 480/480, decode
|
| 107 |
+
462/462, memorize 145/145 nodes; iPhone 17 Pro (Metal): all `fullyGPU`.
|
| 108 |
+
|
| 109 |
+
**Per tracked frame** (encode + memcond + decode + memorize): iPhone 17 Pro **~471 ms**
|
| 110 |
+
(2-slot) / ~751 ms (7-slot); Pixel 8a ~1.0β1.5 s.
|
| 111 |
+
|
| 112 |
+
### Usage (Python, CompiledModel)
|
| 113 |
+
|
| 114 |
+
```python
|
| 115 |
+
import numpy as np
|
| 116 |
+
from ai_edge_litert.compiled_model import CompiledModel
|
| 117 |
+
|
| 118 |
+
enc = CompiledModel.from_file("sam2v_encode.tflite")
|
| 119 |
+
ins, outs = enc.create_input_buffers(0), enc.create_output_buffers(0)
|
| 120 |
+
ins[0].write(np.ascontiguousarray(frame_nchw.ravel().astype(np.float32)))
|
| 121 |
+
enc.run_by_index(0, ins, outs)
|
| 122 |
+
flat = outs[0].read(4_194_304, np.float32) # pix_raw | hi0 | hi1
|
| 123 |
+
# memcond -> decode -> memorize per frame; the full host loop (bank assembly,
|
| 124 |
+
# best-IoU pick, no-object handling) is verify_video.py in the recipe below.
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
### Usage (Kotlin)
|
| 128 |
+
|
| 129 |
+
The Android tracker (`Sam2VideoTracker.kt`: filesDir load, rolling bank, per-frame loop) and
|
| 130 |
+
the demo app live in [LiteRT-Models](https://github.com/john-rocky/LiteRT-Models) β `sam2/`.
|
| 131 |
+
The conversion + verification recipe is in litert-samples:
|
| 132 |
+
[`models/sam2/sam2_hiera_tiny_video/converted`](https://github.com/google-ai-edge/litert-samples/pull/283).
|
| 133 |
+
|
| 134 |
## Conversion
|
| 135 |
|
| 136 |
Converted with `litert-torch` from the Hugging Face `transformers` SAM 2 model. The full
|
sam2v_decode.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c07b021f9e8e689e326d61735e03fd8d80fb0f08be1679ae1fe0cc2b5332abe
|
| 3 |
+
size 17846160
|
sam2v_encode.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c6c457a9904e4f3e9d5be0320c37dec08e7e32c275e4dfce6e81d5151b1de2a3
|
| 3 |
+
size 80107568
|
sam2v_memcond2.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:75d00d674194a881d2a4cdd4c80c382cd603ab2dab407a648788270f2d1b6373
|
| 3 |
+
size 25626848
|
sam2v_memcond7.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dc6d3889a5f4c508183c8c5a336ef45eee7c98461ba9629c0afc835c5cbf6316
|
| 3 |
+
size 25626848
|
sam2v_memorize.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7c14d93b934614def046141fb88d63f0e489c255a518cf5767bf1ad8c2b7d84a
|
| 3 |
+
size 2856400
|
sam2v_mtpe.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8a693877939b52b8c3c4cb7c8a6e902611db9757c2c8def4c7fdece3b1c10787
|
| 3 |
+
size 1792
|
sam2v_no_obj_ptr.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1f4f6ab65e1471b59c6e80f6684f156d87b3cc9e34a0528037056b1b8879d6f6
|
| 3 |
+
size 1024
|
sam2v_prompt.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3510e3807703bc6375d742678025be7edc6e49b1e1214d26abaa408d51b60dff
|
| 3 |
+
size 4096
|
sam2v_tpos_proj.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5efef57e4473602bbae4a38c85e04f984d0755fee341a23c9479843322dfa2a6
|
| 3 |
+
size 65792
|
sam2v_track_sparse.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1f361f79c5cbf6d28a16e0b62cb3ca09a9603fb95b457426e4266943b1fa34b5
|
| 3 |
+
size 2048
|