Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 5b5b1dc

Browse files
author
Shariq F. Bhat
committed
Initial release v1.0
1 parent 5a3cb08 commit 5b5b1dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+56309
-4
lines changed

.gitignore

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1+
*.png
2+
**.gif
3+
.vscode/
4+
*.rdb
5+
**.xml
6+
wandb/
7+
slurm/
8+
tmp/
9+
.logs/
10+
checkpoints/
11+
external_jobs/
112
# Byte-compiled / optimized / DLL files
213
__pycache__/
314
*.py[cod]
415
*$py.class
5-
16+
ptlflow_logs/
17+
output/
18+
log/
19+
.idea/
620
# C extensions
721
*.so
8-
22+
results/
23+
**.DS_Store
24+
**.pt
25+
demo/
926
# Distribution / packaging
1027
.Python
1128
build/
@@ -26,7 +43,9 @@ share/python-wheels/
2643
.installed.cfg
2744
*.egg
2845
MANIFEST
29-
46+
~shortcuts/
47+
**/wandb_logs/
48+
**.db
3049
# PyInstaller
3150
# Usually these files are written by a python script from a template
3251
# before PyInstaller builds the exe, so as to inject date/other infos into it.

README.md

Lines changed: 206 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,206 @@
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+

environment.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: zoe
2+
channels:
3+
- pytorch
4+
- nvidia
5+
- conda-forge
6+
dependencies:
7+
- cuda=11.7.1
8+
- h5py=3.7.0
9+
- hdf5=1.12.2
10+
- matplotlib=3.6.2
11+
- matplotlib-base=3.6.2
12+
- numpy=1.24.1
13+
- opencv=4.6.0
14+
- pip=22.3.1
15+
- python=3.9.7
16+
- pytorch=1.13.1
17+
- pytorch-cuda=11.7
18+
- pytorch-mutex=1.0
19+
- scipy=1.10.0
20+
- torchaudio=0.13.1
21+
- torchvision=0.14.1
22+
- pip:
23+
- huggingface-hub==0.11.1
24+
- timm==0.6.12
25+
- tqdm==4.64.1
26+
- wandb==0.13.9

0 commit comments

Comments
 (0)