ONNX-compatible implementation of Depth Anything 3 for efficient depth estimation inference.
This repository provides scripts to export Depth Anything 3 models to ONNX format and run inference using ONNXRuntime, enabling deployment on a wide range of platforms with minimal dependencies.
- ONNX Export: Convert Depth Anything 3 PyTorch models to ONNX format
- Minimal Dependencies: Inference requires only ONNXRuntime, OpenCV, and NumPy
- Multiple Model Sizes: Support for DA3-Small, DA3-Base, DA3-Large models
- Dynamic Input Support: Flexible input resolution handling
- Easy Integration: Simple API for depth estimation in your projects
ONNX models provide significant performance benefits:
- Reduced Memory: No PyTorch runtime overhead
- Cross-Platform: Runs on CPU, CUDA, TensorRT, DirectML, Coreml, etc.
- Optimized: ONNXRuntime automatically optimizes the graph
- Depth Anything 3 ONNX Performance Report
- Model:
depth-anything/DA3-SMALL - Platform: macOS M4 (CPU)
- Runtime: ONNX Runtime with CPUExecutionProvider
- Model:
| Resolution | Model Size | Mean Time | Std Dev | Min | Max | Median | P95 | P99 | FPS |
|---|---|---|---|---|---|---|---|---|---|
| 224x224 | 96.1 MB | 92.44ms | 2.28ms | 88.89ms | 96.30ms | 92.49ms | 96.11ms | 96.26ms | 10.82 |
| 392x392 | 96.1 MB | 279.67ms | 8.04ms | 271.08ms | 298.78ms | 278.13ms | 298.48ms | 298.72ms | 3.58 |
| 504x504 | 96.1 MB | 506.84ms | 14.97ms | 490.04ms | 545.88ms | 504.01ms | 541.25ms | 544.95ms | 1.97 |
| 672x672 | 96.1 MB | 1101.54ms | 15.72ms | 1078.86ms | 1131.54ms | 1097.70ms | 1121.84ms | 1129.60ms | 0.91 |
| 840x840 | 96.1 MB | 2206.96ms | 63.76ms | 2115.01ms | 2365.68ms | 2209.16ms | 2298.87ms | 2352.32ms | 0.45 |
pip install -r requirements.txtIf you only need to run inference with pre-exported ONNX models:
pip install onnxruntime opencv-python pillow imageio numpy matplotlibTo export PyTorch models to ONNX, you'll need PyTorch:
pip install torch>=2.0.0 torchvision
pip install -r requirements.txtExport a Depth Anything 3 model from HuggingFace to ONNX format:
python export_onnx.py \
--model depth-anything/DA3-SMALL \
--process-res 504 \
--output DA3-SMALL-504.onnxAvailable Models:
depth-anything/DA3-SMALL(80M params)depth-anything/DA3-BASE(120M params)depth-anything/DA3-LARGE(350M params)
Note: The first run will download the model from HuggingFace. Models are cached in ~/.cache/huggingface/.
Run depth estimation on an image using the exported ONNX model:
python run_onnx.py \
--image assets/examples/input.jpg \
--onnx DA3-SMALL-504.onnx \
--process-res 504 \
--export-dir ./output \
--visualizeArguments:
--image: Path to input image--onnx: Path to ONNX model file--process-res: Processing resolution (default: 504)--export-dir: Directory to save depth visualization--visualize: Display visualization with matplotlib
import cv2
import numpy as np
import onnxruntime as ort
from PIL import Image
# Load ONNX model
sess = ort.InferenceSession("DA3-SMALL-504.onnx")
# Load and preprocess image
image = cv2.imread("input.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (504, 504))
image = image.transpose(2, 0, 1).astype(np.float32) / 255.0
image = np.expand_dims(image, axis=0)
# Run inference
depth = sess.run(None, {"image": image})[0]
# Depth shape: (1, 1, 504, 504)
print(f"Depth map shape: {depth.shape}")The export script creates ONNX models with:
- Input:
imagetensor of shape(batch, 3, height, width), dtypefloat32, range[0, 1] - Output:
depthtensor of shape(batch, 1, height, width), dtypefloat32 - Dynamic Axes: Batch size, height, and width are dynamic
- ONNX Opset: 17 (configurable via
--opset)
python export_onnx.py --helpKey Parameters:
--model: HuggingFace model ID or local path--process-res: Fixed square resolution for export (e.g., 504, 672)--output: Output ONNX file path--opset: ONNX opset version (default: 17)
The inference script handles:
- Image loading and preprocessing
- Fixed-square resizing to match export resolution
- ONNX model inference
- Depth map post-processing and visualization
- Resizing output back to original image resolution
python run_onnx.py --helpimport onnxruntime as ort
# For CUDA GPU
sess = ort.InferenceSession("model.onnx", providers=["CUDAExecutionProvider"])
# For CPU
sess = ort.InferenceSession("model.onnx", providers=["CPUExecutionProvider"])
# For TensorRT (fastest on NVIDIA GPUs)
sess = ort.InferenceSession("model.onnx", providers=["TensorrtExecutionProvider"])Depth-Anything-3-Onnx/
├── src/
│ └── depth_anything_3/ # Source code from Depth Anything 3
├── assets/
│ └── examples/ # Sample images
├── export_onnx.py # Export PyTorch to ONNX
├── run_onnx.py # Run inference with ONNX
├── requirements.txt # Python dependencies
├── LICENSE # Apache 2.0 License
└── README.md # This file
Make sure you're running scripts from the project root directory where the src/ folder is located.
If you experience network issues downloading from HuggingFace:
export HF_ENDPOINT=https://hf-mirror.comTry using a smaller model (DA3-SMALL) or enabling optimizations:
sess_options = ort.SessionOptions()
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
sess = ort.InferenceSession("model.onnx", sess_options=sess_options)If you use Depth Anything 3 in your research, please cite:
@article{depthanything3,
title={Depth Anything 3: Recovering the visual space from any views},
author={Haotong Lin and Sili Chen and Jun Hao Liew and Donny Y. Chen and Zhenyu Li and Guang Shi and Jiashi Feng and Bingyi Kang},
journal={arXiv preprint arXiv:2511.10647},
year={2025}
}- Depth Anything 3: Original Repository
- Depth Anything ONNX: Reference Implementation for Depth Anything V1/V2
- Thanks to the Depth Anything team for their amazing work on monocular depth estimation
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
The Depth Anything 3 models have varying licenses:
- Apache 2.0: DA3-SMALL, DA3-BASE, DA3METRIC-LARGE, DA3MONO-LARGE
- CC BY-NC 4.0: DA3-GIANT, DA3-LARGE, DA3NESTED-GIANT-LARGE
Please refer to the official Depth Anything 3 repository for model-specific license information.
- Depth Anything 3 - Official PyTorch implementation
- Depth Anything V2 - Previous version
- Depth Anything ONNX - ONNX implementation for V1/V2
Contributions are welcome! Please feel free to submit issues or pull requests.