Export Meta AI's newest Segment Anything 3 (SAM-3) model to ONNX and then build a TensorRT engine you can deploy for real-time segmentation.
- Minimal, readable export script using Hugging Face
transformers'Sam3ModelandSam3Processor - Clean ONNX graph with instance masks (
pred_masks) and semantic mask outputs - TensorRT-ready: plug the produced ONNX into
trtexecor your own build pipeline - CPU-friendly export for maximum compatibility (flip
deviceto use GPU if you prefer)
-
Request access to the gated model
- Visit https://huggingface.co/facebook/sam3 and click “Access repository” (approval is required before downloads succeed). Make sure your
HF_TOKENhas that access.
- Visit https://huggingface.co/facebook/sam3 and click “Access repository” (approval is required before downloads succeed). Make sure your
-
Install dependencies
pip install torch transformers pillow requests
# Optional: If after a pip install the ONNX export script complains that it cannot find SAM3, it's just because SAM-3 is very new and a formal release of `transformers` has not added support yet. In this case, please install `transformers` using git
git clone https://github.com/huggingface/transformers.git
cd transformers
pip install '.[torch]'
# or uv pip install '.[torch]'Use a PyTorch wheel that matches your CUDA if you plan to export on GPU. By default a GPU is not required to export to ONNX.
- Export to ONNX
export HF_TOKEN=<YOUR TOKEN>
python onnxexport.pyThis downloads facebook/sam3, runs a sample prompt ("ear"), and writes onnx_weights/sam3_static.onnx plus associated weight shards. SAM-3 is ~3.2 GB, so the ONNX exporter will create external data files; if you build TensorRT on another machine you must copy the entire onnx_weights/ directory (not just the .onnx).
- Build a TensorRT engine
Use
trtexec(or your favorite builder) on the generated ONNX:
trtexec --onnx=onnx_weights/sam3_static.onnx --saveEngine=sam3_fp16.plan --fp16 --verbose # fp16
trtexec --onnx=onnx_weights/sam3_static.onnx --saveEngine=sam3_int8.plan --int8 --verbose # int8
trtexec --onnx=onnx_weights/sam3_static.onnx --saveEngine=sam3_fp8.plan --fp8 --verbose # fp8
trtexec --onnx=onnx_weights/sam3_static.onnx --saveEngine=sam3_int4.plan --int4 --verbose # int4- Validate (optional)
- Run
onnxruntimeononnx_weights/sam3_static.onnxto confirm outputs - Benchmark
sam3_fp16.planwithtrtexec --loadEngine=sam3_fp16.plan
- Change the prompt/image in
onnxexport.pyto better reflect your production use case. - Swap
deviceto"cuda"for GPU-side export if your environment supports it. - Add more outputs from
Sam3Model(e.g., scores) by extendingSam3ONNXWrapper.
onnxexport.py— single, documented script that pulls SAM-3, builds clean inputs, and exports ONNXLICENSE— MIT
If this saved you time, drop a ⭐ so others can find it and ship SAM-3 faster.