1
+ import sys
2
+ import pathlib
3
+ root_dir = pathlib .Path (__file__ ).resolve ().parent .parent .parent
4
+ sys .path .insert (0 , str (root_dir ))
5
+
6
+ import os
7
+ import os .path as osp
8
+ import tqdm
9
+ import numpy as np
10
+ import json
11
+
12
+
13
+ def convert_id_to_pair (data_ids ):
14
+ paired_data_ids = []
15
+ for data_id in data_ids :
16
+ sequence_name , view_id = data_id
17
+ if view_id > 0 :
18
+ view_id1 , view_id2 = view_id , view_id - 1
19
+ paired_data_ids .append ((sequence_name , view_id1 , view_id2 ))
20
+ return paired_data_ids
21
+
22
+
23
+ def detect_moving (pc , flow , bg_rot , bg_transl , thresh = 0.2 ):
24
+ # Count the number of moving points
25
+ flow_fitted = np .einsum ('ij,nj->ni' , bg_rot , pc ) + bg_transl - pc
26
+ diff = np .linalg .norm (flow_fitted - flow , axis = 1 )
27
+ n_mov_point = (diff > thresh ).astype (np .float32 ).sum ()
28
+ return n_mov_point
29
+
30
+
31
+ if __name__ == '__main__' :
32
+ from datasets .dataset_waymo import WaymoOpenDataset
33
+ split = 'train'
34
+ # split = 'val'
35
+
36
+ # Convert single-frame data IDs to frame-pair data IDs
37
+ select_frame = 'data_prepare/waymo/splits/%s_sup.json' % (split )
38
+ with open (select_frame , 'r' ) as f :
39
+ data_ids = json .load (f )
40
+ data_ids = convert_id_to_pair (data_ids )
41
+ save_select_frame = 'data_prepare/waymo/splits/%s_sup_paired.json' % (split )
42
+ with open (save_select_frame , 'w' ) as f :
43
+ json .dump (data_ids , f )
44
+
45
+ # Setup the dataset
46
+ DATA_ROOT = "/media/SSD/ziyang/Datasets/Waymo_downsampled"
47
+ if split == 'val' :
48
+ mapping_path = 'data_prepare/waymo/splits/val.txt'
49
+ else :
50
+ mapping_path = 'data_prepare/waymo/splits/train.txt'
51
+ ignore_class_ids = [2 , 3 ] # Ignore Pedestrian & Cyclist
52
+ ignore_npoint_thresh = 50
53
+ select_frame = save_select_frame
54
+ predflow_path = 'flowstep3d_gpf_odo_bound'
55
+ dataset = WaymoOpenDataset (data_root = DATA_ROOT ,
56
+ mapping_path = mapping_path ,
57
+ downsampled = True ,
58
+ select_frame = select_frame ,
59
+ predflow_path = predflow_path ,
60
+ ignore_class_ids = ignore_class_ids ,
61
+ ignore_npoint_thresh = ignore_npoint_thresh )
62
+ moving_thresh = 0.2
63
+ n_mov_point_ratio_thresh = 0.2
64
+
65
+
66
+ moving_samples = []
67
+ sample_ids = list (range (len (dataset )))
68
+ pbar = tqdm .tqdm (total = len (sample_ids ))
69
+ for sid in sample_ids :
70
+ sequence_name , view_id1 , view_id2 = dataset .data_ids [sid ]
71
+ pbar .update ()
72
+ # print (sequence_name, view_id1, view_id2)
73
+
74
+ pcs , segms , flows , _ = dataset [sid ]
75
+ pc , segm , flow = pcs [0 ], segms [0 ], flows [0 ]
76
+
77
+ # Exclude samples with pure background
78
+ if np .unique (segm ).shape [0 ] == 1 :
79
+ continue
80
+
81
+ # Load groundtruth ego-motion
82
+ sequence_path = osp .join ("/media/SSD/ziyang/Datasets/Waymo" , 'data' , sequence_name )
83
+ pose1 , pose2 = np .load (osp .join (sequence_path , 'pose_%04d.npy' % (view_id1 ))), np .load (
84
+ osp .join (sequence_path , 'pose_%04d.npy' % (view_id2 )))
85
+ rot1 , transl1 = pose1 [0 :3 , 0 :3 ], pose1 [0 :3 , 3 ]
86
+ rot2 , transl2 = pose2 [0 :3 , 0 :3 ], pose2 [0 :3 , 3 ]
87
+ rot = rot2 .T @ rot1
88
+ transl = rot2 .T @ (transl1 - transl2 )
89
+
90
+ # Check the number of moving objects
91
+ not_ground = (pc [:, 1 ] >= 0.3 )
92
+ pc_fg , segm_fg , flow_fg = pc [not_ground ], segm [not_ground ], flow [not_ground ]
93
+ n_mov_point = detect_moving (pc_fg , flow_fg , rot , transl , thresh = moving_thresh )
94
+ n_mov_point_ratio = n_mov_point / pc_fg .shape [0 ]
95
+
96
+ if (n_mov_point_ratio > n_mov_point_ratio_thresh ):
97
+ moving_samples .append ((sequence_name , view_id1 , view_id2 ))
98
+
99
+ print (len (dataset ), len (moving_samples ))
100
+ save_file = 'data_prepare/waymo/splits/%s_unsup.json' % (split )
101
+ with open (save_file , 'w' ) as f :
102
+ json .dump (moving_samples , f )
0 commit comments