-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_tracking_eval_tracks10.py
183 lines (152 loc) · 7.26 KB
/
train_tracking_eval_tracks10.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
'''
This script handles the training, tracking and evaluation.
'''
import glob
import argparse
import os
import pandas as pd
import time
import subprocess
from engine.trainval import trainval
from engine.inference import tracking
from utils import resultcsv_2xml
import sys
__author__ = "Yudong Zhang"
def save_args_to_file(args, path):
with open(path, 'a+') as file:
for arg, value in vars(args).items():
if isinstance(value, list):
value = ', '.join(map(str, value))
file.write(f"{arg}: {value}\n")
file.write('--------------------------')
def parse_args_():
parser = argparse.ArgumentParser()
# train params
# data params
parser.add_argument('--trainfilename', type=str, default='tracks10')
parser.add_argument('--train_path', default='dataset/tracks10/trainval_data/past7_depth2_near5/merge_train.txt')
parser.add_argument('--val_path', default='dataset/tracks10/trainval_data/past7_depth2_near5/merge_val.txt')
parser.add_argument('--len_established',type=int,default=7)
parser.add_argument('--len_future',type=int,default=2)
parser.add_argument('--near',type=int,default=5)
# network params
parser.add_argument('--n_layers', type=int, default=1)
parser.add_argument('--d_k', type=int, default=96)
parser.add_argument('--d_v', type=int, default=96)
parser.add_argument('--n_head', type=int, default=6)
parser.add_argument('--d_model', type=int, default=96*6)
parser.add_argument('--d_inner_hid', type=int, default=96*6*2)
parser.add_argument('--n_position',type=int,default=5000)
# training params
parser.add_argument('--epoch', type=int, default=30)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--n_warmup_steps', type=int, default=1000)
parser.add_argument('--lr_mul', type=float, default=2.0)
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--dropout', type=float, default=0.1)
parser.add_argument('--label_smoothing', default = True)
# output and record
# parser.add_argument('--output_dir', type=str, default=outputmodel_path)
parser.add_argument('--use_tb', default=False, action='store_true')
parser.add_argument('--save_mode', type=str, choices=['all', 'best'], default='best')
parser.add_argument('--ckpt_save_root', type=str, default='./checkpoint')
# device
parser.add_argument('--no_cuda', default=False, action='store_true')
# test params
# data params
parser.add_argument('--test_path', type=str, default='dataset/tracks10/GTxml')
parser.add_argument('--testsnr_list', nargs='+', type=int, choices=[1,2,4,7], default=[4,7], help='example: --testsnr_list 1 2 7')
parser.add_argument('--detection_list', nargs='+', default=['testdata'],
help='List of data types. Example: --detection_list ground_truth deepblink_det')
# GT path
parser.add_argument('--GTfolder', type=str, default='dataset/tracks10/GTxml/testdata')
parser.add_argument('--det_keep_rate', type=float, default=1.0)
# save
parser.add_argument('--eval_save_path', type=str, default='./prediction/')
# choose process
parser.add_argument('--train', default=False, action='store_true')
parser.add_argument('--tracking', default=False, action='store_true')
parser.add_argument('--model_ckpt_path', type=str, default=None)
parser.add_argument('--eval', default=False, action='store_true')
opt = parser.parse_args()
return opt
if __name__ == '__main__':
# sys.exit(0)
opt = parse_args_()
print(opt.train)
print(opt.tracking)
trainfilename = opt.trainfilename
assert trainfilename in opt.train_path and trainfilename in opt.val_path
# data param
past = opt.len_established
cand = opt.len_future
near = opt.near
opt.num_cand = near**cand
#==================train====================
now = int(round(time.time()*1000))
nowname = time.strftime('%Y%m%d_%H_%M_%S',time.localtime(now/1000))
expname = nowname+'_'+trainfilename.replace(' ','_')+'_ckpt'
if opt.train:
# make save ckpt folder
outputmodel_path = os.path.join(opt.ckpt_save_root,expname)
if not os.path.exists(outputmodel_path):
os.makedirs(outputmodel_path)
opt.output_dir = outputmodel_path
# save params
save_args_to_file(opt, os.path.join(outputmodel_path,'param.txt'))
# train
trainval(opt)
#==================test=====================
if opt.tracking and not opt.train:
assert opt.model_ckpt_path is not None
# outputmodel_path = opt.model_ckpt_path
# print('outputmodel_path')
# print(outputmodel_path)
if opt.tracking:
detection_list = opt.detection_list
for datatype in detection_list:
# testfilename_list = [trainfilename.replace('1247',str(i)) for i in opt.testsnr_list]
testfilename_list = [os.path.split(fi)[1][:-4] for fi in glob.glob(os.path.join(opt.test_path, datatype, "**.xml"))]
testfilename_list.sort()
for testfilename in testfilename_list:
#==================tracking=====================
savefolder = os.path.join(nowname+'_'+datatype, testfilename.replace(' ','_'))
output_path = os.path.join(opt.eval_save_path,savefolder)
if not os.path.exists(output_path):
os.makedirs(output_path)
# prepare model ckpt
if opt.tracking and not opt.train:
model_p = opt.model_ckpt_path
else:
model_p = glob.glob(outputmodel_path+'/**.chkpt')[-1].replace('\\','/')
print(model_p)
# prepare data path
test_det_pa = os.path.join(opt.test_path, datatype, testfilename+'.xml')
# output csv path
output_csv_pa = os.path.join(output_path, testfilename.replace(' ','_')+'.csv')
# save params
save_args_to_file(opt, os.path.join(output_path, testfilename.replace(' ','_')+'_param.txt'))
keep_track = tracking(
input_detxml=test_det_pa,
output_trackcsv=output_csv_pa,
model_path=model_p,
fract=opt.det_keep_rate,
Past=past,
Cand=cand,
Near=near,
no_cuda=opt.no_cuda
)
#==================evaluation=====================
if opt.eval:
# write csv result to xml file for eval
xmlfilepath = os.path.join(output_path, testfilename.replace(' ','_')+'.xml')
resultcsv_2xml(xmlfilepath, output_csv_pa, None)
# prepare gt path, result path, and output path
ref = os.path.join(opt.GTfolder,testfilename+'.xml')
can = xmlfilepath
out = can.replace('xml','txt')
subprocess.call(
['java', '-jar', 'trackingPerformanceEvaluation.jar',
'-r', ref, '-c', can,'-o',out])
print('[Info] Finish evaluating')
print(f'Save file:{out}')