-
Notifications
You must be signed in to change notification settings - Fork 28
/
utils.py
executable file
·1929 lines (1630 loc) · 91.6 KB
/
utils.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Author: Xingtong Liu, Ayushi Sinha, Masaru Ishii, Gregory D. Hager, Austin Reiter, Russell H. Taylor, and Mathias Unberath
Copyright (C) 2019 Johns Hopkins University - All Rights Reserved
You may use, distribute and modify this code under the
terms of the GNU GENERAL PUBLIC LICENSE Version 3 license for non-commercial usage.
You should have received a copy of the GNU GENERAL PUBLIC LICENSE Version 3 license with
'''
import numpy as np
import cv2
from plyfile import PlyData, PlyElement
import yaml
import random
import torch
import torchvision.utils as vutils
import datetime
import shutil
from pathlib import Path
import matplotlib
matplotlib.use('agg', warn=False, force=True)
from matplotlib import pyplot as plt
def overlapping_visible_view_indexes_per_point(visible_view_indexes_per_point, visible_interval):
temp_array = np.copy(visible_view_indexes_per_point)
view_count = visible_view_indexes_per_point.shape[1]
for i in range(view_count):
visible_view_indexes_per_point[:, i] = \
np.sum(temp_array[:, max(0, i - visible_interval):min(view_count, i + visible_interval)], axis=1)
return visible_view_indexes_per_point
def get_color_file_names_by_bag(root, training_patient_id, validation_patient_id, testing_patient_id):
training_image_list = []
validation_image_list = []
testing_image_list = []
if not isinstance(training_patient_id, list):
training_patient_id = [training_patient_id]
if not isinstance(validation_patient_id, list):
validation_patient_id = [validation_patient_id]
if not isinstance(testing_patient_id, list):
testing_patient_id = [testing_patient_id]
for id in training_patient_id:
training_image_list += list(root.glob('*' + str(id) + '/_start*/0*.jpg'))
for id in testing_patient_id:
testing_image_list += list(root.glob('*' + str(id) + '/_start*/0*.jpg'))
for id in validation_patient_id:
validation_image_list += list(root.glob('*' + str(id) + '/_start*/0*.jpg'))
training_image_list.sort()
testing_image_list.sort()
validation_image_list.sort()
return training_image_list, validation_image_list, testing_image_list
def get_color_file_names(root, split_ratio=(0.9, 0.05, 0.05)):
image_list = list(root.glob('*/_start*/0*.jpg'))
image_list.sort()
split_point = [int(len(image_list) * split_ratio[0]), int(len(image_list) * (split_ratio[0] + split_ratio[1]))]
return image_list[:split_point[0]], image_list[split_point[0]:split_point[1]], image_list[split_point[1]:]
def get_test_color_img(img_file_name, start_h, end_h, start_w, end_w, downsampling_factor, is_hsv, rgb_mode):
img = cv2.imread(img_file_name)
downsampled_img = cv2.resize(img, (0, 0), fx=1. / downsampling_factor, fy=1. / downsampling_factor)
downsampled_img = downsampled_img[start_h:end_h, start_w:end_w, :]
if is_hsv:
downsampled_img = cv2.cvtColor(downsampled_img, cv2.COLOR_BGR2HSV_FULL)
else:
if rgb_mode == "rgb":
downsampled_img = cv2.cvtColor(downsampled_img, cv2.COLOR_BGR2RGB)
downsampled_img = np.array(downsampled_img, dtype="float32")
return downsampled_img
def get_parent_folder_names(root, id_range):
folder_list = []
for i in range(id_range[0], id_range[1]):
folder_list += list(root.glob('*' + str(i) + '/_start*/'))
folder_list.sort()
return folder_list
def downsample_and_crop_mask(mask, downsampling_factor, divide, suggested_h=None, suggested_w=None):
downsampled_mask = cv2.resize(mask, (0, 0), fx=1. / downsampling_factor, fy=1. / downsampling_factor)
end_h_index = downsampled_mask.shape[0]
end_w_index = downsampled_mask.shape[1]
# divide is related to the pooling times of the teacher model
indexes = np.where(downsampled_mask == 255)
h = indexes[0].max() - indexes[0].min()
w = indexes[1].max() - indexes[1].min()
remainder_h = h % divide
remainder_w = w % divide
increment_h = divide - remainder_h
increment_w = divide - remainder_w
target_h = h + increment_h
target_w = w + increment_w
start_h = max(indexes[0].min() - increment_h // 2, 0)
end_h = start_h + target_h
start_w = max(indexes[1].min() - increment_w // 2, 0)
end_w = start_w + target_w
if suggested_h is not None:
if suggested_h != h:
remain_h = suggested_h - target_h
start_h = max(start_h - remain_h // 2, 0)
end_h = min(suggested_h + start_h, end_h_index)
start_h = end_h - suggested_h
if suggested_w is not None:
if suggested_w != w:
remain_w = suggested_w - target_w
start_w = max(start_w - remain_w // 2, 0)
end_w = min(suggested_w + start_w, end_w_index)
start_w = end_w - suggested_w
kernel = np.ones((5, 5), np.uint8)
downsampled_mask_erode = cv2.erode(downsampled_mask, kernel, iterations=1)
cropped_mask = downsampled_mask_erode[start_h:end_h, start_w:end_w]
return cropped_mask, start_h, end_h, start_w, end_w
def read_selected_indexes(prefix_seq):
selected_indexes = []
with open(str(prefix_seq / 'selected_indexes')) as fp:
for line in fp:
selected_indexes.append(int(line))
stride = selected_indexes[1] - selected_indexes[0]
return stride, selected_indexes
def read_visible_image_path_list(data_root):
visible_image_path_list = []
visible_indexes_path_list = list(data_root.rglob("*visible_view_indexes"))
for index_path in visible_indexes_path_list:
with open(str(index_path)) as fp:
for line in fp:
visible_image_path_list.append(int(line))
return visible_image_path_list
def read_visible_view_indexes(prefix_seq):
visible_view_indexes = []
with open(str(prefix_seq / 'visible_view_indexes')) as fp:
for line in fp:
visible_view_indexes.append(int(line))
return visible_view_indexes
def read_camera_intrinsic_per_view(prefix_seq):
camera_intrinsics = []
param_count = 0
temp_camera_intrincis = np.zeros((3, 4))
with open(str(prefix_seq / 'camera_intrinsics_per_view')) as fp:
for line in fp:
# Focal length
if param_count == 0:
temp_camera_intrincis[0][0] = float(line)
param_count += 1
elif param_count == 1:
temp_camera_intrincis[1][1] = float(line)
param_count += 1
elif param_count == 2:
temp_camera_intrincis[0][2] = float(line)
param_count += 1
elif param_count == 3:
temp_camera_intrincis[1][2] = float(line)
temp_camera_intrincis[2][2] = 1.0
camera_intrinsics.append(temp_camera_intrincis)
temp_camera_intrincis = np.zeros((3, 4))
param_count = 0
return camera_intrinsics
def modify_camera_intrinsic_matrix(intrinsic_matrix, start_h, start_w, downsampling_factor):
intrinsic_matrix_modified = np.copy(intrinsic_matrix)
intrinsic_matrix_modified[0][0] = intrinsic_matrix[0][0] / downsampling_factor
intrinsic_matrix_modified[1][1] = intrinsic_matrix[1][1] / downsampling_factor
intrinsic_matrix_modified[0][2] = intrinsic_matrix[0][2] / downsampling_factor - start_w
intrinsic_matrix_modified[1][2] = intrinsic_matrix[1][2] / downsampling_factor - start_h
return intrinsic_matrix_modified
def read_point_cloud(path):
lists_3D_points = []
plydata = PlyData.read(path)
for n in range(plydata['vertex'].count):
temp = list(plydata['vertex'][n])
temp[0] = temp[0]
temp[1] = temp[1]
temp[2] = temp[2]
temp.append(1.0)
lists_3D_points.append(temp)
return lists_3D_points
def read_view_indexes_per_point(prefix_seq, visible_view_indexes, point_cloud_count):
# Read the view indexes per point into a 2-dimension binary matrix
view_indexes_per_point = np.zeros((point_cloud_count, len(visible_view_indexes)))
point_count = -1
with open(str(prefix_seq / 'view_indexes_per_point')) as fp:
for line in fp:
if int(line) < 0:
point_count = point_count + 1
else:
view_indexes_per_point[point_count][visible_view_indexes.index(int(line))] = 1
return view_indexes_per_point
def read_pose_data(prefix_seq):
stream = open(str(prefix_seq / "motion.yaml"), 'r')
doc = yaml.load(stream)
keys, values = doc.items()
poses = values[1]
return poses
def global_scale_estimation(extrinsics, point_cloud):
max_bound = np.zeros((3,), dtype=np.float32)
min_bound = np.zeros((3,), dtype=np.float32)
for i, extrinsic in enumerate(extrinsics):
if i == 0:
max_bound = extrinsic[:3, 3]
min_bound = extrinsic[:3, 3]
else:
temp = extrinsic[:3, 3]
max_bound = np.maximum(max_bound, temp)
min_bound = np.minimum(min_bound, temp)
norm_1 = np.linalg.norm(max_bound - min_bound, ord=2)
max_bound = np.zeros((3,), dtype=np.float32)
min_bound = np.zeros((3,), dtype=np.float32)
for i, point in enumerate(point_cloud):
if i == 0:
max_bound = np.asarray(point[:3], dtype=np.float32)
min_bound = np.asarray(point[:3], dtype=np.float32)
else:
temp = np.asarray(point[:3], dtype=np.float32)
if np.any(np.isnan(temp)):
continue
max_bound = np.maximum(max_bound, temp)
min_bound = np.minimum(min_bound, temp)
norm_2 = np.linalg.norm(max_bound - min_bound, ord=2)
return max(1.0, max(norm_1, norm_2))
def get_extrinsic_matrix_and_projection_matrix(poses, intrinsic_matrix, visible_view_count):
projection_matrices = []
extrinsic_matrices = []
for i in range(visible_view_count):
rigid_transform = quaternion_matrix(
[poses["poses[" + str(i) + "]"]['orientation']['w'], poses["poses[" + str(i) + "]"]['orientation']['x'],
poses["poses[" + str(i) + "]"]['orientation']['y'],
poses["poses[" + str(i) + "]"]['orientation']['z']])
rigid_transform[0][3] = poses["poses[" + str(i) + "]"]['position']['x']
rigid_transform[1][3] = poses["poses[" + str(i) + "]"]['position']['y']
rigid_transform[2][3] = poses["poses[" + str(i) + "]"]['position']['z']
transform = np.asmatrix(rigid_transform)
transform = np.linalg.inv(transform)
extrinsic_matrices.append(transform)
projection_matrices.append(np.dot(intrinsic_matrix, transform))
return extrinsic_matrices, projection_matrices
def get_color_imgs(prefix_seq, visible_view_indexes, start_h, end_h, start_w, end_w, downsampling_factor, is_hsv=False):
imgs = []
for i in visible_view_indexes:
img = cv2.imread(str(prefix_seq / "{:08d}.jpg".format(i)))
downsampled_img = cv2.resize(img, (0, 0), fx=1. / downsampling_factor, fy=1. / downsampling_factor)
cropped_downsampled_img = downsampled_img[start_h:end_h, start_w:end_w, :]
if is_hsv:
cropped_downsampled_img = cv2.cvtColor(cropped_downsampled_img, cv2.COLOR_BGR2HSV_FULL)
imgs.append(cropped_downsampled_img)
height, width, channel = imgs[0].shape
imgs = np.array(imgs, dtype="float32")
imgs = np.reshape(imgs, (-1, height, width, channel))
return imgs
def compute_sanity_threshold(sanity_array, inlier_percentage):
# Use histogram to cluster into different contaminated levels
hist, bin_edges = np.histogram(sanity_array, bins=np.arange(1000) * np.max(sanity_array) / 1000.0,
density=True)
histogram_percentage = hist * np.diff(bin_edges)
percentage = inlier_percentage
# Let's assume there are a certain percent of points in each frame that are not contaminated
# Get sanity threshold from counting histogram bins
max_index = np.argmax(histogram_percentage)
histogram_sum = histogram_percentage[max_index]
pos_counter = 1
neg_counter = 1
# Assume the sanity value is a one-peak distribution
while True:
if max_index + pos_counter < len(histogram_percentage):
histogram_sum = histogram_sum + histogram_percentage[max_index + pos_counter]
pos_counter = pos_counter + 1
if histogram_sum >= percentage:
sanity_threshold_max = bin_edges[max_index + pos_counter]
sanity_threshold_min = bin_edges[max_index - neg_counter + 1]
break
if max_index - neg_counter >= 0:
histogram_sum = histogram_sum + histogram_percentage[max_index - neg_counter]
neg_counter = neg_counter + 1
if histogram_sum >= percentage:
sanity_threshold_max = bin_edges[max_index + pos_counter]
sanity_threshold_min = bin_edges[max_index - neg_counter + 1]
break
if max_index + pos_counter >= len(histogram_percentage) and max_index - neg_counter < 0:
sanity_threshold_max = np.max(bin_edges)
sanity_threshold_min = np.min(bin_edges)
break
return sanity_threshold_min, sanity_threshold_max
def get_clean_point_list(imgs, point_cloud, view_indexes_per_point, mask_boundary, inlier_percentage,
projection_matrices,
extrinsic_matrices, is_hsv):
array_3D_points = np.asarray(point_cloud).reshape((-1, 4))
if inlier_percentage <= 0.0 or inlier_percentage >= 1.0:
return list()
point_cloud_contamination_accumulator = np.zeros(array_3D_points.shape[0], dtype=np.int32)
point_cloud_appearance_count = np.zeros(array_3D_points.shape[0], dtype=np.int32)
height, width, channel = imgs[0].shape
valid_frame_count = 0
mask_boundary = mask_boundary.reshape((-1, 1))
for i in range(len(projection_matrices)):
img = imgs[i]
projection_matrix = projection_matrices[i]
extrinsic_matrix = extrinsic_matrices[i]
img = np.array(img, dtype=np.float32) / 255.0
# imgs might be in HSV or BGR colorspace depending on the settings beyond this function
if not is_hsv:
img_filtered = cv2.bilateralFilter(src=img, d=7, sigmaColor=25, sigmaSpace=25)
img_hsv = cv2.cvtColor(img_filtered, cv2.COLOR_BGR2HSV_FULL)
else:
img_bgr = cv2.cvtColor(img, cv2.COLOR_HSV2BGR_FULL)
img_filtered = cv2.bilateralFilter(src=img_bgr, d=7, sigmaColor=25, sigmaSpace=25)
img_hsv = cv2.cvtColor(img_filtered, cv2.COLOR_BGR2HSV_FULL)
view_indexes_frame = np.asarray(view_indexes_per_point[:, i]).reshape((-1))
visible_point_indexes = np.where(view_indexes_frame > 0.5)
visible_point_indexes = visible_point_indexes[0]
points_3D_camera = np.einsum('ij,mj->mi', extrinsic_matrix, array_3D_points)
points_3D_camera = points_3D_camera / points_3D_camera[:, 3].reshape((-1, 1))
points_2D_image = np.einsum('ij,mj->mi', projection_matrix, array_3D_points)
points_2D_image = points_2D_image / points_2D_image[:, 2].reshape((-1, 1))
visible_points_2D_image = points_2D_image[visible_point_indexes, :].reshape((-1, 3))
visible_points_3D_camera = points_3D_camera[visible_point_indexes, :].reshape((-1, 4))
indexes = np.where((visible_points_2D_image[:, 0] <= width - 1) & (visible_points_2D_image[:, 0] >= 0) &
(visible_points_2D_image[:, 1] <= height - 1) & (visible_points_2D_image[:, 1] >= 0)
& (visible_points_3D_camera[:, 2] > 0))
indexes = indexes[0]
in_image_point_1D_locations = (np.round(visible_points_2D_image[indexes, 0]) +
np.round(visible_points_2D_image[indexes, 1]) * width).astype(
np.int32).reshape((-1))
temp_mask = mask_boundary[in_image_point_1D_locations, :]
indexes_2 = np.where(temp_mask[:, 0] == 255)
indexes_2 = indexes_2[0]
in_mask_point_1D_locations = in_image_point_1D_locations[indexes_2]
points_depth = visible_points_3D_camera[indexes[indexes_2], 2]
img_hsv = img_hsv.reshape((-1, 3))
points_brightness = img_hsv[in_mask_point_1D_locations, 2]
sanity_array = points_depth ** 2 * points_brightness
point_cloud_appearance_count[visible_point_indexes[indexes[indexes_2]]] += 1
if sanity_array.shape[0] < 2:
continue
valid_frame_count += 1
sanity_threshold_min, sanity_threshold_max = compute_sanity_threshold(sanity_array, inlier_percentage)
indexes_3 = np.where((sanity_array <= sanity_threshold_min) | (sanity_array >= sanity_threshold_max))
indexes_3 = indexes_3[0]
point_cloud_contamination_accumulator[visible_point_indexes[indexes[indexes_2[indexes_3]]]] += 1
clean_point_cloud_array = (point_cloud_contamination_accumulator < point_cloud_appearance_count / 2).astype(
np.float32)
print("{} points eliminated".format(int(clean_point_cloud_array.shape[0] - np.sum(clean_point_cloud_array))))
return clean_point_cloud_array
def get_visible_count_per_point(view_indexes_per_point):
appearing_count = np.reshape(np.sum(view_indexes_per_point, axis=-1), (-1, 1))
return appearing_count
def generating_pos_and_increment(idx, visible_view_indexes, adjacent_range):
# We use the remainder of the overall idx to retrieve the visible view
visible_view_idx = idx % len(visible_view_indexes)
adjacent_range_list = []
adjacent_range_list.append(adjacent_range[0])
adjacent_range_list.append(adjacent_range[1])
if len(visible_view_indexes) <= 2 * adjacent_range_list[0]:
adjacent_range_list[0] = len(visible_view_indexes) // 2
if visible_view_idx <= adjacent_range_list[0] - 1:
increment = random.randint(adjacent_range_list[0],
min(adjacent_range_list[1], len(visible_view_indexes) - 1 - visible_view_idx))
elif visible_view_idx >= len(visible_view_indexes) - adjacent_range_list[0]:
increment = -random.randint(adjacent_range_list[0], min(adjacent_range_list[1], visible_view_idx))
else:
# which direction should we increment
direction = random.randint(0, 1)
if direction == 1:
increment = random.randint(adjacent_range_list[0],
min(adjacent_range_list[1], len(visible_view_indexes) - 1 - visible_view_idx))
else:
increment = -random.randint(adjacent_range_list[0], min(adjacent_range_list[1], visible_view_idx))
return [visible_view_idx, increment]
def get_pair_color_imgs(prefix_seq, pair_indexes, start_h, end_h, start_w, end_w, downsampling_factor, is_hsv,
rgb_mode):
imgs = []
for i in pair_indexes:
img = cv2.imread(str(Path(prefix_seq) / "{:08d}.jpg".format(i)))
downsampled_img = cv2.resize(img, (0, 0), fx=1. / downsampling_factor, fy=1. / downsampling_factor)
downsampled_img = downsampled_img[start_h:end_h, start_w:end_w, :]
if is_hsv:
downsampled_img = cv2.cvtColor(downsampled_img, cv2.COLOR_BGR2HSV_FULL)
else:
if rgb_mode == "rgb":
downsampled_img = cv2.cvtColor(downsampled_img, cv2.COLOR_BGR2RGB)
imgs.append(downsampled_img)
height, width, channel = imgs[0].shape
imgs = np.asarray(imgs, dtype=np.uint8)
imgs = imgs.reshape((-1, height, width, channel))
return imgs
def get_torch_training_data(pair_extrinsics, pair_projections, pair_indexes, point_cloud, mask_boundary,
view_indexes_per_point, clean_point_list, visible_view_indexes):
height = mask_boundary.shape[0]
width = mask_boundary.shape[1]
pair_depth_mask_imgs = []
pair_depth_imgs = []
pair_flow_imgs = []
flow_image_1 = np.zeros((height, width, 2), dtype=np.float32)
flow_image_2 = np.zeros((height, width, 2), dtype=np.float32)
pair_flow_mask_imgs = []
flow_mask_image_1 = np.zeros((height, width, 1), dtype=np.float32)
flow_mask_image_2 = np.zeros((height, width, 1), dtype=np.float32)
# We only use inlier points
array_3D_points = np.asarray(point_cloud).reshape((-1, 4))
for i in range(2):
projection_matrix = pair_projections[i]
extrinsic_matrix = pair_extrinsics[i]
if i == 0:
points_2D_image_1 = np.einsum('ij,mj->mi', projection_matrix, array_3D_points)
points_2D_image_1 = np.round(points_2D_image_1 / points_2D_image_1[:, 2].reshape((-1, 1)))
points_3D_camera_1 = np.einsum('ij,mj->mi', extrinsic_matrix, array_3D_points)
points_3D_camera_1 = points_3D_camera_1 / points_3D_camera_1[:, 3].reshape((-1, 1))
else:
points_2D_image_2 = np.einsum('ij,mj->mi', projection_matrix, array_3D_points)
points_2D_image_2 = np.round(points_2D_image_2 / points_2D_image_2[:, 2].reshape((-1, 1)))
points_3D_camera_2 = np.einsum('ij,mj->mi', extrinsic_matrix, array_3D_points)
points_3D_camera_2 = points_3D_camera_2 / points_3D_camera_2[:, 3].reshape((-1, 1))
mask_boundary = mask_boundary.reshape((-1, 1))
flow_image_1 = flow_image_1.reshape((-1, 2))
flow_image_2 = flow_image_2.reshape((-1, 2))
flow_mask_image_1 = flow_mask_image_1.reshape((-1, 1))
flow_mask_image_2 = flow_mask_image_2.reshape((-1, 1))
points_2D_image_1 = points_2D_image_1.reshape((-1, 3))
points_2D_image_2 = points_2D_image_2.reshape((-1, 3))
points_3D_camera_1 = points_3D_camera_1.reshape((-1, 4))
points_3D_camera_2 = points_3D_camera_2.reshape((-1, 4))
point_visibility_1 = np.asarray(view_indexes_per_point[:, visible_view_indexes.index(pair_indexes[0])]).reshape(
(-1))
if len(clean_point_list) != 0:
visible_point_indexes_1 = np.where((point_visibility_1 > 0.5) & (clean_point_list > 0.5))
else:
visible_point_indexes_1 = np.where((point_visibility_1 > 0.5))
visible_point_indexes_1 = visible_point_indexes_1[0]
point_visibility_2 = np.asarray(view_indexes_per_point[:, visible_view_indexes.index(pair_indexes[1])]).reshape(
(-1))
if len(clean_point_list) != 0:
visible_point_indexes_2 = np.where((point_visibility_2 > 0.5) & (clean_point_list > 0.5))
else:
visible_point_indexes_2 = np.where((point_visibility_2 > 0.5))
visible_point_indexes_2 = visible_point_indexes_2[0]
visible_points_3D_camera_1 = points_3D_camera_1[visible_point_indexes_1, :].reshape((-1, 4))
visible_points_2D_image_1 = points_2D_image_1[visible_point_indexes_1, :].reshape((-1, 3))
visible_points_3D_camera_2 = points_3D_camera_2[visible_point_indexes_2, :].reshape((-1, 4))
visible_points_2D_image_2 = points_2D_image_2[visible_point_indexes_2, :].reshape((-1, 3))
in_image_indexes_1 = np.where(
(visible_points_2D_image_1[:, 0] <= width - 1) & (visible_points_2D_image_1[:, 0] >= 0) &
(visible_points_2D_image_1[:, 1] <= height - 1) & (visible_points_2D_image_1[:, 1] >= 0)
& (visible_points_3D_camera_1[:, 2] > 0))
in_image_indexes_1 = in_image_indexes_1[0]
in_image_point_1D_locations_1 = (np.round(visible_points_2D_image_1[in_image_indexes_1, 0]) +
np.round(visible_points_2D_image_1[in_image_indexes_1, 1]) * width).astype(
np.int32).reshape((-1))
temp_mask_1 = mask_boundary[in_image_point_1D_locations_1, :]
in_mask_indexes_1 = np.where(temp_mask_1[:, 0] == 255)
in_mask_indexes_1 = in_mask_indexes_1[0]
in_mask_point_1D_locations_1 = in_image_point_1D_locations_1[in_mask_indexes_1]
flow_mask_image_1[in_mask_point_1D_locations_1, 0] = 1.0
in_image_indexes_2 = np.where(
(visible_points_2D_image_2[:, 0] <= width - 1) & (visible_points_2D_image_2[:, 0] >= 0) &
(visible_points_2D_image_2[:, 1] <= height - 1) & (visible_points_2D_image_2[:, 1] >= 0)
& (visible_points_3D_camera_2[:, 2] > 0))
in_image_indexes_2 = in_image_indexes_2[0]
in_image_point_1D_locations_2 = (np.round(visible_points_2D_image_2[in_image_indexes_2, 0]) +
np.round(visible_points_2D_image_2[in_image_indexes_2, 1]) * width).astype(
np.int32).reshape((-1))
temp_mask_2 = mask_boundary[in_image_point_1D_locations_2, :]
in_mask_indexes_2 = np.where(temp_mask_2[:, 0] == 255)
in_mask_indexes_2 = in_mask_indexes_2[0]
in_mask_point_1D_locations_2 = in_image_point_1D_locations_2[in_mask_indexes_2]
flow_mask_image_2[in_mask_point_1D_locations_2, 0] = 1.0
flow_image_1[in_mask_point_1D_locations_1, :] = points_2D_image_2[
visible_point_indexes_1[in_image_indexes_1[in_mask_indexes_1]],
:2] - \
points_2D_image_1[
visible_point_indexes_1[in_image_indexes_1[in_mask_indexes_1]], :2]
flow_image_2[in_mask_point_1D_locations_2, :] = points_2D_image_1[
visible_point_indexes_2[in_image_indexes_2[in_mask_indexes_2]],
:2] - \
points_2D_image_2[
visible_point_indexes_2[in_image_indexes_2[in_mask_indexes_2]], :2]
flow_image_1[:, 0] /= width
flow_image_1[:, 1] /= height
flow_image_2[:, 0] /= width
flow_image_2[:, 1] /= height
outlier_indexes_1 = np.where((np.abs(flow_image_1[:, 0]) > 5.0) | (np.abs(flow_image_1[:, 1]) > 5.0))[0]
outlier_indexes_2 = np.where((np.abs(flow_image_2[:, 0]) > 5.0) | (np.abs(flow_image_2[:, 1]) > 5.0))[0]
flow_mask_image_1[outlier_indexes_1, 0] = 0.0
flow_mask_image_2[outlier_indexes_2, 0] = 0.0
flow_image_1[outlier_indexes_1, 0] = 0.0
flow_image_2[outlier_indexes_2, 0] = 0.0
flow_image_1[outlier_indexes_1, 1] = 0.0
flow_image_2[outlier_indexes_2, 1] = 0.0
depth_img_1 = np.zeros((height, width, 1), dtype=np.float32)
depth_img_2 = np.zeros((height, width, 1), dtype=np.float32)
depth_mask_img_1 = np.zeros((height, width, 1), dtype=np.float32)
depth_mask_img_2 = np.zeros((height, width, 1), dtype=np.float32)
depth_img_1 = depth_img_1.reshape((-1, 1))
depth_img_2 = depth_img_2.reshape((-1, 1))
depth_mask_img_1 = depth_mask_img_1.reshape((-1, 1))
depth_mask_img_2 = depth_mask_img_2.reshape((-1, 1))
depth_img_1[in_mask_point_1D_locations_1, 0] = points_3D_camera_1[
visible_point_indexes_1[in_image_indexes_1[in_mask_indexes_1]], 2]
depth_img_2[in_mask_point_1D_locations_2, 0] = points_3D_camera_2[
visible_point_indexes_2[in_image_indexes_2[in_mask_indexes_2]], 2]
depth_mask_img_1[in_mask_point_1D_locations_1, 0] = 1.0
depth_mask_img_2[in_mask_point_1D_locations_2, 0] = 1.0
pair_flow_imgs.append(flow_image_1)
pair_flow_imgs.append(flow_image_2)
pair_flow_imgs = np.array(pair_flow_imgs, dtype="float32")
pair_flow_imgs = np.reshape(pair_flow_imgs, (-1, height, width, 2))
pair_flow_mask_imgs.append(flow_mask_image_1)
pair_flow_mask_imgs.append(flow_mask_image_2)
pair_flow_mask_imgs = np.array(pair_flow_mask_imgs, dtype="float32")
pair_flow_mask_imgs = np.reshape(pair_flow_mask_imgs, (-1, height, width, 1))
pair_depth_mask_imgs.append(depth_mask_img_1)
pair_depth_mask_imgs.append(depth_mask_img_2)
pair_depth_mask_imgs = np.array(pair_depth_mask_imgs, dtype="float32")
pair_depth_mask_imgs = np.reshape(pair_depth_mask_imgs, (-1, height, width, 1))
pair_depth_imgs.append(depth_img_1)
pair_depth_imgs.append(depth_img_2)
pair_depth_imgs = np.array(pair_depth_imgs, dtype="float32")
pair_depth_imgs = np.reshape(pair_depth_imgs, (-1, height, width, 1))
return pair_depth_mask_imgs, pair_depth_imgs, pair_flow_mask_imgs, pair_flow_imgs
def init_fn(worker_id):
np.random.seed(10086 + worker_id)
def init_net(net, type="kaiming", mode="fan_in", activation_mode="relu", distribution="normal"):
assert (torch.cuda.is_available())
net = net.cuda()
if type == "glorot":
glorot_weight_zero_bias(net, distribution=distribution)
else:
kaiming_weight_zero_bias(net, mode=mode, activation_mode=activation_mode, distribution=distribution)
return net
def glorot_weight_zero_bias(model, distribution="uniform"):
"""
Initalize parameters of all modules
by initializing weights with glorot uniform/xavier initialization,
and setting biases to zero.
Weights from batch norm layers are set to 1.
Parameters
----------
model: Module
distribution: string
"""
for module in model.modules():
if hasattr(module, 'weight'):
if not ('BatchNorm' in module.__class__.__name__):
if distribution == "uniform":
torch.nn.init.xavier_uniform_(module.weight, gain=1)
else:
torch.nn.init.xavier_normal_(module.weight, gain=1)
else:
torch.nn.init.constant_(module.weight, 1)
if hasattr(module, 'bias'):
if module.bias is not None:
torch.nn.init.constant_(module.bias, 0)
def kaiming_weight_zero_bias(model, mode="fan_in", activation_mode="relu", distribution="uniform"):
if activation_mode == "leaky_relu":
print("Leaky relu is not supported yet")
assert False
for module in model.modules():
if hasattr(module, 'weight'):
if not ('BatchNorm' in module.__class__.__name__):
if distribution == "uniform":
torch.nn.init.kaiming_uniform_(module.weight, mode=mode, nonlinearity=activation_mode)
else:
torch.nn.init.kaiming_normal_(module.weight, mode=mode, nonlinearity=activation_mode)
else:
torch.nn.init.constant_(module.weight, 1)
if hasattr(module, 'bias'):
if module.bias is not None:
torch.nn.init.constant_(module.bias, 0)
def save_model(model, optimizer, epoch, step, model_path, validation_loss):
torch.save({
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
'step': step,
'validation': validation_loss
}, str(model_path))
return
# def save_model(model, optimizer, epoch, step, model_path, failure_sequences, validation_loss):
# try:
# torch.save({
# 'model': model.state_dict(),
# 'optimizer': optimizer.state_dict(),
# 'epoch': epoch,
# 'step': step,
# 'failure': failure_sequences,
# 'validation': validation_loss
# }, str(model_path))
# except IOError:
# torch.save({
# 'model': model.state_dict(),
# 'optimizer': optimizer.state_dict(),
# 'epoch': epoch,
# 'step': step,
# 'validation': validation_loss
# }, str(model_path))
#
# return
def visualize_color_image(title, images, rebias=False, is_hsv=False, idx=None):
if idx is None:
for i in range(images.shape[0]):
image = images.data.cpu().numpy()[i]
image = np.moveaxis(image, source=[0, 1, 2], destination=[2, 0, 1])
if rebias:
image = image * 0.5 + 0.5
if is_hsv:
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR_FULL)
cv2.imshow(title + "_" + str(i), image)
else:
for id in idx:
image = images.data.cpu().numpy()[id]
image = np.moveaxis(image, source=[0, 1, 2], destination=[2, 0, 1])
if rebias:
image = image * 0.5 + 0.5
if is_hsv:
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR_FULL)
cv2.imshow(title + "_" + str(id), image)
def visualize_depth_map(title, depth_maps, min_value_=None, max_value_=None, idx=None, color_mode=cv2.COLORMAP_JET):
min_value_list = []
max_value_list = []
if idx is None:
for i in range(depth_maps.shape[0]):
depth_map_cpu = depth_maps[i].data.cpu().numpy()
if min_value_ is None and max_value_ is None:
min_value = np.min(depth_map_cpu)
max_value = np.max(depth_map_cpu)
min_value_list.append(min_value)
max_value_list.append(max_value)
else:
min_value = min_value_[i]
max_value = max_value_[i]
depth_map_cpu = np.moveaxis(depth_map_cpu, source=[0, 1, 2], destination=[2, 0, 1])
depth_map_visualize = np.abs((depth_map_cpu - min_value) / (max_value - min_value) * 255)
depth_map_visualize[depth_map_visualize > 255] = 255
depth_map_visualize[depth_map_visualize <= 0.0] = 0
depth_map_visualize = cv2.applyColorMap(np.uint8(depth_map_visualize), color_mode)
cv2.imshow(title + "_" + str(i), depth_map_visualize)
return min_value_list, max_value_list
else:
for id in idx:
depth_map_cpu = depth_maps[id].data.cpu().numpy()
if min_value_ is None and max_value_ is None:
min_value = np.min(depth_map_cpu)
max_value = np.max(depth_map_cpu)
min_value_list.append(min_value)
max_value_list.append(max_value)
else:
min_value = min_value_[id]
max_value = max_value_[id]
depth_map_cpu = np.moveaxis(depth_map_cpu, source=[0, 1, 2], destination=[2, 0, 1])
depth_map_visualize = np.abs((depth_map_cpu - min_value) / (max_value - min_value) * 255)
depth_map_visualize[depth_map_visualize > 255] = 255
depth_map_visualize[depth_map_visualize <= 0.0] = 0
depth_map_visualize = cv2.applyColorMap(np.uint8(depth_map_visualize), color_mode)
cv2.imshow(title + "_" + str(id), depth_map_visualize)
return min_value_list, max_value_list
def display_depth_map(depth_map, min_value=None, max_value=None, colormode=cv2.COLORMAP_JET):
if min_value is None or max_value is None:
min_value = np.min(depth_map)
max_value = np.max(depth_map)
depth_map_visualize = np.abs((depth_map - min_value) / (max_value - min_value) * 255)
depth_map_visualize[depth_map_visualize > 255] = 255
depth_map_visualize[depth_map_visualize <= 0.0] = 0
depth_map_visualize = cv2.applyColorMap(np.uint8(depth_map_visualize), colormode)
return depth_map_visualize
def draw_hsv(flows, title, idx=None):
if idx is None:
flows_cpu = flows.data.cpu().numpy()
for i in range(flows_cpu.shape[0]):
flow = np.moveaxis(flows_cpu[i], [0, 1, 2], [2, 0, 1])
h, w = flow.shape[:2]
fx, fy = flow[:, :, 0] * w, flow[:, :, 1] * h
ang = np.arctan2(fy, fx) + np.pi
v = np.sqrt(fx * fx + fy * fy)
hsv = np.zeros((h, w, 3), np.uint8)
hsv[..., 0] = ang * (180 / np.pi / 2)
hsv[..., 1] = 255
hsv[..., 2] = np.uint8(
np.minimum(v, np.sqrt(0.01 * w * w + 0.01 * h * h)) / np.sqrt(0.01 * w * w + 0.01 * h * h) * 255)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow(title + str(i), bgr)
else:
flows_cpu = flows.data.cpu().numpy()
for id in idx:
flow = np.moveaxis(flows_cpu[id], [0, 1, 2], [2, 0, 1])
h, w = flow.shape[:2]
fx, fy = flow[:, :, 0] * w, flow[:, :, 1] * h
ang = np.arctan2(fy, fx) + np.pi
v = np.sqrt(fx * fx + fy * fy)
hsv = np.zeros((h, w, 3), np.uint8)
hsv[..., 0] = ang * (180 / np.pi / 2)
hsv[..., 1] = 255
hsv[..., 2] = np.uint8(
np.minimum(v, np.sqrt(0.01 * w * w + 0.01 * h * h)) / np.sqrt(0.01 * w * w + 0.01 * h * h) * 255)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow(title + str(id), bgr)
def write_event(log, step, **data):
data['step'] = step
data['dt'] = datetime.time().isoformat()
log.write(unicode(json.dumps(data, sort_keys=True)))
log.write(unicode('\n'))
log.flush()
def point_cloud_from_depth(depth_map, color_img, mask_img, intrinsic_matrix, point_cloud_downsampling,
min_threshold=None, max_threshold=None):
point_clouds = []
height, width, channel = color_img.shape
f_x = intrinsic_matrix[0, 0]
c_x = intrinsic_matrix[0, 2]
f_y = intrinsic_matrix[1, 1]
c_y = intrinsic_matrix[1, 2]
for h in range(height):
for w in range(width):
if h % point_cloud_downsampling == 0 and w % point_cloud_downsampling == 0 and mask_img[h, w] > 0.5:
z = depth_map[h, w]
x = (w - c_x) / f_x * z
y = (h - c_y) / f_y * z
b = color_img[h, w, 0]
g = color_img[h, w, 1]
r = color_img[h, w, 2]
if max_threshold is not None and min_threshold is not None:
if np.max([r, g, b]) >= max_threshold and np.min([r, g, b]) <= min_threshold:
point_clouds.append((x, y, z, np.uint8(r), np.uint8(g), np.uint8(b)))
else:
point_clouds.append((x, y, z, np.uint8(r), np.uint8(g), np.uint8(b)))
point_clouds = np.array(point_clouds, dtype='float32')
point_clouds = np.reshape(point_clouds, (-1, 6))
return point_clouds
def write_point_cloud(path, point_cloud):
point_clouds_list = []
for i in range(point_cloud.shape[0]):
point_clouds_list.append((point_cloud[i, 0], point_cloud[i, 1], point_cloud[i, 2], point_cloud[i, 3],
point_cloud[i, 4], point_cloud[i, 5]))
vertex = np.array(point_clouds_list,
dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
el = PlyElement.describe(vertex, 'vertex')
PlyData([el], text=True).write(path)
return
def draw_flow(flows, max_v=None):
batch_size, channel, height, width = flows.shape
flows_x_display = vutils.make_grid(flows[:, 0, :, :].reshape(batch_size, 1, height, width), normalize=False,
scale_each=False)
flows_y_display = vutils.make_grid(flows[:, 1, :, :].reshape(batch_size, 1, height, width), normalize=False,
scale_each=False)
flows_display = torch.cat([flows_x_display[0, :, :].reshape(1, flows_x_display.shape[1], flows_x_display.shape[2]),
flows_y_display[0, :, :].reshape(1, flows_x_display.shape[1], flows_x_display.shape[2])],
dim=0)
flows_display = flows_display.data.cpu().numpy()
flows_display = np.moveaxis(flows_display, source=[0, 1, 2], destination=[2, 0, 1])
h, w = flows_display.shape[:2]
fx, fy = flows_display[:, :, 0], flows_display[:, :, 1] * h / w
ang = np.arctan2(fy, fx) + np.pi
v = np.sqrt(fx * fx + fy * fy)
hsv = np.zeros((h, w, 3), np.uint8)
hsv[..., 0] = ang * (180 / np.pi / 2)
hsv[..., 1] = 255
if max_v is None:
hsv[..., 2] = np.uint8(np.minimum(v / np.max(v), 1.0) * 255)
else:
hsv[..., 2] = np.uint8(np.minimum(v / max_v, 1.0) * 255)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR), np.max(v)
def stack_and_display(phase, title, step, writer, image_list, return_image=False):
writer.add_image(phase + '/Images/' + title,
np.moveaxis(np.vstack(image_list), source=[0, 1, 2], destination=[1, 2, 0]), step)
if return_image:
return np.vstack(image_list)
else:
return
def display_color_sparse_depth_dense_depth_warped_depth_sparse_flow_dense_flow(idx, step, writer, colors_1,
sparse_depths_1, pred_depths_1,
warped_depths_2_to_1,
sparse_flows_1, flows_from_depth_1,
boundaries,
phase="Training", is_return_image=False,
color_reverse=True,
is_hsv=True, rgb_mode="bgr",
):
colors_display = vutils.make_grid((colors_1 * 0.5 + 0.5) * boundaries, normalize=False)
colors_display = np.moveaxis(colors_display.data.cpu().numpy(),
source=[0, 1, 2], destination=[2, 0, 1])
if is_hsv:
colors_display = cv2.cvtColor(colors_display, cv2.COLOR_HSV2RGB_FULL)
else:
if rgb_mode == "bgr":
colors_display = cv2.cvtColor(colors_display, cv2.COLOR_BGR2RGB)
min_depth = torch.min(pred_depths_1)
max_depth = torch.max(pred_depths_1)
pred_depths_display = vutils.make_grid(pred_depths_1, normalize=True, scale_each=False,
range=(min_depth.item(), max_depth.item()))
pred_depths_display = cv2.applyColorMap(np.uint8(255 * np.moveaxis(pred_depths_display.data.cpu().numpy(),
source=[0, 1, 2],
destination=[2, 0, 1])), cv2.COLORMAP_JET)
sparse_depths_display = vutils.make_grid(sparse_depths_1, normalize=True, scale_each=False,
range=(min_depth.item(), max_depth.item()))
sparse_depths_display = cv2.applyColorMap(np.uint8(255 * np.moveaxis(sparse_depths_display.data.cpu().numpy(),
source=[0, 1, 2],
destination=[2, 0, 1])), cv2.COLORMAP_JET)
warped_depths_display = vutils.make_grid(warped_depths_2_to_1, normalize=True, scale_each=False,
range=(min_depth.item(), max_depth.item()))
warped_depths_display = cv2.applyColorMap(np.uint8(255 * np.moveaxis(warped_depths_display.data.cpu().numpy(),
source=[0, 1, 2],
destination=[2, 0, 1])), cv2.COLORMAP_JET)
dense_flows_display, max_v = draw_flow(flows_from_depth_1)
sparse_flows_display, _ = draw_flow(sparse_flows_1, max_v=max_v)
if color_reverse:
pred_depths_display = cv2.cvtColor(pred_depths_display, cv2.COLOR_BGR2RGB)
warped_depths_display = cv2.cvtColor(warped_depths_display, cv2.COLOR_BGR2RGB)
sparse_depths_display = cv2.cvtColor(sparse_depths_display, cv2.COLOR_BGR2RGB)
dense_flows_display = cv2.cvtColor(dense_flows_display, cv2.COLOR_BGR2RGB)
sparse_flows_display = cv2.cvtColor(sparse_flows_display, cv2.COLOR_BGR2RGB)
if is_return_image:
return colors_display, sparse_depths_display.astype(np.float32) / 255.0, pred_depths_display.astype(
np.float32) / 255.0, warped_depths_display.astype(np.float32) / 255.0, sparse_flows_display.astype(
np.float32) / 255.0, dense_flows_display.astype(np.float32) / 255.0
else:
writer.add_image(phase + '/Images/Color_' + str(idx), colors_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Sparse_Depth_' + str(idx), sparse_depths_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Pred_Depth_' + str(idx), pred_depths_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Warped_Depth_' + str(idx), warped_depths_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Sparse_Flow_' + str(idx), sparse_flows_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Dense_Flow_' + str(idx), dense_flows_display, step, dataformats="HWC")
return
def display_color_depth_sparse_flow_dense_flow(idx, step, writer, colors_1, pred_depths_1,
sparse_flows_1, flows_from_depth_1, is_hsv,
phase="Training", is_return_image=False, color_reverse=True
):
colors_display = vutils.make_grid(colors_1 * 0.5 + 0.5, normalize=False)
colors_display = np.moveaxis(colors_display.data.cpu().numpy(),
source=[0, 1, 2], destination=[2, 0, 1])
if is_hsv:
colors_display = cv2.cvtColor(colors_display, cv2.COLOR_HSV2RGB_FULL)
pred_depths_display = vutils.make_grid(pred_depths_1, normalize=True, scale_each=True)
pred_depths_display = cv2.applyColorMap(np.uint8(255 * np.moveaxis(pred_depths_display.data.cpu().numpy(),
source=[0, 1, 2],
destination=[2, 0, 1])), cv2.COLORMAP_JET)
sparse_flows_display, max_v = draw_flow(sparse_flows_1)
dense_flows_display, _ = draw_flow(flows_from_depth_1, max_v=max_v)
if color_reverse:
pred_depths_display = cv2.cvtColor(pred_depths_display, cv2.COLOR_BGR2RGB)
sparse_flows_display = cv2.cvtColor(sparse_flows_display, cv2.COLOR_BGR2RGB)
dense_flows_display = cv2.cvtColor(dense_flows_display, cv2.COLOR_BGR2RGB)
if is_return_image:
return colors_display, pred_depths_display.astype(np.float32) / 255.0, \
sparse_flows_display.astype(np.float32) / 255.0, dense_flows_display.astype(np.float32) / 255.0
else:
writer.add_image(phase + '/Images/Color_' + str(idx), colors_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Pred_Depth_' + str(idx), pred_depths_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Sparse_Flow_' + str(idx), sparse_flows_display, step, dataformats="HWC")
writer.add_image(phase + '/Images/Dense_Flow_' + str(idx), dense_flows_display, step, dataformats="HWC")
return
def display_color_pred_depth_sparse_depth(idx, step, writer, colors_1, pred_depth_maps_1, sparse_depth_maps_1,
phase, return_image=False):
colors_display = vutils.make_grid(colors_1 * 0.5 + 0.5, normalize=False)
colors_display_hsv = np.moveaxis(colors_display.data.cpu().numpy(),