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

depth-anything/DA3METRIC-LARGE on Ascend NPU

1. 模型简介

depth-anything/DA3METRIC-LARGE 是 ByteDance Seed 发布的 Depth Anything 3 Metric Large 模型,专为单目度量深度估计(monocular metric depth estimation)设计。基于 ViT-L backbone (DinoV2) + DPT head,参数量 334M (1.245 GiB safetensors),支持真实尺度深度预测(metric depth),输入任意单张 RGB 图,输出稠密深度图(H×W float32,单位米),附带 sky 分割掩码,适用于自动驾驶、3D 重建、AR 等需真实尺度几何的任务。

  • 架构: DepthAnything3Net = DinoV2 ViT-L (out_layers [4,11,17,23], dim 1024) + DPT head (features 256, out_channels [256,512,1024,1024], output_dim 1)
  • 模型卡: depth-anything/DA3METRIC-LARGE (config.json + model.safetensors, 1.245 GiB, pipeline_tag depth-estimation)
  • 许可证: Apache-2.0
  • 特点: 单一 plain transformer 跨任务泛化,训练于公开学术数据集,首个预判显存 <2GiB 可在单卡 Ascend910B (64GiB) 上 FP32 运行。

2. 验证环境

  • 硬件: Ascend910B (910_9362), 2× HBM 64GiB, NPU 0 mem free 60.91 GiB
  • NPU 驱动: npu-smi 25.5.5, CANN 8.5.1
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, safetensors 0.7.0, Pillow, opencv-python, einops, addict, omegaconf
  • 模型目录: /tmp/da3metric (model.safetensors 1.245 GiB, config.json 847 B), 权重 SHA 来源 HuggingFace 4010e39f3634a45bc60553321fb49fb760bd594e
  • 验证方式: 真实权重在 npu:0 上完成端到端推理,CPU 同权重同输入 smoke consistency,对比深度图 abs diff。

3. 安装依赖

pip install -r requirements.txt
# 需预先安装 CANN 与 torch_npu,本机已内置
# 源码在项目根已 vendor depth_anything_3,无需额外 clone

或手动:

git clone https://github.com/ByteDance-Seed/depth-anything-3
pip install -e ./depth-anything-3 --no-deps
pip install torch torchvision einops huggingface_hub safetensors pillow opencv-python addict omegaconf

4. NPU 推理

默认命令(单图 1208×680 → process_res 504 → 输出 280×504 metric depth):

python inference.py
# 或显式
python inference.py --device npu:0
# 其他模式
python inference.py --mode validate   # CPU vs NPU 一致性 + benchmark
python inference.py --mode benchmark  # 仅 NPU 性能

输入: sample.png (assets/examples/SOH/000.png, 1091690 B) 或任意 PIL Image / 文件路径。预处理: upper_bound_resize 到长边 504,再 pad 到 PATCH_SIZE 14 倍数,ImageNet Normalize。

5. 真实推理结果

NPU (npu:0, Ascend910_9362, torch.float32, autocast bfloat16 forward):

  • 输入: PIL (1208, 680), process_res 504 → preprocessed tensor [1, 3, 280, 504] (upper_bound_resize, mean [0.485,0.456,0.406], std [0.229,0.224,0.225])
  • 前端: Processed Images Done 0.010s
  • 核心: Model Forward Pass Done 0.47–0.55s (首次编译) / 稳定 ~23ms
  • 深度图: shape (1, 280, 504), dtype float32, min 2.445912, max 12.971806, mean 8.497721
  • 细节: depth[0,0,0]=12.716998, center(140,252)=4.433786, is_metric N/A (dict), sky available, conf None (DA3Metric 无置信度)
  • 耗时: 487.57 ms (首次, 含预处理) → 稳定 30.98 ms avg
  • 模型 param device: npu:0, dtype float32 → PASS

CPU (同权重, process_res 224, 合成图 seed 42):

  • 深度图 (1,224,224), mean 1.466572, min 1.339837, max 1.527088, time 56359 ms
  • NPU 同输入 224: mean 1.468282, min 1.340883, max 1.529464, time 349 ms

6. CPU-NPU 一致性验证

smoke consistency,固定种子 42 的 224×224 随机图,process_res 224,同一权重 FP32,同预处理 (ImageNet normalize, upper_bound_resize)。

  • CPU depth mean 1.466572, NPU depth mean 1.468282
  • Abs diff: max 0.002658, mean 0.001710, median 0.001722
  • Relative diff: max 0.001764, mean 0.001163
  • 阈值: max_abs <0.5 && mean_abs <0.05 (metric 米级尺度) → PASS
  • Conf: None on both (模型不输出 conf) → 忽略
  • 说明: 使用 autocast bfloat16 前向,浮点容差放宽,但仍远优于阈值 (max 0.002 <<0.5)。

完整 504 图因 CPU 耗时 >200s 未做全量一致性,仅 smoke 224 验证;NPU 504 结果与官方预期尺度一致(室内 SOH 场景 2–13m)。

7. 性能测试

torch.npu.synchronize() 包围计时,batch=1, process_res=504, 图片 1208×680 → [1,3,280,504] (N=1 view)。

  • 预热 3 次后正式 10 次:
    • avg 30.98 ms, min 30.71 ms, max 31.62 ms, p50 30.90 ms, p90 31.19 ms, p95 31.40 ms
    • 吞吐 32.279 images/s
  • 分解: Preprocess ~6.5ms, Forward ~23ms, Conversion ~0.3ms (稳定后)
  • 首次编译: 0.47s (含图编译), 峰值显存 <4 GiB (free 60.91 → 模型驻留 ~1.3GiB + 激活)
  • 对比 CPU 504 未测完整,CPU 224 需 56s → NPU 加速 ~161× (224); 504 预计 >200s → NPU 加速 >6000×
  • dtype: 权重 float32, 前向 autocast bfloat16 (torch.cuda.is_bf16_supported fallback float16), 输出 float32 depth

8. 自验证截图

  • assets/agent_workflow.png — 完整工作流 (下载/解包/加载/NPU 推理/验证/性能)
  • assets/npu_device_call.png — npu-smi 与 torch.npu 设备调用证据 (npu:0, Ascend910_9362, param device)
  • assets/model_result.png — python inference.py 默认输出 (depth 8.49 mean, PASS)

9. 已知限制

  • DA3Metric-LARGE 仅输出 depth + sky,不输出 conf/intrinsics/extrinsics (推理结果中为 None),属模型设计;
  • 需要 depth_anything_3 源码 (已 vendor 到仓库),依赖 pycolmap/gsplat 已 mock,不影响 metric depth 核心;
  • 显存占用: 模型 1.3 GiB + 编译缓存,batch>1 或多视图 N>4 时需分步;
  • CPU baseline 极慢 (56s@224),正式验证仅 smoke 224,生产建议直接 NPU;
  • 进程间 npu-smi 与 torch.npu 需同环境,HBM 碎片在连续推理后轻微增长。

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, depth-estimation, metric-depth, DA3, VisionTransformer, DPT