ONNX export of Meta's SAM 2.1 hiera-tiny image segmentation model, built for the object selection in Oku3D's photo editor and usable by any ONNX Runtime consumer. The export runs on DirectML as well as on the CPU.
| file | what it does | size |
|---|---|---|
sam2.1-tiny-encoder_fp16.onnx |
image encoder, run once per picture | 70 MB |
sam2.1-tiny-decoder_fp32.onnx |
prompt encoder and mask decoder, run per click or box | 18 MB |
Built from sam2.1_hiera_tiny.pt and facebookresearch/sam2 at commit
2b90b9f.
encoder
input image float32 [1, 3, 1024, 1024]
output image_embed float32 [1, 256, 64, 64]
output high_res_feats_0 float16 [1, 32, 256, 256]
output high_res_feats_1 float16 [1, 64, 128, 128]
decoder
input image_embed float32 [1, 256, 64, 64]
input high_res_feats_0 float32 [1, 32, 256, 256]
input high_res_feats_1 float32 [1, 64, 128, 128]
input point_coords float32 [1, points, 2]
input point_labels float32 [1, points]
input mask_input float32 [1, 1, 256, 256]
input has_mask_input float32 [1]
output masks float32 [1, 4, 256, 256]
output iou float32 [1, 4]
The image is RGB in [0, 1], stretched to 1024 x 1024 as SAM 2's own transform resizes it, aspect ratio not kept. The ImageNet standardization runs inside the graph.
The encoder's image embedding leaves as float32 and its two high-resolution features as float16; the decoder takes all three as float32.
point_coords are pixels of the 1024 x 1024 frame (x, y). point_labels:
1 a point on the object, 0 a point on the background, 2 and 3 a box's
top-left and bottom-right corner. The padding point SAM 2 appends to a
prompt is appended inside the graph. mask_input takes the logits of an
earlier answer when has_mask_input is 1, and is ignored when it is 0.
masks holds the low-resolution logits of every mask token: index 0 is
the single-mask answer, 1 to 3 the three alternatives. Pick as SAM 2's
image predictor does:
iou;Cut the logits at 0 after resizing them to the picture.
CastLike is the plain Cast it amounts to, and allowzero is
cleared on every Reshape whose target shape holds no zero. DirectML
refuses a session holding either as exported.Against Meta's SAM2ImagePredictor on a photograph, fed the predictor's
own resized input, on ONNX Runtime's CPU provider:
| prompt | FP32 encoder | FP16 encoder (shipped) |
|---|---|---|
| one click | identical logits, mask IoU 1.0000 | mask IoU 0.9998 |
| box | identical logits, mask IoU 1.0000 | mask IoU 1.0000 |
With ONNX Runtime 1.24.4 and DirectML on an NVIDIA GeForce RTX 5070 Ti: encoder about 24 ms, decoder about 3.3 ms per prompt, after the first run.
build/ holds the pipeline: Dockerfile, 01_export_onnx.py and
02_optimize_verify.py. With the sam2 source and the checkpoint beside it:
docker build -t oku3d-sam2-export build
docker run --rm -v "$PWD:/work" oku3d-sam2-export python build/01_export_onnx.py \
--source /work/src/sam2 --ckpt /work/checkpoints/sam2.1_hiera_tiny.pt --out /work/out --opset 18
docker run --rm -v "$PWD:/work" oku3d-sam2-export python build/02_optimize_verify.py \
--source /work/src/sam2 --ckpt /work/checkpoints/sam2.1_hiera_tiny.pt --out /work/out --image <photo>
Apache-2.0, as Meta publishes the SAM 2 code and checkpoints; see LICENSE.
@article{ravi2024sam2,
title={SAM 2: Segment Anything in Images and Videos},
author={Ravi, Nikhila and Gabeur, Valentin and Hu, Yuan-Ting and Hu, Ronghang and Ryali, Chaitanya and Ma, Tengyu and Khedr, Haitham and R{\"a}dle, Roman and Rolland, Chloe and Gustafson, Laura and Mintun, Eric and Pan, Junting and Alwala, Kalyan Vasudev and Carion, Nicolas and Wu, Chao-Yuan and Girshick, Ross and Doll{\'a}r, Piotr and Feichtenhofer, Christoph},
journal={arXiv preprint arXiv:2408.00714},
url={https://arxiv.org/abs/2408.00714},
year={2024}
}
sam2.1-tiny-encoder_fp16.onnx 67 MiBsam2.1-tiny-decoder_fp32.onnx 17 MiB