-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_fps.py
639 lines (545 loc) · 32.5 KB
/
eval_fps.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
"""
Copyright (C) 2019 NVIDIA Corporation. All rights reserved.
Licensed under the CC BY-NC-SA 4.0 license
(https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '1'
import torch
from torch.utils.data import DataLoader
import torch.nn.functional as F
from tqdm import tqdm
import numpy as np
import sys
import cv2
import copy
import glob
import time
from models.model import *
from models.refinement_net import RefineModel
from models.modules import *
from datasets.plane_stereo_dataset import PlaneDataset
from datasets.inference_dataset import InferenceDataset
from datasets.nyu_dataset import NYUDataset
from utils import *
from visualize_utils import *
from evaluate_utils import *
from plane_utils import *
from options import parse_args
from config import InferenceConfig
from metrics import *
# 39 58 99 605 615
class PlaneRCNNDetector():
def __init__(self, options, config, modelType, checkpoint_dir=''):
self.options = options
self.config = config
self.modelType = modelType
self.model = MaskRCNN_edge_fpn_resolution(config)
self.model.cuda()
self.model.eval()
if modelType == 'basic':
checkpoint_dir = checkpoint_dir if checkpoint_dir != '' else 'checkpoint/pair_' + options.anchorType
elif modelType == 'pair':
checkpoint_dir = checkpoint_dir if checkpoint_dir != '' else 'checkpoint/pair_' + options.anchorType
elif modelType == 'refine':
checkpoint_dir = checkpoint_dir if checkpoint_dir != '' else 'checkpoint/instance_' + options.anchorType
elif modelType == 'refine_single':
checkpoint_dir = checkpoint_dir if checkpoint_dir != '' else 'checkpoint/refinement_' + options.anchorType
elif modelType == 'occlusion':
checkpoint_dir = checkpoint_dir if checkpoint_dir != '' else 'checkpoint/plane_' + options.anchorType
elif modelType == 'final':
checkpoint_dir = checkpoint_dir if checkpoint_dir != '' else 'checkpoint/planercnn_' + options.anchorType
pass
if options.suffix != '':
checkpoint_dir += '_' + options.suffix
pass
## Indicates that the refinement network is trained separately
separate = modelType == 'refine'
checkpoint_dir += '_ablation_edge_fpn_resolution'
if not separate:
if options.startEpoch >= 0:
self.model.load_state_dict(torch.load(checkpoint_dir + '/checkpoint_' + str(options.startEpoch) + '.pth'))
else:
self.model.load_state_dict(torch.load(checkpoint_dir + '/checkpoint.pth'))
pass
pass
if 'refine' in modelType or 'final' in modelType:
self.refine_model = RefineModel(options)
self.refine_model.cuda()
self.refine_model.eval()
if not separate:
state_dict = torch.load(checkpoint_dir + '/checkpoint_refine.pth')
self.refine_model.load_state_dict(state_dict)
pass
else:
self.model.load_state_dict(torch.load('checkpoint/pair_' + options.anchorType + '_pair/checkpoint.pth'))
self.refine_model.load_state_dict(torch.load('checkpoint/instance_normal_refine_mask_softmax_valid/checkpoint_refine.pth'))
pass
pass
return
def detect(self, sample):
input_pair = []
detection_pair = []
camera = sample[30][0].cuda()
for indexOffset in [0, ]:
images, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, gt_depth, extrinsics, planes, gt_segmentation = sample[indexOffset + 0].cuda(), sample[indexOffset + 1].numpy(), sample[indexOffset + 2].cuda(), sample[indexOffset + 3].cuda(), sample[indexOffset + 4].cuda(), sample[indexOffset + 5].cuda(), sample[indexOffset + 6].cuda(), sample[indexOffset + 7].cuda(), sample[indexOffset + 8].cuda(), sample[indexOffset + 9].cuda(), sample[indexOffset + 10].cuda(), sample[indexOffset + 11].cuda()
rpn_class_logits, rpn_pred_bbox, target_class_ids, mrcnn_class_logits, target_deltas, mrcnn_bbox, target_mask, mrcnn_mask, target_parameters, mrcnn_parameters, detections, detection_masks, detection_gt_parameters, detection_gt_masks, rpn_rois, roi_features, roi_indices, depth_np_pred, _ = self.model.predict([images, image_metas, gt_class_ids, gt_boxes, gt_masks, gt_parameters, camera], mode='inference_detection', use_nms=2, use_refinement=True)
if len(detections) > 0:
detections, detection_masks = unmoldDetections(self.config, camera, detections, detection_masks, depth_np_pred, debug=False)
pass
XYZ_pred, detection_mask, plane_XYZ = calcXYZModule(self.config, camera, detections, detection_masks, depth_np_pred, return_individual=True)
detection_mask = detection_mask.unsqueeze(0)
input_pair.append({'image': images, 'depth': gt_depth, 'mask': gt_masks, 'bbox': gt_boxes, 'extrinsics': extrinsics, 'segmentation': gt_segmentation, 'camera': camera})
if 'nyu_dorn_only' in self.options.dataset:
XYZ_pred[1:2] = sample[27].cuda()
pass
detection_pair.append({'XYZ': XYZ_pred, 'depth': XYZ_pred[1:2], 'mask': detection_mask, 'detection': detections, 'masks': detection_masks, 'depth_np': depth_np_pred, 'plane_XYZ': plane_XYZ})
continue
if ('refine' in self.modelType or 'refine' in self.options.suffix):
pose = sample[26][0].cuda()
pose = torch.cat([pose[0:3], pose[3:6] * pose[6]], dim=0)
pose_gt = torch.cat([pose[0:1], -pose[2:3], pose[1:2], pose[3:4], -pose[5:6], pose[4:5]], dim=0).unsqueeze(0)
camera = camera.unsqueeze(0)
for c in range(1):
detection_dict, input_dict = detection_pair[c], input_pair[c]
new_input_dict = {k: v for k, v in input_dict.items()}
new_input_dict['image'] = (input_dict['image'] + self.config.MEAN_PIXEL_TENSOR.view((-1, 1, 1))) / 255.0 - 0.5
new_input_dict['image_2'] = (sample[13].cuda() + self.config.MEAN_PIXEL_TENSOR.view((-1, 1, 1))) / 255.0 - 0.5
detections = detection_dict['detection']
detection_masks = detection_dict['masks']
depth_np = detection_dict['depth_np']
image = new_input_dict['image']
image_2 = new_input_dict['image_2']
depth_gt = new_input_dict['depth'].unsqueeze(1)
masks_inp = torch.cat([detection_masks.unsqueeze(1), detection_dict['plane_XYZ']], dim=1)
segmentation = new_input_dict['segmentation']
detection_masks = torch.nn.functional.interpolate(detection_masks[:, 80:560].unsqueeze(1), size=(192, 256), mode='nearest').squeeze(1)
image = torch.nn.functional.interpolate(image[:, :, 80:560], size=(192, 256), mode='bilinear')
image_2 = torch.nn.functional.interpolate(image_2[:, :, 80:560], size=(192, 256), mode='bilinear')
masks_inp = torch.nn.functional.interpolate(masks_inp[:, :, 80:560], size=(192, 256), mode='bilinear')
depth_np = torch.nn.functional.interpolate(depth_np[:, 80:560].unsqueeze(1), size=(192, 256), mode='bilinear').squeeze(1)
plane_depth = torch.nn.functional.interpolate(detection_dict['depth'][:, 80:560].unsqueeze(1), size=(192, 256), mode='bilinear').squeeze(1)
segmentation = torch.nn.functional.interpolate(segmentation[:, 80:560].unsqueeze(1).float(), size=(192, 256), mode='nearest').squeeze().long()
new_input_dict['image'] = image
new_input_dict['image_2'] = image_2
results = self.refine_model(image, image_2, camera, masks_inp, detection_dict['detection'][:, 6:9], plane_depth, depth_np)
masks = results[-1]['mask'].squeeze(1)
all_masks = torch.softmax(masks, dim=0)
masks_small = all_masks[1:]
all_masks = torch.nn.functional.interpolate(all_masks.unsqueeze(1), size=(480, 640), mode='bilinear').squeeze(1)
all_masks = (all_masks.max(0, keepdim=True)[1] == torch.arange(len(all_masks)).cuda().long().view((-1, 1, 1))).float()
masks = all_masks[1:]
detection_masks = torch.zeros(detection_dict['masks'].shape).cuda()
detection_masks[:, 80:560] = masks
detection_dict['masks'] = detection_masks
detection_dict['depth_ori'] = detection_dict['depth'].clone()
detection_dict['mask'][:, 80:560] = (masks.max(0, keepdim=True)[0] > (1 - masks.sum(0, keepdim=True))).float()
if self.options.modelType == 'fitting':
masks_cropped = masks_small
ranges = self.config.getRanges(camera).transpose(1, 2).transpose(0, 1)
XYZ = torch.nn.functional.interpolate(ranges.unsqueeze(1), size=(192, 256), mode='bilinear').squeeze(1) * results[-1]['depth'].squeeze(1)
detection_areas = masks_cropped.sum(-1).sum(-1)
A = masks_cropped.unsqueeze(1) * XYZ
b = masks_cropped
Ab = (A * b.unsqueeze(1)).sum(-1).sum(-1)
AA = (A.unsqueeze(2) * A.unsqueeze(1)).sum(-1).sum(-1)
plane_parameters = torch.stack([torch.matmul(torch.inverse(AA[planeIndex]), Ab[planeIndex]) if detection_areas[planeIndex] else detection_dict['detection'][planeIndex, 6:9] for planeIndex in range(len(AA))], dim=0)
plane_offsets = torch.norm(plane_parameters, dim=-1, keepdim=True)
plane_parameters = plane_parameters / torch.clamp(torch.pow(plane_offsets, 2), 1e-4)
detection_dict['detection'][:, 6:9] = plane_parameters
XYZ_pred, detection_mask, plane_XYZ = calcXYZModule(self.config, camera, detection_dict['detection'], detection_masks, detection_dict['depth'], return_individual=True)
detection_dict['depth'] = XYZ_pred[1:2]
pass
continue
pass
return detection_pair
class DepthDetector():
def __init__(self, options, config, modelType, checkpoint_dir=''):
self.options = options
self.config = config
self.modelType = modelType
self.model = MaskRCNNDepth(config)
self.model.cuda()
self.model.eval()
checkpoint_dir = checkpoint_dir if checkpoint_dir != '' else 'checkpoint/depth_np'
if options.suffix != '':
checkpoint_dir += '_' + options.suffix
pass
self.model.load_state_dict(torch.load(checkpoint_dir + '/checkpoint.pth'))
return
def detect(self, sample):
detection_pair = []
camera = sample[30][0].cuda()
for indexOffset in [0, ]:
images, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, gt_depth, extrinsics, planes, gt_segmentation = sample[indexOffset + 0].cuda(), sample[indexOffset + 1].numpy(), sample[indexOffset + 2].cuda(), sample[indexOffset + 3].cuda(), sample[indexOffset + 4].cuda(), sample[indexOffset + 5].cuda(), sample[indexOffset + 6].cuda(), sample[indexOffset + 7].cuda(), sample[indexOffset + 8].cuda(), sample[indexOffset + 9].cuda(), sample[indexOffset + 10].cuda(), sample[indexOffset + 11].cuda()
depth_np_pred = self.model.predict([images, camera], mode='inference_detection', use_nms=2, use_refinement='refinement' in self.options.suffix)
if depth_np_pred.shape != gt_depth.shape:
depth_np_pred = torch.nn.functional.interpolate(depth_np_pred.unsqueeze(1), size=(640, 640), mode='bilinear').squeeze(1)
pass
detection_pair.append({'depth': depth_np_pred, 'mask': torch.ones(depth_np_pred.shape).cuda()})
continue
return detection_pair
class PlaneNetDetector():
def __init__(self, options, config, checkpoint_dir=''):
self.options = options
self.config = config
sys.path.append('../../existing_methods/')
from PlaneNet.planenet_inference import PlaneNetDetector
self.detector = PlaneNetDetector(predictNYU=False)
return
def detect(self, sample):
detection_pair = []
for indexOffset in [0, ]:
images, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, gt_depth, extrinsics, planes, gt_segmentation = sample[indexOffset + 0].cuda(), sample[indexOffset + 1].numpy(), sample[indexOffset + 2].cuda(), sample[indexOffset + 3].cuda(), sample[indexOffset + 4].cuda(), sample[indexOffset + 5].cuda(), sample[indexOffset + 6].cuda(), sample[indexOffset + 7].cuda(), sample[indexOffset + 8].cuda(), sample[indexOffset + 9].cuda(), sample[indexOffset + 10].cuda(), sample[indexOffset + 11].cuda()
image = (images[0].detach().cpu().numpy().transpose((1, 2, 0)) + self.config.MEAN_PIXEL)[80:560]
pred_dict = self.detector.detect(image)
segmentation = pred_dict['segmentation']
segmentation = np.concatenate([np.full((80, 640), fill_value=-1, dtype=np.int32), segmentation, np.full((80, 640), fill_value=-1, dtype=np.int32)], axis=0)
planes = pred_dict['plane']
masks = (segmentation == np.arange(len(planes), dtype=np.int32).reshape((-1, 1, 1))).astype(np.float32)
depth = pred_dict['depth']
depth = np.concatenate([np.zeros((80, 640), dtype=np.int32), depth, np.zeros((80, 640), dtype=np.int32)], axis=0)
detections = np.concatenate([np.ones((len(planes), 4)), np.ones((len(planes), 2)), planes], axis=-1)
detections = torch.from_numpy(detections).float().cuda()
depth = torch.from_numpy(depth).unsqueeze(0).float().cuda()
masks = torch.from_numpy(masks).float().cuda()
detection_pair.append({'depth': depth, 'mask': masks.sum(0, keepdim=True), 'masks': masks, 'detection': detections})
continue
return detection_pair
class PlaneRecoverDetector():
def __init__(self, options, config, checkpoint_dir=''):
self.options = options
self.config = config
sys.path.append('../../existing_methods/')
from planerecover_ori.inference import PlaneRecoverDetector
self.detector = PlaneRecoverDetector()
return
def detect(self, sample):
detection_pair = []
camera = sample[30][0].cuda()
for indexOffset in [0, ]:
images, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, gt_depth, extrinsics, planes, gt_segmentation = sample[indexOffset + 0].cuda(), sample[indexOffset + 1].numpy(), sample[indexOffset + 2].cuda(), sample[indexOffset + 3].cuda(), sample[indexOffset + 4].cuda(), sample[indexOffset + 5].cuda(), sample[indexOffset + 6].cuda(), sample[indexOffset + 7].cuda(), sample[indexOffset + 8].cuda(), sample[indexOffset + 9].cuda(), sample[indexOffset + 10].cuda(), sample[indexOffset + 11].cuda()
image = (images[0].detach().cpu().numpy().transpose((1, 2, 0)) + self.config.MEAN_PIXEL)[80:560]
pred_dict = self.detector.detect(image)
segmentation = pred_dict['segmentation']
segmentation = np.concatenate([np.full((80, 640), fill_value=-1, dtype=np.int32), segmentation, np.full((80, 640), fill_value=-1, dtype=np.int32)], axis=0)
planes = pred_dict['plane']
masks = (segmentation == np.arange(len(planes), dtype=np.int32).reshape((-1, 1, 1))).astype(np.float32)
detections = np.concatenate([np.ones((len(planes), 4)), np.ones((len(planes), 2)), planes], axis=-1)
detections = torch.from_numpy(detections).float().cuda()
masks = torch.from_numpy(masks).float().cuda()
XYZ_pred, detection_mask, plane_XYZ = calcXYZModule(self.config, camera, detections, masks, torch.zeros((1, 640, 640)).cuda(), return_individual=True)
depth = XYZ_pred[1:2]
print(planes)
print(np.unique(segmentation))
for mask_index, mask in enumerate(masks.detach().cpu().numpy()):
cv2.imwrite('test/mask_' + str(mask_index) + '.png', drawMaskImage(mask))
continue
detection_pair.append({'depth': depth, 'mask': masks.sum(0, keepdim=True), 'masks': masks, 'detection': detections})
continue
return detection_pair
class TraditionalDetector():
def __init__(self, options, config, modelType=''):
self.options = options
self.config = config
self.modelType = modelType
if 'pred' in modelType:
sys.path.append('../../')
from PlaneNet.planenet_inference import PlaneNetDetector
self.detector = PlaneNetDetector(predictSemantics=True)
pass
return
def detect(self, sample):
detection_pair = []
for indexOffset in [0, ]:
images, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, gt_depth, extrinsics, planes, gt_segmentation, gt_semantics = sample[indexOffset + 0].cuda(), sample[indexOffset + 1].numpy(), sample[indexOffset + 2].cuda(), sample[indexOffset + 3].cuda(), sample[indexOffset + 4].cuda(), sample[indexOffset + 5].cuda(), sample[indexOffset + 6].cuda(), sample[indexOffset + 7].cuda(), sample[indexOffset + 8].cuda(), sample[indexOffset + 9].cuda(), sample[indexOffset + 10].cuda(), sample[indexOffset + 11].cuda(), sample[indexOffset + 12].cuda()
image = (images[0].detach().cpu().numpy().transpose((1, 2, 0)) + self.config.MEAN_PIXEL)[80:560]
input_dict = {'image': cv2.resize(image, (256, 192))}
if 'gt' in self.modelType:
input_dict['depth'] = cv2.resize(gt_depth[0].detach().cpu().numpy()[80:560], (256, 192))
semantics = gt_semantics[0].detach().cpu().numpy()[80:560]
input_dict['semantics'] = cv2.resize(semantics, (256, 192), interpolation=cv2.INTER_NEAREST)
else:
pred_dict = self.detector.detect(image)
input_dict['depth'] = pred_dict['non_plane_depth'].squeeze()
input_dict['semantics'] = pred_dict['semantics'].squeeze().argmax(-1)
pass
camera = sample[30][0].numpy()
input_dict['info'] = np.array([camera[0], 0, camera[2], 0, 0, camera[1], camera[3], 0, 0, 0, 1, 0, 0, 0, 0, 1, camera[4], camera[5], 1000, 0])
np.save('test/input_dict.npy', input_dict)
os.system('rm test/output_dict.npy')
os.system('python plane_utils.py ' + self.modelType)
output_dict = np.load('test/output_dict.npy', encoding='latin1')[()]
segmentation = cv2.resize(output_dict['segmentation'], (640, 480), interpolation=cv2.INTER_NEAREST)
segmentation = np.concatenate([np.full((80, 640), fill_value=-1, dtype=np.int32), segmentation, np.full((80, 640), fill_value=-1, dtype=np.int32)], axis=0)
planes = output_dict['plane']
masks = (segmentation == np.arange(len(planes), dtype=np.int32).reshape((-1, 1, 1))).astype(np.float32)
plane_depths = calcPlaneDepths(planes, 256, 192, camera, max_depth=10)
depth = (plane_depths * (np.expand_dims(output_dict['segmentation'], -1) == np.arange(len(planes)))).sum(-1)
depth = cv2.resize(depth, (640, 480), interpolation=cv2.INTER_LINEAR)
depth = np.concatenate([np.zeros((80, 640)), depth, np.zeros((80, 640))], axis=0)
detections = np.concatenate([np.ones((len(planes), 4)), np.ones((len(planes), 2)), planes], axis=-1)
detections = torch.from_numpy(detections).float().cuda()
depth = torch.from_numpy(depth).unsqueeze(0).float().cuda()
masks = torch.from_numpy(masks).float().cuda()
detection_pair.append({'depth': depth, 'mask': masks.sum(0, keepdim=True), 'masks': masks, 'detection': detections})
continue
return detection_pair
def evaluate(options):
config = InferenceConfig(options)
config.FITTING_TYPE = options.numAnchorPlanes
if options.dataset == '':
dataset = PlaneDataset(options, config, split='test', random=False, load_semantics=False)
elif options.dataset == 'occlusion':
config_dataset = copy.deepcopy(config)
config_dataset.OCCLUSION = False
dataset = PlaneDataset(options, config_dataset, split='test', random=False, load_semantics=True)
elif 'nyu' in options.dataset:
dataset = NYUDataset(options, config, split='val', random=False)
elif options.dataset == 'synthia':
dataset = SynthiaDataset(options, config, split='val', random=False)
elif options.dataset == 'kitti':
camera = np.zeros(6)
camera[0] = 9.842439e+02
camera[1] = 9.808141e+02
camera[2] = 6.900000e+02
camera[3] = 2.331966e+02
camera[4] = 1242
camera[5] = 375
dataset = InferenceDataset(options, config, image_list=glob.glob('../../Data/KITTI/scene_3/*.png'), camera=camera)
elif options.dataset == '7scene':
camera = np.zeros(6)
camera[0] = 519
camera[1] = 519
camera[2] = 320
camera[3] = 240
camera[4] = 640
camera[5] = 480
dataset = InferenceDataset(options, config, image_list=glob.glob('../../Data/SevenScene/scene_3/*.png'), camera=camera)
elif options.dataset == 'tanktemple':
camera = np.zeros(6)
camera[0] = 0.7
camera[1] = 0.7
camera[2] = 0.5
camera[3] = 0.5
camera[4] = 1
camera[5] = 1
dataset = InferenceDataset(options, config, image_list=glob.glob('../../Data/TankAndTemple/scene_4/*.jpg'), camera=camera)
elif options.dataset == 'make3d':
camera = np.zeros(6)
camera[0] = 0.7
camera[1] = 0.7
camera[2] = 0.5
camera[3] = 0.5
camera[4] = 1
camera[5] = 1
dataset = InferenceDataset(options, config, image_list=glob.glob('../../Data/Make3D/*.jpg'), camera=camera)
elif options.dataset == 'popup':
camera = np.zeros(6)
camera[0] = 0.7
camera[1] = 0.7
camera[2] = 0.5
camera[3] = 0.5
camera[4] = 1
camera[5] = 1
dataset = InferenceDataset(options, config, image_list=glob.glob('../../Data/PhotoPopup/*.jpg'), camera=camera)
elif options.dataset == 'cross' or options.dataset == 'cross_2':
image_list = ['test/cross_dataset/' + str(c) + '_image.png' for c in range(12)]
cameras = []
camera = np.zeros(6)
camera[0] = 587
camera[1] = 587
camera[2] = 320
camera[3] = 240
camera[4] = 640
camera[5] = 480
for c in range(4):
cameras.append(camera)
continue
camera_kitti = np.zeros(6)
camera_kitti[0] = 9.842439e+02
camera_kitti[1] = 9.808141e+02
camera_kitti[2] = 6.900000e+02
camera_kitti[3] = 2.331966e+02
camera_kitti[4] = 1242.0
camera_kitti[5] = 375.0
for c in range(2):
cameras.append(camera_kitti)
continue
camera_synthia = np.zeros(6)
camera_synthia[0] = 133.185088
camera_synthia[1] = 134.587036
camera_synthia[2] = 160.000000
camera_synthia[3] = 96.000000
camera_synthia[4] = 320
camera_synthia[5] = 192
for c in range(2):
cameras.append(camera_synthia)
continue
camera_tanktemple = np.zeros(6)
camera_tanktemple[0] = 0.7
camera_tanktemple[1] = 0.7
camera_tanktemple[2] = 0.5
camera_tanktemple[3] = 0.5
camera_tanktemple[4] = 1
camera_tanktemple[5] = 1
for c in range(2):
cameras.append(camera_tanktemple)
continue
for c in range(2):
cameras.append(camera)
continue
dataset = InferenceDataset(options, config, image_list=image_list, camera=cameras)
elif options.dataset == 'selected':
image_list = glob.glob('test/selected_images/*_image_0.png')
image_list = [filename for filename in image_list if '63_image' not in filename and '77_image' not in filename] + [filename for filename in image_list if '63_image' in filename or '77_image' in filename]
camera = np.zeros(6)
camera[0] = 587
camera[1] = 587
camera[2] = 320
camera[3] = 240
camera[4] = 640
camera[5] = 480
dataset = InferenceDataset(options, config, image_list=image_list, camera=camera)
elif options.dataset == 'comparison':
image_list = ['test/comparison/' + str(index) + '_image_0.png' for index in [65, 11, 24]]
camera = np.zeros(6)
camera[0] = 587
camera[1] = 587
camera[2] = 320
camera[3] = 240
camera[4] = 640
camera[5] = 480
dataset = InferenceDataset(options, config, image_list=image_list, camera=camera)
elif 'inference' in options.dataset:
image_list = glob.glob(options.customDataFolder + '/*.png') + glob.glob(options.customDataFolder + '/*.jpg')
if os.path.exists(options.customDataFolder + '/camera.txt'):
camera = np.zeros(6)
with open(options.customDataFolder + '/camera.txt', 'r') as f:
for line in f:
values = [float(token.strip()) for token in line.split(' ') if token.strip() != '']
for c in range(6):
camera[c] = values[c]
continue
break
pass
else:
camera = [filename.replace('.png', '.txt').replace('.jpg', '.txt') for filename in image_list]
pass
dataset = InferenceDataset(options, config, image_list=image_list, camera=camera)
pass
print('the number of images', len(dataset))
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=0)
epoch_losses = []
data_iterator = tqdm(dataloader, total=len(dataset))
specified_suffix = options.suffix
with torch.no_grad():
detectors = []
for method in options.methods:
if method == 'w':
options.suffix = 'pair_' + specified_suffix if specified_suffix != '' else 'pair'
detectors.append(('warping', PlaneRCNNDetector(options, config, modelType='pair')))
elif method == 'b':
options.suffix = specified_suffix if specified_suffix != '' else ''
detectors.append(('basic', PlaneRCNNDetector(options, config, modelType='pair')))
elif method == 'o':
options.suffix = 'occlusion_' + specified_suffix if specified_suffix != '' else 'occlusion'
detectors.append(('occlusion', PlaneRCNNDetector(options, config, modelType='occlusion')))
elif method == 'p':
detectors.append(('planenet', PlaneNetDetector(options, config)))
elif method == 'e':
detectors.append(('planerecover', PlaneRecoverDetector(options, config)))
elif method == 't':
if 'gt' in options.suffix:
detectors.append(('manhattan_gt', TraditionalDetector(options, config, 'manhattan_gt')))
else:
detectors.append(('manhattan_pred', TraditionalDetector(options, config, 'manhattan_pred')))
pass
elif method == 'n':
options.suffix = specified_suffix if specified_suffix != '' else ''
detectors.append(('non_planar', DepthDetector(options, config, modelType='np')))
elif method == 'r':
options.suffix = specified_suffix if specified_suffix != '' else ''
detectors.append(('refine', PlaneRCNNDetector(options, config, modelType='refine')))
elif method == 's':
options.suffix = specified_suffix if specified_suffix != '' else ''
detectors.append(('refine_single', PlaneRCNNDetector(options, config, modelType='refine_single')))
elif method == 'f':
options.suffix = specified_suffix if specified_suffix != '' else ''
detectors.append(('final', PlaneRCNNDetector(options, config, modelType='final')))
pass
continue
pass
if not options.debug:
for method_name in [detector[0] for detector in detectors]:
os.system('rm ' + options.test_dir + '/*_' + method_name + '.png')
continue
pass
idx_filename = './data/idx_800.npy'
idx = np.load(idx_filename)
all_statistics = []
for name, detector in detectors:
statistics = [[], [], [], []]
for sampleIndex, sample in enumerate(data_iterator):
if options.dataset == '' and sampleIndex not in idx:
continue
if options.testingIndex >= 0 and sampleIndex != options.testingIndex:
if sampleIndex > options.testingIndex:
break
continue
input_pair = []
camera = sample[30][0].cuda()
for indexOffset in [0, ]:
images, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, gt_depth, extrinsics, planes, gt_segmentation = sample[indexOffset + 0].cuda(), sample[indexOffset + 1].numpy(), sample[indexOffset + 2].cuda(), sample[indexOffset + 3].cuda(), sample[indexOffset + 4].cuda(), sample[indexOffset + 5].cuda(), sample[indexOffset + 6].cuda(), sample[indexOffset + 7].cuda(), sample[indexOffset + 8].cuda(), sample[indexOffset + 9].cuda(), sample[indexOffset + 10].cuda(), sample[indexOffset + 11].cuda()
masks = (gt_segmentation == torch.arange(gt_segmentation.max() + 1).cuda().view(-1, 1, 1)).float()
input_pair.append({'image': images, 'depth': gt_depth, 'bbox': gt_boxes, 'extrinsics': extrinsics, 'segmentation': gt_segmentation, 'camera': camera, 'plane': planes[0], 'masks': masks, 'mask': gt_masks})
continue
if sampleIndex >= options.numTestingImages:
break
with torch.no_grad():
torch.cuda.synchronize()
start = time.time()
detection_pair = detector.detect(sample)
torch.cuda.synchronize()
end = time.time()
all_statistics.append(end-start)
np.save(options.test_dir + '/stat_time_ours_'+options.dataset+'.npy', all_statistics)
print(options.test_dir + '/stat_time_ours_'+options.dataset+'.npy')
if __name__ == '__main__':
args = parse_args()
if args.dataset == '':
args.keyname = 'evaluate'
else:
args.keyname = args.dataset
pass
args.test_dir = 'test/' + args.keyname + '/' + args.testSuffix
if args.testingIndex >= 0:
args.debug = True
pass
if args.debug:
args.test_dir += '_debug'
args.printInfo = True
pass
## Write html for visualization
if False:
if False:
info_list = ['image_0', 'segmentation_0', 'segmentation_0_warping', 'depth_0', 'depth_0_warping']
writeHTML(args.test_dir, info_list, numImages=100, convertToImage=False, filename='index', image_width=256)
pass
if False:
info_list = ['image_0', 'segmentation_0', 'detection_0_planenet', 'detection_0_warping', 'detection_0_refine']
writeHTML(args.test_dir, info_list, numImages=20, convertToImage=True, filename='comparison_segmentation')
pass
if False:
info_list = ['image_0', 'segmentation_0', 'segmentation_0_manhattan_gt', 'segmentation_0_planenet', 'segmentation_0_warping']
writeHTML(args.test_dir, info_list, numImages=30, convertToImage=False, filename='comparison_segmentation')
pass
exit(1)
pass
if not os.path.exists(args.test_dir):
os.system("mkdir -p %s"%args.test_dir)
pass
if args.debug and args.dataset == '':
os.system('rm ' + args.test_dir + '/*')
pass
evaluate(args)