|
| 1 | +<!--Copyright 2024 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +# ZoeDepth |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +The ZoeDepth model was proposed in [ZoeDepth: Zero-shot Transfer by Combining Relative and Metric Depth](https://arxiv.org/abs/2302.12288) by Shariq Farooq Bhat, Reiner Birkl, Diana Wofk, Peter Wonka, Matthias Müller. ZoeDepth extends the [DPT](dpt) framework for metric (also called absolute) depth estimation. ZoeDepth is pre-trained on 12 datasets using relative depth and fine-tuned on two domains (NYU and KITTI) using metric depth. A lightweight head is used with a novel bin adjustment design called metric bins module for each domain. During inference, each input image is automatically routed to the appropriate head using a latent classifier. |
| 22 | + |
| 23 | +The abstract from the paper is the following: |
| 24 | + |
| 25 | +*This paper tackles the problem of depth estimation from a single image. Existing work either focuses on generalization performance disregarding metric scale, i.e. relative depth estimation, or state-of-the-art results on specific datasets, i.e. metric depth estimation. We propose the first approach that combines both worlds, leading to a model with excellent generalization performance while maintaining metric scale. Our flagship model, ZoeD-M12-NK, is pre-trained on 12 datasets using relative depth and fine-tuned on two datasets using metric depth. We use a lightweight head with a novel bin adjustment design called metric bins module for each domain. During inference, each input image is automatically routed to the appropriate head using a latent classifier. Our framework admits multiple configurations depending on the datasets used for relative depth pre-training and metric fine-tuning. Without pre-training, we can already significantly improve the state of the art (SOTA) on the NYU Depth v2 indoor dataset. Pre-training on twelve datasets and fine-tuning on the NYU Depth v2 indoor dataset, we can further improve SOTA for a total of 21% in terms of relative absolute error (REL). Finally, ZoeD-M12-NK is the first model that can jointly train on multiple datasets (NYU Depth v2 and KITTI) without a significant drop in performance and achieve unprecedented zero-shot generalization performance to eight unseen datasets from both indoor and outdoor domains.* |
| 26 | + |
| 27 | +<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/zoedepth_architecture_bis.png" |
| 28 | +alt="drawing" width="600"/> |
| 29 | + |
| 30 | +<small> ZoeDepth architecture. Taken from the <a href="https://arxiv.org/abs/2302.12288">original paper.</a> </small> |
| 31 | + |
| 32 | +This model was contributed by [nielsr](https://huggingface.co/nielsr). |
| 33 | +The original code can be found [here](https://github.com/isl-org/ZoeDepth). |
| 34 | + |
| 35 | +## Usage tips |
| 36 | + |
| 37 | +- ZoeDepth is an absolute (also called metric) depth estimation model, unlike DPT which is a relative depth estimation model. This means that ZoeDepth is able to estimate depth in metric units like meters. |
| 38 | + |
| 39 | +The easiest to perform inference with ZoeDepth is by leveraging the [pipeline API](../main_classes/pipelines.md): |
| 40 | + |
| 41 | +```python |
| 42 | +from transformers import pipeline |
| 43 | +from PIL import Image |
| 44 | +import requests |
| 45 | + |
| 46 | +url = "http://images.cocodataset.org/val2017/000000039769.jpg" |
| 47 | +image = Image.open(requests.get(url, stream=True).raw) |
| 48 | + |
| 49 | +pipe = pipeline(task="depth-estimation", model="Intel/zoedepth-nyu-kitti") |
| 50 | +result = pipe(image) |
| 51 | +depth = result["depth"] |
| 52 | +``` |
| 53 | + |
| 54 | +Alternatively, one can also perform inference using the classes: |
| 55 | + |
| 56 | +```python |
| 57 | +from transformers import AutoImageProcessor, ZoeDepthForDepthEstimation |
| 58 | +import torch |
| 59 | +import numpy as np |
| 60 | +from PIL import Image |
| 61 | +import requests |
| 62 | + |
| 63 | +url = "http://images.cocodataset.org/val2017/000000039769.jpg" |
| 64 | +image = Image.open(requests.get(url, stream=True).raw) |
| 65 | + |
| 66 | +image_processor = AutoImageProcessor.from_pretrained("Intel/zoedepth-nyu-kitti") |
| 67 | +model = ZoeDepthForDepthEstimation.from_pretrained("Intel/zoedepth-nyu-kitti") |
| 68 | + |
| 69 | +# prepare image for the model |
| 70 | +inputs = image_processor(images=image, return_tensors="pt") |
| 71 | + |
| 72 | +with torch.no_grad(): |
| 73 | + outputs = model(**inputs) |
| 74 | + predicted_depth = outputs.predicted_depth |
| 75 | + |
| 76 | +# interpolate to original size |
| 77 | +prediction = torch.nn.functional.interpolate( |
| 78 | + predicted_depth.unsqueeze(1), |
| 79 | + size=image.size[::-1], |
| 80 | + mode="bicubic", |
| 81 | + align_corners=False, |
| 82 | +) |
| 83 | + |
| 84 | +# visualize the prediction |
| 85 | +output = prediction.squeeze().cpu().numpy() |
| 86 | +formatted = (output * 255 / np.max(output)).astype("uint8") |
| 87 | +depth = Image.fromarray(formatted) |
| 88 | +``` |
| 89 | + |
| 90 | +## Resources |
| 91 | + |
| 92 | +A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with ZoeDepth. |
| 93 | + |
| 94 | +- A demo notebook regarding inference with ZoeDepth models can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/ZoeDepth). 🌎 |
| 95 | + |
| 96 | +## ZoeDepthConfig |
| 97 | + |
| 98 | +[[autodoc]] ZoeDepthConfig |
| 99 | + |
| 100 | +## ZoeDepthImageProcessor |
| 101 | + |
| 102 | +[[autodoc]] ZoeDepthImageProcessor |
| 103 | + - preprocess |
| 104 | + |
| 105 | +## ZoeDepthForDepthEstimation |
| 106 | + |
| 107 | +[[autodoc]] ZoeDepthForDepthEstimation |
| 108 | + - forward |
0 commit comments