-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.py
211 lines (178 loc) · 6.54 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
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
import sys
import pdb
import os
import shutil
import time
import torch
import cv2
import numpy as np
import dataset
import utils
from external.adaptors import detector
from trackers import ocsort_embedding as tracker_module
def get_main_args():
parser = tracker_module.args.make_parser()
parser.add_argument("--dataset", type=str, default="dance")
parser.add_argument("--result_folder", type=str,
default="results/trackers/")
parser.add_argument("--test_dataset", action="store_true")
parser.add_argument("--exp_name", type=str, default="debug")
parser.add_argument("--min_box_area", type=float,
default=10, help="filter out tiny boxes")
parser.add_argument(
"--aspect_ratio_thresh",
type=float,
default=1.6,
help="threshold for filtering out boxes of which aspect ratio are above the given value.",
)
parser.add_argument("--post", type=bool, default=True,
help="run post-processing linear interpolation.",)
parser.add_argument("--w_assoc_emb", type=float,
default=0.75, help="Combine weight for emb cost")
parser.add_argument(
"--alpha_gate",
type=float,
default=0.9,
help="alpha_gate",
)
parser.add_argument(
"--gate",
type=float,
default=1,
help="gate",
)
parser.add_argument(
"--gate2",
type=float,
default=0.3,
help="gate",
)
parser.add_argument("--new_kf_off", type=bool, default=True)
# TODO:
# --AARM action="store_true"
# --TOPIC action="store_true"
parser.add_argument("--AARM", action="store_true")
parser.add_argument("--TOPIC", action="store_true")
args = parser.parse_args()
if args.dataset == "mot17":
args.result_folder = os.path.join(args.result_folder, "MOT17-val")
elif args.dataset == "mot20":
args.result_folder = os.path.join(args.result_folder, "MOT20-val")
elif args.dataset == "dance":
args.result_folder = os.path.join(args.result_folder, "DANCE-val")
elif args.dataset == "BEE23":
args.result_folder = os.path.join(args.result_folder, "BEE23-val")
elif args.dataset == "gmot":
args.result_folder = os.path.join(args.result_folder, "GMOT-val")
if args.test_dataset:
args.result_folder.replace("-val", "-test")
return args
def main():
np.set_printoptions(suppress=True, precision=5)
args = get_main_args()
if args.dataset == "mot17":
if args.test_dataset:
detector_path = "external/weights/topictrack_mot17.pth.tar"
else:
detector_path = "external/weights/topictrack_ablation.pth.tar"
size = (800, 1440)
elif args.dataset == "mot20":
if args.test_dataset:
detector_path = "external/weights/topictrack_mot20.tar"
size = (896, 1600)
else:
detector_path = "external/weights/topictrack_mot17.pth.tar"
size = (800, 1440)
elif args.dataset == "dance":
detector_path = "external/weights/topictrack_dance.pth.tar"
size = (800, 1440)
elif args.dataset == "BEE23":
detector_path = "external/weights/topictrack_bee23.pth.tar"
size = (800, 1440)
elif args.dataset == "gmot":
detector_path = "external/weights/topictrack_gmot.pth.tar"
size = (800, 1440)
else:
raise RuntimeError(
"Need to update paths for detector for extra datasets.")
det = detector.Detector("yolox", detector_path, args.dataset)
loader = dataset.get_mot_loader(args.dataset, args.test_dataset, size=size)
oc_sort_args = dict(
args=args,
det_thresh=args.track_thresh,
alpha_gate=args.alpha_gate,
gate=args.gate,
gate2=args.gate2,
iou_threshold=args.iou_thresh,
asso_func=args.asso,
delta_t=args.deltat,
inertia=args.inertia,
w_association_emb=args.w_assoc_emb,
new_kf_off=args.new_kf_off,
)
tracker = tracker_module.ocsort.OCSort(**oc_sort_args)
results = {}
frame_count = 0
total_time = 0
for (img, np_img), label, info, idx in loader:
frame_id = info[2].item()
video_name = info[4][0].split("/")[0]
tag = f"{video_name}:{frame_id}"
print(tag)
if video_name not in results:
results[video_name] = []
img = img.cuda()
if frame_id == 1:
tracker.dump_cache()
tracker = tracker_module.ocsort.OCSort(**oc_sort_args)
start_time = time.time()
pred = det(img, tag)
if pred is None:
continue
targets = tracker.update(
pred, img, np_img[0].numpy(), tag, args.AARM, args.TOPIC)
tlwhs, ids = utils.filter_targets(
targets, args.aspect_ratio_thresh, args.min_box_area, args.dataset)
total_time += time.time() - start_time
frame_count += 1
results[video_name].append((frame_id, tlwhs, ids))
det.dump_cache()
tracker.dump_cache()
folder = os.path.join(args.result_folder, args.exp_name, "data")
os.makedirs(folder, exist_ok=True)
for name, res in results.items():
result_filename = os.path.join(folder, f"{name}.txt")
utils.write_results_no_score(result_filename, res)
print(f"Finished, results saved to {folder}")
if args.post:
post_folder = os.path.join(args.result_folder, args.exp_name + "_post")
pre_folder = os.path.join(args.result_folder, args.exp_name)
if os.path.exists(post_folder):
print(f"Overwriting previous results in {post_folder}")
shutil.rmtree(post_folder)
shutil.copytree(pre_folder, post_folder)
post_folder_data = os.path.join(post_folder, "data")
utils.dti(post_folder_data, post_folder_data)
print(
f"Linear interpolation post-processing applied, saved to {post_folder_data}.")
def draw(name, pred, i):
pred = pred.cpu().numpy()
name = os.path.join("data/mot/train", name)
img = cv2.imread(name)
for s in pred:
p = np.round(s[:4]).astype(np.int32)
cv2.rectangle(img, (p[0], p[1]), (p[2], p[3]), (255, 0, 0), 3)
for s in pred:
p = np.round(s[:4]).astype(np.int32)
cv2.putText(
img,
str(int(round(s[4], 2) * 100)),
(p[0] + 20, p[1] + 20),
cv2.FONT_HERSHEY_PLAIN,
2,
(0, 0, 255),
thickness=3,
)
cv2.imwrite(f"debug/{i}.png", img)
if __name__ == "__main__":
main()