--- license: apache-2.0 pipeline_tag: mask-generation library_name: onnx tags: - onnx - moonbox - onnxruntime - efficientvit - efficientvit-sam - segment-anything - image-segmentation --- # EfficientViT-SAM ONNX ONNX builds of [EfficientViT-SAM](https://github.com/mit-han-lab/efficientvit), made by Moonbox for on-device use with ONNX Runtime (CPU, CUDA, DirectML). Each precision is one folder holding the graphs and a small Python `pipeline.py` that does the rest (image preprocessing, prompt coordinates, mask upsampling); what the graphs take and return is under *How to run*. ## Files The last column is the benchmark against the original PyTorch model (details under *How to run*): IoU of every predicted mask with the original's on the same pictures and prompts. | Precision | File | Size | sha256 | Benchmark | |---|---|---|---|---| | fp32 | [`fp32/decoder.onnx`](https://huggingface.co/moonbox-dev/efficientvit-ONNX/blob/main/fp32/decoder.onnx) | 0.3 MB | `72ff3fdc36d9` | mask IoU 0.9999 mean / 0.9991 worst | | fp32 | [`fp32/decoder.onnx.data`](https://huggingface.co/moonbox-dev/efficientvit-ONNX/blob/main/fp32/decoder.onnx.data) | 16.2 MB | `df2017f2c3fc` | mask IoU 0.9999 mean / 0.9991 worst | | fp32 | [`fp32/encoder.onnx`](https://huggingface.co/moonbox-dev/efficientvit-ONNX/blob/main/fp32/encoder.onnx) | 0.4 MB | `35d5b72c6f74` | mask IoU 0.9999 mean / 0.9991 worst | | fp32 | [`fp32/encoder.onnx.data`](https://huggingface.co/moonbox-dev/efficientvit-ONNX/blob/main/fp32/encoder.onnx.data) | 229.0 MB | `f5552293e854` | mask IoU 0.9999 mean / 0.9991 worst | | fp32 | [`fp32/pipeline.py`](https://huggingface.co/moonbox-dev/efficientvit-ONNX/blob/main/fp32/pipeline.py) | 4.2 kB | `3cee6dcd2a80` | mask IoU 0.9999 mean / 0.9991 worst | ## How to run EfficientViT-SAM L2 (MIT Han Lab: SAM's prompt encoder and mask decoder on an EfficientViT-L2 image encoder at 512 px, 61 M parameters) as two ONNX graphs plus `pipeline.py`, a numpy runtime that reproduces the repository's `EfficientViTSamPredictor` around them. - `encoder.onnx`: EfficientViT-L2 + the SAM neck: `image` [1, 3, 512, 512] (normalised, padded) -> `image_embeddings` [1, 256, 64, 64]. Run it once per image. - `decoder.onnx`: SAM's ONNX decoder wrapper (`SamOnnxModel`, all 4 masks; prompts and masks in the 1024 frame): `image_embeddings`, `point_coords` [1, N, 2] and `point_labels` [1, N] (1 / 0 points, 2 / 3 box corners, -1 padding), `mask_input` [1, 1, 256, 256], `has_mask_input` [1], `orig_im_size` [2] -> `masks` [1, 4, H, W] (logits at the image size), `iou_predictions` [1, 4], `low_res_masks` [1, 4, 256, 256]. `pipeline.py` resizes the longest side to 512 (Pillow bilinear, as the repository's SamResize), applies SAM's pixel mean / std and pads bottom-right to 512 x 512. It scales the prompt coordinates to the 1024 frame, and returns masks 1-3 for multimask point prompts and mask 0 for a box. It needs numpy, onnxruntime (or onnxruntime-gpu) and Pillow: ```python import sys, numpy as np from PIL import Image sys.path.insert(0, "fp32") import pipeline sam = pipeline.EfficientViTSAM("fp32", "cuda") emb, size = sam.embed(np.array(Image.open("photo.jpg").convert("RGB"))) masks, scores = sam.point(emb, size, [[410, 300]]) # 3 masks [3, H, W] (bool) box_mask, score = sam.box(emb, size, [120, 80, 700, 560]) ``` **Precisions.** Only `fp32/`: the model doesn't survive 16 bits. 30 conv + BatchNorm pairs (every MBConv point conv of stages 3-4, eight in the neck, the stem) have BatchNorm scales above 20, up to 602 (running variance 5.5e-7). Their outputs are differences of terms about 1000x larger than the result. Measured here: fp16 (decoder fp32) worst mask IoU 0.12 (mean 0.89); weight-only int8 mean 0.33. Neither is published. **Benchmark.** Eight photos (the first eight of the lab's DIS5K-250 set). Each gets a positive point (the ground-truth foreground pixel nearest its centroid; 3 masks) and the ground truth's bounding box (1 mask), so 32 masks per build. The original is the repository's EfficientViTSamPredictor in float32. The build gets the image it preprocesses and the same prompt coordinates (the conversion measured alone). Pass: IoU with the original's mask at least 0.95 on every mask. | | worst mask IoU | mean mask IoU | |---|---|---| | `fp32/` | 0.9991 | 0.9999 | Graphs checked against the original modules (largest absolute difference, float32): encoder 7.0e-5 of 0.58, decoder 2.3e-5 of 14.8 (masks). ## How these were made From [EfficientViT-SAM](https://github.com/mit-han-lab/efficientvit) at revision `de7d7733cc`. The graphs were exported from the original PyTorch modules with torch.onnx (opset 17 / 18) and checked against them. Weight-only quantization: int8 is a per-output-channel int8 weight behind DequantizeLinear, int4 is ONNX Runtime's MatMulNBits (block 32). Each card above says which precision every graph uses. Every precision was benchmarked against the original before upload (pass: IoU at least 0.95 on every mask, otherwise marked below threshold), and the `sha256` column above is what the files must hash to. ## License apache-2.0. These files are a derivative of the original model and carry its license: it is copied in this repository, and it governs your use of these files. The original model card follows unchanged. --- # Original model card # Efficient Vision Foundation Models for High-Resolution Generation and Perception [](https://paperswithcode.com/sota/image-generation-on-imagenet-512x512?p=deep-compression-autoencoder-for-efficient) ## News - (🔥 New) [2025/09/05] We will no longer maintain this codebase. All future updates and announcements will be made on [DC-Gen](https://github.com/dc-ai-projects/DC-Gen). - (🔥 New) [2025/01/24] We released DC-AE-SANA-1.1: [doc](https://github.com/mit-han-lab/efficientvit/blob/master/assets/docs/dc_ae_sana_1.1.md). - (🔥 New) [2025/01/23] DC-AE and SANA are accepted by ICLR 2025. - (🔥 New) [2025/01/14] We released **DC-AE+USiT models**: [model](https://huggingface.co/collections/mit-han-lab/dc-ae-diffusion-670dbb8d6b6914cf24c1a49d), [training](https://github.com/mit-han-lab/efficientvit/blob/master/applications/dc_ae/README.md#dc-ae--usit). Using the default training settings and sampling strategy, DC-AE+USiT-2B achieves 1.72 FID on ImageNet 512x512, surpassing the SOTA diffusion model EDM2-XXL and SOTA auto-regressive image generative models (MAGVIT-v2 and MAR-L). ______________________________________________________________________ - (🔥 New) [2024/12/24] **diffusers** supports DC-AE models. All [DC-AE models in diffusers safetensors](https://huggingface.co/collections/mit-han-lab/dc-ae-670085b9400ad7197bb1009b) are released. [Usage](https://github.com/mit-han-lab/efficientvit/tree/master/applications/dc_ae#deep-compression-autoencoder-diffusers). - [2024/10/21] DC-AE and EfficientViT block are used in our latest text-to-image diffusion model SANA! Check the [project page](https://nvlabs.github.io/Sana/) for more details. - [2024/10/15] We released **Deep Compression Autoencoder (DC-AE)**: [link](#deep-compression-autoencoder-for-efficient-high-resolution-diffusion-models-paper-readme)! - [2024/07/10] EfficientViT is used as the backbone in [Grounding DINO 1.5 Edge](https://arxiv.org/pdf/2405.10300) for efficient open-set object detection. - [2024/07/10] EfficientViT-SAM is used in [MedficientSAM](https://github.com/hieplpvip/medficientsam), the 1st place model in [CVPR 2024 Segment Anything In Medical Images On Laptop Challenge](https://www.codabench.org/competitions/1847/). - [2024/04/06] EfficientViT-SAM is accepted by [eLVM@CVPR'24](https://sites.google.com/view/elvm/home?authuser=0). - [2024/03/19] Online demo of EfficientViT-SAM is available: [https://evitsam.hanlab.ai/](https://evitsam.hanlab.ai/). - [2024/02/07] We released [EfficientViT-SAM](https://arxiv.org/abs/2402.05008), the first accelerated SAM model that matches/outperforms SAM-ViT-H's zero-shot performance, delivering the SOTA performance-efficiency trade-off. - [2023/11/20] EfficientViT is available in the [NVIDIA Jetson Generative AI Lab](https://www.jetson-ai-lab.com/tutorial_efficientvit.html). - [2023/09/12] EfficientViT is highlighted by [MIT home page](https://www.mit.edu/archive/spotlight/efficient-computer-vision/) and [MIT News](https://news.mit.edu/2023/ai-model-high-resolution-computer-vision-0912). - [2023/07/18] EfficientViT is accepted by ICCV 2023. ## Content ### [ICLR 2025] Deep Compression Autoencoder for Efficient High-Resolution Diffusion Models [[paper](https://arxiv.org/abs/2410.10733)] [[readme](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md)] [[poster](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/assets/dc_ae_poster.pdf)] **Deep Compression Autoencoder (DC-AE) is a new family of high-spatial compression autoencoders with a spatial compression ratio of up to 128 while maintaining reconstruction quality. It accelerates all latent diffusion models regardless of the diffusion model architecture.** #### Demo 
Figure 1: We address the reconstruction accuracy drop of high spatial-compression autoencoders.
Figure 2: DC-AE speeds up latent diffusion models.
Figure 3: DC-AE enables efficient text-to-image generation on the laptop: SANA.
- [Usage of Deep Compression Autoencoder](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md#deep-compression-autoencoder) - [Usage of DC-AE-Diffusion](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md#efficient-diffusion-models-with-dc-ae) - [Evaluate Deep Compression Autoencoder](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md#evaluate-deep-compression-autoencoder) - [Demo DC-AE-Diffusion Models](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md#demo-dc-ae-diffusion-models) - [Evaluate DC-AE-Diffusion Models](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md#evaluate-dc-ae-diffusion-models) - [Train DC-AE-Diffusion Models](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md#train-dc-ae-diffusion-models) - [Reference](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/dc_ae/README.md#reference) ### [CVPR 2024 eLVM Workshop] EfficientViT-SAM: Accelerated Segment Anything Model Without Accuracy Loss [[paper](https://arxiv.org/abs/2402.05008)] [[online demo](https://evitsam.hanlab.ai/)] [[readme](https://raw.githubusercontent.com/mit-han-lab/efficientvit/de7d7733cc0329f391b33f1f459271562ec27bd5/applications/efficientvit_sam/README.md)] **EfficientViT-SAM is a new family of accelerated segment anything models by replacing SAM's heavy image encoder with EfficientViT. It delivers a 48.9x measured TensorRT speedup on A100 GPU over SAM-ViT-H without sacrificing accuracy.**