> This is the documentation for LibreYOLO 1.6.0. Current version: https://www.libreyolo.com/docs/models/midas

# MiDaS
MiDaS is monocular relative depth estimation trained with scale-and-shift invariant loss across mixed datasets, the line of work that established the zero-shot depth transfer protocol later families reuse. LibreYOLO supports it for the depth task: predict and zero-shot validation, with no training path.
Tasks: Depth. Install: pip install "libreyolo[midas]".
Verified against LibreYOLO v1.6.0.

## Install

MiDaS needs the `midas` extra for its timm encoders.

```bash
pip install "libreyolo[midas]"
```

## Predict

The s and l checkpoints download from LibreYOLO's mirrors under the publisher's MIT grant and are cached locally.

**Python**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# Downloads the mirrored checkpoint on first use.
model = LibreYOLO("LibreMiDaSl-depth.pt")
result = model(SAMPLE_IMAGE, save=True)

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

**CLI**

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

**Small variant**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# EfficientNet-Lite3 encoder, smaller and faster than the DPT-Large l size.
model = LibreYOLO("LibreMiDaSs-depth.pt")
result = model(SAMPLE_IMAGE, save=True)
```

`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. See [prediction](/docs/v1.6.0/predict) for sources,
streaming and result handling.

## Variants

Two variants with different encoders, not just different scales of the same
one. `s` is MiDaS v2.1 Small, an EfficientNet-Lite3 encoder. `l` is DPT-Large,
a ViT-L/16 encoder with the DPT decoder MiDaS introduced for dense prediction.
They also preprocess differently: `s` uses an upper-bound aspect resize with
ImageNet mean/std normalization, `l` uses a minimal aspect resize with mean and
std of 0.5. Pick `s` for a lighter CNN, `l` for the transformer decoder's
accuracy.

Training is not offered for this family. `LibreMiDaS.train()` raises
`NotImplementedError` unconditionally.

## 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("LibreMiDaSl-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=LibreMiDaSl-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.

**Python**

```python
from libreyolo import LibreYOLO

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

**CLI**

```bash
libreyolo export model=LibreMiDaSl-depth.pt format=onnx
libreyolo export model=LibreMiDaSl-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("LibreMiDaSl-depth.onnx")
result = model(SAMPLE_IMAGE)

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

## 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: MiDaS, Intel Intelligent Systems Lab (Intel ISL)
- Upstream license: MIT
- Upstream source: https://github.com/isl-org/MiDaS
- LibreYOLO code: MIT
- Weights: MIT, republished at https://huggingface.co/LibreYOLO
- Interpretation: The upstream repository and released weights declare MIT. LibreYOLO mirrors the s and l checkpoints under that publisher grant.

## Citation

```bibtex
@ARTICLE {Ranftl2022,
    author  = "Ren\'{e} Ranftl and Katrin Lasinger and David Hafner and Konrad Schindler and Vladlen Koltun",
    title   = "Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-Shot Cross-Dataset Transfer",
    journal = "IEEE Transactions on Pattern Analysis and Machine Intelligence",
    year    = "2022",
    volume  = "44",
    number  = "3"
}

@article{Ranftl2021,
	author    = {Ren\'{e} Ranftl and Alexey Bochkovskiy and Vladlen Koltun},
	title     = {Vision Transformers for Dense Prediction},
	journal   = {ICCV},
	year      = {2021},
}
```

Copied from https://github.com/isl-org/MiDaS#citation


## MiDaS architecture

- Small EfficientNet-Lite3: [SVG](/diagrams/models/midas/s-depth.svg), [interactive diagram](/diagrams/models/midas/s-depth.html)
- Large ViT-L/16: [SVG](/diagrams/models/midas/l-depth.svg), [interactive diagram](/diagrams/models/midas/l-depth.html)
