Transformers 文档

单目深度估计

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

单目深度估计

单目深度估计是一项计算机视觉任务,它涉及从单个图像预测场景的深度信息。换句话说,它是从单个摄像机视角估计场景中物体距离的过程。

单目深度估计有多种应用,包括 3D 重建、增强现实、自动驾驶和机器人技术。这是一项具有挑战性的任务,因为它要求模型理解场景中物体与相应深度信息之间复杂的相互关系,这些关系可能受到光照条件、遮挡和纹理等因素的影响。

深度估计主要分为两类

  • 绝对深度估计:此任务变体旨在提供来自相机的精确深度测量。该术语与度量深度估计可互换使用,其中深度以米或英尺的精确测量值提供。绝对深度估计模型输出的深度图带有表示真实世界距离的数值。

  • 相对深度估计:相对深度估计旨在预测场景中物体或点的深度顺序,而不提供精确测量值。这些模型输出一个深度图,指示场景中哪些部分彼此之间更近或更远,而不提供 A 和 B 的实际距离。

在本指南中,我们将了解如何使用最先进的零样本相对深度估计模型 Depth Anything V2 和绝对深度估计模型 ZoeDepth 进行推理。

查看深度估计任务页面以查看所有兼容的架构和检查点。

在开始之前,我们需要安装最新版本的 Transformers

pip install -q -U transformers

深度估计管道

使用支持深度估计模型的pipeline()进行推理是最简单的方法。从Hugging Face Hub上的检查点实例化一个管道

>>> from transformers import pipeline
>>> import torch
>>> from accelerate.test_utils.testing import get_backend
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
>>> device, _, _ = get_backend()
>>> checkpoint = "depth-anything/Depth-Anything-V2-base-hf"
>>> pipe = pipeline("depth-estimation", model=checkpoint, device=device)

接下来,选择要分析的图像

>>> from PIL import Image
>>> import requests

>>> url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> image
Photo of a bee

将图像传递给管道。

>>> predictions = pipe(image)

该管道返回一个包含两个条目的字典。第一个名为predicted_depth,是一个张量,其值表示每个像素的深度(以米为单位)。第二个名为depth,是一个PIL图像,用于可视化深度估计结果。

我们来看看可视化结果

>>> predictions["depth"]
Depth estimation visualization

手动进行深度估计推理

现在你已经学会了如何使用深度估计管道,我们来看看如何手动重现相同的结果。

首先从Hugging Face Hub 上的检查点加载模型和相关的处理器。这里我们将使用相同的检查点

>>> from transformers import AutoImageProcessor, AutoModelForDepthEstimation

>>> checkpoint = "Intel/zoedepth-nyu-kitti"

>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint)
>>> model = AutoModelForDepthEstimation.from_pretrained(checkpoint).to(device)

使用image_processor为模型准备图像输入,该处理器将负责必要的图像变换,例如调整大小和归一化

>>> pixel_values = image_processor(image, return_tensors="pt").pixel_values.to(device)

将准备好的输入传递给模型

>>> import torch

>>> with torch.no_grad():
...     outputs = model(pixel_values)

让我们对结果进行后处理,以移除任何填充,并将深度图调整为与原始图像大小匹配。`post_process_depth_estimation` 输出一个包含 `"predicted_depth"` 的字典列表。

>>> # ZoeDepth dynamically pads the input image. Thus we pass the original image size as argument
>>> # to `post_process_depth_estimation` to remove the padding and resize to original dimensions.
>>> post_processed_output = image_processor.post_process_depth_estimation(
...     outputs,
...     source_sizes=[(image.height, image.width)],
... )

>>> predicted_depth = post_processed_output[0]["predicted_depth"]
>>> depth = (predicted_depth - predicted_depth.min()) / (predicted_depth.max() - predicted_depth.min())
>>> depth = depth.detach().cpu().numpy() * 255
>>> depth = Image.fromarray(depth.astype("uint8"))

原始实现中,ZoeDepth 模型对原始图像和翻转图像都执行推理,并对结果进行平均。`post_process_depth_estimation` 函数可以通过将翻转的输出传递给可选的 `outputs_flipped` 参数来为我们处理此问题

>>> with torch.no_grad():   
...     outputs = model(pixel_values)
...     outputs_flipped = model(pixel_values=torch.flip(inputs.pixel_values, dims=[3]))
>>> post_processed_output = image_processor.post_process_depth_estimation(
...     outputs,
...     source_sizes=[(image.height, image.width)],
...     outputs_flipped=outputs_flipped,
... )
Depth estimation visualization
< > 在 GitHub 上更新