# 単眼深度推定

単眼深度推定は、単一の画像からシーンの深度情報を予測するコンピュータビジョンのタスクです。言い換えれば、単一のカメラ視点からシーン内のオブジェクトまでの距離を推定するプロセスです。

単眼深度推定には、3D再構成、拡張現実（AR）、自動運転、ロボティクスなど、さまざまな応用があります。これは困難なタスクであり、モデルがシーン内のオブジェクト間の複雑な関係と、それに対応する深度情報を理解する必要があります。深度情報は、照明条件、遮蔽、テクスチャなどの要因の影響を受ける可能性があります。

深度推定には、主に2つのカテゴリがあります。

- **絶対深度推定**: このタスクのバリアントは、カメラからの正確な深度測定値を提供することを目的としています。この用語は、メートルやフィートなどの正確な単位で深度が提供されるメトリック深度推定と、同義に使用されます。絶対深度推定モデルは、実世界の距離を表す数値を含む深度マップを出力します。

- **相対深度推定**: 相対深度推定は、正確な測定値を提供せずに、シーン内のオブジェクトや点の深度順序を予測することを目的としています。これらのモデルは、A と B までの実際の距離を示すことなく、シーンのどの部分が互いに近いか遠いかを示す深度マップを出力します。

このガイドでは、最先端のゼロショット相対深度推定モデルである[Depth Anything V2](https://huggingface.co/depth-anything/Depth-Anything-V2-Large)と、絶対深度推定モデルである[ZoeDepth](../model_doc/zoedepth)を用いた推論のやり方について説明します。

[Depth Estimation](https://huggingface.co/tasks/depth-estimation)タスクのページを参照し、互換性のあるアーキテクチャとチェックポイントのすべてを確認してください。

開始に先立ち、Transformers の最新バージョンをインストールする必要があります。

```bash
pip install -q -U transformers
```

## 深度推定パイプライン

深度推定に対応するモデルで推論を試す最も簡単な方法は、対応する[pipeline()](/docs/transformers/v5.24.0/ja/main_classes/pipelines#transformers.pipeline)を使用することです。[Hugging Face Hub上のチェックポイント](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads)からパイプラインをインスタンス化します:

```py
>>> from transformers import pipeline
from accelerate import Accelerator
>>> import torch
>>> device = Accelerator().device
>>> checkpoint = "depth-anything/Depth-Anything-V2-base-hf"
>>> pipe = pipeline("depth-estimation", model=checkpoint, device=device)
```

次に、分析する画像を選択します:

```py
>>> 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
```

     

画像をパイプラインに渡します。

```py
>>> predictions = pipe(image)
```

パイプラインは2つのエントリを持つ辞書を返します。1つ目の`predicted_depth`は、各ピクセルのメートル単位の深度値を保持するテンソルです。2つ目の`depth`は、深度推定の結果を可視化したPIL画像です。

可視化された結果を見てみましょう：

```py
>>> predictions["depth"]
```

     

## 手動による深度推定推論

深度推定パイプラインの使い方を確認したので、次は同じ結果を手動で再現する方法を見ていきましょう。

[Hugging Face Hub上のチェックポイント](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads)からモデルと関連するプロセッサを読み込むことから始めます。ここでは、以前と同じチェックポイントを使用します:

```py
>>> 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`を使用して、リサイズや正規化などの必要な画像変換を処理し、モデル用の画像入力を準備します:

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

準備した入力をモデルに渡します:

```py
>>> import torch

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

結果を後処理して、パディングを除去し、深度マップを元の画像サイズにリサイズしましょう。`post_process_depth_estimation` は、`"predicted_depth"` を含む辞書のリストを出力します。

```py
>>> # 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引数に反転した出力を渡すことで、この処理を代行してくれます: &gt;&gt;&gt; with torch.no_grad(): ...     outputs = model(pixel_values) ...     outputs_flipped = model(pixel_values=torch.flip(inputs.pixel_values, dims=[3])) &gt;&gt;&gt; post_processed_output = image_processor.post_process_depth_estimation( ...     outputs, ...     source_sizes=[(image.height, image.width)], ...     outputs_flipped=outputs_flipped, ... )

     

