-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluate_depth.py
222 lines (189 loc) · 9.51 KB
/
evaluate_depth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from __future__ import absolute_import, division, print_function
import os
import cv2
import numpy as np
from tqdm import tqdm
import time
import torch
from torch.utils.data import DataLoader
from PIL import Image
import matplotlib
import scipy.stats as st
from utils.layers import disp_to_depth
from utils.utils import readlines, compute_errors
from options import MonodepthOptions
import datasets
import models.encoders as encoders
import models.decoders as decoders
import models.endodac as endodac
cv2.setNumThreads(0) # This speeds up evaluation 5x on our unix systems (OpenCV 3.3.1)
splits_dir = os.path.join(os.path.dirname(__file__), "splits")
def render_depth(disp):
disp = (disp - disp.min()) / (disp.max() - disp.min()) * 255.0
disp = disp.astype(np.uint8)
disp_color = cv2.applyColorMap(disp, cv2.COLORMAP_INFERNO)
return disp_color
def batch_post_process_disparity(l_disp, r_disp):
"""Apply the disparity post-processing method as introduced in Monodepthv1
"""
_, h, w = l_disp.shape
m_disp = 0.5 * (l_disp + r_disp)
l, _ = np.meshgrid(np.linspace(0, 1, w), np.linspace(0, 1, h))
l_mask = (1.0 - np.clip(20 * (l - 0.05), 0, 1))[None, ...]
r_mask = l_mask[:, :, ::-1]
return r_mask * l_disp + l_mask * r_disp + (1.0 - l_mask - r_mask) * m_disp
def evaluate(opt):
"""Evaluates a pretrained model using a specified test set
"""
MIN_DEPTH = 1e-3
MAX_DEPTH = 150
assert sum((opt.eval_mono, opt.eval_stereo)) == 1, \
"Please choose mono or stereo evaluation by setting either --eval_mono or --eval_stereo"
if opt.ext_disp_to_eval is None:
if not opt.model_type == 'depthanything':
opt.load_weights_folder = os.path.expanduser(opt.load_weights_folder)
assert os.path.isdir(opt.load_weights_folder), \
"Cannot find a folder at {}".format(opt.load_weights_folder)
print("-> Loading weights from {}".format(opt.load_weights_folder))
else:
print("Evaluating Depth Anything model")
if opt.model_type == 'endodac':
depther_path = os.path.join(opt.load_weights_folder, "depth_model.pth")
depther_dict = torch.load(depther_path)
elif opt.model_type == 'afsfm':
encoder_path = os.path.join(opt.load_weights_folder, "encoder.pth")
decoder_path = os.path.join(opt.load_weights_folder, "depth.pth")
encoder_dict = torch.load(encoder_path)
if opt.eval_split == 'endovis':
filenames = readlines(os.path.join(splits_dir, opt.eval_split, "test_files.txt"))
dataset = datasets.SCAREDRAWDataset(opt.data_path, filenames,
opt.height, opt.width,
[0], 4, is_train=False)
elif opt.eval_split == 'hamlyn':
dataset = datasets.HamlynDataset(opt.data_path, opt.height, opt.width,
[0], 4, is_train=False)
elif opt.eval_split == 'c3vd':
dataset = datasets.C3VDDataset(opt.data_path, opt.height, opt.width,
[0], 4, is_train=False)
MAX_DEPTH = 100
dataloader = DataLoader(dataset, 1, shuffle=False, num_workers=opt.num_workers,
pin_memory=True, drop_last=False)
if opt.model_type == 'endodac':
depther = endodac.endodac(
backbone_size = "base", r=opt.lora_rank, lora_type=opt.lora_type,
image_shape=(224,280), pretrained_path=opt.pretrained_path,
residual_block_indexes=opt.residual_block_indexes,
include_cls_token=opt.include_cls_token)
model_dict = depther.state_dict()
depther.load_state_dict({k: v for k, v in depther_dict.items() if k in model_dict})
depther.cuda()
depther.eval()
elif opt.model_type == 'afsfm':
encoder = encoders.ResnetEncoder(opt.num_layers, False)
depth_decoder = decoders.DepthDecoder(encoder.num_ch_enc, scales=range(4))
model_dict = encoder.state_dict()
encoder.load_state_dict({k: v for k, v in encoder_dict.items() if k in model_dict})
depth_decoder.load_state_dict(torch.load(decoder_path))
depther = lambda image: depth_decoder(encoder(image))
encoder.cuda()
encoder.eval()
depth_decoder.cuda()
depth_decoder.eval()
else:
print("-> Loading predictions from {}".format(opt.ext_disp_to_eval))
pred_disps = np.load(opt.ext_disp_to_eval)
if opt.eval_split == 'endovis':
filenames = readlines(os.path.join(splits_dir, opt.eval_split, "test_files.txt"))
dataset = datasets.SCAREDRAWDataset(opt.data_path, filenames,
opt.height, opt.width,
[0], 4, is_train=False)
elif opt.eval_split == 'hamlyn':
dataset = datasets.HamlynDataset(opt.data_path, opt.height, opt.width,
[0], 4, is_train=False)
elif opt.eval_split == 'c3vd':
dataset = datasets.C3VDDataset(opt.data_path, opt.height, opt.width,
[0], 4, is_train=False)
MAX_DEPTH = 100
dataloader = DataLoader(dataset, 1, shuffle=False, num_workers=opt.num_workers,
pin_memory=True, drop_last=False)
if opt.eval_split == 'endovis':
gt_path = os.path.join(splits_dir, opt.eval_split, "gt_depths.npz")
gt_depths = np.load(gt_path, fix_imports=True, encoding='latin1')["data"]
if opt.visualize_depth:
vis_dir = os.path.join(opt.load_weights_folder, "vis_depth")
os.makedirs(vis_dir, exist_ok=True)
inference_times = []
sequences = []
keyframes = []
frame_ids = []
errors = []
ratios = []
print("-> Computing predictions with size {}x{}".format(
opt.width, opt.height))
with torch.no_grad():
for i, data in tqdm(enumerate(dataloader)):
input_color = data[("color", 0, 0)].cuda()
if opt.post_process:
# Post-processed results require each image to have two forward passes
input_color = torch.cat((input_color, torch.flip(input_color, [3])), 0)
if opt.ext_disp_to_eval is None:
time_start = time.time()
output = depther(input_color)
inference_time = time.time() - time_start
if opt.model_type == 'endodac' or opt.model_type == 'afsfm':
output_disp = output[("disp", 0)]
pred_disp, _ = disp_to_depth(output_disp, opt.min_depth, opt.max_depth)
pred_disp = pred_disp.cpu()[:, 0].numpy()
pred_disp = pred_disp[0]
else:
pred_disp = pred_disps[i]
inference_time = 1
inference_times.append(inference_time)
if opt.eval_split == 'endovis':
gt_depth = gt_depths[i]
sequence = str(np.array(data['sequence'][0]))
keyframe = str(np.array(data['keyframe'][0]))
frame_id = "{:06d}".format(data['frame_id'][0])
elif opt.eval_split == 'hamlyn' or opt.eval_split == 'c3vd':
gt_depth = data["depth_gt"].squeeze().numpy()
gt_height, gt_width = gt_depth.shape[:2]
pred_disp = cv2.resize(pred_disp, (gt_width, gt_height))
pred_depth = 1/pred_disp
mask = np.logical_and(gt_depth > MIN_DEPTH, gt_depth < MAX_DEPTH)
if opt.visualize_depth:
vis_pred_depth = render_depth(pred_disp)
vis_file_name = os.path.join(vis_dir, sequence + "_" + keyframe + "_" + frame_id + ".png")
cv2.imwrite(vis_file_name, vis_pred_depth)
pred_depth = pred_depth[mask]
gt_depth = gt_depth[mask]
pred_depth *= opt.pred_depth_scale_factor
if not opt.disable_median_scaling:
ratio = np.median(gt_depth) / np.median(pred_depth)
if not np.isnan(ratio).all():
ratios.append(ratio)
pred_depth *= ratio
pred_depth[pred_depth < MIN_DEPTH] = MIN_DEPTH
pred_depth[pred_depth > MAX_DEPTH] = MAX_DEPTH
error = compute_errors(gt_depth, pred_depth)
if not np.isnan(error).all():
errors.append(error)
if not opt.disable_median_scaling:
ratios = np.array(ratios)
med = np.median(ratios)
print(" Scaling ratios | med: {:0.3f} | std: {:0.3f}".format(med, np.std(ratios / med)))
errors = np.array(errors)
mean_errors = np.mean(errors, axis=0)
cls = []
for i in range(len(mean_errors)):
cl = st.t.interval(alpha=0.95, df=len(errors)-1, loc=mean_errors[i], scale=st.sem(errors[:,i]))
cls.append(cl[0])
cls.append(cl[1])
cls = np.array(cls)
print("\n " + ("{:>11} | " * 7).format("abs_rel", "sq_rel", "rmse", "rmse_log", "a1", "a2", "a3"))
print("mean:" + ("&{: 12.3f} " * 7).format(*mean_errors.tolist()) + "\\\\")
print("cls: " + ("& [{: 6.3f}, {: 6.3f}] " * 7).format(*cls.tolist()) + "\\\\")
print("average inference time: {:0.1f} ms".format(np.mean(np.array(inference_times))*1000))
print("\n-> Done!")
if __name__ == "__main__":
options = MonodepthOptions()
evaluate(options.parse())