-
Notifications
You must be signed in to change notification settings - Fork 30k
Depth Anything: update conversion script for V2 #31522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
86d3a5c
Depth Anything: update conversion script for V2
pcuenca 5af464b
Merge remote-tracking branch 'upstream/main' into depth-anything-v2
pcuenca be0ca47
Update docs
pcuenca 5985bcb
Style
pcuenca 79d0f82
Revert "Update docs"
pcuenca 5b029c6
Add docs for depth anything v2
pcuenca 8d30094
Add depth_anything_v2 to MODEL_NAMES_MAPPING
pcuenca 2f7e989
Add tip in original docs
pcuenca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<!--Copyright 2024 The HuggingFace Team. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
|
||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
rendered properly in your Markdown viewer. | ||
|
||
--> | ||
|
||
# Depth Anything V2 | ||
|
||
## Overview | ||
|
||
Depth Anything V2 was introduced in [the paper of the same name](https://arxiv.org/abs/2406.09414) by Lihe Yang et al. It uses the same architecture as the original [Depth Anything model](depth_anything), but uses synthetic data and a larger capacity teacher model to achieve much finer and robust depth predictions. | ||
|
||
The abstract from the paper is the following: | ||
|
||
*This work presents Depth Anything V2. Without pursuing fancy techniques, we aim to reveal crucial findings to pave the way towards building a powerful monocular depth estimation model. Notably, compared with V1, this version produces much finer and more robust depth predictions through three key practices: 1) replacing all labeled real images with synthetic images, 2) scaling up the capacity of our teacher model, and 3) teaching student models via the bridge of large-scale pseudo-labeled real images. Compared with the latest models built on Stable Diffusion, our models are significantly more efficient (more than 10x faster) and more accurate. We offer models of different scales (ranging from 25M to 1.3B params) to support extensive scenarios. Benefiting from their strong generalization capability, we fine-tune them with metric depth labels to obtain our metric depth models. In addition to our models, considering the limited diversity and frequent noise in current test sets, we construct a versatile evaluation benchmark with precise annotations and diverse scenes to facilitate future research.* | ||
|
||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/depth_anything_overview.jpg" | ||
alt="drawing" width="600"/> | ||
|
||
<small> Depth Anything overview. Taken from the <a href="https://arxiv.org/abs/2401.10891">original paper</a>.</small> | ||
|
||
The Depth Anything models were contributed by [nielsr](https://huggingface.co/nielsr). | ||
The original code can be found [here](https://github.com/DepthAnything/Depth-Anything-V2). | ||
|
||
## Usage example | ||
|
||
There are 2 main ways to use Depth Anything V2: either using the pipeline API, which abstracts away all the complexity for you, or by using the `DepthAnythingForDepthEstimation` class yourself. | ||
|
||
### Pipeline API | ||
|
||
The pipeline allows to use the model in a few lines of code: | ||
|
||
```python | ||
>>> from transformers import pipeline | ||
>>> from PIL import Image | ||
>>> import requests | ||
|
||
>>> # load pipe | ||
>>> pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf") | ||
|
||
>>> # load image | ||
>>> url = 'http://images.cocodataset.org/val2017/000000039769.jpg' | ||
>>> image = Image.open(requests.get(url, stream=True).raw) | ||
|
||
>>> # inference | ||
>>> depth = pipe(image)["depth"] | ||
``` | ||
|
||
### Using the model yourself | ||
|
||
If you want to do the pre- and post-processing yourself, here's how to do that: | ||
|
||
```python | ||
>>> from transformers import AutoImageProcessor, AutoModelForDepthEstimation | ||
>>> import torch | ||
>>> import numpy as np | ||
>>> from PIL import Image | ||
>>> import requests | ||
|
||
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
>>> image = Image.open(requests.get(url, stream=True).raw) | ||
|
||
>>> image_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf") | ||
>>> model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf") | ||
|
||
>>> # prepare image for the model | ||
>>> inputs = image_processor(images=image, return_tensors="pt") | ||
|
||
>>> with torch.no_grad(): | ||
... outputs = model(**inputs) | ||
... predicted_depth = outputs.predicted_depth | ||
|
||
>>> # interpolate to original size | ||
>>> prediction = torch.nn.functional.interpolate( | ||
... predicted_depth.unsqueeze(1), | ||
... size=image.size[::-1], | ||
... mode="bicubic", | ||
... align_corners=False, | ||
... ) | ||
|
||
>>> # visualize the prediction | ||
>>> output = prediction.squeeze().cpu().numpy() | ||
>>> formatted = (output * 255 / np.max(output)).astype("uint8") | ||
>>> depth = Image.fromarray(formatted) | ||
``` | ||
|
||
## Resources | ||
|
||
A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with Depth Anything. | ||
|
||
- [Monocular depth estimation task guide](../tasks/depth_estimation) | ||
- [Depth Anything V2 demo](https://huggingface.co/spaces/depth-anything/Depth-Anything-V2). | ||
- A notebook showcasing inference with [`DepthAnythingForDepthEstimation`] can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/Depth%20Anything/Predicting_depth_in_an_image_with_Depth_Anything.ipynb). 🌎 | ||
- [Core ML conversion of the `small` variant for use on Apple Silicon](https://huggingface.co/apple/coreml-depth-anything-v2-small). | ||
|
||
If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. | ||
|
||
## DepthAnythingConfig | ||
|
||
[[autodoc]] DepthAnythingConfig | ||
|
||
## DepthAnythingForDepthEstimation | ||
|
||
[[autodoc]] DepthAnythingForDepthEstimation | ||
- forward |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can also add a note to the docs of v1, stating "there's a v2 available", in red so that it's visible?