-
Notifications
You must be signed in to change notification settings - Fork 17
/
tracklet_utils_3c.py
3124 lines (2717 loc) · 154 KB
/
tracklet_utils_3c.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright ©2019 Gaoang Wang. All rights reserved. Permission is
* hereby granted for academic use. No other use, copying, distribution, or modification
* is permitted without prior written consent. Copyrights for
* third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the
* author.
*/
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import argparse
import facenet
import lfw
import os
import sys
import cv2
import pickle
import time
from functools import wraps
from tensorflow.python.ops import data_flow_ops
from sklearn import metrics
from scipy.optimize import brentq
from scipy import interpolate
from scipy.interpolate import interp1d
from scipy.io import loadmat
from skimage.io import imread
from scipy import misc
from scipy import stats
from scipy import spatial
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
from sklearn import svm
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from collections import Counter
import seq_nn_3d_v2
import track_lib
# Set paths
seq_name = 'basketball_6943'
img_name = 'basketball_6943'
sub_seq_name = ''
file_len = 8
det_path = 'D:/Data/basketball_6943_results/det.txt'
gt_path = ''
img_folder = 'D:/Data/basketball_6943_results/img'
crop_det_folder = 'D:/Data/basketball_6943_results/crop_det/'+seq_name+sub_seq_name
triplet_model = 'D:/Data/UA-Detrac/UA_Detrac_model/MOT'
seq_model = 'D:/Data/MOT/MOT_2d_v2/model.ckpt'
tracking_img_folder = 'D:/Data/basketball_6943_results/tracking_img/'+seq_name+sub_seq_name
tracking_video_path = 'D:/Data/basketball_6943_results/tracking_video/'+seq_name+sub_seq_name+'.avi'
appear_mat_path = 'D:/Data/basketball_6943_results/appear_mat/'+seq_name+'.obj'
txt_result_path = 'D:/Data/basketball_6943_results/txt_result/'+seq_name+sub_seq_name+'.txt'
track_struct_path = 'D:/Data/basketball_6943_results/track_struct/'+seq_name+sub_seq_name+'.obj'
'''
seq_name = 'MOT17-02-FRCNN'
img_name = 'MOT17-02'
sub_seq_name = ''
det_path = 'D:/Data/MOT/MOT17Labels/train/'+seq_name+'/det/det.txt'
gt_path = 'D:/Data/MOT/MOT17Labels/train/'+seq_name+'/gt/gt.txt'
img_folder = 'D:/Data/MOT/MOT17Det/train/'+img_name+sub_seq_name+'/img1'
crop_det_folder = 'D:/Data/MOT/crop_det/'+seq_name+sub_seq_name
triplet_model = 'D:/Data/UA-Detrac/UA_Detrac_model/MOT'
#triplet_model = 'D:/Data/UA-Detrac/UA_Detrac_model/KITTI_model'
#seq_model = 'D:/Data/UA-Detrac/cnn_appear_model_517_128_16600steps/model.ckpt'
#seq_model = 'D:/Data/UA-Detrac/cnn_MOT/model.ckpt'
seq_model = 'D:/Data/UA-Detrac/MOT_2d_v2/model.ckpt'
#seq_model = 'D:/Data/UA-Detrac/semi_train_model/model.ckpt'
tracking_img_folder = 'D:/Data/MOT/tracking_img/'+seq_name+sub_seq_name
tracking_video_path = 'D:/Data/MOT/tracking_video/'+seq_name+sub_seq_name+'.avi'
svm_model_path = 'D:/Data/MOT/MOT17_train_det_crop/cnn_svm_MOT17.pkl'
rand_forest_model_path = 'D:/Data/MOT/MOT17_train_det_crop/rand_forest_MOT17_FRCNN.pkl'
F_path = 'D:/Data/MOT/geometry_info/'+img_name+'_F_set.mat'
appear_mat_path = 'D:/Data/MOT/appear_mat/'+seq_name+'.obj'
save_fea_path = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'.obj'
save_label_path = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_label.obj'
save_remove_path = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_remove_set.obj'
save_all_fea_path = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_all.obj'
save_all_label_path = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_all_label.obj'
save_all_label_path1 = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_all_label0.obj'
save_all_label_path2 = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_all_label1.obj'
save_all_label_path3 = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_all_label2.obj'
save_all_label_path4 = 'D:/Data/MOT/save_fea_mat/'+seq_name+sub_seq_name+'_all_label3.obj'
txt_result_path = 'D:/Data/MOT/txt_result/'+seq_name+sub_seq_name+'.txt'
track_struct_path = 'D:/Data/MOT/track_struct/'+seq_name+sub_seq_name+'.obj'
'''
max_length = 64
feature_size = 4+512
batch_size = 64
num_classes = 2
track_set = []
remove_set = []
#track_set = pickle.load(open(save_label_path,'rb'))
#remove_set = pickle.load(open(save_remove_path,'rb'))
#save_fea_mat = np.zeros((len(track_set),feature_size,max_length,2))
global all_fea_mat
global all_fea_label
all_fea_mat = np.zeros((10000,feature_size,max_length,3))
all_fea_label = np.zeros((10000,4))
def get_tracklet_scores():
global track_struct
# svm score
track_struct['tracklet_mat']['svm_score_mat'] = -1*np.ones((track_struct['tracklet_mat']['xmin_mat'].shape[0], \
track_struct['tracklet_mat']['xmin_mat'].shape[1]))
num_det = track_struct['tracklet_mat']['appearance_fea_mat'].shape[0]
clf = joblib.load(svm_model_path)
pred_s = np.zeros((num_det,1))
pred_s[:,0] = clf.decision_function(track_struct['tracklet_mat']['appearance_fea_mat'][:,2:])
for n in range(num_det):
track_struct['tracklet_mat']['svm_score_mat'][int(track_struct['tracklet_mat']['appearance_fea_mat'][n,0])-1, \
int(track_struct['tracklet_mat']['appearance_fea_mat'][n,1])-1] = pred_s[n,0]
# h_score and y_score
track_struct['tracklet_mat']['h_score_mat'] = -1*np.ones((track_struct['tracklet_mat']['xmin_mat'].shape[0], \
track_struct['tracklet_mat']['xmin_mat'].shape[1]))
track_struct['tracklet_mat']['y_score_mat'] = -1*np.ones((track_struct['tracklet_mat']['xmin_mat'].shape[0], \
track_struct['tracklet_mat']['xmin_mat'].shape[1]))
hloc = np.zeros(num_det)
yloc = np.zeros(num_det)
cnt = 0
for n in range(track_struct['tracklet_mat']['xmin_mat'].shape[0]):
idx = np.where(track_struct['tracklet_mat']['xmin_mat'][n,:]!=-1)[0]
hloc[cnt:cnt+len(idx)] = track_struct['tracklet_mat']['ymax_mat'][n,idx]-track_struct['tracklet_mat']['ymin_mat'][n,idx]
yloc[cnt:cnt+len(idx)] = track_struct['tracklet_mat']['ymax_mat'][n,idx]
cnt = cnt+len(idx)
ph, py = track_lib.estimate_h_y(hloc, yloc)
A = np.ones((hloc.shape[0],2))
A[:,0] = yloc
y_err = (np.matmul(A,ph)-hloc)/hloc
err_std = np.std(y_err)
h_score = np.zeros((y_err.shape[0],1))
h_score[:,0] = np.exp(-np.power(y_err,2)/(err_std*err_std))
A = np.ones((hloc.shape[0],2))
A[:,0] = hloc
y_err = np.matmul(A,py)-yloc
err_std = np.std(y_err)
y_score = np.zeros((y_err.shape[0],1))
y_score[:,0] = np.exp(-np.power(y_err,2)/(err_std*err_std))
#import pdb; pdb.set_trace()
cnt = 0
for n in range(track_struct['tracklet_mat']['xmin_mat'].shape[0]):
idx = np.where(track_struct['tracklet_mat']['xmin_mat'][n,:]!=-1)[0]
track_struct['tracklet_mat']['h_score_mat'][n,idx] = h_score[cnt:cnt+len(idx),0]
track_struct['tracklet_mat']['y_score_mat'][n,idx] = y_score[cnt:cnt+len(idx),0]
cnt = cnt+len(idx)
return
def remove_tracklet(tracklet_mat):
num_tracklet = tracklet_mat['xmin_mat'].shape[0]
tracklet_fea = np.zeros((num_tracklet,17))
for n in range(num_tracklet):
idx = np.where(tracklet_mat['xmin_mat'][n,:]!=-1)[0]
tracklet_fea[n,:] = track_lib.extract_tracklet_feature(tracklet_mat, n, idx)
clf = joblib.load(rand_forest_model_path)
pred_label = clf.predict(tracklet_fea)
temp_remove_set = np.where(pred_label!=1)[0]
temp_remove_set = list(temp_remove_set)
#import pdb; pdb.set_trace()
return temp_remove_set
def preprocessing(tracklet_mat, len_thresh, track_params):
new_tracklet_mat = tracklet_mat
N_tracklet = new_tracklet_mat['xmin_mat'].shape[0]
remove_idx = []
for n in range(N_tracklet):
idx = np.where(new_tracklet_mat['xmin_mat'][n,:]!=-1)[0]
max_det_score = np.max(new_tracklet_mat['det_score_mat'][n,idx])
if len(idx)<len_thresh and max_det_score<track_params['pre_det_score']:
remove_idx.append(n)
new_tracklet_mat['xmin_mat'] = np.delete(new_tracklet_mat['xmin_mat'], remove_idx, 0)
new_tracklet_mat['ymin_mat'] = np.delete(new_tracklet_mat['ymin_mat'], remove_idx, 0)
new_tracklet_mat['xmax_mat'] = np.delete(new_tracklet_mat['xmax_mat'], remove_idx, 0)
new_tracklet_mat['ymax_mat'] = np.delete(new_tracklet_mat['ymax_mat'], remove_idx, 0)
new_tracklet_mat['det_score_mat'] = np.delete(new_tracklet_mat['det_score_mat'], remove_idx, 0)
if track_params['svm_score_flag']==1:
new_tracklet_mat['svm_score_mat'] = np.delete(new_tracklet_mat['svm_score_mat'], remove_idx, 0)
if track_params['h_score_flag']==1:
new_tracklet_mat['h_score_mat'] = np.delete(new_tracklet_mat['h_score_mat'], remove_idx, 0)
if track_params['y_score_flag']==1:
new_tracklet_mat['y_score_mat'] = np.delete(new_tracklet_mat['y_score_mat'], remove_idx, 0)
if track_params['IOU_gt_flag']==1:
new_tracklet_mat['IOU_gt_mat'] = np.delete(new_tracklet_mat['IOU_gt_mat'], remove_idx, 0)
return new_tracklet_mat
#M = [fr_idx, x, y, w, h, score]
def forward_tracking(track_id1, track_id2, bbox1, bbox2, det_score1, det_score2, svm_score1, svm_score2, h_score1, h_score2, y_score1,
y_score2, IOU_gt1, IOU_gt2, mean_color1, mean_color2, fr_idx2, track_params, tracklet_mat, max_id, M_gt):
color_thresh = track_params['color_thresh']
num_fr = track_params['num_fr']
linear_pred_thresh = track_params['linear_pred_thresh']
if len(bbox1)>0:
num1 = bbox1.shape[0]
else:
num1 = 0
if len(bbox2)>0:
num2 = bbox2.shape[0]
else:
num2 = 0
new_track_id1 = track_id1
new_tracklet_mat = tracklet_mat
if fr_idx2==2 and num1>0:
new_track_id1 = list(range(1,num1+1))
'''
new_tracklet_mat['xmin_mat'] = -np.ones((num1, num_fr))
new_tracklet_mat['ymin_mat'] = -np.ones((num1, num_fr))
new_tracklet_mat['xmax_mat'] = -np.ones((num1, num_fr))
new_tracklet_mat['ymax_mat'] = -np.ones((num1, num_fr))
new_tracklet_mat['det_score_mat'] = -np.ones((num1, num_fr))
'''
new_tracklet_mat['xmin_mat'][0:num1,0] = bbox1[:,0]
new_tracklet_mat['ymin_mat'][0:num1,0] = bbox1[:,1]
new_tracklet_mat['xmax_mat'][0:num1,0] = bbox1[:,0]+bbox1[:,2]-1
new_tracklet_mat['ymax_mat'][0:num1,0] = bbox1[:,1]+bbox1[:,3]-1
new_tracklet_mat['det_score_mat'][0:num1,0] = det_score1
if track_params['svm_score_flag']==1:
new_tracklet_mat['svm_score_mat'][0:num1,0] = svm_score1
if track_params['h_score_flag']==1:
new_tracklet_mat['h_score_mat'][0:num1,0] = h_score1
if track_params['y_score_flag']==1:
new_tracklet_mat['y_score_mat'][0:num1,0] = y_score1
if track_params['IOU_gt_flag']==1:
new_tracklet_mat['IOU_gt_mat'][0:num1,0] = IOU_gt1
max_id = num1
if len(bbox1)==0 and len(bbox2)!=0:
idx1 = []
idx2 = []
elif len(bbox1)!=0 and len(bbox2)==0:
idx1 = []
idx2 = []
elif len(bbox1)==0 and len(bbox2)==0:
idx1 = []
idx2 = []
elif len(bbox1)!=0 and len(bbox2)!=0:
# pred bbox1
pred_bbox1 = np.zeros((len(bbox1),4))
if track_params['use_F']==1:
pred_bbox1 = track_lib.pred_bbox_by_F(bbox1, tracklet_mat['F'][:,:,fr_idx2-2], 0, [], [])
else:
for k in range(len(bbox1)):
temp_track_id = new_track_id1[k]-1
t_idx = np.where(new_tracklet_mat['xmin_mat'][temp_track_id,:]!=-1)[0]
if len(t_idx)==0:
import pdb; pdb.set_trace()
t_min = np.min(t_idx)
if t_min<fr_idx2-2-linear_pred_thresh:
t_min = fr_idx2-2-linear_pred_thresh
xx = (new_tracklet_mat['xmin_mat'][temp_track_id,int(t_min):fr_idx2-1]
+new_tracklet_mat['xmax_mat'][temp_track_id,int(t_min):fr_idx2-1])/2
yy = (new_tracklet_mat['ymin_mat'][temp_track_id,int(t_min):fr_idx2-1]
+new_tracklet_mat['ymax_mat'][temp_track_id,int(t_min):fr_idx2-1])/2
ww = (new_tracklet_mat['xmax_mat'][temp_track_id,int(t_min):fr_idx2-1]
-new_tracklet_mat['xmin_mat'][temp_track_id,int(t_min):fr_idx2-1])+1
hh = (new_tracklet_mat['ymax_mat'][temp_track_id,int(t_min):fr_idx2-1]
-new_tracklet_mat['ymin_mat'][temp_track_id,int(t_min):fr_idx2-1])+1
if len(xx)==0:
import pdb; pdb.set_trace()
pred_x = track_lib.linear_pred(xx)
pred_y = track_lib.linear_pred(yy)
pred_w = track_lib.linear_pred(ww)
pred_h = track_lib.linear_pred(hh)
pred_bbox1[k,2] = max(pred_w,1)
pred_bbox1[k,3] = max(pred_h,1)
pred_bbox1[k,0] = pred_x-pred_w/2
pred_bbox1[k,1] = pred_y-pred_h/2
#import pdb; pdb.set_trace()
overlap_mat,_,_,_ = track_lib.get_overlap(pred_bbox1, bbox2)
# color dist
color_dist = np.zeros((len(bbox1),len(bbox2)))
for n1 in range(len(bbox1)):
for n2 in range(len(bbox2)):
color_dist[n1,n2] = np.max(np.absolute(mean_color1[n1,:]-mean_color2[n2,:]))
if np.isnan(np.sum(color_dist)) or np.isnan(np.sum(overlap_mat)):
import pdb; pdb.set_trace()
overlap_mat[color_dist>color_thresh] = 0
idx1, idx2 = track_lib.bbox_associate(overlap_mat, track_params['IOU_thresh'])
# check tracklet generation
if len(M_gt)>0:
M1 = M_gt[M_gt[:,0]==fr_idx2-1,:]
M2 = M_gt[M_gt[:,0]==fr_idx2,:]
real_id1 = -np.ones(len(bbox1))
real_id2 = -np.ones(len(bbox2))
overlap_mat1,_,_,_ = track_lib.get_overlap(bbox1, M1[:,1:5])
r_idx1, r_idx2 = track_lib.bbox_associate(overlap_mat1, 0.5)
if len(r_idx1)!=0:
real_id1[r_idx1] = M1[r_idx2,6]
overlap_mat2,_,_,_ = track_lib.get_overlap(bbox2, M2[:,1:5])
r_idx1, r_idx2 = track_lib.bbox_associate(overlap_mat2, 0.5)
if len(r_idx1)!=0:
real_id2[r_idx1] = M2[r_idx2,6]
for k1 in range(len(idx1)):
if real_id1[idx1[k1]]==real_id2[idx2[k1]] and real_id1[idx1[k1]]!=-1:
new_tracklet_mat['conf_matrix_tracklet'][0,0] = new_tracklet_mat['conf_matrix_tracklet'][0,0]+1
elif real_id1[idx1[k1]]!=real_id2[idx2[k1]]:
new_tracklet_mat['conf_matrix_tracklet'][0,1] = new_tracklet_mat['conf_matrix_tracklet'][0,1]+1
for k1 in range(len(bbox1)):
if k1 not in idx1:
if real_id1[k1]!=-1 and real_id1[k1] in real_id2:
new_tracklet_mat['conf_matrix_tracklet'][1,0] = new_tracklet_mat['conf_matrix_tracklet'][1,0]+1
if len(idx1)==0 and num2>0:
new_track_id2 = list(np.array(range(1,num2+1))+max_id)
'''
new_tracklet_mat['xmin_mat'] = \
np.append(new_tracklet_mat['xmin_mat'], -np.ones((num2,num_fr)), axis=0)
new_tracklet_mat['ymin_mat'] = \
np.append(new_tracklet_mat['ymin_mat'], -np.ones((num2,num_fr)), axis=0)
new_tracklet_mat['xmax_mat'] = \
np.append(new_tracklet_mat['xmax_mat'], -np.ones((num2,num_fr)), axis=0)
new_tracklet_mat['ymax_mat'] = \
np.append(new_tracklet_mat['ymax_mat'], -np.ones((num2,num_fr)), axis=0)
new_tracklet_mat['det_score_mat'] = \
np.append(new_tracklet_mat['det_score_mat'], -np.ones((num2,num_fr)), axis=0)
'''
max_id = max_id+num2
new_tracklet_mat['xmin_mat'][max_id-num2:max_id,fr_idx2-1] = bbox2[:,0]
new_tracklet_mat['ymin_mat'][max_id-num2:max_id,fr_idx2-1] = bbox2[:,1]
new_tracklet_mat['xmax_mat'][max_id-num2:max_id,fr_idx2-1] = bbox2[:,0]+bbox2[:,2]-1
new_tracklet_mat['ymax_mat'][max_id-num2:max_id,fr_idx2-1] = bbox2[:,1]+bbox2[:,3]-1
new_tracklet_mat['det_score_mat'][max_id-num2:max_id,fr_idx2-1] = det_score2
if track_params['svm_score_flag']==1:
new_tracklet_mat['svm_score_mat'][max_id-num2:max_id,fr_idx2-1] = svm_score2
if track_params['h_score_flag']==1:
new_tracklet_mat['h_score_mat'][max_id-num2:max_id,fr_idx2-1] = h_score2
if track_params['y_score_flag']==1:
new_tracklet_mat['y_score_mat'][max_id-num2:max_id,fr_idx2-1] = y_score2
if track_params['IOU_gt_flag']==1:
new_tracklet_mat['IOU_gt_mat'][max_id-num2:max_id,fr_idx2-1] = IOU_gt2
elif len(idx1)>0:
new_track_id2 = []
for n in range(num2):
#import pdb; pdb.set_trace()
temp_idx = np.where(idx2==n)[0]
if len(temp_idx)==0:
max_id = max_id+1
new_track_id2.append(max_id)
'''
new_tracklet_mat['xmin_mat'] = \
np.append(new_tracklet_mat['xmin_mat'], -np.ones((1,num_fr)), axis=0)
new_tracklet_mat['ymin_mat'] = \
np.append(new_tracklet_mat['ymin_mat'], -np.ones((1,num_fr)), axis=0)
new_tracklet_mat['xmax_mat'] = \
np.append(new_tracklet_mat['xmax_mat'], -np.ones((1,num_fr)), axis=0)
new_tracklet_mat['ymax_mat'] = \
np.append(new_tracklet_mat['ymax_mat'], -np.ones((1,num_fr)), axis=0)
new_tracklet_mat['det_score_mat'] = \
np.append(new_tracklet_mat['det_score_mat'], -np.ones((1,num_fr)), axis=0)
'''
#if fr_idx2==20:
# import pdb; pdb.set_trace()
new_tracklet_mat['xmin_mat'][max_id-1,fr_idx2-1] = bbox2[n,0]
new_tracklet_mat['ymin_mat'][max_id-1,fr_idx2-1] = bbox2[n,1]
new_tracklet_mat['xmax_mat'][max_id-1,fr_idx2-1] = bbox2[n,0]+bbox2[n,2]-1
new_tracklet_mat['ymax_mat'][max_id-1,fr_idx2-1] = bbox2[n,1]+bbox2[n,3]-1
new_tracklet_mat['det_score_mat'][max_id-1,fr_idx2-1] = det_score2[n]
if track_params['svm_score_flag']==1:
new_tracklet_mat['svm_score_mat'][max_id-1,fr_idx2-1] = svm_score2[n]
if track_params['h_score_flag']==1:
new_tracklet_mat['h_score_mat'][max_id-1,fr_idx2-1] = h_score2[n]
if track_params['y_score_flag']==1:
new_tracklet_mat['y_score_mat'][max_id-1,fr_idx2-1] = y_score2[n]
if track_params['IOU_gt_flag']==1:
new_tracklet_mat['IOU_gt_mat'][max_id-1,fr_idx2-1] = IOU_gt2[n]
else:
temp_idx = temp_idx[0]
new_track_id2.append(new_track_id1[idx1[temp_idx]])
new_tracklet_mat['xmin_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = bbox2[n,0]
new_tracklet_mat['ymin_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = bbox2[n,1]
new_tracklet_mat['xmax_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = bbox2[n,0]+bbox2[n,2]-1
new_tracklet_mat['ymax_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = bbox2[n,1]+bbox2[n,3]-1
new_tracklet_mat['det_score_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = det_score2[n]
if track_params['svm_score_flag']==1:
new_tracklet_mat['svm_score_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = svm_score2[n]
if track_params['h_score_flag']==1:
new_tracklet_mat['h_score_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = h_score2[n]
if track_params['y_score_flag']==1:
new_tracklet_mat['y_score_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = y_score2[n]
if track_params['IOU_gt_flag']==1:
new_tracklet_mat['IOU_gt_mat'] \
[new_track_id1[idx1[temp_idx]]-1,fr_idx2-1] = IOU_gt2[n]
else:
new_track_id2 = []
#if fr_idx2==20:
# import pdb; pdb.set_trace()
new_max_id = max_id
return new_tracklet_mat, new_track_id1, new_track_id2, new_max_id
def init_clustering():
global track_struct
N_tracklet = track_struct['tracklet_mat']['xmin_mat'].shape[0]
# track interval
track_struct['tracklet_mat']['track_interval'] = np.zeros((N_tracklet, 2))
# track cluster
track_struct['tracklet_mat']['track_cluster'] = []
# track class
track_struct['tracklet_mat']['track_class'] = np.arange(N_tracklet, dtype=int)
# time cluster
track_struct['tracklet_mat']['time_cluster'] = []
for n in range(track_struct['track_params']['num_time_cluster']):
track_struct['tracklet_mat']['time_cluster'].append([])
track_struct['tracklet_mat']['track_cluster_t_idx'] = []
for n in range(N_tracklet):
idx = np.where(track_struct['tracklet_mat']['xmin_mat'][n,:]!=-1)[0]
track_struct['tracklet_mat']['track_interval'][n,0] = np.min(idx)
track_struct['tracklet_mat']['track_interval'][n,1] = np.max(idx)
track_struct['tracklet_mat']['track_cluster'].append([n])
if n in remove_set:
track_struct['tracklet_mat']['track_cluster_t_idx'].append([-1])
else:
min_time_cluster_idx = int(np.floor(max(track_struct['tracklet_mat']['track_interval'][n,0]
-track_struct['track_params']['t_dist_thresh']-5,0)
/track_struct['track_params']['time_cluster_dist']))
max_time_cluster_idx = int(np.floor(min(track_struct['tracklet_mat']['track_interval'][n,1]
+track_struct['track_params']['t_dist_thresh']+5,
track_struct['tracklet_mat']['xmin_mat'].shape[1]-1)
/track_struct['track_params']['time_cluster_dist']))
track_struct['tracklet_mat']['track_cluster_t_idx'].append(list(range(min_time_cluster_idx,max_time_cluster_idx+1)))
for k in range(min_time_cluster_idx,max_time_cluster_idx+1):
track_struct['tracklet_mat']['time_cluster'][k].append(n)
# get center position of each detection location
mask = track_struct['tracklet_mat']['xmin_mat']==-1
track_struct['tracklet_mat']['center_x'] = \
(track_struct['tracklet_mat']['xmin_mat']+track_struct['tracklet_mat']['xmax_mat'])/2
track_struct['tracklet_mat']['center_y'] = \
(track_struct['tracklet_mat']['ymin_mat']+track_struct['tracklet_mat']['ymax_mat'])/2
track_struct['tracklet_mat']['w'] = \
track_struct['tracklet_mat']['xmax_mat']-track_struct['tracklet_mat']['xmin_mat']+1
track_struct['tracklet_mat']['h'] = \
track_struct['tracklet_mat']['ymax_mat']-track_struct['tracklet_mat']['ymin_mat']+1
track_struct['tracklet_mat']['center_x'][mask] = -1
track_struct['tracklet_mat']['center_y'][mask] = -1
track_struct['tracklet_mat']['w'][mask] = -1
track_struct['tracklet_mat']['h'][mask] = -1
# neighbor_track_idx and conflict_track_idx
track_struct['tracklet_mat']['neighbor_track_idx'] = []
track_struct['tracklet_mat']['conflict_track_idx'] = []
for n in range(N_tracklet):
track_struct['tracklet_mat']['neighbor_track_idx'].append([])
track_struct['tracklet_mat']['conflict_track_idx'].append([])
for n in range(N_tracklet-1):
for m in range(n+1, N_tracklet):
t_min1 = track_struct['tracklet_mat']['track_interval'][n,0]
t_max1 = track_struct['tracklet_mat']['track_interval'][n,1]
t_min2 = track_struct['tracklet_mat']['track_interval'][m,0]
t_max2 = track_struct['tracklet_mat']['track_interval'][m,1]
overlap_len = min(t_max2,t_max1)-max(t_min1,t_min2)+1
overlap_r = overlap_len/(t_max1-t_min1+1+t_max2-t_min2+1-overlap_len)
if overlap_len>0 and overlap_r>track_struct['track_params']['track_overlap_thresh']:
track_struct['tracklet_mat']['conflict_track_idx'][n].append(m)
track_struct['tracklet_mat']['conflict_track_idx'][m].append(n)
continue
if overlap_len>0 and overlap_r<=track_struct['track_params']['track_overlap_thresh']:
# check the search region
t1 = int(max(t_min1,t_min2))
t2 = int(min(t_max2,t_max1))
if (t_min1<=t_min2 and t_max1>=t_max2) or (t_min1>=t_min2 and t_max1<=t_max2) or overlap_len>4:
track_struct['tracklet_mat']['conflict_track_idx'][n].append(m)
track_struct['tracklet_mat']['conflict_track_idx'][m].append(n)
continue
cand_t = np.array(range(t1,t2+1))
dist_x = abs(track_struct['tracklet_mat']['center_x'][n,cand_t] \
-track_struct['tracklet_mat']['center_x'][m,cand_t])
dist_y = abs(track_struct['tracklet_mat']['center_y'][n,cand_t] \
-track_struct['tracklet_mat']['center_y'][m,cand_t])
w1 = track_struct['tracklet_mat']['w'][n,cand_t]
h1 = track_struct['tracklet_mat']['h'][n,cand_t]
w2 = track_struct['tracklet_mat']['w'][m,cand_t]
h2 = track_struct['tracklet_mat']['h'][m,cand_t]
min_len = np.min([np.min(w1),np.min(h1),np.min(w2),np.min(h2)])
min_dist_x1 = np.min(dist_x/min_len)
min_dist_y1 = np.min(dist_y/min_len)
min_dist_x2 = np.min(dist_x/min_len)
min_dist_y2 = np.min(dist_y/min_len)
if min_dist_x1<track_struct['track_params']['search_radius'] \
and min_dist_y1<track_struct['track_params']['search_radius'] \
and min_dist_x2<track_struct['track_params']['search_radius'] \
and min_dist_y2<track_struct['track_params']['search_radius']:
track_struct['tracklet_mat']['neighbor_track_idx'][n].append(m)
track_struct['tracklet_mat']['neighbor_track_idx'][m].append(n)
if overlap_len<=0 and min(abs(t_min1-t_max2),abs(t_min2-t_max1)) \
<track_struct['track_params']['t_dist_thresh']:
if t_min1>=t_max2:
t1 = int(t_min1)
t2 = int(t_max2)
else:
t1 = int(t_max1)
t2 = int(t_min2)
#***********************************
tr_t1 = np.array(range(int(t_min1),int(t_max1+1)))
tr_x1 = track_struct['tracklet_mat']['center_x'][n,int(t_min1):int(t_max1+1)]
tr_y1 = track_struct['tracklet_mat']['center_y'][n,int(t_min1):int(t_max1+1)]
if len(tr_t1)>10:
if t_min1>=t_max2:
tr_t1 = tr_t1[0:10]
tr_x1 = tr_x1[0:10]
tr_y1 = tr_y1[0:10]
else:
tr_t1 = tr_t1[-10:]
tr_x1 = tr_x1[-10:]
tr_y1 = tr_y1[-10:]
ts_x1 = track_lib.linear_pred_v2(tr_t1, tr_x1, np.array([t2]))
ts_y1 = track_lib.linear_pred_v2(tr_t1, tr_y1, np.array([t2]))
dist_x1 = abs(ts_x1[0]-track_struct['tracklet_mat']['center_x'][m,t2])
dist_y1 = abs(ts_y1[0]-track_struct['tracklet_mat']['center_y'][m,t2])
tr_t2 = np.array(range(int(t_min2),int(t_max2+1)))
tr_x2 = track_struct['tracklet_mat']['center_x'][m,int(t_min2):int(t_max2+1)]
tr_y2 = track_struct['tracklet_mat']['center_y'][m,int(t_min2):int(t_max2+1)]
if len(tr_t2)>10:
if t_min2>t_max1:
tr_t2 = tr_t2[0:10]
tr_x2 = tr_x2[0:10]
tr_y2 = tr_y2[0:10]
else:
tr_t2 = tr_t2[-10:]
tr_x2 = tr_x2[-10:]
tr_y2 = tr_y2[-10:]
ts_x2 = track_lib.linear_pred_v2(tr_t2, tr_x2, np.array([t1]))
ts_y2 = track_lib.linear_pred_v2(tr_t2, tr_y2, np.array([t1]))
dist_x2 = abs(ts_x2[0]-track_struct['tracklet_mat']['center_x'][n,t1])
dist_y2 = abs(ts_y2[0]-track_struct['tracklet_mat']['center_y'][n,t1])
dist_x = min(dist_x1, dist_x2)
dist_y = min(dist_y1, dist_y2)
#***********************************
#import pdb; pdb.set_trace()
'''
dist_x = abs(track_struct['tracklet_mat']['center_x'][n,t1] \
-track_struct['tracklet_mat']['center_x'][m,t2])
dist_y = abs(track_struct['tracklet_mat']['center_y'][n,t1] \
-track_struct['tracklet_mat']['center_y'][m,t2])
'''
w1 = track_struct['tracklet_mat']['w'][n,t1]
h1 = track_struct['tracklet_mat']['h'][n,t1]
w2 = track_struct['tracklet_mat']['w'][m,t2]
h2 = track_struct['tracklet_mat']['h'][m,t2]
min_len = np.min([np.min(w1),np.min(h1),np.min(w2),np.min(h2)])
min_dist_x1 = dist_x/min_len
min_dist_y1 = dist_y/min_len
min_dist_x2 = dist_x/min_len
min_dist_y2 = dist_y/min_len
if min_dist_x1<track_struct['track_params']['search_radius'] \
and min_dist_y1<track_struct['track_params']['search_radius'] \
and min_dist_x2<track_struct['track_params']['search_radius'] \
and min_dist_y2<track_struct['track_params']['search_radius']:
track_struct['tracklet_mat']['neighbor_track_idx'][n].append(m)
track_struct['tracklet_mat']['neighbor_track_idx'][m].append(n)
# update neighbor idx
#***********************************
for n in range(len(track_set)):
temp_label = track_set[n,2]
if temp_label==1:
if abs(track_struct['tracklet_mat']['track_interval'][track_set[n,0],1]-
track_struct['tracklet_mat']['track_interval'][track_set[n,1],0])>60:
continue
if track_set[n,0] not in track_struct['tracklet_mat']['neighbor_track_idx'][track_set[n,1]]:
track_struct['tracklet_mat']['neighbor_track_idx'][track_set[n,1]].append(track_set[n,0])
track_struct['tracklet_mat']['neighbor_track_idx'][track_set[n,0]].append(track_set[n,1])
if track_set[n,0] in track_struct['tracklet_mat']['conflict_track_idx'][track_set[n,1]]:
track_struct['tracklet_mat']['conflict_track_idx'][track_set[n,1]].remove(track_set[n,0])
track_struct['tracklet_mat']['conflict_track_idx'][track_set[n,0]].remove(track_set[n,1])
else:
if track_set[n,0] in track_struct['tracklet_mat']['neighbor_track_idx'][track_set[n,1]]:
track_struct['tracklet_mat']['neighbor_track_idx'][track_set[n,1]].remove(track_set[n,0])
track_struct['tracklet_mat']['neighbor_track_idx'][track_set[n,0]].remove(track_set[n,1])
if track_set[n,0] not in track_struct['tracklet_mat']['conflict_track_idx'][track_set[n,1]]:
track_struct['tracklet_mat']['conflict_track_idx'][track_set[n,1]].append(track_set[n,0])
track_struct['tracklet_mat']['conflict_track_idx'][track_set[n,0]].append(track_set[n,1])
# cluster cost
track_struct['tracklet_mat']['cluster_cost'] = []
for n in range(N_tracklet):
#track_struct['tracklet_mat']['cluster_cost'].append(0)
# bias term
#***************************************
track_struct['tracklet_mat']['cluster_cost'].append(track_struct['track_params']['cost_bias'])
# save all comb cost for two tracklets
# comb_track_cost [track_id1, track_id2, cost]
# track_struct['tracklet_mat']['comb_track_cost'] = []
# save feature mat for training
'''
if len(track_struct['tracklet_mat']['track_set'])>0:
track_struct['tracklet_mat']['save_fea_mat'] = np.zeros((len(track_struct['tracklet_mat']['track_set']), feature_size, max_length, 2))
else:
track_struct['tracklet_mat']['save_fea_mat'] = []
'''
return
def comb_cost(tracklet_set, feature_size, max_length, img_size, sess,
batch_X_x, batch_X_y, batch_X_w, batch_X_h, batch_X_a, batch_mask_1,
batch_mask_2, batch_Y, keep_prob, y_conv):
#comb_track_cost_list = tracklet_mat['comb_track_cost'].copy()
#comb_track_cost = np.array(tracklet_mat['comb_track_cost'].copy())
#save_fea_mat = tracklet_mat['save_fea_mat'].copy()
#track_set = tracklet_mat['track_set'].copy()
global track_struct
global all_fea_mat
global all_fea_label
#import pdb; pdb.set_trace()
tracklet_mat = track_struct['tracklet_mat']
'''
temp_sum = np.sum(all_fea_mat[:,4,:,1], axis=1)
if len(np.where(temp_sum!=0)[0])==0:
fea_id = 0
else:
fea_id = int(np.max(np.where(temp_sum!=0)[0]))+1
'''
#print(fea_id)
#import pdb; pdb.set_trace()
# cnn classifier
N_tracklet = len(tracklet_set)
track_interval = tracklet_mat['track_interval']
sort_idx = np.argsort(track_interval[np.array(tracklet_set),1])
cost = 0
if len(sort_idx)<=1:
return cost
remove_ids = []
#comb_fea_mat = np.zeros((len(sort_idx)-1,feature_size,max_length,2))
#comb_fea_label = np.zeros((len(sort_idx)-1,4))
comb_fea_mat = np.zeros((int(len(sort_idx)*(len(sort_idx)-1)/2),feature_size,max_length,3))
comb_fea_label = np.zeros((int(len(sort_idx)*(len(sort_idx)-1)/2),4))
temp_cost_list = []
X1 = []
X2 = []
#print(len(comb_track_cost))
cnt = -1
for n in range(0, len(sort_idx)-1):
for kk in range(n+1,len(sort_idx)):
cnt = int(cnt+1)
track_id1 = tracklet_set[sort_idx[n]]
track_id2 = tracklet_set[sort_idx[kk]]
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
remove_ids.append(cnt)
continue
if tracklet_mat['comb_track_cost_mask'][track_id1,track_id2]==1:
cost = cost+tracklet_mat['comb_track_cost'][track_id1,track_id2]
remove_ids.append(cnt)
continue
comb_fea_label[cnt,0] = track_id1
comb_fea_label[cnt,1] = track_id2
#if track_id1==32 and track_id2==46:
# import pdb; pdb.set_trace()
'''
start_time = time.time()
if len(comb_track_cost)>0:
search_idx = np.where(np.logical_and(comb_track_cost[:,0]==track_id1, comb_track_cost[:,1]==track_id2))
if len(search_idx[0])>0:
remove_ids.append(n)
#import pdb; pdb.set_trace()
cost = cost+comb_track_cost[search_idx[0][0],2]
elapsed_time = time.time() - start_time
print(elapsed_time)
continue
'''
temp_cost_list.append([track_id1,track_id2])
# t starts from 0
#import pdb; pdb.set_trace()
t1_min = int(track_interval[track_id1,0])
t1_max = int(track_interval[track_id1,1])
t2_min = int(track_interval[track_id2,0])
t2_max = int(track_interval[track_id2,1])
t_min = int(min(t1_min,t2_min))
t_max = int(max(t1_max,t2_max))
if t_max-t_min+1<=max_length:
comb_fea_mat[cnt,:,t1_min-t_min:t1_max-t_min+1,1] = 1
comb_fea_mat[cnt,0,t1_min-t_min:t1_max-t_min+1,0] = 0.5*(tracklet_mat['xmin_mat'][track_id1,t1_min:t1_max+1]
+tracklet_mat['xmax_mat'][track_id1,t1_min:t1_max+1])/img_size[1]
comb_fea_mat[cnt,1,t1_min-t_min:t1_max-t_min+1,0] = 0.5*(tracklet_mat['ymin_mat'][track_id1,t1_min:t1_max+1]
+tracklet_mat['ymax_mat'][track_id1,t1_min:t1_max+1])/img_size[0]
comb_fea_mat[cnt,2,t1_min-t_min:t1_max-t_min+1,0] = (tracklet_mat['xmax_mat'][track_id1,t1_min:t1_max+1]
-tracklet_mat['xmin_mat'][track_id1,t1_min:t1_max+1]+1)/img_size[1]
comb_fea_mat[cnt,3,t1_min-t_min:t1_max-t_min+1,0] = (tracklet_mat['ymax_mat'][track_id1,t1_min:t1_max+1]
-tracklet_mat['ymin_mat'][track_id1,t1_min:t1_max+1]+1)/img_size[0]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id1+1)[0]
if comb_fea_mat[cnt,4:,t1_min-t_min:t1_max-t_min+1,0].shape[1]!=np.transpose(tracklet_mat['appearance_fea_mat'] \
[cand_idx,2:]).shape[1]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,4:,t1_min-t_min:t1_max-t_min+1,0] = np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
X1.append(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
comb_fea_mat[cnt,:,t2_min-t_min:t2_max-t_min+1,2] = 1
#print(t_min)
#print(t2_min)
#print(t2_max)
#import pdb; pdb.set_trace()
comb_fea_mat[cnt,0,t2_min-t_min:t2_max-t_min+1,0] = 0.5*(tracklet_mat['xmin_mat'][track_id2,t2_min:t2_max+1]
+tracklet_mat['xmax_mat'][track_id2,t2_min:t2_max+1])/img_size[1]
comb_fea_mat[cnt,1,t2_min-t_min:t2_max-t_min+1,0] = 0.5*(tracklet_mat['ymin_mat'][track_id2,t2_min:t2_max+1]
+tracklet_mat['ymax_mat'][track_id2,t2_min:t2_max+1])/img_size[0]
comb_fea_mat[cnt,2,t2_min-t_min:t2_max-t_min+1,0] = (tracklet_mat['xmax_mat'][track_id2,t2_min:t2_max+1]
-tracklet_mat['xmin_mat'][track_id2,t2_min:t2_max+1]+1)/img_size[1]
comb_fea_mat[cnt,3,t2_min-t_min:t2_max-t_min+1,0] = (tracklet_mat['ymax_mat'][track_id2,t2_min:t2_max+1]
-tracklet_mat['ymin_mat'][track_id2,t2_min:t2_max+1]+1)/img_size[0]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id2+1)[0]
if comb_fea_mat[cnt,4:,t2_min-t_min:t2_max-t_min+1,0].shape[1]!=np.transpose(tracklet_mat['appearance_fea_mat'] \
[cand_idx,2:]).shape[1]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,4:,t2_min-t_min:t2_max-t_min+1,0] = np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
X2.append(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
else:
t_len1 = t1_max-t1_min+1
t_len2 = t2_max-t2_min+1
t_len_min = min(t_len1,t_len2)
mid_t = int(0.5*(t1_max+t2_min))
if mid_t-t1_min+1>=0.5*max_length and t2_max-mid_t+1<=0.5*max_length:
t2_end = t2_max
t1_start = t2_end-max_length+1
#t1_start = mid_t-int(0.5*max_length)+1
#t2_end = t1_start+max_length-1
elif mid_t-t1_min+1<=0.5*max_length and t2_max-mid_t+1>=0.5*max_length:
t1_start = t1_min
t2_end = t1_start+max_length-1
else: # mid_t-t1_min+1>=0.5*max_length and t2_max-mid_t+1>=0.5*max_length:
t1_start = mid_t-int(0.5*max_length)+1
t2_end = t1_start+max_length-1
comb_fea_mat[cnt,:,0:t1_max-t1_start+1,1] = comb_fea_mat[cnt,:,0:t1_max-t1_start+1,1]+1
if comb_fea_mat[cnt,0,0:t1_max-t1_start+1,0].shape[0] \
!=tracklet_mat['xmax_mat'][track_id1,t1_start:t1_max+1].shape[0]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,0,0:t1_max-t1_start+1,0] = 0.5*(tracklet_mat['xmin_mat'][track_id1,t1_start:t1_max+1]
+tracklet_mat['xmax_mat'][track_id1,t1_start:t1_max+1])/img_size[1]
comb_fea_mat[cnt,1,0:t1_max-t1_start+1,0] = 0.5*(tracklet_mat['ymin_mat'][track_id1,t1_start:t1_max+1]
+tracklet_mat['ymax_mat'][track_id1,t1_start:t1_max+1])/img_size[0]
comb_fea_mat[cnt,2,0:t1_max-t1_start+1,0] = (tracklet_mat['xmax_mat'][track_id1,t1_start:t1_max+1]
-tracklet_mat['xmin_mat'][track_id1,t1_start:t1_max+1]+1)/img_size[1]
comb_fea_mat[cnt,3,0:t1_max-t1_start+1,0] = (tracklet_mat['ymax_mat'][track_id1,t1_start:t1_max+1]
-tracklet_mat['ymin_mat'][track_id1,t1_start:t1_max+1]+1)/img_size[0]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id1+1)[0]
cand_idx = cand_idx[t1_start-t1_min:]
comb_fea_mat[cnt,4:,0:t1_max-t1_start+1,0] = np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
X1.append(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
comb_fea_mat[cnt,:,t2_min-t1_start:t2_end-t1_start+1,2] = 1
if comb_fea_mat[cnt,0,t2_min-t1_start:t2_end-t1_start+1,0].shape[0] \
!=tracklet_mat['xmin_mat'][track_id2,t2_min:t2_end+1].shape[0]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,0,t2_min-t1_start:t2_end-t1_start+1,0] = 0.5*(tracklet_mat['xmin_mat'][track_id2,t2_min:t2_end+1]
+tracklet_mat['xmax_mat'][track_id2,t2_min:t2_end+1])/img_size[1]
comb_fea_mat[cnt,1,t2_min-t1_start:t2_end-t1_start+1,0] = 0.5*(tracklet_mat['ymin_mat'][track_id2,t2_min:t2_end+1]
+tracklet_mat['ymax_mat'][track_id2,t2_min:t2_end+1])/img_size[0]
comb_fea_mat[cnt,2,t2_min-t1_start:t2_end-t1_start+1,0] = (tracklet_mat['xmax_mat'][track_id2,t2_min:t2_end+1]
-tracklet_mat['xmin_mat'][track_id2,t2_min:t2_end+1]+1)/img_size[1]
comb_fea_mat[cnt,3,t2_min-t1_start:t2_end-t1_start+1,0] = (tracklet_mat['ymax_mat'][track_id2,t2_min:t2_end+1]
-tracklet_mat['ymin_mat'][track_id2,t2_min:t2_end+1]+1)/img_size[0]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id2+1)[0]
#import pdb; pdb.set_trace()
cand_idx = cand_idx[0:t2_end-t2_min+1]
comb_fea_mat[cnt,4:,t2_min-t1_start:t2_end-t1_start+1,0] \
= np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
X2.append(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
#if track_id1==34 and track_id2==39:
# import pdb; pdb.set_trace()
# remove overlap detections
t_overlap = np.where(comb_fea_mat[cnt,0,:,1]>1)
if len(t_overlap)>0:
t_overlap = t_overlap[0]
comb_fea_mat[cnt,:,t_overlap,:] = 0
if len(track_set)>0:
search_idx = np.where(np.logical_and(track_set[:,0]==track_id1, track_set[:,1]==track_id2))
if len(search_idx[0])>0:
#save_fea_mat[search_idx[0][0],:,:,:] = comb_fea_mat[n,:,:,:]
if track_set[search_idx[0][0],2]==1:
comb_fea_label[cnt,2] = 1
else:
comb_fea_label[cnt,3] = 1
if len(remove_ids)>0:
comb_fea_mat = np.delete(comb_fea_mat, np.array(remove_ids), axis=0)
comb_fea_label = np.delete(comb_fea_label, np.array(remove_ids), axis=0)
if len(comb_fea_mat)>0:
if track_struct['track_params']['use_net']==0:
for n in range(len(X1)):
pair_cost = spatial.distance.cdist(X1[n], X2[n], 'euclidean')
min_cost = np.min(pair_cost)
cost = cost+min_cost-7
tracklet_mat['comb_track_cost_mask'][temp_cost_list[n][0],temp_cost_list[n][1]] = 1
tracklet_mat['comb_track_cost'][temp_cost_list[n][0],temp_cost_list[n][1]] = min_cost-7
#cost = cost+track_struct['track_params']['cost_bias']*len(sort_idx)
return cost
#*************************************
comb_fea_mat = track_lib.interp_batch(comb_fea_mat)
#*************************************
max_batch_size = 16
num_batch = int(np.ceil(comb_fea_mat.shape[0]/max_batch_size))
pred_y = np.zeros((comb_fea_mat.shape[0],2))
for n in range(num_batch):
if n!=num_batch-1:
batch_size = max_batch_size
else:
batch_size = int(comb_fea_mat.shape[0]-(num_batch-1)*max_batch_size)
batch_size = comb_fea_mat.shape[0]
x = np.zeros((batch_size,1,max_length,1))
y = np.zeros((batch_size,1,max_length,1))
w = np.zeros((batch_size,1,max_length,1))
h = np.zeros((batch_size,1,max_length,1))
ap = np.zeros((batch_size,feature_size-4,max_length,1))
mask_1 = np.zeros((batch_size,1,max_length,2))
mask_2 = np.zeros((batch_size,feature_size-4,max_length,2))
x[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,0,:,0]
y[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,1,:,0]
w[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,2,:,0]
h[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,3,:,0]
ap[:,:,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,4:,:,0]
mask_1[:,0,:,:] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,0,:,1:]
mask_2[:,:,:,:] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,4:,:,1:]
pred_y[n*max_batch_size:n*max_batch_size+batch_size,:] = sess.run(y_conv, feed_dict={batch_X_x: x,
batch_X_y: y,
batch_X_w: w,
batch_X_h: h,
batch_X_a: ap,
batch_mask_1: mask_1,
batch_mask_2: mask_2,
batch_Y: np.zeros((batch_size,2)),
keep_prob: 1.0})
for n in range(len(pred_y)):
if np.sum(comb_fea_label[n,2:4])>0:
continue
if pred_y[n,0]>pred_y[n,1]:
comb_fea_label[n,2] = 1
else:
comb_fea_label[n,3] = 1
if comb_fea_mat.shape[0]!=len(pred_y):
import pdb; pdb.set_trace()
#print(comb_fea_label)
'''
all_fea_mat[fea_id:fea_id+len(pred_y),:,:,:] = comb_fea_mat
all_fea_label[fea_id:fea_id+len(pred_y),:] = comb_fea_label
'''
#if len(np.where(np.logical_and(comb_fea_label[:,0]==428,comb_fea_label[:,1]==435))[0])>0:
# import pdb; pdb.set_trace()
#print(comb_fea_label)
cost = cost+np.sum(pred_y[:,1]-pred_y[:,0])
#import pdb; pdb.set_trace()
if pred_y.shape[0]!=len(temp_cost_list):
import pdb; pdb.set_trace()
for n in range(pred_y.shape[0]):
#import pdb; pdb.set_trace()
'''
if tracklet_mat['comb_track_cost_mask'].shape[0]<=temp_cost_list[n][0] \
or tracklet_mat['comb_track_cost_mask'].shape[1]<=temp_cost_list[n][1]:
import pdb; pdb.set_trace()
'''
tracklet_mat['comb_track_cost_mask'][temp_cost_list[n][0],temp_cost_list[n][1]] = 1
tracklet_mat['comb_track_cost'][temp_cost_list[n][0],temp_cost_list[n][1]] = pred_y[n,1]-pred_y[n,0]
#comb_track_cost_list = comb_track_cost_list+temp_cost_list
#print(np.sum(tracklet_mat['comb_track_cost_mask']))
#cost = cost+track_struct['track_params']['cost_bias']*len(sort_idx)
return cost
def get_split_cost(track_id, sess, img_size, batch_X_x, batch_X_y, batch_X_w, batch_X_h,
batch_X_a, batch_mask_1, batch_mask_2, batch_Y, keep_prob, y_conv):
#comb_track_cost_list = tracklet_mat['comb_track_cost'].copy()
#save_fea_mat = tracklet_mat['save_fea_mat'].copy()
global track_struct
tracklet_mat = track_struct['tracklet_mat']
new_cluster_cost = np.zeros((2,1))
if len(tracklet_mat['track_cluster'][track_id])<2:
cost = float("inf")
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
track_interval = tracklet_mat['track_interval'].copy()
change_cluster_idx = [len(tracklet_mat['track_cluster']), tracklet_mat['track_class'][track_id]]
new_cluster_set = []
new_cluster_set.append([track_id])
remain_tracks = tracklet_mat['track_cluster'][tracklet_mat['track_class'][track_id]].copy()
remain_tracks.remove(track_id)
new_cluster_set.append(remain_tracks)
# get cost
if len(remain_tracks)>1:
sort_idx = np.argsort(track_interval[np.array(new_cluster_set[1]),1])
for n in range(0, len(sort_idx)-1):
track_id1 = new_cluster_set[1][sort_idx[n]]
track_id2 = new_cluster_set[1][sort_idx[n+1]]
#if track_id1==42:
# import pdb; pdb.set_trace()
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
#*********************************
new_cluster_cost[1,0] = comb_cost(remain_tracks, feature_size,
max_length,