|
1 | | -# ZoeDepth |
| 1 | +# **ZoeDepth: Combining relative and metric depth** (Official implementation) <!-- omit in toc --> |
| 2 | + |
| 3 | +## **Table of Contents** <!-- omit in toc --> |
| 4 | +- [**Usage**](#usage) |
| 5 | + - [Using torch hub](#using-torch-hub) |
| 6 | + - [Using local copy](#using-local-copy) |
| 7 | + - [Using local torch hub](#using-local-torch-hub) |
| 8 | + - [or load the models manually](#or-load-the-models-manually) |
| 9 | + - [Using ZoeD models to predict depth](#using-zoed-models-to-predict-depth) |
| 10 | +- [**Environment setup**](#environment-setup) |
| 11 | +- [**Sanity checks** (Recommended)](#sanity-checks-recommended) |
| 12 | +- [Model files](#model-files) |
| 13 | +- [**Evaluation**](#evaluation) |
| 14 | + - [Evaluating offical models](#evaluating-offical-models) |
| 15 | + - [Evaluating local checkpoint](#evaluating-local-checkpoint) |
| 16 | +- [**Training**](#training) |
| 17 | +- [**Citation**](#citation) |
| 18 | + |
| 19 | + |
| 20 | +## **Usage** |
| 21 | +It is recommended to fetch the latest [MiDaS repo](https://github.com/isl-org/MiDaS) via torch hub before proceeding: |
| 22 | +```python |
| 23 | +import torch |
| 24 | + |
| 25 | +torch.hub.help("intel-isl/MiDaS", "DPT_BEiT_L_384", force_reload=True) # Triggers fresh download of MiDaS repo |
| 26 | +``` |
| 27 | +### **ZoeDepth models** <!-- omit in toc --> |
| 28 | +### Using torch hub |
| 29 | +```python |
| 30 | +import torch |
| 31 | + |
| 32 | +repo = "intel-isl/ZoeDepth" |
| 33 | +# Zoe_N |
| 34 | +model_zoe_n = torch.hub.load(repo, "ZoeD_N", pretrained=True) |
| 35 | + |
| 36 | +# Zoe_K |
| 37 | +model_zoe_k = torch.hub.load(repo, "ZoeD_K", pretrained=True) |
| 38 | + |
| 39 | +# Zoe_NK |
| 40 | +model_zoe_nk = torch.hub.load(repo, "ZoeD_NK", pretrained=True) |
| 41 | +``` |
| 42 | +### Using local copy |
| 43 | +Clone this repo: |
| 44 | +```bash |
| 45 | +git clone https://github.com/isl-org/ZoeDepth.git && cd ZoeDepth |
| 46 | +``` |
| 47 | +#### Using local torch hub |
| 48 | +You can use local source for torch hub to load the ZoeDepth models, for example: |
| 49 | +```python |
| 50 | +import torch |
| 51 | + |
| 52 | +# Zoe_N |
| 53 | +model_zoe_n = torch.hub.load(".", "ZoeD_N", source="local" pretrained=True) |
| 54 | +``` |
| 55 | + |
| 56 | +#### or load the models manually |
| 57 | +```python |
| 58 | +from zoedepth.models.builder import build_model |
| 59 | +from zoedepth.utils.config import get_config |
| 60 | + |
| 61 | +# ZoeD_N |
| 62 | +conf = get_config("zoedepth", "infer") |
| 63 | +model_zoe_n = build_model(conf) |
| 64 | + |
| 65 | +# ZoeD_K |
| 66 | +conf = get_config("zoedepth", "infer", config_version="kitti") |
| 67 | +model_zoe_k = build_model(conf) |
| 68 | + |
| 69 | +# ZoeD_NK |
| 70 | +conf = get_config("zoedepth_nk", "infer") |
| 71 | +model_zoe_nk = build_model(conf) |
| 72 | +``` |
| 73 | + |
| 74 | +### Using ZoeD models to predict depth |
| 75 | +```python |
| 76 | +##### sample prediction |
| 77 | +DEVICE = "cuda" if torch.cuda.is_available() else "cpu" |
| 78 | +zoe = model_zoe_n.to(DEVICE) |
| 79 | + |
| 80 | + |
| 81 | +# Local file |
| 82 | +from PIL import Image |
| 83 | +image = Image.open("/path/to/image.jpg").convert("RGB") # load |
| 84 | +depth_numpy = zoe.infer_pil(image) # as numpy |
| 85 | + |
| 86 | +depth_pil = zoe.infer_pil(image, output_type="pil") # as 16-bit PIL Image |
| 87 | + |
| 88 | +depth_tensor = zoe.infer_pil(image, output_type="tensor") # as torch tensor |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +# Tensor |
| 93 | +from zoedepth.utils.misc import pil_to_batched_tensor |
| 94 | +X = pil_to_batched_tensor(image).to(DEVICE) |
| 95 | +depth_tensor = zoe.infer(X) |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +# From URL |
| 100 | +from zoedepth.utils.misc import get_image_from_url |
| 101 | + |
| 102 | +# Example URL |
| 103 | +URL = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS4W8H_Nxk_rs3Vje_zj6mglPOH7bnPhQitBH8WkqjlqQVotdtDEG37BsnGofME3_u6lDk&usqp=CAU" |
| 104 | + |
| 105 | + |
| 106 | +image = get_image_from_url(URL) # fetch |
| 107 | +depth = zoe.infer_pil(image) |
| 108 | + |
| 109 | +# Save raw |
| 110 | +from zoedepth.utils.misc import save_raw_16bit |
| 111 | +fpath = "/path/to/output.png" |
| 112 | +save_raw_16bit(depth, fpath) |
| 113 | + |
| 114 | +# Colorize output |
| 115 | +from zoedepth.utils.misc import colorize |
| 116 | + |
| 117 | +colored = colorize(depth) |
| 118 | + |
| 119 | +# save colored output |
| 120 | +fpath_colored = "/path/to/output_colored.png" |
| 121 | +Image.fromarray(colored).save(fpath_colored) |
| 122 | +``` |
| 123 | + |
| 124 | +## **Environment setup** |
| 125 | +The project depends on : |
| 126 | +- [pytorch](https://pytorch.org/) (Main framework) |
| 127 | +- [timm](https://timm.fast.ai/) (Backbone helper for MiDaS) |
| 128 | +- pillow, matplotlib, scipy, h5py, opencv (utilities) |
| 129 | + |
| 130 | +Install environment using `environment.yml` : |
| 131 | + |
| 132 | +Using [mamba](https://github.com/mamba-org/mamba) (fastest): |
| 133 | +```bash |
| 134 | +mamba env create -n zoe --file environment.yml |
| 135 | +mamba activate zoe |
| 136 | +``` |
| 137 | +Using conda : |
| 138 | + |
| 139 | +```bash |
| 140 | +conda env create -n zoe --file environment.yml |
| 141 | +conda activate zoe |
| 142 | +``` |
| 143 | + |
| 144 | +## **Sanity checks** (Recommended) |
| 145 | +Check if models can be loaded: |
| 146 | +```bash |
| 147 | +python sanity_hub.py |
| 148 | +``` |
| 149 | +Try a demo prediction pipeline: |
| 150 | +```bash |
| 151 | +python sanity.py |
| 152 | +``` |
| 153 | +This will save a file `pred.png` in the root folder, showing RGB and corresponding predicted depth side-by-side. |
| 154 | +## Model files |
| 155 | +Models are defined under `models/` folder, with `models/<model_name>_<version>.py` containing model definitions and `models/config_<model_name>.json` containing configuration. |
| 156 | + |
| 157 | +Single metric head models (Zoe_N and Zoe_K from the paper) have the common definition and are defined under `models/zoedepth` while as the multi-headed model (Zoe_NK) is defined under `models/zoedepth_nk`. |
| 158 | +## **Evaluation** |
| 159 | +Download the required dataset and change the `DATASETS_CONFIG` dictionary in `utils/config.py` accordingly. |
| 160 | +### Evaluating offical models |
| 161 | +On NYU-Depth-v2 for example: |
| 162 | + |
| 163 | +For ZoeD_N: |
| 164 | +```bash |
| 165 | +python evaluate.py -m zoedepth -d nyu |
| 166 | +``` |
| 167 | + |
| 168 | +For ZoeD_NK: |
| 169 | +```bash |
| 170 | +python evaluate.py -m zoedepth_nk -d nyu |
| 171 | +``` |
| 172 | + |
| 173 | +### Evaluating local checkpoint |
| 174 | +```bash |
| 175 | +python evaluate.py -m zoedepth --pretrained_resource="local::/path/to/local/ckpt.pt" -d nyu |
| 176 | +``` |
| 177 | +Pretrained resources are prefixed with `url::` to indicate weights should be fetched from a url, or `local::` to indicate path is a local file. Refer to `models/model_io.py` for details. |
| 178 | + |
| 179 | +The dataset name should match the corresponding key in `utils.config.DATASETS_CONFIG` . |
| 180 | + |
| 181 | +## **Training** |
| 182 | +Download training datasets as per instructions given [here](https://github.com/cleinc/bts/tree/master/pytorch#nyu-depvh-v2). Then for training a single head model on NYU-Depth-v2 : |
| 183 | +```bash |
| 184 | +python train_mono.py -m zoedepth --pretrained_resource="" |
| 185 | +``` |
| 186 | + |
| 187 | +For training the Zoe-NK model: |
| 188 | +```bash |
| 189 | +python train_mix.py -m zoedepth_nk --pretrained_resource="" |
| 190 | +``` |
| 191 | + |
| 192 | +## **Citation** |
| 193 | +TODO: Add reference here after release |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
0 commit comments