forked from johnwlambert/argoverse_cbgs_kf_tracker
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_ab3dmot.py
executable file
·328 lines (251 loc) · 10.8 KB
/
run_ab3dmot.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
#!/usr/bin/env python3
import time
import shutil
import argparse
import copy
import numpy as np
import os
from pathlib import Path
import pdb
from tqdm import tqdm
import uuid
from ab3dmot import AB3DMOT
import argoverse
from argoverse.data_loading.object_label_record import json_label_dict_to_obj_record
from argoverse.data_loading.simple_track_dataloader import SimpleArgoverseTrackingDataLoader
from argoverse.utils.se2 import SE2
from transform_utils import (
yaw_to_quaternion3d,
se2_to_yaw,
get_B_SE2_A,
rotmat2d
)
from json_utils import read_json_file, save_json_dict
from argoverse.evaluation.competition_util import generate_tracking_zip
from argoverse.evaluation.eval_tracking import eval_tracks
from argoverse.map_representation.map_api import ArgoverseMap
def check_mkdir(dirpath):
""" """
if not Path(dirpath).exists():
os.makedirs(dirpath, exist_ok=True)
class UUIDGeneration():
def __init__(self):
self.mapping = {}
def get_uuid(self,seed):
if seed not in self.mapping:
self.mapping[seed] = uuid.uuid4().hex
return self.mapping[seed]
uuid_gen = UUIDGeneration()
def yaw_from_bbox_corners(det_corners: np.ndarray) -> float:
"""
Use basic trigonometry on cuboid to get orientation angle.
Args:
- det_corners: corners of bounding box
Returns:
- yaw
"""
p1 = det_corners[1]
p5 = det_corners[5]
dy = p1[1] - p5[1]
dx = p1[0] - p5[0]
# the orientation angle of the car
yaw = np.arctan2(dy, dx)
return yaw
def run_ab3dmot(
classname: str,
pose_dir: str,
dets_dump_dir: str,
tracks_dump_dir: str,
max_age: int = 3,
min_hits: int = 1,
min_conf: float = 0.3,
match_algorithm: str = 'h',
match_threshold: float = 4,
match_distance: float = 'iou',
p: np.ndarray = np.eye(10),
thr_estimate: float = 0.8,
thr_prune: float = 0.1,
ps: float = 0.9
) -> None:
"""
#path to argoverse tracking dataset test set, we will add our predicted labels into per_sweep_annotations_amodal/
#inside this folder
Filtering occurs in the city frame, not the egovehicle frame.
Args:
- classname: string, either 'VEHICLE' or 'PEDESTRIAN'
- pose_dir: string
- dets_dump_dir: string
- tracks_dump_dir: string
- max_age: integer
- min_hits: integer
Returns:
- None
"""
dl = SimpleArgoverseTrackingDataLoader(data_dir=pose_dir, labels_dir=dets_dump_dir)
am = ArgoverseMap()
for log_id in tqdm(dl.sdb.get_valid_logs()):
print(log_id)
city_name = dl.get_city_name(log_id)
labels_folder = dets_dump_dir + "/" + log_id + "/per_sweep_annotations_amodal/"
lis = os.listdir(labels_folder)
lidar_timestamps = [ int(file.split(".")[0].split("_")[-1]) for file in lis]
lidar_timestamps.sort()
previous_frame_bbox = []
ab3dmot = AB3DMOT(thr_estimate=thr_estimate, thr_prune=thr_prune, ps=ps)
print(labels_folder)
tracked_labels_copy = []
for j, current_lidar_timestamp in enumerate(lidar_timestamps):
dets = dl.get_labels_at_lidar_timestamp(log_id, current_lidar_timestamp)
dets_copy = dets
transforms = []
city_SE3_egovehicle = dl.get_city_to_egovehicle_se3(log_id, current_lidar_timestamp)
egovehicle_SE3_city = city_SE3_egovehicle.inverse()
transformed_labels = []
conf = []
for l_idx, l in enumerate(dets):
if l['label_class'] != classname:
# will revisit in other tracking pass
continue
if l["score"] < min_conf:
# print('Skipping det with confidence ', l["score"])
continue
det_obj = json_label_dict_to_obj_record(l)
det_corners_egovehicle_fr = det_obj.as_3d_bbox()
transforms += [city_SE3_egovehicle]
if city_SE3_egovehicle is None:
print('Was None')
# convert detection from egovehicle frame to city frame
det_corners_city_fr = city_SE3_egovehicle.transform_point_cloud(det_corners_egovehicle_fr)
ego_xyz = np.mean(det_corners_city_fr, axis=0)
# Check the driveable/roi area
#da = am.remove_non_driveable_area_points(np.array([ego_xyz]), city_name=city_name)
# if len(da) == 0 and l['label_class'] == 'VEHICLE':
# continue
# roi = am.remove_non_roi_points(np.array([ego_xyz]), city_name=city_name)
# if len(roi) == 0:
# continue
yaw = yaw_from_bbox_corners(det_corners_city_fr)
transformed_labels += [ [ego_xyz[0], ego_xyz[1], ego_xyz[2], yaw, l["length"],l["width"],l["height"]]]
conf += [l["score"]]
if len(transformed_labels) > 0:
transformed_labels = np.array(transformed_labels)
else:
transformed_labels = np.empty((0,7))
dets_all = {
"dets": transformed_labels,
"info": np.zeros(transformed_labels.shape),
"conf": conf
}
# perform measurement update in the city frame.
dets_with_object_id = ab3dmot.update(
dets_all,
match_distance,
match_threshold,
match_algorithm,
p
)
tracked_labels = []
for det in dets_with_object_id:
# move city frame tracks back to ego-vehicle frame
xyz_city = np.array([det[0].item(), det[1].item(), det[2].item()]).reshape(1,3)
city_yaw_object = det[3]
city_se2_object = SE2(rotation=rotmat2d(city_yaw_object), translation=xyz_city.squeeze()[:2])
city_se2_egovehicle, city_yaw_ego = get_B_SE2_A(city_SE3_egovehicle)
ego_se2_city = city_se2_egovehicle.inverse()
egovehicle_se2_object = ego_se2_city.right_multiply_with_se2(city_se2_object)
# recreate all 8 points
# transform them
# compute yaw from 8 points once more
egovehicle_SE3_city = city_SE3_egovehicle.inverse()
xyz_ego = egovehicle_SE3_city.transform_point_cloud(xyz_city).squeeze()
# update for new yaw
# transform all 8 points at once, then compute yaw on the fly
ego_yaw_obj = se2_to_yaw(egovehicle_se2_object)
qx,qy,qz,qw = yaw_to_quaternion3d(ego_yaw_obj)
tracked_labels.append({
"center": {"x": xyz_ego[0], "y": xyz_ego[1], "z": xyz_ego[2]},
"rotation": {"x": qx , "y": qy, "z": qz , "w": qw},
"length": det[4],
"width": det[5],
"height": det[6],
"track_label_uuid": uuid_gen.get_uuid(det[7]),
"timestamp": current_lidar_timestamp ,
"label_class": classname
})
tracked_labels_copy = copy.deepcopy(tracked_labels)
label_dir = os.path.join(tracks_dump_dir, log_id, "per_sweep_annotations_amodal")
check_mkdir(label_dir)
json_fname = f"tracked_object_labels_{current_lidar_timestamp}.json"
json_fpath = os.path.join(label_dir, json_fname)
if Path(json_fpath).exists():
# accumulate tracks of another class together
prev_tracked_labels = read_json_file(json_fpath)
tracked_labels.extend(prev_tracked_labels)
save_json_dict(json_fpath, tracked_labels)
if __name__ == '__main__':
split = 'val'
#path_dataset = "/media/sda1/argoverse-tracking"
path_dataset = "../../Data/argoverse"
path_detections = f"{path_dataset}/argoverse_detections_2020/{split}"
path_data = f"{path_dataset}/{split}"
path_results = f"{path_dataset}/results/results_tracking_{split}_cbgs"
filename_v = f"{path_dataset}/results/v_grid_{split}_cbgs.txt"
filename_p = f"{path_dataset}/results/p_grid_{split}_cbgs.txt"
min_conf = 0.3
#match_threshold = 10
match_distance = 'iou'
p_pos_mult, p_vel_mult = 1, 1
p = np.eye(10)
p[0:7,0:7] *= p_pos_mult
p[7:,7:] *= p_vel_mult
thr_prune = 0.01
for thr_estimate in [0.25, 0.3, 0.35]:
for ps in [0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]:
if os.path.exists(path_results):
shutil.rmtree(path_results)
match_threshold = 0.001
min_conf = 0.3
time_start = time.time()
# Vehicle
run_ab3dmot('VEHICLE', path_data, path_detections, path_results,
min_conf=min_conf,
match_algorithm ='h',
match_threshold = match_threshold,
match_distance = match_distance,
p = p,
thr_estimate = thr_estimate,
thr_prune=thr_prune,
ps = ps
)
print(f"Elapsed tracking vehicle {time.time() - time_start}")
time_start = time.time()
with open(filename_v, "a+") as out_file:
out_file.write(f"{p_vel_mult} min_conf {min_conf} match_thr {match_threshold} {thr_prune} {thr_estimate} {ps} ")
eval_tracks(path_results, path_data, 0, 100, out_file, 'average',
diffatt=None, category='VEHICLE')
print(f"Elapsed evaluating vehicle {time.time() - time_start}")
if os.path.exists(path_results):
shutil.rmtree(path_results)
match_threshold = 0.001
min_conf = 0.26
time_start = time.time()
# Pedestrian
run_ab3dmot('PEDESTRIAN', path_data, path_detections, path_results,
min_conf=min_conf,
match_algorithm ='h',
match_threshold = match_threshold,
match_distance = match_distance,
p = p,
thr_estimate = thr_estimate,
thr_prune=thr_prune,
ps = ps
)
print(f"Elapsed tracking pedestrian {time.time() - time_start}")
time_start = time.time()
with open(filename_p, "a+") as out_file:
out_file.write(f"{p_vel_mult} min_conf {min_conf} match_thr {match_threshold} {thr_prune} {thr_estimate} {ps} ")
eval_tracks(path_results, path_data, 0, 100, out_file, 'average',
diffatt=None, category='PEDESTRIAN')
print(f"Elapsed evaluating pedestrian {time.time() - time_start}")
generate_tracking_zip(f"{path_dataset}/results/results_tracking_{split}_cbgs",
f"{path_dataset}/results")