-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
349 lines (300 loc) · 12.2 KB
/
test.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import argparse
import gc
import os
import numpy as np
import torch
import torch.nn.functional as F
from tqdm import tqdm
from datasets.messytable import MessytableDataset
from utils.cascade_metrics import compute_err_metric, compute_obj_err
from configs.config import cfg
from utils.test_util import (load_from_dataparallel_model, save_img, save_obj_err_file)
from utils.util import (depth_error_img, disp_error_img, get_time_string,
setup_logger)
from utils.warp_ops import apply_disparity_cu
from utils.losses import AllLosses # put all new losses here
parser = argparse.ArgumentParser()
parser.add_argument(
"--config-file",
type=str,
default="./configs/temp.yaml",
metavar="FILE",
help="Config files",
)
parser.add_argument(
"--local_rank", type=int, default=0, help="Rank of device in distributed training"
)
args = parser.parse_args()
cfg.merge_from_file(args.config_file)
cuda_device = torch.device("cuda:{}".format(args.local_rank))
# Tensorboard and logger
os.makedirs(cfg.SOLVER.LOGDIR, exist_ok=True)
log_dir = os.path.join(cfg.SOLVER.LOGDIR, f"{get_time_string()}")
os.mkdir(log_dir)
logger = setup_logger(
cfg.NAME, distributed_rank=args.local_rank, save_dir=cfg.SOLVER.LOGDIR
)
logger.info(f"Input args:\n{args}")
logger.info(f"Running with configs:\n{cfg}")
def test(model, adapter, loss_class, test_loader, logger, log_dir):
if cfg.MODEL.ADAPTER:
adapter_model = adapter[0]
adapter_model.eval()
model.eval()
total_err_metrics = {
"epe": 0,
"bad1": 0,
"bad2": 0,
"depth_abs_err": 0,
"depth_err2": 0,
"depth_err4": 0,
"depth_err8": 0,
}
total_obj_disp_err = np.zeros(cfg.SIM.OBJ_NUM)
total_obj_depth_err = np.zeros(cfg.SIM.OBJ_NUM)
total_obj_depth_4_err = np.zeros(cfg.SIM.OBJ_NUM)
total_obj_count = np.zeros(cfg.SIM.OBJ_NUM)
os.mkdir(os.path.join(log_dir, "pred_disp"))
os.mkdir(os.path.join(log_dir, "gt_disp"))
os.mkdir(os.path.join(log_dir, "pred_disp_abs_err_cmap"))
os.mkdir(os.path.join(log_dir, "pred_depth"))
os.mkdir(os.path.join(log_dir, "gt_depth"))
os.mkdir(os.path.join(log_dir, "pred_depth_abs_err_cmap"))
for iteration, data in enumerate(tqdm(test_loader)):
if cfg.LOSSES.ONREAL:
img_L = data["img_real_L"].cuda() # [bs, 1, H, W]
img_R = data["img_real_R"].cuda()
else:
img_L = data["img_sim_L"].cuda() # [bs, 1, H, W]
img_R = data["img_sim_R"].cuda()
img_disp_l = data["img_disp_L"].cuda()
img_depth_l = data["img_depth_L"].cuda()
img_label = data["img_label"].cuda()
img_focal_length = data["focal_length"].cuda()
img_baseline = data["baseline"].cuda()
prefix = data["prefix"][0]
img_disp_l = F.interpolate(
img_disp_l, (540, 960), mode="nearest", recompute_scale_factor=False
)
img_depth_l = F.interpolate(
img_depth_l, (540, 960), mode="nearest", recompute_scale_factor=False
)
img_label = F.interpolate(
img_label, (540, 960), mode="nearest", recompute_scale_factor=False
).type(torch.int)
img_disp_r = data["img_disp_R"].cuda()
img_depth_r = data["img_depth_R"].cuda()
img_disp_r = F.interpolate(
img_disp_r, (540, 960), mode="nearest", recompute_scale_factor=False
)
img_depth_r = F.interpolate(
img_depth_r, (540, 960), mode="nearest", recompute_scale_factor=False
)
img_disp_l = apply_disparity_cu(img_disp_r, img_disp_r.type(torch.int))
img_depth_l = apply_disparity_cu(img_depth_r, img_disp_r.type(torch.int)) # [bs, 1, H, W]
# If test on real dataset need to crop input image to (540, 960)
if cfg.LOSSES.ONREAL:
robot_mask = data["robot_mask"].cuda()
img_robot_mask = F.interpolate(
robot_mask.unsqueeze(0), (540, 960), mode="nearest", recompute_scale_factor=False
).type(torch.int)
img_L = F.interpolate(
img_L,
(540, 960),
mode="bilinear",
recompute_scale_factor=False,
align_corners=False,
)
img_R = F.interpolate(
img_R,
(540, 960),
mode="bilinear",
recompute_scale_factor=False,
align_corners=False,
)
if cfg.MODEL.ADAPTER:
with torch.no_grad():
img_L_transformed, img_R_transformed = adapter_model(img_L, img_R)
# Pad the imput image and depth disp image to 960 * 544
right_pad = cfg.REAL.PAD_WIDTH - 960
top_pad = cfg.REAL.PAD_HEIGHT - 540
img_L = F.pad(
img_L, (0, right_pad, top_pad, 0, 0, 0, 0, 0), mode="constant", value=0
)
img_R = F.pad(
img_R, (0, right_pad, top_pad, 0, 0, 0, 0, 0), mode="constant", value=0
)
if cfg.MODEL.ADAPTER:
img_L_transformed = F.pad(
img_L_transformed,
(0, right_pad, top_pad, 0, 0, 0, 0, 0),
mode="constant",
value=0,
)
img_R_transformed = F.pad(
img_R_transformed,
(0, right_pad, top_pad, 0, 0, 0, 0, 0),
mode="constant",
value=0,
)
if cfg.LOSSES.ONREAL:
robot_mask = img_robot_mask == 0
else:
robot_mask = torch.ones(img_depth_l.shape).cuda()==1
if cfg.LOSSES.EXCLUDE_BG:
# Mask ground pixel to False
img_ground_mask = (img_depth_l > 0) & (img_depth_l < 1.25)
mask = (
(img_disp_l < cfg.MODEL.MAX_DISP)
* (img_disp_l > 0)
* img_ground_mask
* robot_mask
)
else:
mask = (img_disp_l < cfg.MODEL.MAX_DISP) * (img_disp_l > 0) * robot_mask
# Exclude uncertain pixel from realsense_depth_pred
if cfg.LOSSES.EXCLUDE_ZEROS:
if cfg.LOSSES.ONREAL:
img_depth_realsense = data["img_depth_real_realsense"].cuda()
else:
img_depth_realsense = data["img_depth_sim_realsense"].cuda()
img_depth_realsense = F.interpolate(
img_depth_realsense.unsqueeze(0),
(540, 960),
mode="nearest",
recompute_scale_factor=False,
)
realsense_zeros_mask = img_depth_realsense > 0
mask = mask * realsense_zeros_mask
mask = mask.type(torch.bool)
ground_mask = (
torch.logical_not(mask).squeeze(0).squeeze(0).detach().cpu().numpy()
)
values = {
'img_L': img_L,
'img_R': img_R,
}
if cfg.MODEL.ADAPTER:
values['img_L_transformed'] = img_L_transformed
values['img_R_transformed'] = img_R_transformed
output, pred_disp = loss_class.forward(values, train=False)
pred_disp = pred_disp[:, :, top_pad:, :] if right_pad == 0 else pred_disp[:, :, top_pad:, :-right_pad]
pred_depth = img_focal_length * img_baseline / pred_disp # pred depth in m
# Get loss metric
err_metrics = compute_err_metric(
img_disp_l, img_depth_l, pred_disp, img_focal_length, img_baseline, mask
)
for k in total_err_metrics.keys():
total_err_metrics[k] += err_metrics[k]
logger.info(f"Test instance {prefix} - {err_metrics}")
# Get object error
obj_disp_err, obj_depth_err, obj_depth_4_err, obj_count = compute_obj_err(
img_disp_l,
img_depth_l,
pred_disp,
img_focal_length,
img_baseline,
img_label,
mask,
cfg.SIM.OBJ_NUM,
)
total_obj_disp_err += obj_disp_err
total_obj_depth_err += obj_depth_err
total_obj_depth_4_err += obj_depth_4_err
total_obj_count += obj_count
# Get disparity image
pred_disp_np = pred_disp.squeeze(0).squeeze(0).detach().cpu().numpy() # [H, W]
pred_disp_np[ground_mask] = -1
# Get disparity ground truth image
gt_disp_np = img_disp_l.squeeze(0).squeeze(0).detach().cpu().numpy()
gt_disp_np[ground_mask] = -1
# Get disparity error image
pred_disp_err_np = disp_error_img(pred_disp, img_disp_l, mask)
# Get depth image
pred_depth_np = (
pred_depth.squeeze(0).squeeze(0).detach().cpu().numpy()
) # in m, [H, W]
# crop depth map to [0.2m, 2m]
# pred_depth_np[pred_depth_np < 0.2] = -1
# pred_depth_np[pred_depth_np > 2] = -1
pred_depth_np[ground_mask] = -1
# Get depth ground truth image
gt_depth_np = img_depth_l.squeeze(0).squeeze(0).detach().cpu().numpy()
gt_depth_np[ground_mask] = -1
# Get depth error image
pred_depth_err_np = depth_error_img(pred_depth * 1000, img_depth_l * 1000, mask)
# Save images
save_img(
log_dir,
prefix,
pred_disp_np,
gt_disp_np,
pred_disp_err_np,
pred_depth_np,
gt_depth_np,
pred_depth_err_np,
)
# Get final error metrics
for k in total_err_metrics.keys():
total_err_metrics[k] /= len(test_loader)
logger.info(f"\nTest on {len(test_loader)} instances\n {total_err_metrics}")
# Save object error to csv file
total_obj_disp_err /= total_obj_count
total_obj_depth_err /= total_obj_count
total_obj_depth_4_err /= total_obj_count
save_obj_err_file(
total_obj_disp_err, total_obj_depth_err, total_obj_depth_4_err, log_dir
)
logger.info(f"Successfully saved object error to obj_err.txt")
# Get error on real and 3d printed objects
real_depth_error = 0
real_depth_error_4mm = 0
printed_depth_error = 0
printed_depth_error_4mm = 0
real_obj_id = cfg.REAL.OBJ
for i in range(cfg.SIM.OBJ_NUM):
if i in real_obj_id:
real_depth_error += total_obj_depth_err[i]
real_depth_error_4mm += total_obj_depth_4_err[i]
else:
printed_depth_error += total_obj_depth_err[i]
printed_depth_error_4mm += total_obj_depth_4_err[i]
real_depth_error /= len(real_obj_id)
real_depth_error_4mm /= len(real_obj_id)
printed_depth_error /= cfg.SIM.OBJ_NUM - len(real_obj_id)
printed_depth_error_4mm /= cfg.SIM.OBJ_NUM - len(real_obj_id)
logger.info(
f"Real objects - absolute depth error: {real_depth_error}, depth 4mm: {real_depth_error_4mm} \n"
f"3D printed objects - absolute depth error {printed_depth_error}, depth 4mm: {printed_depth_error_4mm}"
)
if __name__ == "__main__":
test_dataset = MessytableDataset(split_sim=cfg.SIM.TEST, split_real = cfg.REAL.TEST, onReal=True, train=False)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, num_workers=0)
logger.info(f"Loaded the checkpoint: {cfg.MODEL.CHECKPOINT}")
if cfg.MODEL.ADAPTER:
from nets.adapter import Adapter
adapter_model = Adapter().to(cuda_device)
adapter_model_dict = load_from_dataparallel_model(cfg.MODEL.CHECKPOINT, "Adapter")
adapter_model.load_state_dict(adapter_model_dict)
backbone = cfg.MODEL.BACKBONE
if backbone=="psmnet" and cfg.MODEL.ADAPTER:
from nets.psmnet.psmnet import PSMNet
model = PSMNet(maxdisp=cfg.MODEL.MAX_DISP).to(cuda_device)
elif backbone=="psmnet":
from nets.psmnet.psmnet_3 import PSMNet
model = PSMNet(maxdisp=cfg.MODEL.MAX_DISP).to(cuda_device)
elif backbone=="dispnet":
from nets.dispnet.dispnet import DispNet
model = DispNet().to(cuda_device)
elif backbone=="raft":
from nets.raft.raft_stereo import RAFTStereo
model = RAFTStereo().to(cuda_device)
else:
print("Model not implemented!")
model_dict = load_from_dataparallel_model(cfg.MODEL.CHECKPOINT, "Model")
model.load_state_dict(model_dict)
loss_class = AllLosses(model, cfg.MODEL.BACKBONE, cfg.MODEL.ADAPTER)
if cfg.MODEL.ADAPTER:
test(model, [adapter_model], loss_class, test_loader, logger, log_dir)
else:
test(model, [], loss_class, test_loader, logger, log_dir)