Skip to content

Latest commit

 

History

History
111 lines (80 loc) · 3.17 KB

File metadata and controls

111 lines (80 loc) · 3.17 KB

ONNX Export for Depth Anything V3

Exports DA3 models to ONNX for TensorRT / DeepStream deployment. Exported models go to ../models/ (gitignored).


Setup

conda create -n da3-export python=3.11
conda activate da3-export
pip install -r requirements.txt
pip install -e path/to/depth-anything-3

torch 2.6 is required — newer versions trigger torch.export which fails on DA3's data-dependent ops. Opset 17 forces the old TorchScript exporter.


Required patches (all models)

src/depth_anything_3/api.py — prevents fp16 Cast nodes in the ONNX graph:

# Before
autocast_dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
# After
autocast_dtype = torch.float32  # fp16 inserts Cast nodes causing mixed-type errors; TRT handles fp16 via network-mode=2

src/depth_anything_3/model/dinov2/layers/rope.py — required only for RoPE models (DA3-SMALL/BASE/LARGE):

# line ~56 — replace torch.cartesian_prod (no ONNX symbolic)
positions = torch.stack(
    torch.meshgrid(y_coords, x_coords, indexing="ij"), dim=-1
).reshape(-1, 2)

# line ~183 — replace int(positions.max()) (data-dependent shape)
max_position = positions.shape[1]  # n_tokens >= max(H/P, W/P); shape is static for ONNX

How to Export

Run from inside path/to/depth-anything-3 so depth_anything_3 is importable:

conda activate da3-export
cd path/to/depth-anything-3

python /path/to/depth_anything_v3_deepstream/export/export.py \
    --model-dir <model-id> \
    --height <H> --width <W> \
    --output-dir /path/to/depth_anything_v3_deepstream/models/ \
    --opset 17

Resolution constraints

Width must be divisible by 14 (DA3 patch size) and by 4 (DeepStream requirement). Height must be divisible by 14.

Models

Model Params Resolution Output
DA3METRIC-LARGE 334M 504×504 metric depth
DA3MONO-LARGE 334M 504×504 relative depth
DA3-SMALL 34M 504×504 relative depth
DA3-BASE 135M 504×504 relative depth
DA3-LARGE-1.1 411M 504×504 relative depth
DA3-GIANT-1.1 1.15B 504×504 relative depth

DA3NESTED-* (composite) are omitted — not single-model pipelines.

DA3METRIC-LARGE and DA3MONO-LARGE

python /path/to/depth_anything_v3_deepstream/export/export.py \
    --model-dir depth-anything/DA3METRIC-LARGE  # or DA3MONO-LARGE \
    --height 504 --width 504 \
    --output-dir /path/to/depth_anything_v3_deepstream/models/ \
    --opset 17
Input   image: [1, 3, 504, 504]  float32
Output  depth: [1, 1, 504, 504]  float32

Metric depth formula (DA3METRIC-LARGE only): depth_m = focal_px * raw_output / 300.0

DA3-SMALL, DA3-BASE, DA3-LARGE-1.1, DA3-GIANT-1.1

Apply the rope.py patches above, then:

python /path/to/depth_anything_v3_deepstream/export/export.py \
    --model-dir depth-anything/DA3-SMALL  # or DA3-BASE, DA3-LARGE-1.1, DA3-GIANT-1.1 \
    --height 504 --width 504 \
    --output-dir /path/to/depth_anything_v3_deepstream/models/ \
    --opset 17
Input   image: [1, 3, 504, 504]  float32
Output  depth: [1, 1, 504, 504]  float32  (relative depth, affine-invariant)