-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrbpf_KITTI_det_scores.py
2371 lines (1978 loc) · 102 KB
/
rbpf_KITTI_det_scores.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
import numpy as np
from filterpy.kalman import KalmanFilter
from filterpy.common import Q_discrete_white_noise
from filterpy.monte_carlo import stratified_resample
import filterpy
#import matplotlib.pyplot as plt
#import matplotlib.cm as cmx
#import matplotlib.colors as colors
from scipy.stats import multivariate_normal
from scipy.stats import gamma
from scipy.special import gdtrc
import random
import copy
import math
from numpy.linalg import inv
import pickle
import sys
import resource
import errno
from munkres import Munkres
from collections import deque
#sys.path.insert(0, "/Users/jkuck/rotation3/clearmetrics")
#import clearmetrics
sys.path.insert(0, "./KITTI_helpers")
from learn_params1 import get_meas_target_set
from learn_params1 import get_meas_target_sets_lsvm_and_regionlets
from learn_params1 import get_meas_target_sets_regionlets_general_format
from learn_params1 import get_meas_target_sets_mscnn_general_format
from learn_params1 import get_meas_target_sets_mscnn_and_regionlets
from jdk_helper_evaluate_results import eval_results
#from multiple_meas_per_time_assoc_priors import HiddenState
#from proposal2_helper import possible_measurement_target_associations
#from proposal2_helper import memoized_birth_clutter_prior
#from proposal2_helper import sample_birth_clutter_counts
#from proposal2_helper import sample_target_deaths_proposal2
random.seed(5)
np.random.seed(seed=5)
import cProfile
import time
import os
from run_experiment_batch_sherlock import DIRECTORY_OF_ALL_RESULTS
from run_experiment_batch_sherlock import CUR_EXPERIMENT_BATCH_NAME
from run_experiment_batch_sherlock import SEQUENCES_TO_PROCESS
from run_experiment_batch_sherlock import get_description_of_run
USE_CREATE_CHILD = True #speed up copying during resampling
RUN_ONLINE = True #save online results
#near online mode wait this many frames before picking max weight particle
ONLINE_DELAY = 3
#Write results of the particle with the largest importance
#weight times current likelihood, double check doing this correctly
FIND_MAX_IMPRT_TIMES_LIKELIHOOD = False
#if true only update a target with at most one measurement
#(i.e. not regionlets and then lsvm)
MAX_1_MEAS_UPDATE = True
######DIRECTORY_OF_ALL_RESULTS = '/atlas/u/jkuck/rbpf_target_tracking'
######CUR_EXPERIMENT_BATCH_NAME = 'test_copy_correctness_orig_copy'
#######run on these sequences
#######SEQUENCES_TO_PROCESS = [0]
######SEQUENCES_TO_PROCESS = [i for i in range(21)]
#Variables defined in main ARE global I think, not needed here (triple check...)
#define global variables, which will be set in main
#SCORE_INTERVALS = None
#TARGET_EMISSION_PROBS = None
#CLUTTER_PROBABILITIES = None
#BIRTH_PROBABILITIES = None
#MEAS_NOISE_COVS = None
#BORDER_DEATH_PROBABILITIES = None
#NOT_BORDER_DEATH_PROBABILITIES = None
#SEQUENCES_TO_PROCESS = [i for i in range(21)]
#eval_results('./rbpf_KITTI_results', SEQUENCES_TO_PROCESS)
#sleep(5)
#RBPF algorithmic paramters
RESAMPLE_RATIO = 2.0 #resample when get_eff_num_particles < N_PARTICLES/RESAMPLE_RATIO
DEBUG = False
USE_PYTHON_GAUSSIAN = False #if False bug, using R_default instead of S, check USE_CONSTANT_R
#default time between succesive measurement time instances (in seconds)
default_time_step = .1
USE_CONSTANT_R = True
#For testing why score interval for R are slow
CACHED_LIKELIHOODS = 0
NOT_CACHED_LIKELIHOODS = 0
#from learn_params
#BIRTH_COUNT_PRIOR = [0.9371030016191306, 0.0528085689376012, 0.007223813675426578, 0.0016191306513887158, 0.000747291069871715, 0.00012454851164528583, 0, 0.00012454851164528583, 0.00012454851164528583, 0, 0, 0, 0, 0.00012454851164528583]
#from learn_params1, not counting 'ignored' ground truth
BIRTH_COUNT_PRIOR = [0.95640802092415, 0.039357329679910326, 0.0027400672561962883, 0.0008718395815170009, 0.00012454851164528583, 0.00012454851164528583, 0, 0.00024909702329057166, 0, 0, 0.00012454851164528583]
def get_score_index(score_intervals, score):
"""
Inputs:
- score_intervals: a list specifying detection score ranges for which parameters have been specified
- score: the score of a detection
Output:
- index: output the 0 indexed score interval this score falls into
"""
index = 0
for i in range(1, len(score_intervals)):
if(score > score_intervals[i]):
index += 1
else:
break
assert(score > score_intervals[index]), (score, score_intervals[index], score_intervals[index+1])
if(index < len(score_intervals) - 1):
assert(score <= score_intervals[index+1]), (score, score_intervals[index], score_intervals[index+1])
return index
#regionlet detection with score > 2.0:
#from learn_params
#P_TARGET_EMISSION = 0.813482
#from learn_params1, not counting 'ignored' ground truth
P_TARGET_EMISSION = 0.813358070501
#death probabiltiies, for sampling AFTER associations, conditioned on un-association
#DEATH_PROBABILITIES = [-99, 0.1558803061934586, 0.24179829890643986, 0.1600831600831601, 0.10416666666666667, 0.08835341365461848, 0.04081632653061224, 0.06832298136645963, 0.06201550387596899, 0.04716981132075472, 0.056818181818181816, 0.013333333333333334, 0.028985507246376812, 0.03278688524590164, 0.0, 0.0, 0.0, 0.05, 0.0, 0.0625, 0.03571428571428571, 0.0, 0.0, 0.043478260869565216, 0.0, 0.05555555555555555, 0.0, 0.0625, 0.07142857142857142, 0.0, 0.0, 0.0, 0.0, 0.0, 0.09090909090909091, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.16666666666666666, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3333333333333333, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
#BORDER_DEATH_PROBABILITIES = [-99, 0.3290203327171904, 0.5868263473053892, 0.48148148148148145, 0.4375, 0.42424242424242425, 0.2222222222222222, 0.35714285714285715, 0.2222222222222222, 0.0, 0.3333333333333333, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
#NOT_BORDER_DEATH_PROBABILITIES = [-99, 0.05133928571428571, 0.006134969325153374, 0.03468208092485549, 0.025735294117647058, 0.037037037037037035, 0.02247191011235955, 0.04081632653061224, 0.05, 0.05, 0.036585365853658534, 0.013888888888888888, 0.030303030303030304, 0.03389830508474576, 0.0, 0.0, 0.0, 0.05128205128205128, 0.0, 0.06451612903225806, 0.037037037037037035, 0.0, 0.0, 0.045454545454545456, 0.0, 0.05555555555555555, 0.0, 0.0625, 0.07142857142857142, 0.0, 0.0, 0.0, 0.0, 0.0, 0.09090909090909091, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.16666666666666666, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3333333333333333, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
#from learn_params.py
#BORDER_DEATH_PROBABILITIES = [-99, 0.3290203327171904, 0.5868263473053892, 0.48148148148148145, 0.4375, 0.42424242424242425]
#NOT_BORDER_DEATH_PROBABILITIES = [-99, 0.05133928571428571, 0.006134969325153374, 0.03468208092485549, 0.025735294117647058, 0.037037037037037035]
#BORDER_DEATH_PROBABILITIES = [-99, 0.3116591928251121, 0.5483870967741935, 0.5833333333333334, 0.8571428571428571, 1.0]
#NOT_BORDER_DEATH_PROBABILITIES = [-99, 0.001880843060242297, 0.026442307692307692, 0.04918032786885246, 0.06818181818181818, 0.008]
#BORDER_DEATH_PROBABILITIES = [-99, 0.3290203327171904, 0.5868263473053892, 0.48148148148148145, 0.4375, 0.42424242424242425]
#NOT_BORDER_DEATH_PROBABILITIES = [-99, 0.05133928571428571, 0.006134969325153374, 0.03468208092485549, 0.025735294117647058, 0.037037037037037035]
#BORDER_DEATH_PROBABILITIES = [-99, 0.8, 0.5, 0.3, 0.4, 0.8]
#NOT_BORDER_DEATH_PROBABILITIES = [-99, 0.07, 0.025, 0.03, 0.03, 0.006]
#BORDER_DEATH_PROBABILITIES = [-99, 0.9430523917995444, 0.6785714285714286, 0.4444444444444444, 0.5, 1.0]
#NOT_BORDER_DEATH_PROBABILITIES = [-99, 0.08235294117647059, 0.02284263959390863, 0.04150943396226415, 0.041237113402061855, 0.00684931506849315]
#from learn_params
#CLUTTER_COUNT_PRIOR = [0.7860256569933989, 0.17523975588491716 - .001, 0.031635321957902605, 0.004857391954166148, 0.0016191306513887158, 0.0003736455349358575, 0.00024909702329057166, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0, .001/20.0]
#from learn_params1, not counting 'ignored' ground truth
CLUTTER_COUNT_PRIOR = [0.5424333167268651, 0.3045211109727239, 0.11010088429443268, 0.0298916427948686, 0.008718395815170008, 0.003113712791132146, 0.0009963880931622867, 0.00012454851164528583, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06, 5e-06]
p_clutter_likelihood = 1.0/float(1242*375)
#p_birth_likelihood = 0.035
p_birth_likelihood = 1.0/float(1242*375)
#Kalman filter defaults
#Think about doing this in a more principled way!!!
#P_default = np.array([[57.54277774, 0, 0, 0],
# [0, 10, 0, 0],
# [0, 0, 17.86392672, 0],
# [0, 0, 0, 3]])
P_default = np.array([[40.64558317, 0, 0, 0],
[0, 10, 0, 0],
[0, 0, 5.56278505, 0],
[0, 0, 0, 3]])
#regionlet detection with score > 2.0:
#from learn_params
#R_default = np.array([[ 5.60121574e+01, -3.60666228e-02],
# [ -3.60666228e-02, 1.64772050e+01]])
#from learn_params1, not counting 'ignored' ground truth
#R_default = np.array([[ 40.64558317, 0.14036472],
# [ 0.14036472, 5.56278505]])
R_default = np.array([[ 0.0, 0.0],
[ 0.0, 0.0]])
#learned from all GT
#Q_default = np.array([[ 84.30812679, 84.21851631, -4.01491901, -8.5737873 ],
# [ 84.21851631, 84.22312789, -3.56066467, -8.07744876],
# [ -4.01491901, -3.56066467, 4.59923143, 5.19622064],
# [ -8.5737873 , -8.07744876, 5.19622064, 6.10733628]])
#also learned from all GT
Q_default = np.array([[ 60.33442497, 102.95992102, -5.50458177, -0.22813535],
[ 102.95992102, 179.84877761, -13.37640528, -9.70601621],
[ -5.50458177, -13.37640528, 4.56034398, 9.48945108],
[ -0.22813535, -9.70601621, 9.48945108, 22.32984314]])
Q_default = 4*Q_default
#measurement function matrix
H = np.array([[1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0]])
USE_LEARNED_DEATH_PROBABILITIES = True
#Gamma distribution parameters for calculating target death probabilities
alpha_death = 2.0
beta_death = 1.0
theta_death = 1.0/beta_death
print Q_default
print R_default
#for only displaying targets older than this
min_target_age = .2
#state parameters, during data generation uniformly sample new targets from range:
min_pos = -5.0
max_pos = 5.0
min_vel = -1.0
max_vel = 1.0
#The maximum allowed distance for a ground truth target and estimated target
#to be associated with each other when calculating MOTA and MOTP
MAX_ASSOCIATION_DIST = 1
CAMERA_PIXEL_WIDTH = 1242
CAMERA_PIXEL_HEIGHT = 375
def get_cmap(N):
'''Returns a function that maps each index in 0, 1, ... N-1 to a distinct
RGB color.'''
color_norm = colors.Normalize(vmin=0, vmax=N-1)
scalar_map = cmx.ScalarMappable(norm=color_norm, cmap='hsv')
def map_index_to_rgb_color(index):
return scalar_map.to_rgba(index)
return map_index_to_rgb_color
class Target:
def __init__(self, cur_time, id_, measurement = None, width=-1, height=-1):
# if measurement is None: #for data generation
# position = np.random.uniform(min_pos,max_pos)
# velocity = np.random.uniform(min_vel,max_vel)
# self.x = np.array([[position], [velocity]])
# self.P = P_default
# else:
assert(measurement != None)
self.x = np.array([[measurement[0]], [0], [measurement[1]], [0]])
self.P = P_default
self.width = width
self.height = height
assert(self.x.shape == (4, 1))
self.birth_time = cur_time
#Time of the last measurement data association with this target
self.last_measurement_association = cur_time
self.id_ = id_ #named id_ to avoid clash with built in id
self.death_prob = -1 #calculate at every time instance
self.all_states = [(self.x, self.width, self.height)]
self.all_time_stamps = [round(cur_time, 1)]
self.measurements = []
self.measurement_time_stamps = []
#if target's predicted location is offscreen, set to True and then kill
self.offscreen = False
self.updated_this_time_instance = True
def near_border(self):
near_border = False
x1 = self.x[0][0] - self.width/2.0
x2 = self.x[0][0] + self.width/2.0
y1 = self.x[2][0] - self.height/2.0
y2 = self.x[2][0] + self.height/2.0
if(x1 < 10 or x2 > (CAMERA_PIXEL_WIDTH - 15) or y1 < 10 or y2 > (CAMERA_PIXEL_HEIGHT - 15)):
near_border = True
return near_border
def kf_update(self, measurement, width, height, cur_time, meas_noise_cov):
""" Perform Kalman filter update step and replace predicted position for the current time step
with the updated position in self.all_states
Input:
- measurement: the measurement (numpy array)
- cur_time: time when the measurement was taken (float)
!!!!!!!!!PREDICTION HAS BEEN RUN AT THE BEGINNING OF TIME STEP FOR EVERY TARGET!!!!!!!!!
"""
reformat_meas = np.array([[measurement[0]],
[measurement[1]]])
assert(self.x.shape == (4, 1))
if USE_CONSTANT_R:
S = np.dot(np.dot(H, self.P), H.T) + R_default
else:
S = np.dot(np.dot(H, self.P), H.T) + meas_noise_cov
K = np.dot(np.dot(self.P, H.T), inv(S))
residual = reformat_meas - np.dot(H, self.x)
updated_x = self.x + np.dot(K, residual)
# updated_self.P = np.dot((np.eye(self.P.shape[0]) - np.dot(K, H)), self.P) #NUMERICALLY UNSTABLE!!!!!!!!
updated_P = self.P - np.dot(np.dot(K, S), K.T) #not sure if this is numerically stable!!
self.x = updated_x
self.P = updated_P
self.width = width
self.height = height
assert(self.all_time_stamps[-1] == round(cur_time, 1) and self.all_time_stamps[-2] != round(cur_time, 1))
assert(self.x.shape == (4, 1)), (self.x.shape, np.dot(K, residual).shape)
self.all_states[-1] = (self.x, self.width, self.height)
self.updated_this_time_instance = True
self.last_measurement_association = cur_time
def kf_predict(self, dt, cur_time):
"""
Run kalman filter prediction on this target
Inputs:
-dt: time step to run prediction on
-cur_time: the time the prediction is made for
"""
assert(self.all_time_stamps[-1] == round((cur_time - dt), 1))
F = np.array([[1.0, dt, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, dt],
[0.0, 0.0, 0.0, 1.0]])
x_predict = np.dot(F, self.x)
P_predict = np.dot(np.dot(F, self.P), F.T) + Q_default
self.x = x_predict
self.P = P_predict
self.all_states.append((self.x, self.width, self.height))
self.all_time_stamps.append(round(cur_time, 1))
if(self.x[0][0]<0 or self.x[0][0]>=CAMERA_PIXEL_WIDTH or \
self.x[2][0]<0 or self.x[2][0]>=CAMERA_PIXEL_HEIGHT):
# print '!'*40, "TARGET IS OFFSCREEN", '!'*40
self.offscreen = True
assert(self.x.shape == (4, 1))
self.updated_this_time_instance = False
################### def target_death_prob(self, cur_time, prev_time):
################### """ Calculate the target death probability if this was the only target.
################### Actual target death probability will be (return_val/number_of_targets)
################### because we limit ourselves to killing a max of one target per measurement.
###################
################### Input:
################### - cur_time: The current measurement time (float)
################### - prev_time: The previous time step when a measurement was received (float)
###################
################### Return:
################### - death_prob: Probability of target death if this is the only target (float)
################### """
###################
################### #scipy.special.gdtrc(b, a, x) calculates
################### #integral(gamma_dist(k = a, theta = b))from x to infinity
################### last_assoc = self.last_measurement_association
###################
################### #I think this is correct
################### death_prob = gdtrc(theta_death, alpha_death, prev_time - last_assoc) \
################### - gdtrc(theta_death, alpha_death, cur_time - last_assoc)
################### death_prob /= gdtrc(theta_death, alpha_death, prev_time - last_assoc)
################### return death_prob
###################
#################### #this is used in paper's code
#################### time_step = cur_time - prev_time
####################
#################### death_prob = gdtrc(theta_death, alpha_death, cur_time - last_assoc) \
#################### - gdtrc(theta_death, alpha_death, cur_time - last_assoc + time_step)
#################### death_prob /= gdtrc(theta_death, alpha_death, cur_time - last_assoc)
#################### return death_prob
def target_death_prob(self, cur_time, prev_time):
""" Calculate the target death probability if this was the only target.
Actual target death probability will be (return_val/number_of_targets)
because we limit ourselves to killing a max of one target per measurement.
Input:
- cur_time: The current measurement time (float)
- prev_time: The previous time step when a measurement was received (float)
Return:
- death_prob: Probability of target death if this is the only target (float)
"""
################## #scipy.special.gdtrc(b, a, x) calculates
################## #integral(gamma_dist(k = a, theta = b))from x to infinity
################## last_assoc = self.last_measurement_association
##################
################## #I think this is correct
################## death_prob = gdtrc(theta_death, alpha_death, prev_time - last_assoc) \
################## - gdtrc(theta_death, alpha_death, cur_time - last_assoc)
################## death_prob /= gdtrc(theta_death, alpha_death, prev_time - last_assoc)
################## return death_prob
if(self.offscreen == True):
cur_death_prob = 1.0
else:
frames_since_last_assoc = int(round((cur_time - self.last_measurement_association)/default_time_step))
assert(abs(float(frames_since_last_assoc) - (cur_time - self.last_measurement_association)/default_time_step) < .00000001)
if(self.near_border()):
if frames_since_last_assoc < len(BORDER_DEATH_PROBABILITIES):
cur_death_prob = BORDER_DEATH_PROBABILITIES[frames_since_last_assoc]
else:
cur_death_prob = BORDER_DEATH_PROBABILITIES[-1]
# cur_death_prob = 1.0
else:
if frames_since_last_assoc < len(NOT_BORDER_DEATH_PROBABILITIES):
cur_death_prob = NOT_BORDER_DEATH_PROBABILITIES[frames_since_last_assoc]
else:
cur_death_prob = NOT_BORDER_DEATH_PROBABILITIES[-1]
# cur_death_prob = 1.0
assert(cur_death_prob >= 0.0 and cur_death_prob <= 1.0), cur_death_prob
return cur_death_prob
class Measurement:
#a collection of measurements at a single time instance
def __init__(self, time = -1):
#self.val is a list of numpy arrays of measurement x, y locations
self.val = []
#list of widths of each bounding box
self.widths = []
#list of widths of each bounding box
self.heights = []
#list of scores for each individual measurement
self.scores = []
self.time = time
class TargetSet:
"""
Contains ground truth states for all targets. Also contains all generated measurements.
"""
def __init__(self):
self.living_targets = []
self.all_targets = [] #alive and dead targets
self.living_count = 0 #number of living targets
self.total_count = 0 #number of living targets plus number of dead targets
self.measurements = [] #generated measurements for a generative TargetSet
self.parent_target_set = None
self.living_targets_q = deque([-1 for i in range(ONLINE_DELAY)])
def create_child(self):
child_target_set = TargetSet()
child_target_set.parent_target_set = self
child_target_set.total_count = self.total_count
child_target_set.living_count = self.living_count
child_target_set.all_targets = copy.deepcopy(self.living_targets)
for target in child_target_set.all_targets:
child_target_set.living_targets.append(target)
child_target_set.living_targets_q = copy.deepcopy(self.living_targets_q)
return child_target_set
def create_new_target(self, measurement, width, height, cur_time):
if RUN_ONLINE:
global NEXT_TARGET_ID
new_target = Target(cur_time, NEXT_TARGET_ID, np.squeeze(measurement), width, height)
NEXT_TARGET_ID += 1
else:
new_target = Target(cur_time, self.total_count, np.squeeze(measurement), width, height)
self.living_targets.append(new_target)
self.all_targets.append(new_target)
self.living_count += 1
self.total_count += 1
if not USE_CREATE_CHILD:
assert(len(self.living_targets) == self.living_count and len(self.all_targets) == self.total_count)
def kill_target(self, living_target_index):
"""
Kill target self.living_targets[living_target_index], note that living_target_index
may not be the target's id_ (or index in all_targets)
"""
#kf predict was run for this time instance, but the target actually died, so remove the predicted state
del self.living_targets[living_target_index].all_states[-1]
del self.living_targets[living_target_index].all_time_stamps[-1]
del self.living_targets[living_target_index]
self.living_count -= 1
if not USE_CREATE_CHILD:
assert(len(self.living_targets) == self.living_count and len(self.all_targets) == self.total_count)
def plot_all_target_locations(self, title):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
for i in range(self.total_count):
life = len(self.all_targets[i].all_states) #length of current targets life
locations_1D = [self.all_targets[i].all_states[j][0] for j in range(life)]
ax.plot(self.all_targets[i].all_time_stamps, locations_1D,
'-o', label='Target %d' % i)
legend = ax.legend(loc='lower left', shadow=True)
plt.title('%s, unique targets = %d, #targets alive = %d' % \
(title, self.total_count, self.living_count)) # subplot 211 title
def plot_generated_measurements(self):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
time_stamps = [self.measurements[i].time for i in range(len(self.measurements))
for j in range(len(self.measurements[i].val))]
locations = [self.measurements[i].val[j][0] for i in range(len(self.measurements))
for j in range(len(self.measurements[i].val))]
ax.plot(time_stamps, locations,'o')
plt.title('Generated Measurements')
def collect_ancestral_targets(self, descendant_target_ids=[]):
"""
Inputs:
- descendant_target_ids: a list of target ids that exist in the calling child's all_targets list
(or the all_targets list of a descendant of the calling child)
Outputs:
- every_target: every target in this TargetSet's all_targets list and
#every target in any of this TargetSet's ancestors' all_targets lists that does not
#appear in the all_targets list of a descendant
"""
every_target = []
found_target_ids = descendant_target_ids
for target in self.all_targets:
if(not target.id_ in found_target_ids):
every_target.append(target)
found_target_ids.append(target.id_)
if self.parent_target_set == None:
return every_target
else:
ancestral_targets = self.parent_target_set.collect_ancestral_targets(found_target_ids)
every_target = every_target + ancestral_targets # + operator used to concatenate lists!
return every_target
def write_online_results(self, online_results_filename, frame_idx, total_frame_count):
if frame_idx == ONLINE_DELAY:
f = open(online_results_filename, "w") #write over old results if first frame
else:
f = open(online_results_filename, "a") #write at end of file
if ONLINE_DELAY == 0:
for target in self.living_targets:
assert(target.all_time_stamps[-1] == round(frame_idx*default_time_step, 1))
x_pos = target.all_states[-1][0][0][0]
y_pos = target.all_states[-1][0][2][0]
width = target.all_states[-1][1]
height = target.all_states[-1][2]
left = x_pos - width/2.0
top = y_pos - height/2.0
right = x_pos + width/2.0
bottom = y_pos + height/2.0
f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
(frame_idx, target.id_, left, top, right, bottom))
else:
print self.living_targets_q
(delayed_frame_idx, delayed_liv_targets) = self.living_targets_q[0]
print delayed_frame_idx
print delayed_liv_targets
assert(delayed_frame_idx == frame_idx - ONLINE_DELAY), (delayed_frame_idx, frame_idx, ONLINE_DELAY)
for target in delayed_liv_targets:
assert(target.all_time_stamps[-1] == round((frame_idx - ONLINE_DELAY)*default_time_step, 1)), (target.all_time_stamps[-1], frame_idx, ONLINE_DELAY, round((frame_idx - ONLINE_DELAY)*default_time_step, 1))
x_pos = target.all_states[-1][0][0][0]
y_pos = target.all_states[-1][0][2][0]
width = target.all_states[-1][1]
height = target.all_states[-1][2]
left = x_pos - width/2.0
top = y_pos - height/2.0
right = x_pos + width/2.0
bottom = y_pos + height/2.0
f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
(frame_idx - ONLINE_DELAY, target.id_, left, top, right, bottom))
if frame_idx == total_frame_count - 1:
q_idx = 1
for cur_frame_idx in range(frame_idx - ONLINE_DELAY + 1, total_frame_count - 1):
print '-'*20
print cur_frame_idx
print frame_idx - ONLINE_DELAY + 1
print total_frame_count
print q_idx
print len(self.living_targets_q)
(delayed_frame_idx, delayed_liv_targets) = self.living_targets_q[q_idx]
q_idx+=1
assert(delayed_frame_idx == cur_frame_idx), (delayed_frame_idx, cur_frame_idx, ONLINE_DELAY)
for target in delayed_liv_targets:
assert(target.all_time_stamps[-1] == round((cur_frame_idx)*default_time_step, 1))
x_pos = target.all_states[-1][0][0][0]
y_pos = target.all_states[-1][0][2][0]
width = target.all_states[-1][1]
height = target.all_states[-1][2]
left = x_pos - width/2.0
top = y_pos - height/2.0
right = x_pos + width/2.0
bottom = y_pos + height/2.0
f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
(cur_frame_idx, target.id_, left, top, right, bottom))
for target in self.living_targets:
assert(target.all_time_stamps[-1] == round(frame_idx*default_time_step, 1))
x_pos = target.all_states[-1][0][0][0]
y_pos = target.all_states[-1][0][2][0]
width = target.all_states[-1][1]
height = target.all_states[-1][2]
left = x_pos - width/2.0
top = y_pos - height/2.0
right = x_pos + width/2.0
bottom = y_pos + height/2.0
f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
(frame_idx, target.id_, left, top, right, bottom))
def write_targets_to_KITTI_format(self, num_frames, filename):
if USE_CREATE_CHILD:
every_target = self.collect_ancestral_targets()
f = open(filename, "w")
for frame_idx in range(num_frames):
timestamp = round(frame_idx*default_time_step, 1)
for target in every_target:
if timestamp in target.all_time_stamps:
x_pos = target.all_states[target.all_time_stamps.index(timestamp)][0][0][0]
y_pos = target.all_states[target.all_time_stamps.index(timestamp)][0][2][0]
width = target.all_states[target.all_time_stamps.index(timestamp)][1]
height = target.all_states[target.all_time_stamps.index(timestamp)][2]
left = x_pos - width/2.0
top = y_pos - height/2.0
right = x_pos + width/2.0
bottom = y_pos + height/2.0
f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
(frame_idx, target.id_, left, top, right, bottom))
# left = target.x[0][0] - target.width/2
# top = target.x[2][0] - target.height/2
# right = target.x[0][0] + target.width/2
# bottom = target.x[2][0] + target.height/2
# f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
# (frame_idx, target.id_, left, top, right, bottom))
f.close()
else:
f = open(filename, "w")
for frame_idx in range(num_frames):
timestamp = round(frame_idx*default_time_step, 1)
for target in self.all_targets:
if timestamp in target.all_time_stamps:
x_pos = target.all_states[target.all_time_stamps.index(timestamp)][0][0][0]
y_pos = target.all_states[target.all_time_stamps.index(timestamp)][0][2][0]
width = target.all_states[target.all_time_stamps.index(timestamp)][1]
height = target.all_states[target.all_time_stamps.index(timestamp)][2]
left = x_pos - width/2.0
top = y_pos - height/2.0
right = x_pos + width/2.0
bottom = y_pos + height/2.0
f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
(frame_idx, target.id_, left, top, right, bottom))
# left = target.x[0][0] - target.width/2
# top = target.x[2][0] - target.height/2
# right = target.x[0][0] + target.width/2
# bottom = target.x[2][0] + target.height/2
# f.write( "%d %d Car -1 -1 2.57 %d %d %d %d -1 -1 -1 -1000 -1000 -1000 -10 1\n" % \
# (frame_idx, target.id_, left, top, right, bottom))
f.close()
class Particle:
def __init__(self, id_):
#Targets tracked by this particle
self.targets = TargetSet()
self.importance_weight = 1.0/N_PARTICLES
self.likelihood_DOUBLE_CHECK_ME = -1
#cache for memoizing association likelihood computation
self.assoc_likelihood_cache = {}
self.id_ = id_ #will be the same as the parent's id when copying in create_child
self.parent_id = -1
#for debugging
self.c_debug = -1
self.imprt_re_weight_debug = -1
self.pi_birth_debug = -1
self.pi_clutter_debug = -1
self.pi_targets_debug = []
def create_child(self):
global NEXT_PARTICLE_ID
child_particle = Particle(NEXT_PARTICLE_ID)
NEXT_PARTICLE_ID += 1
child_particle.importance_weight = self.importance_weight
child_particle.targets = self.targets.create_child()
return child_particle
def create_new_target(self, measurement, width, height, cur_time):
self.targets.create_new_target(measurement, width, height, cur_time)
def update_target_death_probabilities(self, cur_time, prev_time):
for target in self.targets.living_targets:
target.death_prob = target.target_death_prob(cur_time, prev_time)
def sample_target_deaths(self):
"""
Implemented to possibly kill multiple targets at once, seems
reasonbale but CHECK TECHNICAL DETAILS!!
death_prob for every target should have already been calculated!!
Input:
- cur_time: The current measurement time (float)
- prev_time: The previous time step when a measurement was received (float)
"""
original_num_targets = self.targets.living_count
num_targets_killed = 0
indices_to_kill = []
for (index, cur_target) in enumerate(self.targets.living_targets):
death_prob = cur_target.death_prob
assert(death_prob < 1.0 and death_prob > 0.0)
if (random.random() < death_prob):
indices_to_kill.append(index)
num_targets_killed += 1
#important to delete largest index first to preserve values of the remaining indices
for index in reversed(indices_to_kill):
self.targets.kill_target(index)
assert(self.targets.living_count == (original_num_targets - num_targets_killed))
#print "targets killed = ", num_targets_killed
def sample_data_assoc_and_death_mult_meas_per_time_proposal_distr_1(self, measurement_lists, \
cur_time, measurement_scores):
"""
Input:
- measurement_lists: a list where measurement_lists[i] is a list of all measurements from the current
time instance from the ith measurement source (i.e. different object detection algorithms
or different sensors)
- measurement_scores: a list where measurement_scores[i] is a list containing scores for every measurement in
measurement_list[i]
Output:
- measurement_associations: A list where measurement_associations[i] is a list of association values
for each measurements in measurement_lists[i]. Association values correspond to:
measurement_associations[i][j] = -1 -> measurement is clutter
measurement_associations[i][j] = self.targets.living_count -> measurement is a new target
measurement_associations[i][j] in range [0, self.targets.living_count-1] -> measurement is of
particle.targets.living_targets[measurement_associations[i][j]]
- imprt_re_weight: After processing this measurement the particle's
importance weight will be:
new_importance_weight = old_importance_weight * imprt_re_weight
- targets_to_kill: a list containing the indices of targets that should be killed, beginning
with the smallest index in increasing order, e.g. [0, 4, 6, 33]
"""
#get death probabilities for each target in a numpy array
num_targs = self.targets.living_count
p_target_deaths = []
for target in self.targets.living_targets:
p_target_deaths.append(target.death_prob)
assert(p_target_deaths[len(p_target_deaths) - 1] >= 0 and p_target_deaths[len(p_target_deaths) - 1] <= 1)
(targets_to_kill, measurement_associations, proposal_probability, unassociated_target_death_probs) = \
self.sample_proposal_distr3(measurement_lists, self.targets.living_count, p_target_deaths, \
cur_time, measurement_scores)
living_target_indices = []
for i in range(self.targets.living_count):
if(not i in targets_to_kill):
living_target_indices.append(i)
# exact_probability = self.get_exact_prob_hidden_and_data(measurement_list, living_target_indices, self.targets.living_count,
# measurement_associations, p_target_deaths)
exact_probability = 1.0
for meas_source_index in range(len(measurement_lists)):
cur_assoc_prob = self.get_exact_prob_hidden_and_data(meas_source_index, measurement_lists[meas_source_index], \
living_target_indices, self.targets.living_count, measurement_associations[meas_source_index],\
unassociated_target_death_probs, measurement_scores[meas_source_index], SCORE_INTERVALS[meas_source_index])
exact_probability *= cur_assoc_prob
exact_death_prob = self.calc_death_prior(living_target_indices, p_target_deaths)
exact_probability *= exact_death_prob
assert(num_targs == self.targets.living_count)
#double check targets_to_kill is sorted
assert(all([targets_to_kill[i] <= targets_to_kill[i+1] for i in xrange(len(targets_to_kill)-1)]))
imprt_re_weight = exact_probability/proposal_probability
assert(imprt_re_weight != 0.0), (exact_probability, proposal_probability)
self.likelihood_DOUBLE_CHECK_ME = exact_probability
return (measurement_associations, targets_to_kill, imprt_re_weight)
def associate_measurements_proposal_distr3(self, meas_source_index, measurement_list, total_target_count, \
p_target_deaths, measurement_scores):
"""
Try sampling associations with each measurement sequentially
Input:
- measurement_list: a list of all measurements from the current time instance
- total_target_count: the number of living targets on the previous time instace
- p_target_deaths: a list of length len(total_target_count) where
p_target_deaths[i] = the probability that target i has died between the last
time instance and the current time instance
Output:
- list_of_measurement_associations: list of associations for each measurement
- proposal_probability: proposal probability of the sampled deaths and associations
"""
list_of_measurement_associations = []
proposal_probability = 1.0
#sample measurement associations
birth_count = 0
clutter_count = 0
remaining_meas_count = len(measurement_list)
for (index, cur_meas) in enumerate(measurement_list):
score_index = get_score_index(SCORE_INTERVALS[meas_source_index], measurement_scores[index])
#create proposal distribution for the current measurement
#compute target association proposal probabilities
proposal_distribution_list = []
for target_index in range(total_target_count):
cur_target_likelihood = self.memoized_assoc_likelihood(cur_meas, meas_source_index, target_index, MEAS_NOISE_COVS[meas_source_index][score_index], score_index)
targ_likelihoods_summed_over_meas = 0.0
for meas_index in range(len(measurement_list)):
temp_score_index = get_score_index(SCORE_INTERVALS[meas_source_index], measurement_scores[meas_index]) #score_index for the meas_index in this loop
targ_likelihoods_summed_over_meas += self.memoized_assoc_likelihood(measurement_list[meas_index], meas_source_index, target_index, MEAS_NOISE_COVS[meas_source_index][temp_score_index], temp_score_index)
if((targ_likelihoods_summed_over_meas != 0.0) and (not target_index in list_of_measurement_associations)\
and p_target_deaths[target_index] < 1.0):
cur_target_prior = TARGET_EMISSION_PROBS[meas_source_index][score_index]*cur_target_likelihood \
/targ_likelihoods_summed_over_meas
# cur_target_prior = P_TARGET_EMISSION*cur_target_likelihood \
# /targ_likelihoods_summed_over_meas
else:
cur_target_prior = 0.0
proposal_distribution_list.append(cur_target_likelihood*cur_target_prior)
#compute birth association proposal probability
cur_birth_prior = 0.0
for i in range(birth_count+1, min(len(BIRTH_PROBABILITIES[meas_source_index][score_index]), remaining_meas_count + birth_count + 1)):
cur_birth_prior += BIRTH_PROBABILITIES[meas_source_index][score_index][i]*(i - birth_count)/remaining_meas_count
proposal_distribution_list.append(cur_birth_prior*p_birth_likelihood)
#compute clutter association proposal probability
cur_clutter_prior = 0.0
for i in range(clutter_count+1, min(len(CLUTTER_PROBABILITIES[meas_source_index][score_index]), remaining_meas_count + clutter_count + 1)):
cur_clutter_prior += CLUTTER_PROBABILITIES[meas_source_index][score_index][i]*(i - clutter_count)/remaining_meas_count
proposal_distribution_list.append(cur_clutter_prior*p_clutter_likelihood)
#normalize the proposal distribution
proposal_distribution = np.asarray(proposal_distribution_list)
assert(np.sum(proposal_distribution) != 0.0), (len(proposal_distribution), proposal_distribution, birth_count, clutter_count, len(measurement_list), total_target_count)
proposal_distribution /= float(np.sum(proposal_distribution))
assert(len(proposal_distribution) == total_target_count+2)
sampled_assoc_idx = np.random.choice(len(proposal_distribution),
p=proposal_distribution)
if(sampled_assoc_idx <= total_target_count): #target or birth association
list_of_measurement_associations.append(sampled_assoc_idx)
if(sampled_assoc_idx == total_target_count):
birth_count += 1
else: #clutter association
assert(sampled_assoc_idx == total_target_count+1)
list_of_measurement_associations.append(-1)
clutter_count += 1
proposal_probability *= proposal_distribution[sampled_assoc_idx]
remaining_meas_count -= 1
assert(remaining_meas_count == 0)
return(list_of_measurement_associations, proposal_probability)
def sample_proposal_distr3(self, measurement_lists, total_target_count,
p_target_deaths, cur_time, measurement_scores):
"""
Try sampling associations with each measurement sequentially
Input:
- measurement_lists: type list, measurement_lists[i] is a list of all measurements from the current
time instance from the ith measurement source (i.e. different object detection algorithms
or different sensors)
- measurement_scores: type list, measurement_scores[i] is a list containing scores for every measurement in
measurement_list[i]
- total_target_count: the number of living targets on the previous time instace
- p_target_deaths: a list of length len(total_target_count) where
p_target_deaths[i] = the probability that target i has died between the last
time instance and the current time instance
Output:
- targets_to_kill: a list of targets that have been sampled to die (not killed yet)
- measurement_associations: type list, measurement_associations[i] is a list of associations for
the measurements in measurement_lists[i]
- proposal_probability: proposal probability of the sampled deaths and associations
"""
assert(len(measurement_lists) == len(measurement_scores))
measurement_associations = []
proposal_probability = 1.0
for meas_source_index in range(len(measurement_lists)):
(cur_associations, cur_proposal_prob) = self.associate_measurements_proposal_distr3\
(meas_source_index, measurement_lists[meas_source_index], total_target_count, \
p_target_deaths, measurement_scores[meas_source_index])
measurement_associations.append(cur_associations)
proposal_probability *= cur_proposal_prob
assert(len(measurement_associations) == len(measurement_lists))
############################################################################################################
#sample target deaths from unassociated targets
unassociated_targets = []
unassociated_target_death_probs = []
for i in range(total_target_count):
target_unassociated = True
for meas_source_index in range(len(measurement_associations)):
if (i in measurement_associations[meas_source_index]):
target_unassociated = False
if target_unassociated:
unassociated_targets.append(i)
unassociated_target_death_probs.append(p_target_deaths[i])
else:
unassociated_target_death_probs.append(0.0)
if USE_LEARNED_DEATH_PROBABILITIES:
(targets_to_kill, death_probability) = \
self.sample_target_deaths_proposal3(unassociated_targets, cur_time)
else:
(targets_to_kill, death_probability) = \
self.sample_target_deaths_proposal2(unassociated_targets, cur_time)
#probability of sampling all associations
proposal_probability *= death_probability
assert(proposal_probability != 0.0)
#debug
for meas_source_index in range(len(measurement_associations)):
for i in range(total_target_count):
assert(measurement_associations[meas_source_index].count(i) == 0 or \
measurement_associations[meas_source_index].count(i) == 1), (measurement_associations[meas_source_index], measurement_list, total_target_count, p_target_deaths)
#done debug
return (targets_to_kill, measurement_associations, proposal_probability, unassociated_target_death_probs)
def sample_target_deaths_proposal3(self, unassociated_targets, cur_time):
"""
Sample target deaths, given they have not been associated with a measurement, using probabilities
learned from data.
Also kill all targets that are offscreen.
Inputs:
- unassociated_targets: a list of target indices that have not been associated with a measurement
Output:
- targets_to_kill: a list of targets that have been sampled to die (not killed yet)
- probability_of_deaths: the probability of the sampled deaths
"""
targets_to_kill = []
probability_of_deaths = 1.0
for target_idx in range(len(self.targets.living_targets)):
#kill offscreen targets with probability 1.0
if(self.targets.living_targets[target_idx].offscreen == True):
targets_to_kill.append(target_idx)
elif(target_idx in unassociated_targets):
cur_death_prob = self.targets.living_targets[target_idx].death_prob
if(random.random() < cur_death_prob):
targets_to_kill.append(target_idx)
probability_of_deaths *= cur_death_prob
else:
probability_of_deaths *= (1 - cur_death_prob)
return (targets_to_kill, probability_of_deaths)
def calc_death_prior(self, living_target_indices, p_target_deaths):
death_prior = 1.0
for (cur_target_index, cur_target_death_prob) in enumerate(p_target_deaths):
if cur_target_index in living_target_indices:
death_prior *= (1.0 - cur_target_death_prob)
assert((1.0 - cur_target_death_prob) != 0.0), cur_target_death_prob
else:
death_prior *= cur_target_death_prob
assert((cur_target_death_prob) != 0.0), cur_target_death_prob