-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRun_multi_cat_transformer.py
192 lines (175 loc) · 9.11 KB
/
Run_multi_cat_transformer.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
import os
from os.path import join, isdir
from tracker_stage3_transformer import *
import numpy as np
import torch
import argparse
import pickle
import random
import math
import warnings
warnings.simplefilter("ignore", UserWarning)
def genConfig(seq_path, set_type):
path, seqname = os.path.split(seq_path)
if 'RGBT' in set_type:
img_list_visible = sorted([seq_path + '/visible/' + p for p in os.listdir(seq_path + '/visible') if os.path.splitext(p)[1] in ['.jpg','.png','.bmp']])
img_list_infrared = sorted([seq_path + '/infrared/' + p for p in os.listdir(seq_path + '/infrared') if os.path.splitext(p)[1] in ['.jpg','.png','.bmp']])
gt = np.loadtxt(seq_path + '/init.txt', delimiter=',')
elif 'GTOT' in set_type:
img_list_visible = sorted([seq_path + '/v/' + p for p in os.listdir(seq_path + '/v') if os.path.splitext(p)[1] in ['.jpg','.png','.bmp']])
img_list_infrared = sorted([seq_path + '/i/' + p for p in os.listdir(seq_path + '/i') if os.path.splitext(p)[1] in ['.jpg','.png','.bmp']])
gt = np.loadtxt(seq_path + '/init.txt')
if gt.shape[1] == 8:
x_min = np.min(gt[:, [0, 2, 4, 6]], axis=1)[:, None]
y_min = np.min(gt[:, [1, 3, 5, 7]], axis=1)[:, None]
x_max = np.max(gt[:, [0, 2, 4, 6]], axis=1)[:, None]
y_max = np.max(gt[:, [1, 3, 5, 7]], axis=1)[:, None]
gt = np.concatenate((x_min, y_min, x_max - x_min, y_max - y_min), axis=1)
return img_list_visible,img_list_infrared,gt
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-set_type", default = 'RGBT234')
# parser.add_argument("-model_path", default = './models/GTOT_ALL852.pth')
parser.add_argument("-result_path", default = './results/')
parser.add_argument("-visual_log",default=False, action= 'store_true')
parser.add_argument("-visualize",default=False, action='store_true')
parser.add_argument("-adaptive_align",default=True, action='store_false')
parser.add_argument("-padding",default=1.2, type = float)
parser.add_argument("-jitter",default=True, action='store_false')
args = parser.parse_args()
##################################################################################
#########################Just modify opts in this script.#########################
######################Becuase of synchronization of options#######################
##################################################################################
## option setting
# opts['model_path']=args.model_path
# opts['result_path']='./results/'+args.set_type+'/'+args.model_path.split('/')[-1].split('.')[0]+'/'
opts['visual_log']=args.visual_log
opts['set_type']=args.set_type
opts['visualize'] = args.visualize
opts['adaptive_align'] = args.adaptive_align
opts['padding'] = args.padding
opts['jitter'] = args.jitter
##################################################################################
############################Do not modify opts anymore.###########################
######################Becuase of synchronization of options#######################
##################################################################################
if opts['set_type'] == 'RGBT234':
## for RGBT234
opts['lr_init'] = 0.0003
opts['lr_update'] = 0.0003
opts['lr_mult'] = {'fc6':10}
opts['maxiter_update'] = 15
opts['maxiter_init'] = 50
elif opts['set_type'] == 'GTOT':
## for GTOT
opts['lr_init'] = 0.00035
opts['lr_update'] = 0.0002
opts['lr_mult'] = {'fc6':11}
opts['maxiter_update'] = 10
opts['maxiter_init'] = 65
opts['trans_f_expand'] = 1.4
print opts
## path initialization
dataset_path = '/data/dataset/'
seq_home = dataset_path + opts['set_type']
seq_list = [f for f in os.listdir(seq_home) if isdir(join(seq_home,f))]
model_path = '/home/liulei/CAT++/models/GTOT_model_stage3_transformer/'
while True:
flag = False
model_list = os.listdir(model_path)
# import pdb
# pdb.set_trace()
model_list.sort(key=lambda x:int(x.split('i')[1].split('.')[0]))
model_list.reverse()
# gpuid = 3
for model in model_list:
torch.cuda.empty_cache()
# try:
# args.model_path = os.path.join(model_path, model)
# opts['model_path'] = '/home/user/liulei/cat/models/GTOT_multi/GTOT_multi1011.pth'
opts['model_path']=os.path.join(model_path, model)
# torch.cuda.set_device(args.gpuid)
opts['result_path'] = os.path.join(args.result_path, args.set_type) #+'/'+args.model_path.split('/')[-3]+
if not os.path.exists(opts['result_path']):
os.mkdir(opts['result_path'])
opts['result_path'] = os.path.join(opts['result_path'], opts['model_path'].split('/')[-3])
if not os.path.exists(opts['result_path']):
os.mkdir(opts['result_path'])
opts['result_path'] = os.path.join(opts['result_path'], opts['model_path'].split('/')[-2]+opts['model_path'].split('/')[-1].split('.')[0]+'ADRNet_nobb')
if not os.path.exists(opts['result_path']):
os.mkdir(opts['result_path'])
print(opts['model_path'])
else:
print(opts['model_path'], 'exists!')
continue
# except:
# continue
# import pynvml
# pynvml.nvmlInit()
# while True:
# handle = pynvml.nvmlDeviceGetHandleByIndex(gpuid)
# meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
# used = meminfo.used / 1024 /1024
# if used<7000:
# torch.cuda.set_device(gpuid)
# break
# else:
# time.sleep(60)
iou_list=[]
fps_list=dict()
bb_result = dict()
result = dict()
iou_list_nobb=[]
bb_result_nobb = dict()
for num,seq in enumerate(seq_list):
torch.cuda.empty_cache()
np.random.seed(123)
torch.manual_seed(456)
torch.cuda.manual_seed(789)
# seed = 3407
# os.environ['PYTHONHASHSEED'] = str(seed)
# torch.manual_seed(seed)
# torch.cuda.manual_seed(seed)
# torch.cuda.manual_seed_all(seed)
# np.random.seed(seed)
# random.seed(seed)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
# torch.backends.cudnn.enabled = False
seq_path = seq_home + '/' + seq
img_list_v,img_list_i,gt=genConfig(seq_path,opts['set_type'])
iou_result, iou_result_bb, result, result_bb, fps = run_mdnet(img_list_v, img_list_i, gt[0], gt, seq = seq, display=opts['visualize'])
# iou_result, result_bb, fps, result_nobb = run_mdnet(img_list_v, img_list_i, gt[0], gt, seq = seq, display=opts['visualize'])
enable_frameNum = 0.
for iidx in range(len(iou_result)):
if (math.isnan(iou_result[iidx])==False):
enable_frameNum += 1.
else:
## gt is not alowed
iou_result[iidx] = 0.
iou_result_bb[iidx] = 0.
iou_list.append(iou_result_bb.sum()/enable_frameNum)
iou_list_nobb.append(iou_result.sum()/enable_frameNum)
bb_result[seq] = result_bb
fps_list[seq]=fps
bb_result_nobb[seq] = result
print '{} {} : nobb:{}, bb:{}, total mIoUnobb:{}, total mIoUbb:{}, fps:{}'.format(num,seq,iou_result.mean(), iou_result_bb.mean(), sum(iou_list_nobb)/len(iou_list_nobb), sum(iou_list)/len(iou_list),sum(fps_list.values())/len(fps_list))
save_path_bb = os.path.join(opts['result_path'], 'bb'+seq+'.txt')
save_path_nobb = os.path.join(opts['result_path'], 'nobb'+seq+'.txt')
# np.savetxt(save_path_bb,result_bb)
# np.savetxt(save_path_nobb,result)
for i in range(len(result_bb)):
with open(save_path_bb, 'a') as f:
res='{} {} {} {} {} {} {} {}'.format(result_bb[i][0],result_bb[i][1],result_bb[i][0]+result_bb[i][2],result_bb[i][1],result_bb[i][0]+result_bb[i][2],result_bb[i][1]+result_bb[i][3],result_bb[i][0],result_bb[i][1]+result_bb[i][3])
f.write(res)
f.write('\n')
with open(save_path_nobb, 'a') as f:
res='{} {} {} {} {} {} {} {}'.format(result[i][0],result[i][1],result[i][0]+result[i][2],result[i][1],result[i][0]+result[i][2],result[i][1]+result[i][3],result[i][0],result[i][1]+result[i][3])
f.write(res)
f.write('\n')
print('overall')
flag = True
break
if not flag:
break