-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
executable file
·176 lines (159 loc) · 6.81 KB
/
main.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
# ------------------------------------------------------------------------
# Copyright (c) 2024 megvii-research. All Rights Reserved.
# ------------------------------------------------------------------------
import json, yaml
import logging
import copy
import argparse
import os
import time
import multiprocessing
from tqdm import tqdm
from datetime import datetime
from functools import partial
from tracker.base_tracker import Base3DTracker
from dataset.baseversion_dataset import BaseVersionTrackingDataset
from evaluation.static_evaluation.kitti.evaluation_HOTA.scripts.run_kitti import (
eval_kitti,
)
from evaluation.static_evaluation.nuscenes.eval import eval_nusc
from evaluation.static_evaluation.waymo.eval import eval_waymo
from utils.kitti_utils import save_results_kitti
from utils.nusc_utils import save_results_nuscenes, save_results_nuscenes_for_motion
from utils.waymo_utils.convert_result import save_results_waymo
def run(scene_id, scenes_data, cfg, args, tracking_results):
"""
Info: This function tracks objects in a given scene, processes frame data, and stores tracking results.
Parameters:
input:
scene_id: ID of the scene to process.
scenes_data: Dictionary with scene data.
cfg: Configuration settings for tracking.
args: Additional arguments.
tracking_results: Dictionary to store results.
output:
tracking_results: Updated tracking results for the scene.
"""
scene_data = scenes_data[scene_id]
dataset = BaseVersionTrackingDataset(scene_id, scene_data, cfg=cfg)
tracker = Base3DTracker(cfg=cfg)
all_trajs = {}
for index in tqdm(range(len(dataset)), desc=f"Processing {scene_id}"):
frame_info = dataset[index]
frame_id = frame_info.frame_id
cur_sample_token = frame_info.cur_sample_token
all_traj = tracker.track_single_frame(frame_info)
result_info = {
"frame_id": frame_id,
"cur_sample_token": cur_sample_token,
"trajs": copy.deepcopy(all_traj),
"transform_matrix": frame_info.transform_matrix,
}
all_trajs[frame_id] = copy.deepcopy(result_info)
if cfg["TRACKING_MODE"] == "GLOBAL":
trajs = tracker.post_processing()
for index in tqdm(
range(len(dataset)), desc=f"Trajectory Postprocessing {scene_id}"
):
frame_id = dataset[index].frame_id
for track_id in sorted(list(trajs.keys())):
for bbox in trajs[track_id].bboxes:
if (
bbox.frame_id == frame_id
and bbox.is_interpolation
and track_id not in all_trajs[frame_id]["trajs"].keys()
):
all_trajs[frame_id]["trajs"][track_id] = bbox
for index in tqdm(
range(len(dataset)), desc=f"Trajectory Postprocessing {scene_id}"
):
frame_id = dataset[index].frame_id
for track_id in sorted(list(trajs.keys())):
det_score = 0
for bbox in trajs[track_id].bboxes:
det_score = bbox.det_score
break
if (
track_id in all_trajs[frame_id]["trajs"].keys()
and det_score <= cfg["THRESHOLD"]["GLOBAL_TRACK_SCORE"]
):
del all_trajs[frame_id]["trajs"][track_id]
tracking_results[scene_id] = all_trajs
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="MCTrack")
parser.add_argument(
"--dataset",
type=str,
default="kitti",
help="Which Dataset: kitti/nuscenes/waymo",
)
parser.add_argument("--eval", "-e", action="store_true", help="evaluation")
parser.add_argument("--load_image", "-lm", action="store_true", help="load_image")
parser.add_argument("--load_point", "-lp", action="store_true", help="load_point")
parser.add_argument("--debug", action="store_true", help="debug")
parser.add_argument("--mode", "-m", action="store_true", help="online or offline")
parser.add_argument("--process", "-p", type=int, default=1, help="multi-process!")
args = parser.parse_args()
if args.dataset == "kitti":
cfg_path = "./config/kitti.yaml"
elif args.dataset == "nuscenes":
cfg_path = "./config/nuscenes.yaml"
elif args.dataset == "waymo":
cfg_path = "./config/waymo.yaml"
if args.mode:
cfg_path = cfg_path.replace(".yaml", "_offline.yaml")
cfg = yaml.load(open(cfg_path, "r"), Loader=yaml.Loader)
save_path = os.path.join(
os.path.dirname(cfg["SAVE_PATH"]),
cfg["DATASET"],
datetime.now().strftime("%Y%m%d_%H%M%S"),
)
os.makedirs(save_path, exist_ok=True)
cfg["SAVE_PATH"] = save_path
start_time = time.time()
detections_root = os.path.join(
cfg["DETECTIONS_ROOT"], cfg["DETECTOR"], cfg["SPLIT"] + ".json"
)
with open(detections_root, "r", encoding="utf-8") as file:
print(f"Loading data from {detections_root}...")
data = json.load(file)
print("Data loaded successfully.")
if args.debug:
if args.dataset == "kitti":
scene_lists = [str(scene_id).zfill(4) for scene_id in cfg["TRACKING_SEQS"]]
elif args.dataset == "nuscenes":
scene_lists = [scene_id for scene_id in data.keys()][:2]
else:
scene_lists = [scene_id for scene_id in data.keys()][:2]
else:
scene_lists = [scene_id for scene_id in data.keys()]
manager = multiprocessing.Manager()
tracking_results = manager.dict()
if args.process > 1:
pool = multiprocessing.Pool(args.process)
func = partial(
run, scenes_data=data, cfg=cfg, args=args, tracking_results=tracking_results
)
pool.map(func, scene_lists)
pool.close()
pool.join()
else:
for scene_id in tqdm(scene_lists, desc="Running scenes"):
run(scene_id, data, cfg, args, tracking_results)
tracking_results = dict(tracking_results)
if args.dataset == "kitti":
save_results_kitti(tracking_results, cfg)
if args.eval:
eval_kitti(cfg)
if args.dataset == "nuscenes":
save_results_nuscenes(tracking_results, save_path)
save_results_nuscenes_for_motion(tracking_results, save_path)
if args.eval:
eval_nusc(cfg)
elif args.dataset == "waymo":
save_results_waymo(tracking_results, save_path)
if args.eval:
eval_waymo(cfg, save_path)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time} seconds")