-
Notifications
You must be signed in to change notification settings - Fork 30
/
syntheticexample.py
139 lines (116 loc) · 5.92 KB
/
syntheticexample.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
#!/usr/bin/env python
# script which runs a synthetic model plus clutter and GM-PHD tracking,
# and generates a plot of how things evolved over time.
# (c) 2012 Dan Stowell and Queen Mary University of London.
"""
This file is part of gmphd, GM-PHD filter in python by Dan Stowell.
gmphd is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
gmphd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with gmphd. If not, see <http://www.gnu.org/licenses/>.
"""
from gmphd import *
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from syntheticexamplestuff import *
###############################################################
# user config options:
niters = 100
birthprob = 0.1 # 0.05 # 0 # 0.2
survivalprob = 0.975 # 0.95 # 1
detectprob = 0.95# 0.999
clutterintensitytot = 5 #2 #4 # typical num clutter items per frame
bias = 2 #8 # tendency to prefer false-positives over false-negatives in the filtered output
initcount = 0 #2 # 4
obsntype = 'chirp' # 'chirp' or 'spect'
transntype = 'vibrato' # 'fixedvel' or 'vibrato'
###############################################################
# setting up variables
transnmatrix = transntypes[transntype]
obsnmatrix = obsntypes[obsntype]['obsnmatrix']
directlystatetospec = dot(obsntypes[obsntype]['obstospec'], obsnmatrix)
#### This one deliberately ignores the vibrato component - doing it for the plot in the paper
directlystatetospec001 = array([[0., 0., 1.]])
birthintensity1 = birthprob / len(birthgmm)
print("birthgmm: each component has weight %g" % birthintensity1)
for comp in birthgmm:
comp.weight = birthintensity1
clutterintensity = clutterintensityfromtot(clutterintensitytot, obsntype)
print("clutterintensity: %g" % clutterintensity)
### Create the "true" state and the model:
trueitems = [TrackableThing(obsnmatrix, transnmatrix) for _ in range(initcount)]
if initcount != 0:
print("True states at init:")
for item in trueitems:
print(list(item.state.T))
g = Gmphd(birthgmm, survivalprob, 0.7, transnmatrix, 1e-9 * array([[1,0,0], [0,1,0], [0,0,1]]), obsnmatrix, obsntypes[obsntype]['noisecov'], clutterintensity)
###############################################################
results = []
for whichiter in range(niters):
print("--%i----------------------------------------------------------------------" % whichiter)
# the "real" state evolves
trueitems = updatetrueitems(trueitems, survivalprob, birthprob, obsnmatrix, transnmatrix)
# we make our observations of it
(obsset, groundtruth) = getobservations(trueitems, clutterintensitytot, obsntype, directlystatetospec, detectprob)
print("OBSSET sent to g.update():")
print(obsset)
# we run our inference using the observations
updateandprune(g, obsset)
resultdict = collateresults(g, obsset, bias, obsntype, directlystatetospec, trueitems, groundtruth)
# also manually grab a version using the 001 matrix, for the paper:
resultdict001 = collateresults(g, obsset, bias, obsntype, directlystatetospec001, trueitems, groundtruth)
resultdict['estspec001'] = resultdict001['estspec']
results.append(resultdict)
###############################################################
# plot the results
fig = plt.figure()
#dist_obs = dist0(array([moment['groundtruth'] for moment in results]), \
# array([moment['obsspec'] for moment in results]))
#dist_infer = dist0(array([moment['groundtruth'] for moment in results]), \
# array([moment['estspec'] for moment in results]))
# True trajectories:
ax = fig.add_subplot(511)
ax.imshow(array([moment['groundtruth'] for moment in results]).T, aspect='auto', interpolation='nearest', cmap=cm.binary) #, norm=normer, cmap=cmap)
plt.ylabel('True', fontsize='x-small')
plt.xticks( fontsize='x-small' )
plt.yticks( arange(0, 60, 10), ('', '', '', '', '', '') )
# Noisy observations:
ax = fig.add_subplot(512)
ax.imshow(array([moment['obsspec'] for moment in results]).T, aspect='auto', interpolation='nearest', cmap=cm.binary) #, norm=normer, cmap=cmap)
plt.ylabel('Observed', fontsize='x-small')
plt.xticks( fontsize='x-small' )
plt.yticks( arange(0, 60, 10), ('', '', '', '', '', '') )
## Intensity plot (GMM density):
#ax = fig.add_subplot(521)
#ax.imshow((array([moment['intensity'] for moment in results]).T), aspect='auto') #, norm=normer, cmap=cmap)
#plt.ylabel('Intensity\n(%g)' % (max([max(moment['intensity']) for moment in results])))
plt.xticks( fontsize='x-small' )
plt.yticks( arange(0, 60, 10), ('', '', '', '', '', '') )
# Estimated locations:
ax = fig.add_subplot(513)
ax.imshow(array([[min(x,1.0) for x in moment['estspec']] for moment in results]).T, aspect='auto', interpolation='nearest', cmap=cm.binary)
plt.ylabel('Estimated', fontsize='x-small')
plt.xticks( fontsize='x-small' )
plt.yticks( arange(0, 60, 10), ('', '', '', '', '', '') )
ax = fig.add_subplot(514)
ax.imshow(array([[min(x,1.0) for x in moment['estspec001']] for moment in results]).T, aspect='auto', interpolation='nearest', cmap=cm.binary)
plt.ylabel('Estimated\n(no vibrato)', fontsize='x-small')
plt.xticks( fontsize='x-small' )
plt.yticks( arange(0, 60, 10), ('', '', '', '', '', '') )
# True and est count:
ax = fig.add_subplot(515)
ax.plot(range(len(results)), [sum(array(moment['groundtruth'])) for moment in results], 'b-', \
range(len(results)), [moment['integral'] for moment in results], 'r-')
plt.ylim(ymin=0)
plt.ylabel('True&Est #', fontsize='x-small')
plt.xticks( fontsize='x-small' )
plt.yticks( fontsize='x-small' )
plt.savefig("plot_testgmphd.pdf", papertype='A4', format='pdf')
fig.show()
#raw_input("Press Enter to continue...")