-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathevaluate_cityscapes.py
executable file
·298 lines (227 loc) · 11.8 KB
/
evaluate_cityscapes.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from __future__ import absolute_import, division, print_function
import os
import cv2
import numpy as np
import torch
from torch.utils.data import DataLoader
import argparse
from layers import disp_to_depth
import datasets
import networks
from utils.utils import readlines
from utils.evaluation_utils import *
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")
STEREO_SCALE_FACTOR = 10*datasets.CityScapesDataset.base_line
def evaluate_depth(opt):
"""Evaluates a pretrained model using a specified test set
"""
MIN_DEPTH = datasets.CityScapesDataset.min_depth
MAX_DEPTH = datasets.CityScapesDataset.max_depth
assert sum((opt.eval_mono, opt.eval_stereo)) == 1, \
"Please choose mono or stereo evaluation by setting either --eval_mono or --eval_stereo"
device = torch.device("cpu" if opt.no_cuda else "cuda")
if opt.ext_disp_to_eval is None:
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))
filenames = readlines(os.path.join(splits_dir, "cityscapes", "test_files.txt"))
depth_encoder_path = os.path.join(opt.load_weights_folder, "encoder.pth")
depth_decoder_path = os.path.join(opt.load_weights_folder, "depth.pth")
encoder_dict = torch.load(depth_encoder_path)
img_ext = '.png'
dataset = datasets.CityScapesDataset(opt.data_path, filenames,
encoder_dict['height'], encoder_dict['width'],
[0], 4, is_train=False, img_ext=img_ext)
dataloader = DataLoader(dataset, opt.batch_size, shuffle=False, num_workers=opt.num_workers,
pin_memory=True, drop_last=False)
depth_encoder = networks.ResnetEncoder(18, False)
depth_decoder = networks.DepthDecoder(depth_encoder.num_ch_enc)
model_dict = depth_encoder.state_dict()
depth_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(depth_decoder_path))
depth_encoder.to(device)
depth_encoder.eval()
depth_decoder.to(device)
depth_decoder.eval()
pred_disps = []
gt_depths = []
object_masks = []
print("-> Computing predictions with size {}x{}".format(
encoder_dict['width'], encoder_dict['height']))
with torch.no_grad():
for data in dataloader:
input_color = data[("color", 0, 0)].to(device)
gt_depth = data[("depth_gt", 0)]
gt_depth = gt_depth.cpu()[:, 0].numpy()
gt_depths.append(gt_depth)
object_mask = data[("object_mask", 0)]
object_mask = object_mask.cpu()[:, 0].numpy()
object_masks.append(object_mask)
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)
output = depth_decoder(depth_encoder(input_color))
pred_disp, _ = disp_to_depth(output[("disp", 0, 0)], opt.min_depth, opt.max_depth)
pred_disp = pred_disp.cpu()[:, 0].numpy()
if opt.post_process:
N = pred_disp.shape[0] // 2
pred_disp = batch_post_process_disparity(pred_disp[:N], pred_disp[N:, :, ::-1])
pred_disps.append(pred_disp)
pred_disps = np.concatenate(pred_disps)
gt_depths = np.concatenate(gt_depths)
object_masks = np.concatenate(object_masks)
else:
# Load predictions from file
print("-> Loading predictions from {}".format(opt.ext_disp_to_eval))
pred_disps = np.load(opt.ext_disp_to_eval)
if opt.save_pred_disps:
output_path = os.path.join(
opt.load_weights_folder, "disps_{}_split.npy".format("cityscapes"))
print("-> Saving predicted disparities to ", output_path)
np.save(output_path, pred_disps)
gt_path = os.path.join(splits_dir, "cityscapes", "gt_depths.npz")
object_mask_path = os.path.join(splits_dir, "cityscapes", "object_mask.npz")
if "gt_depths" in vars() and "object_masks" in vars():
if not os.path.exists(gt_path):
np.savez_compressed(gt_path, data=gt_depths)
if not os.path.exists(object_mask_path):
np.savez_compressed(object_mask_path, data=object_masks)
else:
if os.path.exists(gt_path):
gt_depths = np.load(gt_path, fix_imports=True, encoding='latin1')["data"]
else:
raise RuntimeError('Execute prediction once to save the gt depths and object masks first.')
if os.path.exists(object_mask_path):
object_masks = np.load(object_mask_path, fix_imports=True, encoding='latin1')["data"]
else:
raise RuntimeError('Execute prediction once to save the gt depths and object masks first.')
print("-> Evaluating")
if opt.eval_stereo:
print("Stereo evaluation - "
"disabling median scaling, scaling by {}".format(STEREO_SCALE_FACTOR))
opt.disable_median_scaling = True
opt.pred_depth_scale_factor = STEREO_SCALE_FACTOR
else:
print("Mono evaluation - using median scaling")
ratios = []
background_errors = []
background_pixel_nums = []
object_errors = []
object_pixel_nums = []
for i in range(pred_disps.shape[0]):
gt_depth = gt_depths[i]
gt_height, gt_width = gt_depth.shape[:2]
pred_disp = pred_disps[i]
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)
background_mask = np.logical_and(object_masks[i] == 0, mask)
background_pred_depth = pred_depth[background_mask]
background_gt_depth = gt_depth[background_mask]
object_mask = np.logical_and(object_masks[i] > 0, mask)
object_pred_depth = pred_depth[object_mask]
object_gt_depth = gt_depth[object_mask]
background_pred_depth *= opt.pred_depth_scale_factor
object_pred_depth *= opt.pred_depth_scale_factor
if not opt.disable_median_scaling:
ratio = np.median(background_gt_depth) / np.median(background_pred_depth)
ratios.append(ratio)
background_pred_depth *= ratio
object_pred_depth *= ratio
background_pred_depth[background_pred_depth < MIN_DEPTH] = MIN_DEPTH
background_pred_depth[background_pred_depth > MAX_DEPTH] = MAX_DEPTH
background_errors.append(compute_errors(background_gt_depth, background_pred_depth))
background_pixel_nums.append(background_pred_depth.shape[0])
object_pred_depth[object_pred_depth < MIN_DEPTH] = MIN_DEPTH
object_pred_depth[object_pred_depth > MAX_DEPTH] = MAX_DEPTH
object_errors.append(compute_errors(object_gt_depth, object_pred_depth))
object_pixel_nums.append(object_pred_depth.shape[0])
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)))
mean_background_errors = np.average(np.nan_to_num(np.asarray(background_errors)),
axis=0, weights=np.asarray(background_pixel_nums))
print("\n Background: \n" + ("{:>8} | " * 7).format("abs_rel", "sq_rel", "rmse", "rmse_log", "a1", "a2", "a3"))
print(("&{: 8.3f} " * 7).format(*mean_background_errors.tolist()) + "\\\\")
mean_object_errors = np.average(np.nan_to_num(np.asarray(object_errors)),
axis=0, weights=np.asarray(object_pixel_nums))
print("\n Objects: \n" + ("{:>8} | " * 7).format("abs_rel", "sq_rel", "rmse", "rmse_log", "a1", "a2", "a3"))
print(("&{: 8.3f} " * 7).format(*mean_object_errors.tolist()) + "\\\\")
print("\n-> Done!")
print(sum(background_pixel_nums))
print(sum(object_pixel_nums))
result_path = os.path.join(opt.load_weights_folder, "result_{}_split.txt".format("cityscapes"))
f = open(result_path, 'w+')
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)), file=f)
print("\n Background: \n" + ("{:>8} | " * 7).format("abs_rel", "sq_rel", "rmse", "rmse_log",
"a1", "a2", "a3"), file=f)
print(("&{: 8.3f} " * 7).format(*mean_background_errors.tolist()) + "\\\\", file=f)
print("\n Objects: \n" + ("{:>8} | " * 7).format("abs_rel", "sq_rel", "rmse", "rmse_log",
"a1", "a2", "a3"), file=f)
print(("&{: 8.3f} " * 7).format(*mean_object_errors.tolist()) + "\\\\", file=f)
print("\n-> Done!", file=f)
f.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Monocular Depth Evaluation Options for CityScapes")
parser.add_argument("--data_path",
type=str,
help="path of the training data",
required=True)
parser.add_argument("--load_weights_folder",
type=str,
help="name of model to load")
parser.add_argument("--eval_stereo",
help="if set evaluates in stereo mode",
action="store_true")
parser.add_argument("--eval_mono",
help="if set evaluates in mono mode",
action="store_true")
parser.add_argument("--disable_median_scaling",
help="if set disables median scaling in evaluation",
action="store_true")
parser.add_argument("--pred_depth_scale_factor",
help="if set multiplies predictions by this number",
type=float,
default=1.0)
parser.add_argument("--ext_disp_to_eval",
type=str,
help="optional path to a .npy disparities file to evaluate")
parser.add_argument("--save_pred_disps",
help="if set saves predicted disparities",
action="store_true")
parser.add_argument("--post_process",
help="if set will perform the flipping post processing "
"from the original monodepth paper",
action="store_true")
parser.add_argument("--batch_size",
type=int,
help="batch size",
default=16)
parser.add_argument("--num_workers",
type=int,
help="number of dataloader workers",
default=8)
parser.add_argument("--png",
help="if set, trains from raw png files (instead of jpgs)",
action="store_true")
parser.add_argument("--no_cuda",
help="if set disables CUDA",
action="store_true")
parser.add_argument("--min_depth",
type=float,
help="minimum depth",
default=0.1)
parser.add_argument("--max_depth",
type=float,
help="maximum depth",
default=100.0)
opt = parser.parse_args()
opt.height = 192
opt.width = 512
evaluate_depth(opt)