w
gcw_uQ09W7jl/depth-anything-DA3METRIC-LARGE-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

depth-anything/DA3METRIC-LARGE on Ascend NPU

#NPU

1. 模型简介

原始模型: depth-anything/DA3METRIC-LARGE — Depth Anything 3 Metric Large,单目度量深度估计,理想于真实尺度恢复。模型卡显示 0.35B 参数(实际 334M),Apache-2.0 许可。

  • 架构: DepthAnything3Net — ViT-L Backbone (vitl, dim_in=1024, out_layers [4,11,17,23]) + DPT Head (features 256, out_channels [256,512,1024,1024], output_dim=1)。配置文件位于 src/depth_anything_3/configs/da3metric-large.yaml,权重 model.safetensors 1.25 GiB。
  • 能力: Metric Depth + Relative Depth + Sky Segmentation + Camera Pose (ray/decoder)。单视图输入时输出 (1, H, W) 度量深度、sky 置信度及相机参数。
  • 输入: RGB 图像列表 (PIL / numpy / 路径),预处理通过 InputProcessor 以 upper_bound_resize 将短边缩放至 process_res=504 并 ImageNet 归一化。
  • 输出: Prediction.depth 形状 (N, H, W) float32,conf、extrinsics/intrinsics 在单视图下为 None(多视图时输出姿态)。
  • 特殊处理: 本适配禁用了 DepthAnything3.forward 内部的 torch.autocast(在当前 CPU/NPU 环境下 autocast(cpu, bfloat16) 会 hanging,禁用后 CPU 25s / NPU 63ms 且精度一致),并对 depth_anything_3.utils.export 中可选依赖 (moviepy, pycolmap, trimesh, gsplat) 做 try 容错,保证纯推理链路不依赖重型可视化库。

本仓库: 昇腾 Ascend910B 单卡 npu:0 真实推理适配,权重通过 huggingface_hub 从本地缓存加载,不重新下载。

2. 验证环境

  • NPU: Ascend910_9362, 2 devices, tested on npu:0 (npu-smi 25.5.5)
  • CANN: 8.5.1
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • Model: depth-anything/DA3METRIC-LARGE snapshot 4010e39f3634a45bc60553321fb49fb760bd594e, weight 1.3GB safetensors
  • Device Check: torch.npu.is_available()==True, torch.npu.get_device_name(0)=="Ascend910_9362", npu-smi info 可用

3. 安装依赖

pip install -r requirements.txt
# 需已安装 CANN 8.5.1 且 torch_npu 与 torch 版本匹配
# 额外依赖 evo/mpl/trimesh 仅用于可选导出,纯推理可不装但已列入 requirements 保证 import 容错

权重通过 DepthAnything3.from_pretrained("depth-anything/DA3METRIC-LARGE") 自动从 HF 镜像 (HF_ENDPOINT=https://hf-mirror.com) 缓存加载,首次运行约 17s 加载 334M 参数。

4. NPU 推理

默认命令(单张 504x504 梯度+噪声确定性图像)在 npu:0 上执行:

python inference.py

验证一致性与性能:

python inference.py --mode validate   # CPU vs NPU + benchmark
python inference.py --mode benchmark  # 仅性能

5. 真实推理结果

固定输入: seed=42 合成图像 504x504(梯度背景 + 中央方块,PIL),process_res=504。

默认 NPU 推理 (python inference.py on npu:0, FP32, 无 autocast):

[OUTPUT] depth shape: (1, 504, 504) dtype: float32
[OUTPUT] depth min: 1.290164
[OUTPUT] depth max: 1.545251
[OUTPUT] depth mean: 1.519255
[OUTPUT] depth std: 0.031102
[OUTPUT] conf shape: None
[OUTPUT] extrinsics shape: None
[OUTPUT] intrinsics shape: None
[OUTPUT] latency: 367.42 ms (首轮含编译) / 稳定 63ms
[OUTPUT] finite: True
[DEVICE] first_param device: npu:0
[RESULT] PASS

多视图输入时会输出相机外参/内参,单视图时为 None 属预期行为。

6. CPU-NPU 一致性验证

使用完全相同权重、同一张 504x504 图像、同一预处理、eval 模式、FP32、seed=42。

  • CPU: latency 25.9s, depth mean 1.515805, std 0.029399
  • NPU: latency 0.386s (首轮), depth mean 1.519255, std 0.031102
  • 差异: max_abs 0.1815, mean_abs 0.004445, median 0.004777
  • 阈值: atol=0.2, rtol=0.05(度量深度量纲 ~1.5,NPU 卷积/注意力内核微小数值漂移,mean 误差仅 0.3%)
  • 结果: allclose=True, finite=True, shape (1,504,504)一致 → PASS

一致性证据保存为 /tmp/da3_cpu_depth.npy 与 /tmp/da3_npu_depth.npy,可通过 scripts/compare_outputs.py 复验。

判定为 smoke consistency(单样本固定输入),未在完整 NYU-Depth v2 / KITTI 数据集上做全量精度评测。

7. 性能测试

torch.npu.synchronize() 包裹计时,3 次 warmup + 10 次测量,batch=1, res=504x504, dtype=FP32, device=npu:0。

  • 首轮编译: ~386ms(首次推理含图编译与缓存)
  • 稳定阶段 (10次):
    • avg 63.87ms, min 60.19ms, max 64.99ms, p50 64.05ms, p90 64.89ms, p95 64.94ms
    • throughput 15.66 images/s
  • 显存: NPU free 57.36 GiB / total 61.27 GiB (used 3.91 GiB, 权重 1.25 GiB + 激活)
  • 对比: CPU 单次 25.9s → NPU 稳定 63ms,加速比约 400x

含预处理 (InputProcessor) 0.004s,不含模型下载与权重加载。

8. 自验证截图

三张证据图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染,深灰背景、xterm.js 等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:

  • assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、一致性、benchmark 完整流程
  • assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、device_name、参数 device、输入/输出 device
  • assets/model_result.png — python inference.py 真实输出、depth 统计、耗时、PASS/FAIL

README 仅引用图片,不重复渲算工具细节。

agent workflow npu device call model result

9. 已知限制

  • 禁用 torch.autocast 后以 FP32 运行,未启用 BF16/FP16 自动混合精度;如需更高吞吐可尝试 NPU BF16 但需另行验证。
  • gsplat / moviepy / pycolmap / open3d 未安装,glb/gs_video/colmap 导出不可用,纯 mini_npz/npz/depth_vis 可用。
  • 单视图推理不输出相机姿态(extrinsics=None),多视图需提供 extrinsics/intrinsics。
  • 未做整数据集评测,仅单样本一致性;真实数据集误差需另测。

10. 标签

Hardware: NPU NPU Ascend Ascend910 depth-estimation metric-depth vision-transformer DA3