# Depth Anything V2
Depth Anything V2 is a DINOv2 encoder paired with a DPT decoder that predicts a dense relative inverse-depth map from a single image. LibreYOLO supports it for the depth task: predict and zero-shot validation, with no training path.
Tasks: Depth. Install: pip install libreyolo.
Verified against LibreYOLO v1.5.0.

## Install

Depth Anything V2 needs no optional extra. Everything it imports is in the base install.

```bash
pip install libreyolo
```

## Predict

Weights download from Hugging Face on first use and are cached locally.

**Python**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

model = LibreYOLO("LibreDepthAnythingV2s-depth.pt")
result = model(SAMPLE_IMAGE, save=True)

depth = result.depth_map
print(depth.min, depth.max, depth.mean)
```

**CLI**

```bash
libreyolo predict model=LibreDepthAnythingV2s-depth.pt source=https://raw.githubusercontent.com/LibreYOLO/libreyolo/release/libreyolo/assets/parkour.jpg save=True
```

**Read the depth map**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

model = LibreYOLO("LibreDepthAnythingV2s-depth.pt")
result = model(SAMPLE_IMAGE)

depth = result.depth_map    # DepthMap: dense (H, W), higher = closer
raw = depth.data                # tensor, no metric unit or cross-image scale
normalized = depth.normalized() # rescaled to [0, 1] for visualization
```

`result.depth_map` carries a dense relative inverse-depth map: higher values
mean closer to the camera, and the values have no metric unit or cross-image
scale. `save=True` writes a colormapped visualization of that map to disk;
`Results.plot()` renders the depth map as a PIL image, and `pil=False` returns
the array instead. Input resolution must divide evenly by 14, the DINOv2
patch grid the DPT head builds on; LibreYOLO checks this before running and
raises if it does not. See [prediction](/docs/predict) for sources, streaming
and result handling.

## Variants

Four encoder sizes, s/b/l/g, corresponding to ViT-S/B/L/G. The checkpoint table
below lists only s, b and l; no Giant checkpoint is published. All four share
the same input resolution, so choosing a size trades encoder capacity, not
image size. Licensing is also a factor: the Small checkpoint is Apache-2.0,
while Base and Large are CC-BY-NC-4.0, see Licensing below.

Training and fine-tuning are not offered for this family. `LibreDepthAnythingV2.train()`
raises `NotImplementedError` unconditionally; convert a compatible upstream
checkpoint instead, with `weights/convert_depth_anything_v2_weights.py`.

## Validate

`val()` runs the shared depth validator: it aligns each prediction to its
ground truth with a per-image least-squares scale and shift, then reports the
standard zero-shot relative-depth metrics, AbsRel, RMSE and the three delta
thresholds.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreDepthAnythingV2s-depth.pt")
metrics = model.val(data="my-dataset.yaml")

print(metrics["metrics/abs_rel"])
print(metrics["metrics/rmse"])
print(metrics["metrics/delta1"])
```

**CLI**

```bash
libreyolo val model=LibreDepthAnythingV2s-depth.pt data=my-dataset.yaml
```

## Export

| Task | ONNX | TorchScript | ExecuTorch | TensorRT | OpenVINO | Paddle | MNN | RKNN | ncnn | TFLite | CoreML | Core AI |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Depth | yes | yes | yes | yes | yes |  |  |  |  |  |  | yes |

A "yes" means the export is supported. An empty cell means the exporter refuses that combination.

An exported artifact loads back through `LibreYOLO()` on its file suffix, so a
`.onnx` or `.engine` file behaves like a checkpoint and returns the same
`Results`, with `depth_map` in place of boxes. [Export](/docs/export) lists the
arguments every format accepts.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreDepthAnythingV2s-depth.pt")
model.export(format="onnx")
model.export(format="tensorrt", half=True)
```

**CLI**

```bash
libreyolo export model=LibreDepthAnythingV2s-depth.pt format=onnx
libreyolo export model=LibreDepthAnythingV2s-depth.pt format=tensorrt half=True
```

**Use the exported file**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# The factory routes on the file suffix, so an exported artifact loads
# like any checkpoint and returns the same Results object.
model = LibreYOLO("LibreDepthAnythingV2s-depth.onnx")
result = model(SAMPLE_IMAGE)

print(result.depth_map.data.shape)
```

## Checkpoints

Every published weight file for this family.

| File | Input (px) | Task | Weights license |
| --- | --- | --- | --- |
| `LibreDepthAnythingV2s-depth.pt` |  | Depth | apache-2.0 |
| `LibreDepthAnythingV2l-depth.pt` |  | Depth | cc-by-nc-4.0 |
| `LibreDepthAnythingV2b-depth.pt` |  | Depth | cc-by-nc-4.0 |

## Licensing

Check the license on the Hugging Face repository of the specific weights you download. That repository is authoritative and licenses are not always uniform across a family. This is a description of the licenses involved, not legal advice.

- Original work: Depth Anything V2, The University of Hong Kong and TikTok
- Upstream license: Apache-2.0 (Small checkpoint); CC-BY-NC-4.0 (Base and Large checkpoints)
- Upstream source: https://github.com/DepthAnything/Depth-Anything-V2
- LibreYOLO code: Apache-2.0
- Weights: Apache-2.0 (Small checkpoint); CC-BY-NC-4.0 (Base and Large checkpoints), republished at https://huggingface.co/LibreYOLO
- Interpretation: The two licenses are not interchangeable. The Small checkpoint is Apache-2.0, a permissive license: it can be used in commercial and closed-source products, it asks you to keep its license text and attribution notices with any redistributed copy, and it grants a patent license. The Base and Large checkpoints are CC-BY-NC-4.0, which forbids commercial use outright and requires attribution on any redistribution, so treat them as research and evaluation weights unless you obtain separate terms from the authors. LibreYOLO's own code for this family is MIT throughout, and training is not offered for this family so there is no self-trained-weights exception to reach for.

## Citation

```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}
}
```

Copied from https://github.com/DepthAnything/Depth-Anything-V2#citation


## Depth Anything V2 architecture

- s: [SVG](/diagrams/models/depth-anything-v2/s-depth.svg), [interactive diagram](/diagrams/models/depth-anything-v2/s-depth.html)
- b: [SVG](/diagrams/models/depth-anything-v2/b-depth.svg), [interactive diagram](/diagrams/models/depth-anything-v2/b-depth.html)
- l: [SVG](/diagrams/models/depth-anything-v2/l-depth.svg), [interactive diagram](/diagrams/models/depth-anything-v2/l-depth.html)
- g: [SVG](/diagrams/models/depth-anything-v2/g-depth.svg), [interactive diagram](/diagrams/models/depth-anything-v2/g-depth.html)
- Family topology with explicit g FFN exception: [SVG](/diagrams/models/depth-anything-v2/family.svg), [interactive diagram](/diagrams/models/depth-anything-v2/family.html)
