Run this model with HF Jobs
Run SharpAI/sam2-hiera-tiny-onnx on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/SharpAI/sam2-hiera-tiny-onnx. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/SharpAI/sam2-hiera-tiny-onnx on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use SharpAI/sam2-hiera-tiny-onnx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use SharpAI/sam2-hiera-tiny-onnx with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained("SharpAI/sam2-hiera-tiny-onnx") 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("SharpAI/sam2-hiera-tiny-onnx") 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
Upload sam2-hiera-tiny ONNX models
Browse filesFiles changed (6)
- .gitattributes+1-34
- README.mdAdded+175
- config.jsonAdded+13
- decoder.onnxAdded+3
- encoder.onnxAdded+3
- encoder.with_runtime_opt.ortAdded+3
1234567891011121314151617181920212223242526272829303132333435*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text12*.onnx filter=lfs diff=lfs merge=lfs -text*.ort filter=lfs diff=lfs merge=lfs -text
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175---license: apache-2.0tags: - sam2 - segment-anything - onnx - webgpu - computer-vision - image-segmentationlibrary_name: onnxruntime---
# SAM2-HIERA-TINY - ONNX Format for WebGPU
**Powered by [Segment Anything 2 (SAM2)](https://github.com/facebookresearch/segment-anything-2) from Meta Research**
This repository contains ONNX-converted models from [facebook/sam2-hiera-tiny](https://huggingface.co/facebook/sam2-hiera-tiny), optimized for WebGPU deployment in browsers.
## Model Information
- **Original Model**: [facebook/sam2-hiera-tiny](https://huggingface.co/facebook/sam2-hiera-tiny)- **Version**: SAM 2.0- **Size**: 38.9M parameters- **Description**: Tiny variant - fastest inference, good for real-time applications- **Format**: ONNX (encoder + decoder)- **Optimization**: Encoder optimized to .ort format for WebGPU
## Files
- `encoder.onnx` - Image encoder (ONNX format)- `encoder.with_runtime_opt.ort` - Image encoder (optimized for WebGPU)- `decoder.onnx` - Mask decoder (ONNX format)- `config.json` - Model configuration
## Usage
### In Browser with ONNX Runtime Web
```javascriptimport * as ort from 'onnxruntime-web/webgpu';
// Load encoder (use optimized .ort version for WebGPU)const encoderURL = 'https://huggingface.co/SharpAI/sam2-hiera-tiny-onnx/resolve/main/encoder.with_runtime_opt.ort';const encoderSession = await ort.InferenceSession.create(encoderURL, { executionProviders: ['webgpu'], graphOptimizationLevel: 'disabled'});
// Load decoderconst decoderURL = 'https://huggingface.co/SharpAI/sam2-hiera-tiny-onnx/resolve/main/decoder.onnx';const decoderSession = await ort.InferenceSession.create(decoderURL, { executionProviders: ['webgpu']});
// Run encoderconst imageData = preprocessImage(image); // Your preprocessingconst encoderOutputs = await encoderSession.run({ image: imageData });
// Run decoder with pointconst point_coords = new ort.Tensor('float32', [x, y, 0, 0], [1, 2, 2]);const point_labels = new ort.Tensor('float32', [1, -1], [1, 2]);const mask_input = new ort.Tensor('float32', new Float32Array(256 * 256).fill(0), [1, 1, 256, 256]);const has_mask_input = new ort.Tensor('float32', [0], [1]);
const decoderOutputs = await decoderSession.run({ image_embed: encoderOutputs.image_embed, high_res_feats_0: encoderOutputs.high_res_feats_0, high_res_feats_1: encoderOutputs.high_res_feats_1, point_coords: point_coords, point_labels: point_labels, mask_input: mask_input, has_mask_input: has_mask_input});
// Get masksconst masks = decoderOutputs.masks; // Shape: [1, num_masks, 256, 256]```
### In Python with ONNX Runtime
```pythonimport onnxruntime as ortimport numpy as np
# Load modelsencoder_session = ort.InferenceSession("encoder.onnx")decoder_session = ort.InferenceSession("decoder.onnx")
# Run encoderencoder_outputs = encoder_session.run(None, {"image": image_tensor})
# Run decoderdecoder_outputs = decoder_session.run(None, { "image_embed": encoder_outputs[0], "high_res_feats_0": encoder_outputs[1], "high_res_feats_1": encoder_outputs[2], "point_coords": point_coords, "point_labels": point_labels, "mask_input": mask_input, "has_mask_input": has_mask_input})
masks = decoder_outputs[0]```
## Input/Output Specifications
### Encoder
**Input:**- `image`: Float32[1, 3, 1024, 1024] - Normalized RGB image
**Outputs:**- `image_embed`: Float32[1, 256, 64, 64] - Image embeddings- `high_res_feats_0`: Float32[1, 32, 256, 256] - High-res features (level 0)- `high_res_feats_1`: Float32[1, 64, 128, 128] - High-res features (level 1)
### Decoder
**Inputs:**- `image_embed`: Float32[1, 256, 64, 64] - From encoder- `high_res_feats_0`: Float32[1, 32, 256, 256] - From encoder- `high_res_feats_1`: Float32[1, 64, 128, 128] - From encoder- `point_coords`: Float32[1, 2, 2] - Point coordinates [[x, y], [0, 0]]- `point_labels`: Float32[1, 2] - Point labels [1, -1] (1=foreground, -1=padding)- `mask_input`: Float32[1, 1, 256, 256] - Previous mask (zeros if none)- `has_mask_input`: Float32[1] - Flag [0] or [1]
**Outputs:**- `masks`: Float32[1, 3, 256, 256] - Generated masks (3 candidates)- `iou_predictions`: Float32[1, 3] - IoU scores for each mask- `low_res_masks`: Float32[1, 3, 256, 256] - Low-resolution masks
## Browser Requirements
- Chrome 113+ with WebGPU enabled (`chrome://flags/#enable-unsafe-webgpu`)- Firefox Nightly with WebGPU enabled- Safari Technology Preview with WebGPU enabled
## Performance
Typical inference times on Chrome with WebGPU:- **Encoder**: {'2-3s' if 'tiny' in model_name else '3-5s' if 'small' in model_name else '4-6s' if 'base' in model_name else '8-10s'}- **Decoder**: 0.1-0.5s per point
## License
This model is released under the Apache 2.0 license, following the original SAM2 model.
## Citation
```bibtex@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}, year={2024}}```
## Related Resources
- **Original SAM2**: [facebookresearch/segment-anything-2](https://github.com/facebookresearch/segment-anything-2)- **WebGPU Demo**: [Aegis AI SAM2 WebGPU Demo](https://github.com/yourusername/Aegis-AI/tree/main/tools/sam2-webgpu)- **Conversion Tool**: [SAM2 ONNX Converter](https://github.com/yourusername/Aegis-AI/tree/main/tools/sam2-converter)
## Acknowledgments
- **Meta Research** for the original SAM2 model- **Microsoft** for ONNX Runtime- **SamExporter** for conversion tools
---
*Converted and optimized by [Aegis AI](https://github.com/yourusername/Aegis-AI)*12345678910111213{ "model_name": "sam2-hiera-tiny", "checkpoint_id": "facebook/sam2-hiera-tiny", "checkpoint_path": "/Users/simba/.cache/huggingface/hub/models--facebook--sam2-hiera-tiny/snapshots/7c218beaf0bb87874785f32b582f640134fc1c09/sam2_hiera_tiny.pt", "version": "2.0", "size": "38.9M", "encoder_path": "encoder.onnx", "encoder_optimized_path": "encoder.with_runtime_opt.ort", "decoder_path": "decoder.onnx", "image_size": 1024, "mask_size": 256, "conversion_date": "1763352682.598863"}123version https://git-lfs.github.com/spec/v1oid sha256:63198f1f1e273d8f2f4a9d1baf926e53a01d78dc50e0674640e1513dc00d9927size 20639854123version https://git-lfs.github.com/spec/v1oid sha256:df265cb552475e1b3a6cb57c939e57c95ed849bfc2f985c06efab85d8bca6db9size 134261315123version https://git-lfs.github.com/spec/v1oid sha256:45e4b9607e78ad1a83661153340025b5ca2c49ef7ab3cce53f72ea48fd91c6desize 134451280.gitattributes+1-34
README.mdAdded+175
config.jsonAdded+13
decoder.onnxAdded+3
encoder.onnxAdded+3
encoder.with_runtime_opt.ortAdded+3