Skip to content

APPLE DEPTH PRO · MONOCULAR METRIC DEPTH

Apple Depth Pro:从一张图片得到清晰的绝对深度

Depth Pro 是 Apple 面向零样本单目深度估计的基础模型。它输出以米为单位的绝对距离、估计焦距和清晰边界,且不需要相机元数据。先在浏览器生成深度图,再在本地复现。

Depth Pro 的不同之处

大多数单目深度模型只预测相对深度:远近顺序正确但尺度未知。Depth Pro 预测带绝对尺度的深度,并从图片本身估计焦距,因此即使没有 EXIF 数据的照片也能使用。Apple 报告其多尺度视觉 Transformer 能保持细小结构和发丝级边界的清晰度,同时在标准 GPU 上约 0.3 秒生成 2.25 MP 的深度图。

在浏览器中运行

本站的深度图生成器通过 Transformers.js 加载 ONNX 移植版,并使用 WebGPU 在你的 GPU 上运行。q4f16 权重约 600 MB,首次运行后由浏览器缓存。照片不会离开设备。

Transformers.js · WebGPU
import { AutoModelForDepthEstimation, AutoProcessor, RawImage } from "@huggingface/transformers";

const id = "onnx-community/DepthPro-ONNX";
const processor = await AutoProcessor.from_pretrained(id);
const model = await AutoModelForDepthEstimation.from_pretrained(id, { device: "webgpu", dtype: "q4f16" });

const image = await RawImage.read("photo.jpg");
const { predicted_depth, focallength_px } = await model(await processor(image));
// predicted_depth: metric depth (metres); resize to the input and normalise for display.

用 Python 运行 Depth Pro

Transformers 集成在后处理后返回以米为单位的深度、视场角和焦距。可在 CUDA、Apple Silicon(MPS)或 CPU(较慢)上运行。

Python · transformers ≥ 4.48
import torch
from PIL import Image
from transformers import DepthProForDepthEstimation, DepthProImageProcessorFast

device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
processor = DepthProImageProcessorFast.from_pretrained("apple/DepthPro-hf")
model = DepthProForDepthEstimation.from_pretrained("apple/DepthPro-hf").to(device).eval()

image = Image.open("photo.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="pt").to(device)
with torch.no_grad():
    outputs = model(**inputs)

post = processor.post_process_depth_estimation(outputs, target_sizes=[(image.height, image.width)])[0]
depth_m = post["predicted_depth"]          # metres, shape (H, W)
focal_px = float(post["focal_length"])     # estimated focal length in pixels
print(depth_m.min().item(), depth_m.max().item(), focal_px)

官方 ml-depth-pro 命令行

Apple 仓库提供了一个小型命令行工具,会下载 checkpoint 并为任意输入图片输出深度图。需要与论文完全一致的参考实现时使用它。

Shell · official repository
git clone https://github.com/apple/ml-depth-pro.git
cd ml-depth-pro
conda create -n depth-pro -y python=3.9
conda activate depth-pro
pip install -e .
source get_pretrained_models.sh          # downloads checkpoints/depth_pro.pt
depth-pro-run -i ./data/example.jpg      # writes a depth map next to the input

Depth Pro 与 Depth Anything 的区别

Depth Anything V2 Small 体积约小十倍、速度更快,且无需 WebGPU,但输出的是相对深度。需要米制距离、焦距或用于合成的清晰边缘时选 Depth Pro;快速预览和低端设备选 Depth Anything。生成器支持在同一张图上切换两者。

常见问题

Depth Pro 能在 Mac 上运行吗?

可以。PyTorch 模型可通过 MPS 后端在 Apple Silicon 上运行,浏览器工具也能在任何支持 WebGPU 的 Mac 上运行。每张图片需要几秒,而不是 Apple 在数据中心 GPU 上测得的 0.3 秒。

有官方的 Core ML 或 iPhone 版本吗?

官方发布的是 PyTorch 版本。可以自行转换为 Core ML 或 Core AI,但 Apple 仓库不提供该流程;请仔细测量内存,因为原生 1536 × 1536 输入对手机来说负担较大。

绝对尺度有多准确?

Apple 报告在多个数据集上取得了零样本绝对深度的领先精度,但尺度仍取决于焦距估计。单张图片的米制数值应视为估计值,测量前请用已知尺寸的物体验证。