Ascend-SACT/DepthAnythingV3
模型介绍
文件和版本
Pull Requests
讨论
分析

DepthAnythingV3-昇腾离线推理指导

  • 概述

    • 输入输出数据
  • 推理环境准备

  • 快速上手

    • 创建容器
    • 获取源码
    • 准备数据集
    • 模型推理
  • 模型推理性能&精度


概述

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

输入输出数据

  • 输入数据

    输入数据数据类型大小数据排布格式
    inputRGB_FP32batchsize x 3 x 518 x 518NCHW
  • 输出数据

    输出数据数据类型大小数据排布格式
    depthFLOAT32(batchsize, 1, 518, 518)NCHW,逐像素深度值(exp 激活,正值)。
    depth_confFLOAT32(batchsize, 1, 518, 518)NCHW,逐像素深度置信度(exp+1,取值 [1, +∞))。
    extrinsicsFLOAT32(batchsize, 1, 3, 4)行优先矩阵,相机外参(world-to-camera 的旋转平移矩阵)。
    intrinsicsFLOAT32(batchsize, 1, 3, 3)行优先矩阵,相机内参矩阵(fx、fy、cx、cy,单位:像素)。

推理环境准备

1. 环境配置

配置项配置/版本
NPUAtlas 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 版本配套表

快速上手

创建容器

  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

获取源码

  1. 获取Depth-Anything-3源码

    git clone https://github.com/ByteDance-Seed/Depth-Anything-3.git
    cd Depth-Anything-3
    git checkout 3d835ec1a5802d64a8b8b15f817a1ab54809bfe4
    
  2. 将本仓源码拷贝到当前目录并打上补丁

    # 打上onnx/om推理适配补丁
    git apply depth_anything_v3.patch
  3. 安装依赖

    pip install -r requirements.txt 

准备数据集

  1. 获取原始数据集

    模型推理只需任意 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

模型推理

  1. 模型转换

    使用PyTorch将模型权重文件.safetensors 转换为.onnx文件,修补后使用ATC工具将.onnx文件转为离线推理模型文件.om文件。

    1. 获取权重文件

      从 HuggingFace 仓库 depth-anything/DA3-SMALL 获取权重文件(国内可用 hf-mirror.com 镜像下载),放入如下目录

      ./weights/
      ├── config.json
      └── model.safetensors
      
    2. 导出onnx文件

      在代码主目录使用export_onnx.py导出onnx文件

      python export_onnx.py --weights ./weights/model.safetensors --config ./weights/config.json --height 518 --width 518
      参数说明
      weightspytorch权重文件
      config模型配置文件
      height/width输入高宽(须为14的整数倍)

      当前目录下生成depth_anything_v3_da3-small_n518x518.onnx文件

    3. 修补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
    4. 使用ATC工具将ONNX模型转OM模型

      1. 执行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

  2. 整体目录结构

    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
    
  3. 开始推理验证

    1. 执行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深度图输出路径
      omom 权重文件
      deviceNPU 设备号

      执行成功后生成 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-72.4e-4
depth_conf 相对MAE基准<1e-55.8e-3
extrinsics MAE基准<1e-54.9e-5
intrinsics 相对MAE基准<1e-55.3e-4
推理性能39.37 s/it0.805 s/it0.014 s/it

说明: 精度指标为与 PyTorch(CPU) 输出的一致性误差;推理性能为单张 518x518 图片的平均时延(OM 为预热后 20 次均值)。OM(Ascend Atlas 800I A2) 相对 ONNX(CPU) 加速约 58 倍。