### Install Depth Anything V2 and Dependencies Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Clone the repository, navigate to the directory, and install the required Python packages for the main project and the metric depth sub-project. ```bash git clone https://github.com/DepthAnything/Depth-Anything-V2 cd Depth-Anything-V2 pip install -r requirements.txt # gradio_imageslider gradio matplotlib opencv-python torch torchvision # metric_depth sub-project has its own requirements: pip install -r metric_depth/requirements.txt ``` -------------------------------- ### Clone Repository and Install Dependencies Source: https://github.com/depthanything/depth-anything-v2/blob/main/metric_depth/README.md Clone the Depth Anything V2 repository and install the required Python packages. Ensure you have the necessary dependencies before proceeding. ```bash git clone https://github.com/DepthAnything/Depth-Anything-V2 cd Depth-Anything-V2/metric_depth pip install -r requirements.txt ``` -------------------------------- ### Clone Repository and Install Dependencies Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md Clone the Depth Anything V2 repository and install the necessary Python packages using the provided requirements file. Ensure you have a compatible Python environment. ```bash git clone https://github.com/DepthAnything/Depth-Anything-V2 cd Depth-Anything-V2 pip install -r requirements.txt ``` -------------------------------- ### Start Gradio Demo Locally Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md Run the Gradio demo application locally by executing the provided Python script. This allows for an interactive experience with the Depth Anything V2 model. ```bash python app.py ``` -------------------------------- ### Generate 3D Point Cloud from Metric Depth Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This script generates a 3D point cloud from metric depth predictions and camera intrinsics, saving it as a .ply file. Ensure Open3D is installed. ```bash python metric_depth/depth_to_pointcloud.py \ --encoder vitl \ --load-from checkpoints/depth_anything_v2_metric_hypersim_vitl.pth \ --max-depth 20 \ --img-path indoor_images/ \ --outdir pointclouds/ \ --focal-length-x 525.0 \ --focal-length-y 525.0 ``` -------------------------------- ### Load Depth Anything V2 Model with Transformers Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md Use the Depth Anything V2 model through the Hugging Face Transformers library. Ensure you have the latest Transformers installed and can connect to Hugging Face. Predictions might differ slightly from OpenCV due to image library differences. ```python from transformers import pipeline from PIL import Image pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf") image = Image.open('your/image/path') depth = pipe(image)["depth"] ``` -------------------------------- ### Perform End-to-End Depth Inference Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Use the `infer_image` method to get depth predictions from a raw BGR image. The method handles resizing, normalization, forward pass, and up-sampling. Optionally, specify `input_size` for finer detail and save the normalized depth map for visualization. ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 model = DepthAnythingV2(encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024]) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_vitl.pth', map_location='cpu')) model.eval() raw_image = cv2.imread('photo.jpg') # HxWx3 BGR uint8 # Default input_size=518; increase for finer detail (e.g. 756) depth = model.infer_image(raw_image, input_size=518) # returns HxW float32 numpy # Normalise to 0-255 for visualisation depth_vis = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0 depth_vis = depth_vis.astype('uint8') cv2.imwrite('depth_output.png', depth_vis) ``` -------------------------------- ### DepthAnythingV2.infer_image - End-to-End Depth Inference Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This method performs end-to-end depth inference on a raw BGR NumPy image. It handles internal resizing, normalization, forward pass, and up-sampling. The method is decorated with `@torch.no_grad()` for efficiency. An example shows how to optionally specify `input_size` and visualize the output depth map. ```APIDOC ## `DepthAnythingV2.infer_image` — end-to-end depth inference Accepts a raw BGR NumPy image (as returned by `cv2.imread`), internally resizes it to a multiple of 14 with the lower-bound strategy, normalises with ImageNet statistics, runs the forward pass, and bilinearly up-samples the result back to the original resolution. Decorated with `@torch.no_grad()` so no gradient tracking is needed. ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 model = DepthAnythingV2(encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024]) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_vitl.pth', map_location='cpu')) model.eval() raw_image = cv2.imread('photo.jpg') # HxWx3 BGR uint8 # Default input_size=518; increase for finer detail (e.g. 756) depth = model.infer_image(raw_image, input_size=518) # returns HxW float32 numpy # Normalise to 0-255 for visualisation depth_vis = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0 depth_vis = depth_vis.astype('uint8') cv2.imwrite('depth_output.png', depth_vis) ``` ``` -------------------------------- ### Reproduce Training Source: https://github.com/depthanything/depth-anything-v2/blob/main/metric_depth/README.md Prepare the Hypersim and Virtual KITTI 2 datasets, then run the `dist_train.sh` script to reproduce the training process. ```bash bash dist_train.sh ``` -------------------------------- ### Initialize and Load Depth Anything V2 Model Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Instantiate the DepthAnythingV2 model with chosen encoder configurations and load pre-trained weights. The model is then moved to the appropriate device (CUDA, MPS, or CPU) and set to evaluation mode. ```python import cv2 import torch import numpy as np from depth_anything_v2.dpt import DepthAnythingV2 DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu' # Encoder configs — choose one: vits / vitb / vitl / vitg model_configs = { 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]}, 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]}, 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]}, 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}, } encoder = 'vitl' model = DepthAnythingV2(**model_configs[encoder]) model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{encoder}.pth', map_location='cpu')) model = model.to(DEVICE).eval() raw_image = cv2.imread('assets/examples/demo01.jpg') # BGR, HxWx3 uint8 depth = model.infer_image(raw_image) # HxW float32 numpy array (relative depth) print(f"Depth shape: {depth.shape}, range: [{depth.min():.3f}, {depth.max():.3f}]") # Depth shape: (480, 640), range: [0.312, 18.754] ``` -------------------------------- ### DepthAnythingV2 Model Initialization and Inference Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This snippet demonstrates how to initialize the DepthAnythingV2 model with different encoder configurations, load pre-trained weights, and perform end-to-end depth inference on a single image. It shows how to select model size and device, and prints the shape and range of the resulting depth map. ```APIDOC ## DepthAnythingV2 — model class The central PyTorch `nn.Module` that wraps a DINOv2 encoder and a DPT depth head. Instantiate with an encoder name and its matching channel configuration, load weights, then call `infer_image` for end-to-end prediction. When `max_depth` is passed the model operates in metric mode (absolute metres); omitting it produces relative (unit-less) depth. ```python import cv2 import torch import numpy as np from depth_anything_v2.dpt import DepthAnythingV2 DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu' # Encoder configs — choose one: vits / vitb / vitl / vitg model_configs = { 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]}, 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]}, 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]}, 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}, } encoder = 'vitl' model = DepthAnythingV2(**model_configs[encoder]) model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{encoder}.pth', map_location='cpu')) model = model.to(DEVICE).eval() raw_image = cv2.imread('assets/examples/demo01.jpg') # BGR, HxWx3 uint8 depth = model.infer_image(raw_image) # HxW float32 numpy array (relative depth) print(f"Depth shape: {depth.shape}, range: [{depth.min():.3f}, {depth.max():.3f}]") # Depth shape: (480, 640), range: [0.312, 18.754] ``` ``` -------------------------------- ### Run Depth Anything V2 Script on Videos Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md Execute the provided Python script to process videos using Depth Anything V2. Similar to image processing, specify the encoder, video path, and output directory. Larger models offer better temporal consistency. ```bash python run_video.py \ --encoder \ --video-path assets/examples_video --outdir video_depth_vis \ [--input-size ] [--pred-only] [--grayscale] ``` -------------------------------- ### Run Inference Script on Images (Indoor Scenes) Source: https://github.com/depthanything/depth-anything-v2/blob/main/metric_depth/README.md Execute the `run.py` script for indoor scene depth estimation using the specified encoder, checkpoint, and max depth. Provide input image path and output directory. ```bash # indoor scenes python run.py \ --encoder vitl \ --load-from checkpoints/depth_anything_v2_metric_hypersim_vitl.pth \ --max-depth 20 \ --img-path --outdir [--input-size ] [--save-numpy] ``` -------------------------------- ### Visualize DA-2K Annotations Source: https://github.com/depthanything/depth-anything-v2/blob/main/DA-2K.md Use this command to visualize the annotations within the DA-2K benchmark. You can optionally filter by scene type. ```bash python visualize.py [--scene-type ] ``` -------------------------------- ### Load and Use Pre-trained Metric Depth Models Source: https://github.com/depthanything/depth-anything-v2/blob/main/metric_depth/README.md Load a pre-trained Depth Anything V2 model for metric depth estimation. Specify the encoder type, dataset (indoor/outdoor), and max depth. The model can then be used to infer depth from raw images. ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 model_configs = { 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]}, 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]}, 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]} } encoder = 'vitl' # or 'vits', 'vitb' dataset = 'hypersim' # 'hypersim' for indoor model, 'vkitti' for outdoor model max_depth = 20 # 20 for indoor model, 80 for outdoor model model = DepthAnythingV2(**{**model_configs[encoder], 'max_depth': max_depth}) model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_metric_{dataset}_{encoder}.pth', map_location='cpu')) model.eval() raw_img = cv2.imread('your/image/path') depth = model.infer_image(raw_img) # HxW depth map in meters in numpy ``` -------------------------------- ### Run Inference Script on Images (Outdoor Scenes) Source: https://github.com/depthanything/depth-anything-v2/blob/main/metric_depth/README.md Execute the `run.py` script for outdoor scene depth estimation using the specified encoder, checkpoint, and max depth. Provide input image path and output directory. ```bash # outdoor scenes python run.py \ --encoder vitl \ --load-from checkpoints/depth_anything_v2_metric_vkitti_vitl.pth \ --max-depth 80 \ --img-path --outdir [--input-size ] [--save-numpy] ``` -------------------------------- ### Run Depth Anything V2 Script on Images Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md Execute the provided Python script to process images using Depth Anything V2. Specify the encoder type, image path, and output directory. Optional arguments allow for input size adjustment, saving only predictions, and grayscale output. ```bash python run.py \ --encoder \ --img-path --outdir \ [--input-size ] [--pred-only] [--grayscale] ``` ```bash python run.py --encoder vitl --img-path assets/examples --outdir depth_vis ``` -------------------------------- ### Distributed Metric-Depth Training Script Source: https://context7.com/depthanything/depth-anything-v2/llms.txt The `metric_depth/train.py` script is used for distributed fine-tuning of metric-depth models. It supports training on datasets like Hypersim and Virtual KITTI 2 using `torchrun`. Key arguments include encoder choice, dataset, image size, depth range, epochs, batch size, learning rate, and paths for pre-trained models and saving experiments. To resume training, omit `--pretrained-from` and provide the saved checkpoint path directly. ```bash # Single-node 4-GPU training on Hypersim (indoor, max 20m) torchrun --nproc_per_node=4 metric_depth/train.py \ --encoder vitl \ --dataset hypersim \ --img-size 518 \ --min-depth 0.001 \ --max-depth 20 \ --epochs 40 \ --bs 2 \ --lr 5e-6 \ --pretrained-from checkpoints/depth_anything_v2_vitl.pth \ --save-path experiments/metric_hypersim_vitl # Resume from a checkpoint (omit --pretrained-from, pass the saved .pth directly) torchrun --nproc_per_node=4 metric_depth/train.py \ --encoder vitl \ --dataset vkitti \ --max-depth 80 \ --epochs 40 \ --bs 2 \ --lr 5e-6 \ --pretrained-from checkpoints/depth_anything_v2_vitl.pth \ --save-path experiments/metric_vkitti_vitl ``` -------------------------------- ### run.py Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Command-line script for batch image depth estimation. Supports single images, directories, or text files of paths, with options for output format and resolution. ```APIDOC ## `run.py` — batch image depth estimation script ### Description Command-line script for running relative depth estimation on a single image, a directory of images, or a text file of paths. Saves side-by-side originals + colourised depth maps (Spectral_r palette) or depth-only PNGs. ### Usage Examples ```bash # Single image — colour depth map alongside original python run.py \ --encoder vitl \ --img-path assets/examples/demo01.jpg \ --outdir depth_vis # Full directory, depth-only, grayscale, higher resolution for fine detail python run.py \ --encoder vitl \ --img-path assets/examples \ --outdir depth_vis \ --input-size 756 \ --pred-only \ --grayscale # Text file listing absolute paths, one per line python run.py \ --encoder vits \ --img-path my_images.txt \ --outdir depth_vis \ --pred-only # Output: depth_vis/.png for every input image ``` ``` -------------------------------- ### Python API for Single-Image Point Cloud Generation Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This Python code demonstrates the equivalent API for generating a single-image point cloud using Depth Anything V2, including model loading, inference, and point cloud creation with Open3D. ```python import cv2, numpy as np, open3d as o3d, torch from depth_anything_v2.dpt import DepthAnythingV2 from PIL import Image model = DepthAnythingV2(encoder='vitl', features=256, out_channels=[256,512,1024,1024], max_depth=20) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_metric_hypersim_vitl.pth', map_location='cpu')) model.eval() color_image = Image.open('scene.jpg').convert('RGB') width, height = color_image.size image_bgr = cv2.imread('scene.jpg') depth = model.infer_image(image_bgr, height) fx, fy = 525.0, 525.0 x, y = np.meshgrid(np.arange(width), np.arange(height)) x = (x - width / 2) / fx y = (y - height / 2) / fy z = np.array(Image.fromarray(depth).resize((width, height), Image.NEAREST)) points = np.stack((x * z, y * z, z), axis=-1).reshape(-1, 3) colors = np.array(color_image).reshape(-1, 3) / 255.0 pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points) pcd.colors = o3d.utility.Vector3dVector(colors) o3d.io.write_point_cloud('output.ply', pcd) ``` -------------------------------- ### Load Depth Anything V2 Model with PyTorch Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md Load and infer depth from an image using the Depth Anything V2 model directly with PyTorch. Ensure you have the model checkpoints downloaded. ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu' model_configs = { 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]}, 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]}, 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]}, 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]} } encoder = 'vitl' # or 'vits', 'vitb', 'vitg' model = DepthAnythingV2(**model_configs[encoder]) model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{encoder}.pth', map_location='cpu')) model = model.to(DEVICE).eval() raw_img = cv2.imread('your/image/path') depth = model.infer_image(raw_img) # HxW raw depth map in numpy ``` -------------------------------- ### Batch Image Depth Estimation Script (`run.py`) Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Command-line script for batch relative depth estimation. Supports single images, directories, or text files of paths. Can save side-by-side originals and colorized depth maps, or depth-only PNGs. ```bash # Single image — colour depth map alongside original python run.py \ --encoder vitl \ --img-path assets/examples/demo01.jpg \ --outdir depth_vis # Full directory, depth-only, grayscale, higher resolution for fine detail python run.py \ --encoder vitl \ --img-path assets/examples \ --outdir depth_vis \ --input-size 756 \ --pred-only \ --grayscale # Text file listing absolute paths, one per line python run.py \ --encoder vits \ --img-path my_images.txt \ --outdir depth_vis \ --pred-only # Output: depth_vis/.png for every input image ``` -------------------------------- ### Run Video Depth Inference Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Use this script to generate depth maps for videos. Specify the input video path or a directory of videos and an output directory. Options include predicting depth only and outputting grayscale. ```bash python run_video.py \ --encoder vitl \ --video-path assets/examples_video/ferris_wheel.mp4 \ --outdir video_depth_vis ``` ```bash python run_video.py \ --encoder vitl \ --video-path assets/examples_video \ --outdir video_depth_vis \ --pred-only \ --grayscale ``` -------------------------------- ### Depth Anything V2 via Hugging Face Transformers Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Load and use Depth Anything V2 models directly through the Hugging Face Transformers library. Specify the desired model ID from the available options. ```python from transformers import pipeline from PIL import Image import numpy as np # Available HF model IDs: # depth-anything/Depth-Anything-V2-Small-hf # depth-anything/Depth-Anything-V2-Base-hf ``` -------------------------------- ### Project 2D Images to Point Clouds Source: https://github.com/depthanything/depth-anything-v2/blob/main/metric_depth/README.md Use the `depth_to_pointcloud.py` script to project 2D depth images into 3D point clouds. Specify the encoder, checkpoint, max depth, input image path, and output directory. ```bash python depth_to_pointcloud.py \ --encoder vitl \ --load-from checkpoints/depth_anything_v2_metric_hypersim_vitl.pth \ --max-depth 20 \ --img-path --outdir ``` -------------------------------- ### Image to Tensor Preprocessing with DepthAnythingV2 Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Converts a raw BGR image to a normalized NCHW torch.Tensor for model input. Returns the tensor and original dimensions for upsampling. Requires torchvision.transforms. ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 model = DepthAnythingV2(encoder='vits', features=64, out_channels=[48, 96, 192, 384]) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_vits.pth', map_location='cpu')) model.eval() raw_image = cv2.imread('scene.jpg') tensor, (orig_h, orig_w) = model.image2tensor(raw_image, input_size=518) # tensor: torch.Tensor shape [1, 3, H', W'] on the detected device # (orig_h, orig_w): e.g. (720, 1280) — used to resize depth back with torch.no_grad(): import torch.nn.functional as F depth = model.forward(tensor) # [1, H', W'] depth = F.interpolate(depth[:, None], (orig_h, orig_w), mode='bilinear', align_corners=True)[0, 0] # [H, W] depth_np = depth.cpu().numpy() print(f"Tensor shape: {tensor.shape}, restored depth: {depth_np.shape}") # Tensor shape: torch.Size([1, 3, 518, 938]), restored depth: (720, 1280) ``` -------------------------------- ### DA-2K Benchmark Annotations Loading Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This Python snippet demonstrates how to load annotations for the DA-2K benchmark, which provides pair-wise relative depth annotations. The annotations are stored in a JSON file. The structure of the loaded data is a dictionary where keys are image paths and values are lists of annotations. ```python import json # Download: https://huggingface.co/datasets/depth-anything/DA-2K with open('DA-2K/annotations.json') as f: annotations = json.load(f) # Structure: # { "images/indoor/img001.jpg": [ ``` -------------------------------- ### Metric depth mode Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Instantiates the model with `max_depth` for metric depth estimation, enabling regression of absolute depth in meters. Supports different checkpoints for indoor and outdoor scenes. ```APIDOC ## Metric depth mode — `DepthAnythingV2` with `max_depth` ### Description When fine-tuned for metric depth, the model is instantiated with an additional `max_depth` argument. The DPT head's final sigmoid activation is replaced, enabling it to regress absolute depth in metres. Six pre-trained checkpoints are available (Small/Base/Large × Indoor/Outdoor). ### Usage Example ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu' model = DepthAnythingV2( encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024], max_depth=20 # 20m for indoor (Hypersim), 80m for outdoor (VKITTI) ) model.load_state_dict( torch.load('checkpoints/depth_anything_v2_metric_hypersim_vitl.pth', map_location='cpu') ) model = model.to(DEVICE).eval() raw_image = cv2.imread('indoor_scene.jpg') depth_meters = model.infer_image(raw_image) # absolute depth in metres print(f"Min: {depth_meters.min():.2f}m Max: {depth_meters.max():.2f}m") # Min: 0.42m Max: 7.83m ``` ``` -------------------------------- ### run_video.py Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Processes video files frame-by-frame to estimate depth, outputting a video with original and/or depth map frames. ```APIDOC ## `run_video.py` — video depth estimation script ### Description Processes MP4 (or any OpenCV-supported) video files frame-by-frame, writing an output video with the same frame rate. In default mode each output frame is a horizontal concatenation `[original | white_gap | colourised_depth]`; use `--pred-only` for depth-only output. ### Usage Example ```bash # Process a video and save the output with side-by-side original and depth map python run_video.py --video-path assets/examples/demo.mp4 --outdir video_output # Process a video and save only the depth map frames python run_video.py --video-path assets/examples/demo.mp4 --outdir video_output --pred-only ``` ``` -------------------------------- ### Video Depth Estimation Script (`run_video.py`) Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Processes MP4 or other OpenCV-supported video files frame-by-frame. Outputs a video with the same frame rate. Default mode concatenates original and colorized depth; use `--pred-only` for depth-only output. ```bash ``` -------------------------------- ### Infer Image Depth with DepthAnythingV2 Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Loads a pre-trained DepthAnythingV2 model and infers depth from an image. This snippet demonstrates basic usage for relative depth estimation and accuracy calculation on a benchmark dataset. ```python import cv2, torch, numpy as np from depth_anything_v2.dpt import DepthAnythingV2 model = DepthAnythingV2(encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024]) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_vitl.pth', map_location='cpu')) model.eval() correct = total = 0 for img_path, pairs in annotations.items(): raw = cv2.imread(f'DA-2K/{img_path}') depth = model.infer_image(raw) # HxW, larger value = farther away for pair in pairs: h1, w1 = pair['point1'] h2, w2 = pair['point2'] # point1 is always the closer one → should have SMALLER depth value pred_closer = 'point1' if depth[h1, w1] < depth[h2, w2] else 'point2' correct += (pred_closer == pair['closer_point']) total += 1 print(f"DA-2K Accuracy: {correct / total * 100:.2f}%") ``` -------------------------------- ### Image Preprocessing Transforms Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This Python code defines a `Compose` transform pipeline for image preprocessing using `torchvision.transforms`. It includes `Resize` for aspect-ratio-preserving scaling (required to be a multiple of 14 for ViT patch size), `NormalizeImage` with specified mean and standard deviation, and `PrepareForNet` to transpose dimensions (HWC to CHW) and ensure a float32 contiguous layout. The output is a tensor ready for network input. ```python import cv2 import numpy as np from torchvision.transforms import Compose from depth_anything_v2.util.transform import Resize, NormalizeImage, PrepareForNet transform = Compose([ Resize( width=518, height=518, resize_target=False, # do not resize depth/mask, image only keep_aspect_ratio=True, ensure_multiple_of=14, # required by ViT patch size resize_method='lower_bound', image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), PrepareForNet(), ]) raw_image = cv2.imread('photo.jpg') image_rgb = cv2.cvtColor(raw_image, cv2.COLOR_BGR2RGB) / 255.0 # float64 HWC sample = transform({'image': image_rgb}) tensor = sample['image'] # float32 CHW numpy print(f"Transformed shape: {tensor.shape}") # e.g. (3, 518, 938) print(f"Value range: [{tensor.min():.3f}, {tensor.max():.3f}]") ``` -------------------------------- ### Metric Depth Estimation with DepthAnythingV2 Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Estimates absolute depth in meters when the model is fine-tuned with the `max_depth` argument. Replaces the DPT head's sigmoid activation for regression. Requires specific pre-trained checkpoints for indoor or outdoor scenes. ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu' model = DepthAnythingV2( encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024], max_depth=20 # 20m for indoor (Hypersim), 80m for outdoor (VKITTI) ) model.load_state_dict( torch.load('checkpoints/depth_anything_v2_metric_hypersim_vitl.pth', map_location='cpu') ) model = model.to(DEVICE).eval() raw_image = cv2.imread('indoor_scene.jpg') depth_meters = model.infer_image(raw_image) # absolute depth in metres print(f"Min: {depth_meters.min():.2f}m Max: {depth_meters.max():.2f}m") # Min: 0.42m Max: 7.83m ``` -------------------------------- ### Depth Anything V2 Citation (BibTeX) Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md BibTeX entry for citing the Depth Anything V2 paper. Use this in LaTeX documents for academic referencing. ```bibtex @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} } ``` -------------------------------- ### Depth Anything V1 Citation (BibTeX) Source: https://github.com/depthanything/depth-anything-v2/blob/main/README.md BibTeX entry for citing the original Depth Anything paper presented at CVPR. Include this for referencing the foundational work. ```bibtex @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} } ``` -------------------------------- ### Metric Depth Inference Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This script performs metric depth inference and can optionally save the raw float32 depth array as a .npy file. It requires specifying a model checkpoint and max depth. ```bash python metric_depth/run.py \ --encoder vitl \ --load-from checkpoints/depth_anything_v2_metric_hypersim_vitl.pth \ --max-depth 20 \ --img-path indoor_images/ \ --outdir metric_depth_vis \ --save-numpy ``` ```bash python metric_depth/run.py \ --encoder vitl \ --load-from checkpoints/depth_anything_v2_metric_vkitti_vitl.pth \ --max-depth 80 \ --img-path outdoor_images/ \ --outdir metric_depth_vis \ --pred-only ``` -------------------------------- ### Depth Estimation Pipeline Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Use the `pipeline` function for depth estimation from an image. The result is a PIL Image in 16-bit grayscale, which can be converted to a NumPy array for further processing. Note potential minor differences from OpenCV due to Pillow upsampling. ```python from transformers import pipeline from PIL import Image import numpy as np pipe = pipeline(task='depth-estimation', model='depth-anything/Depth-Anything-V2-Large-hf') image = Image.open('photo.jpg') result = pipe(image) depth = result['depth'] # PIL.Image in 16-bit grayscale depth_np = np.array(depth) print(f"Depth shape: {depth_np.shape}, dtype: {depth_np.dtype}") ``` -------------------------------- ### DepthAnythingV2.forward Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Performs a raw forward pass through the model using a preprocessed NCHW tensor. It extracts intermediate features and feeds them through the DPT head, returning a squeezed tensor. ```APIDOC ## DepthAnythingV2.forward — raw forward pass ### Description Accepts a preprocessed NCHW tensor, extracts intermediate features from DINOv2 at four selected transformer layers (encoder-dependent), feeds them through the DPT head, applies ReLU, and returns a squeezed `[B, H', W']` tensor. Use this directly when batching or when you need to stay in tensor-land. ### Usage Example ```python import torch from depth_anything_v2.dpt import DepthAnythingV2 model = DepthAnythingV2(encoder='vitb', features=128, out_channels=[96, 192, 384, 768]) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_vitb.pth', map_location='cpu')) model = model.cuda().eval() # Simulate a batch of 2 pre-processed images (already resized + normalised) dummy_batch = torch.randn(2, 3, 518, 518).cuda() with torch.no_grad(): depth_batch = model(dummy_batch) # shape: [2, 37, 37] (518/14 = 37 patches) print(depth_batch.shape) # torch.Size([2, 37, 37]) # After interpolate back to original resolution: import torch.nn.functional as F depth_full = F.interpolate(depth_batch[:, None], (480, 640), mode='bilinear', align_corners=True)[:, 0] print(depth_full.shape) # torch.Size([2, 480, 640]) ``` ``` -------------------------------- ### SiLogLoss for Metric Depth Training Source: https://context7.com/depthanything/depth-anything-v2/llms.txt The `SiLogLoss` module is used for fine-tuning metric depth models. It calculates the scale-invariant logarithmic error between predicted and ground-truth depth, masked to valid pixels. `lambd=0.5` is the default value per the SiLog formulation. Ensure predictions and targets are on the CUDA device. ```python import torch from metric_depth.util.loss import SiLogLoss criterion = SiLogLoss(lambd=0.5).cuda() # Simulated batch: predicted depth and ground-truth in metres pred = torch.rand(2, 480, 640).clamp(0.1, 20.0).cuda() target = torch.rand(2, 480, 640).clamp(0.1, 20.0).cuda() valid_mask = (target > 0.5) & (target < 15.0) # boolean mask of valid GT pixels loss = criterion(pred, target, valid_mask) loss.backward() print(f"SiLog loss: {loss.item():.4f}") # e.g. 0.3821 ``` -------------------------------- ### Raw Forward Pass with DepthAnythingV2 Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Performs a raw forward pass on a preprocessed NCHW tensor. Use this for batch processing or when staying in tensor-land. Extracts features from DINOv2 and feeds them through the DPT head. ```python import torch from depth_anything_v2.dpt import DepthAnythingV2 model = DepthAnythingV2(encoder='vitb', features=128, out_channels=[96, 192, 384, 768]) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_vitb.pth', map_location='cpu')) model = model.cuda().eval() # Simulate a batch of 2 pre-processed images (already resized + normalised) dummy_batch = torch.randn(2, 3, 518, 518).cuda() with torch.no_grad(): depth_batch = model(dummy_batch) # shape: [2, 37, 37] (518/14 = 37 patches) print(depth_batch.shape) # torch.Size([2, 37, 37]) # After interpolate back to original resolution: import torch.nn.functional as F depth_full = F.interpolate(depth_batch[:, None], (480, 640), mode='bilinear', align_corners=True)[:, 0] print(depth_full.shape) # torch.Size([2, 480, 640]) ``` -------------------------------- ### Gradio App Core Prediction Callback Source: https://context7.com/depthanything/depth-anything-v2/llms.txt This Python function is the core callback for the Gradio app, handling image input, depth inference, and generating outputs for 16-bit raw disparity and 8-bit colored depth maps. ```python import numpy as np, matplotlib, tempfile from PIL import Image from depth_anything_v2.dpt import DepthAnythingV2 import torch cmap = matplotlib.colormaps.get_cmap('Spectral_r') def on_submit(image: np.ndarray): """image: HxWx3 RGB uint8 from gr.Image(type='numpy')""" original_image = image.copy() depth = model.infer_image(image[:, :, ::-1]) # RGB → BGR for model # 16-bit raw output (disparity-like) raw_depth = Image.fromarray(depth.astype('uint16')) tmp_raw = tempfile.NamedTemporaryFile(suffix='.png', delete=False) raw_depth.save(tmp_raw.name) # Normalised 8-bit coloured depth_norm = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0 depth_u8 = depth_norm.astype(np.uint8) colored = (cmap(depth_u8)[:, :, :3] * 255).astype(np.uint8) gray = Image.fromarray(depth_u8) tmp_gray = tempfile.NamedTemporaryFile(suffix='.png', delete=False) gray.save(tmp_gray.name) return [(original_image, colored), tmp_gray.name, tmp_raw.name] ``` -------------------------------- ### eval_depth for Depth Evaluation Metrics Source: https://context7.com/depthanything/depth-anything-v2/llms.txt The `eval_depth` function computes standard monocular depth metrics from 1-D tensors of predicted and target values. It returns a dictionary containing threshold accuracies (`d1/d2/d3`) and various error metrics (`abs_rel`, `sq_rel`, `rmse`, `rmse_log`, `log10`, `silog`). Ensure input tensors are already filtered by a valid mask. ```python import torch from metric_depth.util.metric import eval_depth # 1-D tensors of valid pixels only pred = torch.rand(10000).clamp(0.1, 20.0) target = torch.rand(10000).clamp(0.1, 20.0) metrics = eval_depth(pred, target) for k, v in metrics.items(): print(f" {k:10s}: {v:.4f}") # d1 : 0.3417 # d2 : 0.6023 # d3 : 0.8201 # abs_rel : 0.5512 # sq_rel : 0.9134 # rmse : 8.2341 # rmse_log : 0.6012 # log10 : 0.2491 # silog : 0.5988 ``` -------------------------------- ### DepthAnythingV2.image2tensor Source: https://context7.com/depthanything/depth-anything-v2/llms.txt Converts a raw BGR np.ndarray to a normalised NCHW torch.Tensor ready for forward pass. It returns both the tensor and the original (H, W) dimensions for upsampling. ```APIDOC ## DepthAnythingV2.image2tensor — preprocessing helper ### Description Converts a raw BGR `np.ndarray` to a normalised NCHW `torch.Tensor` ready for `forward()`, returning both the tensor and the original `(H, W)` needed for upsampling. Uses `torchvision.transforms.Compose` with `Resize` (keeping aspect ratio, snapping dimensions to multiples of 14), `NormalizeImage` (ImageNet mean/std), and `PrepareForNet` (HWC→CHW, contiguous float32). ### Usage Example ```python import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 model = DepthAnythingV2(encoder='vits', features=64, out_channels=[48, 96, 192, 384]) model.load_state_dict(torch.load('checkpoints/depth_anything_v2_vits.pth', map_location='cpu')) model.eval() raw_image = cv2.imread('scene.jpg') tensor, (orig_h, orig_w) = model.image2tensor(raw_image, input_size=518) # tensor: torch.Tensor shape [1, 3, H', W'] on the detected device # (orig_h, orig_w): e.g. (720, 1280) — used to resize depth back with torch.no_grad(): import torch.nn.functional as F depth = model.forward(tensor) # [1, H', W'] depth = F.interpolate(depth[:, None], (orig_h, orig_w), mode='bilinear', align_corners=True)[0, 0] # [H, W] depth_np = depth.cpu().numpy() print(f"Tensor shape: {tensor.shape}, restored depth: {depth_np.shape}") # Tensor shape: torch.Size([1, 3, 518, 938]), restored depth: (720, 1280) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.