Depth Anything V2

repository·main·Indexed 27 days ago

https://github.com/depthanything/depth-anything-v2

A high-performance model for robust relative and metric depth estimation. It supports multiple model scales (Small, Base, Large, and Giant) and provides tools for image and video inference, 3D point cloud projection, and evaluation via the DA-2K benchmark. The project includes a Python API, CLI scripts for processing, and integration with Hugging Face Transformers.

Tokens
3.9K
Snippets
14
Records
19
Agent score
94%

What's inside Depth Anything V2

  1. Setup Depth Anything V2 for Metric Depth Estimation

    main

    To use the metric depth estimation codebase, clone the repository, navigate to the metric_depth directory, and install the required dependencies. You must also download the pre-trained checkpoints and place them in a checkpoints directory within the metric_depth folder.

    git clone https://github.com/DepthAnything/Depth-Anything-V2
    cd Depth-Anything-V2/metric_depth
    pip install -r requirements.txt
  2. Install and prepare Depth Anything V2

    main

    To set up the repository, clone it, install the required dependencies, and download the model checkpoints into a checkpoints directory.

    1. Clone the repository:
      git clone https://github.com/DepthAnything/Depth-Anything-V2
      cd Depth-Anything-V2
    2. Install dependencies:
      pip install -r requirements.txt
    3. Download checkpoints from the Pre-trained Models section and place them in the checkpoints folder.
    git clone https://github.com/DepthAnything/Depth-Anything-V2
    cd Depth-Anything-V2
    pip install -r requirements.txt
  3. Run the Depth Anything V2 Gradio Demo

    main
    The app.py file provides a Gradio-based web interface for interacting with the Depth Anything V2 model. It allows users to upload an image, compute its depth map, and view the results using an image slider. The demo outputs a colored depth map for visualization, a grayscale depth map for download, and a 16-bit raw output (disparity) for high-precision use.
  4. Visualize DA-2K annotations

    main

    You can visualize the annotations using the visualize.py script. You can optionally filter the visualization by specific scene types.

    Available --scene-type values:

    • indoor
    • outdoor
    • non_real
    • transparent_reflective
    • adverse_style
    • aerial
    • underwater
    • object

    To include all scene types, omit the argument or set it to an empty string "".

    python visualize.py [--scene-type <type>]
  5. Use Depth Anything V2 via Hugging Face Transformers

    main

    If you prefer not to clone the repository, you can use the transformers library. Note that predictions may differ slightly from the native implementation due to upsampling differences between OpenCV and Pillow.

    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"]
  6. Use Depth Anything V2 via Python API

    main

    You can integrate the models directly into your Python code using the DepthAnythingV2 class from depth_anything_v2.dpt. This method is recommended over the Transformers integration to avoid slight prediction differences caused by upsampling differences between OpenCV and Pillow.

    Supported encoders: vits, vitb, vitl, vitg.

    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
  7. Use DepthAnythingV2 API for Metric Depth Inference

    main

    You can use the DepthAnythingV2 class to perform metric depth estimation in Python. The infer_image method returns a depth map in meters as a numpy array.

    Note the following configuration requirements:

    • Indoor models: Use dataset='hypersim' and max_depth=20.
    • Outdoor models: Use dataset='vkitti' and max_depth=80.
    • Encoders: Supported values are 'vits', 'vitb', and 'vitl'.
    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
  8. Pre-trained Models Reference

    main

    Depth Anything V2 provides four model scales for relative depth estimation. Note that the Giant model is listed as 'Coming soon'.

    | Model | Params | Checkpoint |
    |:-|-:|:-:|
    | Depth-Anything-V2-Small | 24.8M | [Download](https://huggingface.co/depth-anything/Depth-Anything-V2-Small/resolve/main/depth_anything_v2_vits.pth?download=true) |
    | Depth-Anything-V2-Base | 97.5M | [Download](https://huggingface.co/depth-anything/Depth-Anything-V2-Base/resolve/main/depth_anything_v2_vitb.pth?download=true) |
    | Depth-Anything-V2-Large | 335.3M | [Download](https://huggingface.co/depth-anything/Depth-Anything-V2-Large/resolve/main/depth_anything_v2_vitl.pth?download=true) |
    | Depth-Anything-V2-Giant | 1.3B | Coming soon |