-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_stats.py
560 lines (382 loc) · 17.2 KB
/
get_stats.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
# <Copyright 2019, Argo AI, LLC. Released under the MIT license.>
import argparse
import glob
import json
import logging
import os
import pathlib
import pickle
import sys
from typing import Any, Dict, List, TextIO, Tuple, Union
import motmetrics as mm
import numpy as np
from shapely.geometry.polygon import Polygon
from argoverse.evaluation.eval_utils import get_pc_inside_bbox, label_to_bbox, leave_only_roi_region
from argoverse.utils.json_utils import read_json_file
from argoverse.utils.ply_loader import load_ply
from argoverse.utils.se3 import SE3
from argoverse.utils.transform import quat2rotmat
from collections import defaultdict
import os
import sys
import numpy as np
from transform_utils import convert_3dbox_to_8corner
from iou_utils import iou3d
from sklearn.utils.linear_assignment_ import linear_assignment
from pyquaternion import Quaternion
# import argparse
tracking_names = ["VEHICLE", "PEDESTRIAN"]
def rotation_to_positive_z_angle(rotation):
q = Quaternion(rotation)
angle = q.angle if q.axis[2] > 0 else -q.angle
return angle
def get_mean():
# 1. read tracked files from ground truth
# 2. extract x, y, z, h, w, l, rot -> angle
path_datasets = glob.glob(os.path.join(args.path_dataset_root, "*"))
gt_trajectory_map = {tracking_name: defaultdict(dict) for tracking_name in tracking_names}
# store every detection data to compute mean and variance
gt_box_data = {tracking_name: [] for tracking_name in tracking_names}
for path_dataset in path_datasets: # path_tracker_output, path_dataset in zip(path_tracker_outputs, path_datasets):
log_id = pathlib.Path(path_dataset).name
if len(log_id) == 0 or log_id.startswith("_"):
continue
# path_tracker_output = os.path.join(path_tracker_output_root, log_id)
# print('\npath_tracker_output: ', path_tracker_output) # read tracked log files ALL, worked
# /media/basic/Transcend/argoai_tracking/argoverse-tracking/val_output/val-split-track-preds-maxage15-minhits5-conf0.3/033669d3-3d6b-3d3d-bd93-7985d86653ea
path_track_data = sorted(
glob.glob(os.path.join(os.fspath(path_dataset), \
"per_sweep_annotations_amodal", "*"))
)
# logger.info("log_id = %s", log_id)
# city_info_fpath = f"{path_dataset}/city_info.json"
# city_info = read_json_file(city_info_fpath)
# city_name = city_info["city_name"]
# logger.info("city name = %s", city_name)
for ind_frame in range(len(path_track_data)):
if ind_frame % 50 == 0:
# print("%d/%d" % (ind_frame, len(path_track_data)))
print("%d/%d" % (ind_frame, len(path_track_data)))
timestamp_lidar = int(path_track_data[ind_frame].split("/")[-1].split("_")[-1].split(".")[0])
# print('\ntimestamp: ', timestamp_lidar)
path_gt = os.path.join(
path_dataset, "per_sweep_annotations_amodal", f"tracked_object_labels_{timestamp_lidar}.json"
)
# print('\npath_dataset gt: ', path_gt) # corrected for reading val, all log files
#/media/basic/Transcend/argoai_tracking/argoverse-tracking/val/00c561b9-2057-358d-82c6-5b06d76cebcf/per_sweep_annotations_amodal/tracked_object_labels_315969629019741000.json
# gt_data = read_json_file(path_gt)
if not os.path.exists(path_gt):
# logger.warning("Missing ", path_gt)
continue
gt_data = read_json_file(path_gt)
for i in range(len(gt_data)):
if gt_data[i]["label_class"] not in tracking_names:
print('\nignored: ', gt_data[i]["label_class"])
continue
bbox, orientation = label_to_bbox(gt_data[i])
x, y, z = gt_data[i]["center"]["x"], \
gt_data[i]["center"]["y"], \
gt_data[i]["center"]["z"]
center = np.array([gt_data[i]["center"]["x"], gt_data[i]["center"]["y"], gt_data[i]["center"]["z"]])
w = gt_data[i]['width']
l = gt_data[i]["length"]
h = gt_data[i]["height"]
rotation = [gt_data[i]['rotation']['x'],
gt_data[i]['rotation']['y'],
gt_data[i]['rotation']['z'],
gt_data[i]['rotation']['w']]
# print('\nx,y,z, w, l, h: ', x, y, z, w, l, h)
z_angle = rotation_to_positive_z_angle(rotation)
# print('\nz_angle: ', z_angle)
box_data = np.array([
h, w, l,
x, y, z,
z_angle,
0, 0, 0, 0 # x_dot, y_dot, z_dot, a_dot
])
# print('\nbox_data: ', box_data)
track_label_uuid = gt_data[i]["track_label_uuid"]
cat = gt_data[i]["label_class"]
gt_trajectory_map[cat][track_label_uuid][ind_frame] = box_data
# compute x_dot, y_dot, z_dot
# if we can find the same object in the previous frame, get the velocity
if track_label_uuid in gt_trajectory_map[cat] \
and ind_frame - 1 \
in gt_trajectory_map[cat][track_label_uuid]:
residual_vel = box_data[3:7] - \
gt_trajectory_map[cat][track_label_uuid][ind_frame-1][3:7]
box_data[7:11] = residual_vel
gt_trajectory_map[cat][track_label_uuid][ind_frame] = box_data
# back fill
if gt_trajectory_map[cat][track_label_uuid][ind_frame-1][7] == 0:
gt_trajectory_map[cat][track_label_uuid][ind_frame-1][7:11] = residual_vel
gt_box_data[gt_data[i]["label_class"]].append(box_data)
gt_box_data = {tracking_name: np.stack(gt_box_data[tracking_name], axis=0) for tracking_name in tracking_names}
mean = {tracking_name: np.mean(gt_box_data[tracking_name], axis=0) for tracking_name in tracking_names}
std = {tracking_name: np.std(gt_box_data[tracking_name], axis=0) for tracking_name in tracking_names}
var = {tracking_name: np.var(gt_box_data[tracking_name], axis=0) for tracking_name in tracking_names}
print('\nh, w, l, x, y, z, a, x_dot, y_dot, z_dot, a_dot\n')
print('\nmean: ', mean, '\n'
'\nstd: ', std, '\n',
'\nvar: ', var) #Q
# return mean, std, var #Q
# for R, H
def matching_and_get_diff_stats():
# gt_path = args.path_dataset_root
# pr_path = args.path_tracker_output
tracking_names = ["VEHICLE", "PEDESTRIAN"]
diff = {tracking_name: [] for tracking_name in tracking_names} # [h, w, l, x, y, z, a]
diff_vel = {tracking_name: [] for tracking_name in tracking_names} # [x_dot, y_dot, z_dot, a_dot]
match_diff_t_map = {tracking_name: {} for tracking_name in tracking_names}
# similar to main.py class AB3DMOT update()
reorder = [3, 4, 5, 6, 2, 1, 0]
reorder_back = [6, 5, 4, 0, 1, 2, 3]
# gt_all = {tracking_name: defaultdict(dict) for tracking_name in tracking_names}
# pr_all = {tracking_name: defaultdict(dict) for tracking_name in tracking_names}
gt_trajectory_map = {tracking_name: defaultdict(dict) for tracking_name in tracking_names}
pr_trajectory_map = {tracking_name: defaultdict(dict) for tracking_name in tracking_names}
gts_ids = list()
prs_ids = list()
tmp_prs = list()
tmp_gts = list()
path_datasets = glob.glob(os.path.join(args.path_dataset_root, "*"))
for path_dataset in path_datasets: # path_tracker_output, path_dataset in zip(path_tracker_outputs, path_datasets):
log_id = pathlib.Path(path_dataset).name
if len(log_id) == 0 or log_id.startswith("_"):
continue
# path_tracker_output = os.path.join(path_tracker_output_root, log_id)
# print('\npath_tracker_output: ', path_tracker_output) # read tracked log files ALL, worked
# /media/basic/Transcend/argoai_tracking/argoverse-tracking/val_output/val-split-track-preds-maxage15-minhits5-conf0.3/033669d3-3d6b-3d3d-bd93-7985d86653ea
print('\npath_dataset: ', path_dataset)
path_track_data = sorted(
glob.glob(os.path.join(os.fspath(path_dataset), \
"per_sweep_annotations_amodal", "*"))
)
# iterate each *.json in each log_id
for ind_frame in range(len(path_track_data)):
if ind_frame % 50 == 0:
# print("%d/%d" % (ind_frame, len(path_track_data)))
print("%d/%d" % (ind_frame, len(path_track_data)))
timestamp_lidar = int(path_track_data[ind_frame].split("/")[-1].split("_")[-1].split(".")[0])
# print('\ntimestamp: ', timestamp_lidar)
# path of each json of gt
path_gt = os.path.join(
path_dataset, "per_sweep_annotations_amodal", f"tracked_object_labels_{timestamp_lidar}.json"
)
# print('\npath_dataset gt: ', path_gt) # corrected for reading val, all log files
#/media/basic/Transcend/argoai_tracking/argoverse-tracking/val/00c561b9-2057-358d-82c6-5b06d76cebcf/per_sweep_annotations_amodal/tracked_object_labels_315969629019741000.json
# gt_data = read_json_file(path_gt)
if not os.path.exists(path_gt):
# logger.warning("Missing ", path_gt)
continue
gt_data = read_json_file(path_gt)
# get data from gt
for i in range(len(gt_data)):
if gt_data[i]["label_class"] not in tracking_names:
# print('\nGT ignored: ', gt_data[i]["label_class"])
continue
bbox, orientation = label_to_bbox(gt_data[i])
x, y, z = gt_data[i]["center"]["x"], \
gt_data[i]["center"]["y"], \
gt_data[i]["center"]["z"]
center = np.array([gt_data[i]["center"]["x"], gt_data[i]["center"]["y"], gt_data[i]["center"]["z"]])
w = gt_data[i]['width']
l = gt_data[i]["length"]
h = gt_data[i]["height"]
rotation = [gt_data[i]['rotation']['x'],
gt_data[i]['rotation']['y'],
gt_data[i]['rotation']['z'],
gt_data[i]['rotation']['w']]
# print('\nx,y,z, w, l, h: ', x, y, z, w, l, h)
z_angle = rotation_to_positive_z_angle(rotation)
# print('\nz_angle: ', z_angle)
box_data = np.array([
h, w, l,
x, y, z,
z_angle
# 0, 0, 0, 0 # x_dot, y_dot, z_dot, a_dot
])
# print('\nbox_data: ', box_data)
track_label_uuid = gt_data[i]["track_label_uuid"]
cat = gt_data[i]["label_class"]
# get all gt
# print('\n', 'GT '*20, '\n')
# print('\ncat: ', cat)
# print('\nlog_id: ', log_id)
# print('\ntrack_label_uuid: ', track_label_uuid)
# print('\nind_frame: ', ind_frame)
# gt_trajectory_map[cat][log_id][track_label_uuid][ind_frame] = box_data
gt_trajectory_map[cat][track_label_uuid][ind_frame] = box_data
tmp_gts.append(box_data)
# gts = np.stack([np.array([box_data])], axis=0)
gts_ids.append(track_label_uuid)
gts = np.stack(tmp_gts, axis=0)
# here to get preds
path_datasets_output = glob.glob(os.path.join(args.path_tracker_output, "*"))
for path_dataset_trck in path_datasets_output:
# get tracked data
log_id = pathlib.Path(path_dataset_trck).name
if len(log_id) == 0 or log_id.startswith("_"):
continue
path_tracker_output = os.path.join(args.path_tracker_output, log_id)
print('\npath_tracker_output: ', path_tracker_output) # read tracked log files ALL, worked
# /media/basic/Transcend/argoai_tracking/argoverse-tracking/val_output/val-split-track-preds-maxage15-minhits5-conf0.3/033669d3-3d6b-3d3d-bd93-7985d86653ea
path_track_data = sorted(
glob.glob(os.path.join(os.fspath(path_tracker_output), \
"per_sweep_annotations_amodal", "*"))
)
print("log_id = %s", log_id)
for ind_frame_track in range(len(path_track_data)):
if ind_frame_track % 50 == 0:
# print("%d/%d" % (ind_frame_track, len(path_track_data)))
print("%d/%d" % (ind_frame_track, len(path_track_data)))
timestamp_lidar = int(path_track_data[ind_frame_track].split("/")[-1].split("_")[-1].split(".")[0])
path_pr = os.path.join(
path_dataset_trck, "per_sweep_annotations_amodal", f"tracked_object_labels_{timestamp_lidar}.json"
)
# print('\npath_dataset gt: ', path_pr) # corrected for reading val, all log files
#/media/basic/Transcend/argoai_tracking/argoverse-tracking/val/00c561b9-2057-358d-82c6-5b06d76cebcf/per_sweep_annotations_amodal/tracked_object_labels_315969629019741000.json
if not os.path.exists(path_pr):
print("Missing pr", path_pr)
continue
pr_data = read_json_file(path_pr)
# get data from pr
for i in range(len(pr_data)):
if pr_data[i]["label_class"] not in tracking_names:
# print('\nPR ignored: ', pr_data[i]["label_class"])
continue
bbox, orientation = label_to_bbox(pr_data[i])
x, y, z = pr_data[i]["center"]["x"], \
pr_data[i]["center"]["y"], \
pr_data[i]["center"]["z"]
center = np.array([pr_data[i]["center"]["x"], pr_data[i]["center"]["y"], pr_data[i]["center"]["z"]])
w = pr_data[i]['width']
l = pr_data[i]["length"]
h = pr_data[i]["height"]
rotation = [pr_data[i]['rotation']['x'],
pr_data[i]['rotation']['y'],
pr_data[i]['rotation']['z'],
pr_data[i]['rotation']['w']]
# print('\nx,y,z, w, l, h: ', x, y, z, w, l, h)
z_angle = rotation_to_positive_z_angle(rotation)
# print('\nz_angle: ', z_angle)
box_data = np.array([
h, w, l,
x, y, z,
z_angle
# 0, 0, 0, 0 # x_dot, y_dot, z_dot, a_dot
])
# print('\nbox_data: ', box_data)
track_label_uuid = pr_data[i]["track_label_uuid"]
cat = pr_data[i]["label_class"]
# print('\n', 'PR '*20, '\n')
# print('\ncat: ', cat)
# print('\nlog_id: ', log_id)
# print('\ntrack_label_uuid: ', track_label_uuid)
# print('\nind_frame: ', ind_frame_track)
# get all gt
# pr_trajectory_map[cat][log_id][track_label_uuid][ind_frame_track] = box_data
pr_trajectory_map[cat][track_label_uuid][ind_frame_track] = box_data
tmp_prs.append(box_data)
# prs = np.stack([np.array([box_data])], axis=0)
prs_ids.append(track_label_uuid)
prs = np.stack(tmp_prs, axis=0)
prs = prs[101:1000, reorder]
gts = gts[101:1000, reorder]
# if matching_dist == '3d_iou':
dets_8corner = [convert_3dbox_to_8corner(det_tmp) for det_tmp in prs]
gts_8corner = [convert_3dbox_to_8corner(gt_tmp) for gt_tmp in gts]
print('\n Computing distance matrix...')
print('\n dets len: ', len(dets_8corner))
print('\n gts_8corner len: ', len(gts_8corner))
iou_matrix = np.zeros((len(dets_8corner),len(gts_8corner)),dtype=np.float32)
for d,det in enumerate(dets_8corner):
for g,gt in enumerate(gts_8corner):
iou_matrix[d,g] = iou3d(det,gt)[0]
#print('iou_matrix: ', iou_matrix)
distance_matrix = -iou_matrix
threshold = 0.1
print('\n linear_assignment...')
matched_indices = linear_assignment(distance_matrix)
#print('matched_indices: ', matched_indices)
prs = prs[:, reorder_back]
gts = gts[:, reorder_back]
print('\n linear_assignment...DONE')
# loop each category/tracking_name
fl = False
for tracking_name in tracking_names:
# get pair id
for pair_id in range(matched_indices.shape[0]):
# compute diff_values
if distance_matrix[matched_indices[pair_id][0]][matched_indices[pair_id][1]] \
< threshold:
print('\n Computing diff_value...')
diff_value = prs[matched_indices[pair_id][0]] - gts[matched_indices[pair_id][1]]
diff[tracking_name].append(diff_value)
gt_track_id = gts_ids[matched_indices[pair_id][1]]
# update match_diff_t_map
if ind_frame not in match_diff_t_map[tracking_name]:
match_diff_t_map[tracking_name][ind_frame] = {gt_track_id: diff_value}
else:
match_diff_t_map[tracking_name][ind_frame][gt_track_id] = diff_value
# check if we have previous time_step's matching pair for current gt object
#print('t: ', t)
#print('len(match_diff_t_map): ', len(match_diff_t_map))
# compute diff_vel
try:
if ind_frame > 0 and ind_frame-1 in match_diff_t_map[tracking_name] \
and gt_track_id in match_diff_t_map[tracking_name][ind_frame-1]:
diff_vel_value = diff_value - \
match_diff_t_map[tracking_name][ind_frame-1][gt_track_id]
diff_vel[tracking_name].append(diff_vel_value)
except ValueError:
fl = True
diff = {tracking_name: np.stack(diff[tracking_name], axis=0) for tracking_name in tracking_names}
mean = {tracking_name: np.mean(diff[tracking_name], axis=0) for tracking_name in tracking_names}
std = {tracking_name: np.std(diff[tracking_name], axis=0) for tracking_name in tracking_names}
var = {tracking_name: np.var(diff[tracking_name], axis=0) for tracking_name in tracking_names}
print('Diff: Global coordinate system')
print('h, w, l, x, y, z, a\n')
print('mean: ', mean)
print('std: ', std)
print('var: ', var)
if not fl:
diff_vel = {tracking_name: np.stack(diff_vel[tracking_name], axis=0) for tracking_name in tracking_names}
mean_vel = {tracking_name: np.mean(diff_vel[tracking_name], axis=0) for tracking_name in tracking_names}
std_vel = {tracking_name: np.std(diff_vel[tracking_name], axis=0) for tracking_name in tracking_names}
var_vel = {tracking_name: np.var(diff_vel[tracking_name], axis=0) for tracking_name in tracking_names}
print('Diff: Global coordinate system')
print('h, w, l, x, y, z, a\n')
print('mean: ', mean)
print('std: ', std)
print('var: ', var)
print('\nh_dot, w_dot, l_dot, x_dot, y_dot, z_dot, a_dot\n')
print('mean_vel: ', mean_vel)
print('std_vel: ', std_vel)
print('var_vel: ', var_vel)
else:
print('Diff: Global coordinate system')
print('h, w, l, x, y, z, a\n')
print('mean: ', mean)
print('std: ', std)
print('var: ', var)
print(mean, std, var)
# return mean, std, var, mean_vel, std_vel, var_vel #R
if __name__ == '__main__':
# Settings.
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--path_dataset_root",
type=str,
default="/media/basic/Transcend/argoai_tracking/argoverse-tracking/val/",
)
parser.add_argument(
"--path_tracker_output",
type=str,
default="/media/basic/Transcend/argoai_tracking/argoverse-tracking/val_output/val-split-track-preds-maxage15-minhits5-conf0.3/"
)
parser.add_argument("--category", type=str, default="VEHICLE", required=False)
args = parser.parse_args()
# get_mean()
# # for observation noise covariance R
matching_and_get_diff_stats()