18
18
from tracking_utils .log import logger
19
19
from tracking_utils .timer import Timer
20
20
from tracking_utils .evaluation import Evaluator
21
- import sys
22
- sys .path .append ('/mnt/batch/tasks/shared/LS_root/mounts/clusters/emo-experiment/code/Users/sganesh68/efficient-object-tracking/src/lib/datasets' )
23
- import dataset .jde as datasets
21
+ import datasets .dataset .jde as datasets
24
22
25
23
from tracking_utils .utils import mkdir_if_missing
24
+ from sklearn .metrics .pairwise import cosine_similarity
26
25
from opts import opts
27
26
28
27
@@ -79,77 +78,90 @@ def eval_seq(opt, dataloader, data_type, result_filename, save_dir=None, show_im
79
78
frame_id = 0
80
79
prev_online_targets = []
81
80
prev_img = None
82
- eigen_threshold = float (opt .eigen_threshold )
81
+ similarity_threshold = float (opt .similarity_threshold )
83
82
detect_frame_interval = int (opt .detect_frame_interval )
84
- if frame_rate < 15 :
85
- detect_frame_interval = int (detect_frame_interval / 2 )
83
+ similarity_computation = opt .similarity_computation
84
+ if opt .adaptive_freq_forced_detection == 'True' and frame_rate < 15 :
85
+ logger .info ('reducing max num of consecutive frames that can be skipped since the video is less than 15 FPS' )
86
+ detect_frame_interval = int (detect_frame_interval / 4 )
87
+ else :
88
+ logger .info ('retaining max num of consecutive frames that can be skipped' )
86
89
num_detect = 0
87
90
num_skipped = 0
88
- prev_area = 0
89
91
num_consecutive_skips = 0
90
- total_areas = []
91
- largest_areas = []
92
- #for path, img, img0 in dataloader:
92
+ timer_decision_to_skip = Timer ()
93
+ timer_predict_next_pos = Timer ()
94
+ timer_detect_and_update = Timer ()
95
+ total_detections = 0
93
96
for i , (path , img , img0 ) in enumerate (dataloader ):
94
97
#if i % 8 != 0:
95
98
#continue
96
99
if frame_id % 20 == 0 :
97
100
logger .info ('Processing frame {} ({:.2f} fps)' .format (frame_id , 1. / max (1e-5 , timer .average_time )))
98
101
99
-
100
-
101
102
# run tracking
102
103
timer .tic ()
103
104
104
105
if i > 0 :
106
+ timer_decision_to_skip .tic ()
105
107
total_corr = 0
106
108
num_boxes_counted = 0
109
+ total_detections += len (prev_online_targets )
110
+ image_prev = Image .fromarray (prev_img )
111
+ imgGray_prev = image_prev .convert ('L' )
112
+ image_0 = Image .fromarray (img0 )
113
+ imgGray_0 = image_0 .convert ('L' )
107
114
for prev_track in prev_online_targets :
108
115
#filter targets like below
109
116
previous_position_tlbr = prev_track .tlbr
110
117
predicted_curr_position_tlbr = prev_track .predict_tlbr_without_updating_state ()
111
- prev_detected_box , curr_predicted_box = get_crop_image_same_size (prev_img , previous_position_tlbr , img0 , predicted_curr_position_tlbr )
112
- #prev_detected_box, curr_predicted_box = get_crop_image_same_size_flatten(prev_img, previous_position_tlbr, img0, predicted_curr_position_tlbr)
118
+ prev_detected_box , curr_predicted_box = get_crop_image_same_size (imgGray_prev , previous_position_tlbr , imgGray_0 , predicted_curr_position_tlbr )
113
119
114
120
prev_tlwh = prev_track .tlwh
115
121
vertical = prev_tlwh [2 ] / prev_tlwh [3 ] > 1.6
116
122
curr_area = prev_tlwh [2 ] * prev_tlwh [3 ]
117
123
if curr_area > opt .min_box_area and not vertical :
118
- corr_curr = compute_norm_corr_coeff (prev_detected_box , curr_predicted_box )
124
+ if similarity_computation == 'ncc' :
125
+ corr_curr = compute_norm_corr_coeff (prev_detected_box , curr_predicted_box )
126
+ elif similarity_computation == 'hog' :
127
+ corr_curr = compute_hog_distance (prev_detected_box , curr_predicted_box )
128
+ elif similarity_computation == 'no' :
129
+ corr_curr = 1
130
+
119
131
total_corr += corr_curr
120
- #print(i, num_boxes_counted, previous_position_tlbr, predicted_curr_position_tlbr, eig_curr)
121
132
num_boxes_counted += 1
122
- #print('index', i, previous_position_tlbr, predicted_curr_position_tlbr)
123
133
124
134
avg_corr = (total_corr / num_boxes_counted ) if num_boxes_counted > 0 else 0
125
- print ('avg_corr' , avg_corr , 'corr_' + str (i ) ,total_corr , 'num_boxes counted' , num_boxes_counted )
135
+ timer_decision_to_skip .toc ()
136
+ #print('avg_corr', avg_corr, 'corr_'+str(i) ,total_corr, 'num_boxes counted', num_boxes_counted)
126
137
else :
127
138
avg_corr = 0
128
139
129
-
130
140
if use_cuda :
131
141
blob = torch .from_numpy (img ).cuda ().unsqueeze (0 )
132
142
else :
133
143
blob = torch .from_numpy (img ).unsqueeze (0 )
134
144
135
- if avg_corr < eigen_threshold or num_consecutive_skips >= detect_frame_interval :
145
+ if avg_corr < similarity_threshold or num_consecutive_skips >= detect_frame_interval :
146
+ timer_detect_and_update .tic ()
136
147
online_targets = tracker .update (blob , img0 )
137
148
prev_online_targets = online_targets
138
- prev_img = img0
139
149
num_detect += 1
140
150
num_consecutive_skips = 0
141
- print ('detect at ' , i , ' prev_area: ' , prev_area )
151
+ timer_detect_and_update .toc ()
152
+ logger .debug ('detect at ' + str (i )+ ' avg_corr: ' + str (avg_corr ))
142
153
else :
143
- #eig = compute_eigen_values_consecutive(prev_img, img0 )
154
+ timer_predict_next_pos . tic ( )
144
155
STrack .multi_predict (prev_online_targets )
145
156
online_targets = prev_online_targets
157
+ timer_predict_next_pos .toc ()
146
158
num_consecutive_skips += 1
147
159
num_skipped += 1
160
+ logger .debug ('skip at ' + str (i )+ ' avg_corr: ' + str (avg_corr ))
161
+
148
162
online_tlwhs = []
149
163
online_ids = []
150
- #online_scores = []
151
- tot_area = 0
152
- max_area = - 1
164
+ prev_img = img0
153
165
for t in online_targets :
154
166
tlwh = t .tlwh
155
167
tid = t .track_id
@@ -159,21 +171,10 @@ def eval_seq(opt, dataloader, data_type, result_filename, save_dir=None, show_im
159
171
online_tlwhs .append (tlwh )
160
172
online_ids .append (tid )
161
173
curr_area = tlwh [2 ] * tlwh [3 ]
162
- tot_area += curr_area
163
- #online_scores.append(t.score)
164
- if curr_area > max_area :
165
- max_area = curr_area
166
-
167
- prev_area = tot_area
168
- largest_areas .append (max_area )
169
- total_areas .append (tot_area )
170
174
timer .toc ()
171
- #print('largest_areas:', largest_areas)
172
- #print('total_areas:', total_areas)
173
175
174
176
# save results
175
177
results .append ((frame_id + 1 , online_tlwhs , online_ids ))
176
- #results.append((frame_id + 1, online_tlwhs, online_ids, online_scores))
177
178
if show_image or save_dir is not None :
178
179
online_im = vis .plot_tracking (img0 , online_tlwhs , online_ids , frame_id = frame_id ,
179
180
fps = 1. / timer .average_time )
@@ -183,9 +184,12 @@ def eval_seq(opt, dataloader, data_type, result_filename, save_dir=None, show_im
183
184
cv2 .imwrite (os .path .join (save_dir , '{:05d}.jpg' .format (frame_id )), online_im )
184
185
frame_id += 1
185
186
# save results
186
- print ('num_detect:' , num_detect , "num_skipped:" , num_skipped )
187
+ logger .info ('num_detect:' , num_detect , "num_skipped:" , num_skipped )
188
+ logger .info ('timer_decision_to_skip' , timer_decision_to_skip .average_time , timer_decision_to_skip .calls )
189
+ logger .info ('timer_predict_next_pos' , timer_predict_next_pos .average_time , timer_predict_next_pos .calls )
190
+ logger .info ('timer_detect_and_update' , timer_detect_and_update .average_time , timer_detect_and_update .calls )
187
191
write_results (result_filename , results , data_type )
188
- #write_results_score(result_filename, results, data_type)
192
+
189
193
return frame_id , timer .average_time , timer .calls
190
194
191
195
def get_image_as_array (img ):
@@ -208,13 +212,14 @@ def compute_norm_corr_coeff(img1, img2):
208
212
result = cv2 .matchTemplate (img1 ,img2 ,cv2 .TM_CCOEFF_NORMED )
209
213
return result [0 ][0 ]
210
214
211
- def get_crop_image_same_size (img1 , boundingbox1 , img2 , boundingbox2 ):
215
+ def get_crop_image_same_size (img1 , boundingbox1 , img2 , boundingbox2 , crop_size = ( 128 , 128 ) ):
212
216
img_crop1 = get_image_crop (img1 , boundingbox1 )
213
217
img_crop2 = get_image_crop (img2 , boundingbox2 )
214
- img_crop2_resized = img_crop2 .resize (img_crop1 .size )
215
- img_crop1 = np .array (img_crop1 )
218
+ img_crop1_resized = img_crop1 .resize (crop_size )
219
+ img_crop2_resized = img_crop2 .resize (crop_size )
220
+ img_crop1_resized = np .array (img_crop1_resized )
216
221
img_crop2_resized = np .array (img_crop2_resized )
217
- return img_crop1 , img_crop2_resized
222
+ return img_crop1_resized , img_crop2_resized
218
223
219
224
def compute_eigen_value_similarity (img1 , img2 ):
220
225
img1 = img1 .reshape (- 1 )
@@ -224,10 +229,26 @@ def compute_eigen_value_similarity(img1, img2):
224
229
eig = np .sort (eig_1 )
225
230
return eig [0 ]
226
231
232
+ def compute_hog_distance (prev_detected_box , curr_predicted_box ):
233
+ prev_box_features = compute_hog (prev_detected_box )
234
+ curr_box_features = compute_hog (curr_predicted_box )
235
+ similarity = cosine_similarity (prev_box_features .T , curr_box_features .T )[0 ][0 ]
236
+ return similarity
237
+
238
+
239
+ def compute_hog (detected_crop_gray ):
240
+ hog = cv2 .HOGDescriptor ()
241
+ hog_feature = hog .compute (detected_crop_gray )
242
+ hog_feature = hog_feature .reshape (- 1 , 1 )
243
+
244
+ # Normalize feature vectors
245
+ norm = np .linalg .norm (hog_feature )
246
+ if norm != 0 :
247
+ hog_feature /= norm
248
+ return hog_feature
249
+
227
250
def get_image_crop (img1 , boundingbox1 ):
228
- image_1 = Image .fromarray (img1 )
229
- imgGray_1 = image_1 .convert ('L' )
230
- img_crop1 = imgGray_1 .crop (boundingbox1 )
251
+ img_crop1 = img1 .crop (boundingbox1 )
231
252
return img_crop1
232
253
233
254
def get_crop_image_same_size_flatten (img1 , boundingbox1 , img2 , boundingbox2 ):
@@ -388,12 +409,12 @@ def main(opt, data_root='/data/MOT16/train', det_root=None, seqs=('MOT16-05',),
388
409
seqs_str = opt .seq_name
389
410
data_root = os .path .join (opt .data_dir , opt .data_path )
390
411
seqs = [seq .strip () for seq in seqs_str .split ()]
412
+ #logger.info("data_root "+ data_root)
391
413
392
414
main (opt ,
393
415
data_root = data_root ,
394
416
seqs = seqs ,
395
- exp_name = 'MOT15_val_mot17_Feb23_mandskip_adap24' ,
396
- #exp_name='MOT15_test_samplevideo_'+seqs_str,
417
+ exp_name = 'MOT_val_exptname' ,
397
418
show_image = False ,
398
419
save_images = False ,
399
420
save_videos = True )
0 commit comments