Instructions to use bigshanedogg/Depth-Anything-V2-Large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use bigshanedogg/Depth-Anything-V2-Large with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("depth-estimation", model="bigshanedogg/Depth-Anything-V2-Large", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("bigshanedogg/Depth-Anything-V2-Large", trust_remote_code=True, device_map="auto") - DepthAnythingV2
How to use bigshanedogg/Depth-Anything-V2-Large with DepthAnythingV2:
# Install from https://github.com/DepthAnything/Depth-Anything-V2 # Load the model and infer depth from an image import cv2 import torch from huggingface_hub import hf_hub_download from depth_anything_v2.dpt import DepthAnythingV2 # instantiate the model model = DepthAnythingV2(encoder="<ENCODER>", features=<NUMBER_OF_FEATURES>, out_channels=<OUT_CHANNELS>) # load the weights filepath = hf_hub_download(repo_id="bigshanedogg/Depth-Anything-V2-Large", filename="depth_anything_v2_<ENCODER>.pth", repo_type="model") state_dict = torch.load(filepath, map_location="cpu") model.load_state_dict(state_dict) model.eval() raw_img = cv2.imread("your/image/path") depth = model.infer_image(raw_img) # HxW raw depth map in numpy - Notebooks
- Google Colab
- Kaggle
# pip install -U transformers accelerate
# Load model directly
from transformers import AutoModel
model = AutoModel.from_pretrained("bigshanedogg/Depth-Anything-V2-Large", trust_remote_code=True, device_map="auto")Depth-Anything-V2-Large β transformers format (original code)
Unofficial transformers port of Depth Anything V2 Large (depth-anything/Depth-Anything-V2-Large, depth_anything_v2_vitl.pth) that
runs the original code. Loads with trust_remote_code. model.safetensors holds the checkpoint's tensors under
their original names, converted from .pth (every tensor checked equal).
Not an official release. Depth Anything V2 was created by Lihe Yang, Bingyi Kang, Zilong Huang, Zhen Zhao, Xiaogang Xu, Jiashi Feng and Hengshuang Zhao β https://github.com/DepthAnything/Depth-Anything-V2, paper "Depth Anything V2" (arXiv 2406.09414).
What it is
DepthAnythingV2ForDepthEstimation holds the official DepthAnythingV2's submodules (pretrained: DINOv2 ViT-L/14,
depth_head: DPT) and computes depth with that class's own forward, returning the standard DepthEstimatorOutput
(predicted_depth); the official infer_image and image2tensor are kept. Code vendored from
DepthAnything/Depth-Anything-V2 @ a561b849 into dav2_dinov2.py / dav2_dpt.py (relative depth) and
dav2_metric_dinov2.py / dav2_metric_dpt.py (metric_depth/); edits: imports, and Block β NestedTensorBlock
where the original imported NestedTensorBlock as Block. DepthAnythingV2Processor (processing_depth_anything_v2.py,
processor_config.json) wraps DepthAnythingV2ImageProcessor, the image2tensor transform.
transformers' own DepthAnythingForDepthEstimation (the -hf repos) is a re-implementation: on the same input it
matches the original only for square inputs; for non-square inputs its DINOv2 position-embedding interpolation differs
(0.8β1.9 % max relative depth difference measured on Small). This package runs the original modules instead.
Verified against the official code (same process, V100, 3 images incl. non-square): preprocessing, predicted_depth
(also as a tuple), the hidden states, infer_image, the processor's post-processing and fp16 autocast outputs are
bit-identical.
Usage
import torch
from PIL import Image
from transformers import AutoModelForDepthEstimation, AutoProcessor
model = AutoModelForDepthEstimation.from_pretrained("Depth-Anything-V2-Large", trust_remote_code=True).eval().cuda()
processor = AutoProcessor.from_pretrained("Depth-Anything-V2-Large", trust_remote_code=True)
image = Image.open("image.jpg") # or a path (read with cv2, as the original scripts)
inputs = processor(images=image, return_tensors="pt").to("cuda")
with torch.no_grad():
outputs = model(**inputs) # outputs.predicted_depth: (1, H', W') relative inverse depth
depth = processor.post_process_depth_estimation(outputs, target_sizes=[image.size[::-1]])[0]["predicted_depth"]
# or, exactly as the original scripts: model.infer_image(cv2.imread("image.jpg"))
AutoModelloads the same class.output_hidden_states=Trueadds the DINOv2 features the DPT head reads;return_dict=Falsereturns a tuple.post_process_depth_estimationresizes as the originalinfer_image(bilinear,align_corners=True), not with transformers' bicubic.
Runtime packages beyond torch / transformers: opencv-python, torchvision (as the original).
Licensing
- Weights (
depth-anything/Depth-Anything-V2-Large): CC-BY-NC-4.0 (non-commercial) βLICENSE. - Code (vendored files and the port): Apache-2.0 β
LICENSE.code. - Attribution, sources and the changes made here:
NOTICE.
Citation
If you use this model, please cite the original work, as its authors ask:
@article{depth_anything_v2,
title={Depth Anything V2},
author={Yang, Lihe and Kang, Bingyi and Huang, Zilong and Zhao, Zhen and Xu, Xiaogang and Feng, Jiashi and Zhao, Hengshuang},
journal={arXiv:2406.09414},
year={2024}
}
@inproceedings{depth_anything_v1,
title={Depth Anything: Unleashing the Power of Large-Scale Unlabeled Data},
author={Yang, Lihe and Kang, Bingyi and Huang, Zilong and Xu, Xiaogang and Feng, Jiashi and Zhao, Hengshuang},
booktitle={CVPR},
year={2024}
}
- Downloads last month
- 4
Model tree for bigshanedogg/Depth-Anything-V2-Large
Base model
depth-anything/Depth-Anything-V2-Large
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("depth-estimation", model="bigshanedogg/Depth-Anything-V2-Large", trust_remote_code=True)