Depth Anything V3 (DA3) 是字节跳动 Seed 团队发布的深度估计基础模型,采用朴素的 Vision Transformer (ViT) 骨干网络搭配 Dual-DPT 头部,可从单张或多视角图像中预测空间一致的几何信息,同时输出逐像素深度图、深度置信度以及相机内外参数,在单目深度估计和多视角几何任务上均大幅超越前代方法。
论文
Depth Anything 3: Recovering the Visual Space from Any Views
ByteDance Seed
参考实现:
url= https://github.com/ByteDance-Seed/Depth-Anything-3
commit id = 3d835ec1a5802d64a8b8b15f817a1ab54809bfe4输入数据
| 输入数据 | 数据类型 | 大小 | 数据排布格式 |
|---|---|---|---|
| input | RGB_FP32 | batchsize x 3 x 518 x 518 | NCHW |
输出数据
| 输出数据 | 数据类型 | 大小 | 数据排布格式 |
|---|---|---|---|
| depth | FLOAT32 | (batchsize, 1, 518, 518) | NCHW,逐像素深度值(exp 激活,正值)。 |
| depth_conf | FLOAT32 | (batchsize, 1, 518, 518) | NCHW,逐像素深度置信度(exp+1,取值 [1, +∞))。 |
| extrinsics | FLOAT32 | (batchsize, 1, 3, 4) | 行优先矩阵,相机外参(world-to-camera 的旋转平移矩阵)。 |
| intrinsics | FLOAT32 | (batchsize, 1, 3, 3) | 行优先矩阵,相机内参矩阵(fx、fy、cx、cy,单位:像素)。 |
| 配置项 | 配置/版本 |
|---|---|
| NPU | Atlas 800I A2 * 1 |
| 驱动固件 | 25.5.2 |
| 容器镜像 | swr.cn-south-1.myhuaweicloud.com/ascendhub/torch-onnx-inference:cann8.3.rc1_torch2.1.0-800I-A2-openeuler24.03-py3.11-aarch64 |
表 1 版本配套表
docker run -it -d --net=host --shm-size=1g \
--name depthanythingv3 \
--device=/dev/davinci_manager:rwm \
--device=/dev/hisi_hdc:rwm \
--device=/dev/devmm_svm:rwm \
--device=/dev/davinci0:rwm \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver:ro \
-v /usr/local/Ascend/firmware/:/usr/local/Ascend/firmware:ro \
-v /usr/local/sbin:/usr/local/sbin:ro \
-v /path-to-weights:/path-to-weights:ro \
-v /home:/home \
swr.cn-south-1.myhuaweicloud.com/ascendhub/torch-onnx-inference:cann8.3.rc1_torch2.1.0-800I-A2-openeuler24.03-py3.11-aarch64 bash获取Depth-Anything-3源码
git clone https://github.com/ByteDance-Seed/Depth-Anything-3.git
cd Depth-Anything-3
git checkout 3d835ec1a5802d64a8b8b15f817a1ab54809bfe4
将本仓源码拷贝到当前目录并打上补丁
# 打上onnx/om推理适配补丁
git apply depth_anything_v3.patch安装依赖
pip install -r requirements.txt 获取原始数据集
模型推理只需任意 RGB 图片(assets/ 目录下自带示例图)。
精度与性能评测使用 HiRoom 室内场景数据集,数据集存放在 workspace/benchmark_dataset 文件夹中
下载地址:
https://www.modelscope.cn/datasets/depth-anything/DA3-BENCH
目录结构
./workspace/benchmark_dataset/hiroom/
├── data
│ ├── 20241230
│ │ ├── 828763
│ │ │ ├── cam_sampled_12
│ │ │ │ ├── image
│ │ │ │ │ ├── xx.jpg
│ │ ├── 828786
│ │ │ ├── cam_sampled_10
│ │ │ │ ├── image
│ │ │ │ │ ├── xx.jpg
├── fused_pcd模型转换
使用PyTorch将模型权重文件.safetensors 转换为.onnx文件,修补后使用ATC工具将.onnx文件转为离线推理模型文件.om文件。
获取权重文件
从 HuggingFace 仓库 depth-anything/DA3-SMALL 获取权重文件(国内可用 hf-mirror.com 镜像下载),放入如下目录
./weights/
├── config.json
└── model.safetensors
导出onnx文件
在代码主目录使用export_onnx.py导出onnx文件
python export_onnx.py --weights ./weights/model.safetensors --config ./weights/config.json --height 518 --width 518| 参数 | 说明 |
|---|---|
| weights | pytorch权重文件 |
| config | 模型配置文件 |
| height/width | 输入高宽(须为14的整数倍) |
当前目录下生成depth_anything_v3_da3-small_n518x518.onnx文件
修补onnx文件(关键步骤)
原始导出的 ONNX 中 /head/Gather_67 为 [1,518,518,2] 张量最后一轴上的标量索引 Gather,ATC 会将其下沉为 te_gatherv2 kernel,在 NPU的 AI Vector Core 上触发硬件异常(aivec error exception, retcode 0x7bc83),导致 OM 推理整体失败(acl 返回 507011)。使用 fix_onnx_gather.py 将其替换为数值完全等价的 Slice + Squeeze:
python fix_onnx_gather.py --input depth_anything_v3_da3-small_n518x518.onnx --output depth_anything_v3_da3-small_n518x518_fix.onnx使用ATC工具将ONNX模型转OM模型
执行ATC命令將onnx模型转换为om模型
atc --model='./depth_anything_v3_da3-small_n518x518_fix.onnx' --framework=5 --output='depth_anything_v3_da3-small_n518x518_fix' --input_shape='input:1,3,518,518' --soc_version=Ascend{芯片型号} --precision_mode=allow_fp32_to_fp16
执行成功后,将在当前目录生成 depth_anything_v3_da3-small_n518x518_fix.om
整体目录结构
Depth-Anything-3/
├──src
├──export_onnx.py # pth转onnx脚本
├──fix_onnx_gather.py # onnx修补脚本(Gather→Slice+Squeeze)
├──infer_torch.py # pytorch推理脚本
├──infer_onnx.py # onnx推理脚本
├──infer_om.py # om推理脚本
├──compare_torch_onnx.py # pytorch vs onnx 精度对比脚本
├──compare_onnx_om.py # onnx vs om 精度性能对比脚本
├──depth_anything_v3_da3-small_n518x518.onnx
├──depth_anything_v3_da3-small_n518x518_fix.onnx
├──depth_anything_v3_da3-small_n518x518_fix.om
├──workspace/benchmark_dataset/hiroom/
├──weights/
├── config.json
└── model.safetensors
开始推理验证
执行om推理
python infer_om.py --image ./assets/images/da3_radar.png --output da3_radar_om.png --om depth_anything_v3_da3-small_n518x518_fix.om --device 0参数说明:
| 参数 | 说明 |
|---|---|
| image | 输入图片路径 |
| output | 深度图输出路径 |
| om | om 权重文件 |
| device | NPU 设备号 |
执行成功后生成 depth_om.png 深度图,并打印 depth/depth_conf/extrinsics/intrinsics 四个输出的 shape、数值范围及单帧时延。
在 HiRoom 数据集(518x518 输入)上的精度/性能评测,分別评测pytorch(cpu)、onnx(cpu)、om(npu)上的精度和性能。精度指标为各后端输出与 PyTorch(CPU) 基准输出之间的一致性误差(MAE / 相对MAE)。
# 批量精度/性能对比
python compare_torch_onnx.py --num_images 12
python compare_onnx_om.py --num_images 12评估结果:
| 指标 | Pytorch(CPU) | ONNX (CPU) | OM (Ascend NPU) |
|---|---|---|---|
| depth MAE | 基准 | 2.1e-7 | 2.4e-4 |
| depth_conf 相对MAE | 基准 | <1e-5 | 5.8e-3 |
| extrinsics MAE | 基准 | <1e-5 | 4.9e-5 |
| intrinsics 相对MAE | 基准 | <1e-5 | 5.3e-4 |
| 推理性能 | 39.37 s/it | 0.805 s/it | 0.014 s/it |
说明: 精度指标为与 PyTorch(CPU) 输出的一致性误差;推理性能为单张 518x518 图片的平均时延(OM 为预热后 20 次均值)。OM(Ascend Atlas 800I A2) 相对 ONNX(CPU) 加速约 58 倍。