forked from danstowell/gmphd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmphd.py
276 lines (226 loc) · 10.3 KB
/
gmphd.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
#!/usr/bin/env python
import numpy as np
from operator import itemgetter, attrgetter
from scipy.stats import multivariate_normal
import math
from matplotlib import path
import utils
import itertools
import copy
# GM-PHD implementation in Python by Dan Stowell modified by Tommaso Fabbri
#
# Based on the description in Vo and Ma (2006).
# (c) 2012 Dan Stowell and Queen Mary University of London.
# (c) 2016 Tommaso Fabbri and University of Pisa - Automation & Robotics Laboratory
# All rights reserved.
#
# NOTE: SPAWNING IS NOT IMPLEMENTED.
"""
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/>.
"""
np.set_printoptions(precision=3)
class GmphdComponent:
"""
GM-PHD Gaussian component.
The Gaussian component is defined by:
_weight
_mean
_covariance
"""
def __init__(self, weight, mean, cov):
self._weight = np.float64(weight)
self._mean = np.array(mean, dtype=np.float64)
self._cov = np.array(cov, dtype=np.float64)
self._mean.resize((self._mean.size, 1))
self._cov.resize((self._mean.size, self._mean.size))
def __repr__(self):
str_ = 'W: {0} M: {1} C: {2}'.format(self._weight, self._mean.tolist(), self._cov.tolist())
return str_
class GMPHD:
birth_w = 0.001
def __str__(self):
for i in self.gm:
return i.__str__()
def __init__(self, birthgmm, survival, detection, f, q, h, r, clutter):
"""
'gm' list of GmphdComponent
'birthgmm' List of GmphdComponent items which makes up the GMM of birth probabilities.
'survival' Survival probability.
'detection' Detection probability.
'f' State transition matrix F.
'q' Process noise covariance Q.
'h' Observation matrix H.
'r' Observation noise covariance R.
'clutter' Clutter intensity.
"""
self.gm = []
self.birthgmm = birthgmm
self.survival = np.float64(survival) # p_{s,k}(x) in paper
self.detection = np.float64(detection) # p_{d,k}(x) in paper
self.f = np.array(f, dtype=np.float64) # state transition matrix (F_k-1 in paper)
self.q = np.array(q, dtype=np.float64) # process noise covariance (Q_k-1 in paper)
self.h = np.array(h, dtype=np.float64) # observation matrix (H_k in paper)
self.r = np.array(r, dtype=np.float64) # observation noise covariance (R_k in paper)
self.clutter = np.float64(clutter) # clutter intensity
self.swath_width = np.float64(50) # uFldSensor Swath Width
self.swath_length = np.float64(4) # uFldSensor Swath Length
def update_ufldsensor_data(self, width, length):
self.swath_width = width
self.swath_length = length
def predict_birth(self, born_components):
# Prediction for birth targets
born = [GmphdComponent(comp._weight,
np.dot(self.f, comp._mean),
self.q + np.dot(np.dot(self.f, comp._cov), self.f.T)
) for comp in born_components]
return born
def predict_existing(self, nav_status, sss_path):
# Prediction for existing targets
if len(self.gm) == 0:
return []
means = np.asarray(np.array([ comp._mean for comp in self.gm]))
means = np.squeeze(means)
means = np.squeeze(means)
means = np.reshape(means, (means.size / 2, 2))
if len(means) > 0:
gmm_mask = utils.inside_polygon(means, sss_path)
gmm_fov = list(itertools.compress(self.gm, gmm_mask))
predicted = []
for idx, comp in enumerate(self.gm):
if gmm_mask[idx]:
predicted.append(GmphdComponent(self.survival * comp._weight,
np.dot(self.f, comp._mean), self.q + np.dot(np.dot(self.f, comp._cov), self.f.T)))
else:
predicted.append(self.gm[idx])
return predicted
def auv_update(self, measures, nav_status, sss_path):
'''
Construction of the update components
s = R + H * _Cov * H.T
eta = H * _Mean
K = _Cov * H.T * ( H * _Cov * H.T + R) ^ -1
Pkk = ( I - K * H ) * _Cov
'''
if len(self.gm) == 0:
return
eta = [np.dot(self.h, comp._mean) for comp in self.gm]
s = [self.r + np.dot(np.dot(self.h, comp._cov), self.h.T) for comp in self.gm]
k = []
for index, comp in enumerate(self.gm):
k.append(np.dot(np.dot(comp._cov, self.h.T), np.linalg.inv(s[index])))
pkk = []
for index, comp in enumerate(self.gm):
pkk.append(np.dot(np.eye(np.shape(k[index])[0]) - np.dot(k[index], self.h), comp._cov))
'''
The predicted components are kept with a decay (1 - Pd) iff they are inside the FOV, contained into pr_gm
'''
# Extraction of the coordinates of the components inside the RFS
means = np.asarray(np.array([ comp._mean for comp in self.gm]))
means = np.squeeze(means)
means = np.reshape(means, (means.size / 2, 2))
# Temp copy of the RFS after the prediction step
pr_gm = copy.deepcopy(self.gm)
# Mask of the components inside the FOV
gmm_mask = utils.inside_polygon(means, sss_path)
# print(gmm_mask.shape, pr_gm)
# gmm_fov = list(itertools.compress(self.gmm, gmm_mask))
for idx, comp in enumerate(pr_gm):
if gmm_mask[idx]:
pr_gm[idx]._weight *= (1 - self.detection)
for i in np.ndindex(measures.shape[1]):
z = measures[:, i]
temp_gm = []
for j, comp in enumerate(self.gm):
# Computation of q_k
mvn = multivariate_normal(eta[j].squeeze(), s[j])
mvn_result = mvn.pdf(z.squeeze())
temp_gm.append(GmphdComponent(
self.detection * comp._weight * mvn_result,
comp._mean + np.dot(k[j], z - eta[j]),
comp._cov))
# The Kappa thing (clutter and reweight)
weight_sum = np.sum(comp._weight for comp in temp_gm)
if weight_sum >= 1e-9:
weight_factor = 1.0 / (self.clutter + weight_sum)
for comp in temp_gm:
comp._weight *= weight_factor
pr_gm.extend(temp_gm)
self.gm = pr_gm
def run_iteration(self, measures, born_components):
# Prediction for birthed targets
print('Measures: ')
print(measures)
pr_born = self.predict_birth(born_components)
# Prediction for existing targets
predicted = self.predict_existing()
predicted.extend(pr_born)
print('Predicted components:'.format(predicted))
# Update
self.update(measures, predicted)
print('Updated components:'.format(self.gm))
# Prune
self.prune()
print('Pruning: '.format(self.gm))
def run_auv_iteration(self, measures, born_components, nav_status, swath_w, swath_l):
# pr_born = self.predict_birth(born_components)
sss_path = utils.evaluate_sss_path(nav_status, swath_w, swath_l)
predicted = self.predict_existing(nav_status, sss_path)
predicted.extend(born_components)
self.gm = predicted
self.auv_update(measures, nav_status, sss_path)
self.prune()
def prune(self, truncation_thresh=1e-6, merge_thresh=0.01, max_components=100):
temp_sum_0 = np.sum([i._weight for i in self.gm])
# Truncation step
I = filter(lambda comp: comp._weight > truncation_thresh, self.gm)
l = 0 # count the number of features/components
pruned_gm = []
# Merge step
while len(I) > 0:
l += 1
j = np.argmax(i._weight for i in I)
L = []
indexes = []
# Loop for compute the Mahalanobis distance among the components survived the trunctation step
for index, i in enumerate(I):
temp = np.dot((i._mean - I[j]._mean).T, np.linalg.inv(i._cov))
mah_dist = np.float64(np.dot(temp, (i._mean - I[j]._mean)))
if mah_dist <= merge_thresh:
L.append(i)
indexes.append(index)
# L contains the components with Mahalanobis distance greater than merge_thresh
# between component j and the others
temp_weight = np.sum([i._weight for i in L])
temp_mean = (1.0 / temp_weight) * np.sum([i._weight * i._mean for i in L], axis=0)
temp_cov = np.zeros((temp_mean.size, temp_mean.size))
for i in L:
# print 'TM', temp_mean.tolist()
# print i._mean.tolist()
temp_cov += (i._cov + np.dot((temp_mean - i._mean).T, (temp_mean - i._mean)))
pruned_gm.append(GmphdComponent(temp_weight, temp_mean, temp_cov))
I = [i for j, i in enumerate(I) if j not in indexes]
pruned_gm.sort(key=attrgetter('_weight'))
pruned_gm.reverse()
pruned_gm = pruned_gm[:max_components]
# temp_sum_1 = np.sum(i._weight for i in pruned_gm)
# for i in pruned_gm:
# i._weight *= temp_sum_0 / temp_sum_1
self.gm = pruned_gm
def create_birth(measures):
sigma_r = 2.0/3
R = [[math.pow(2*sigma_r, 2), 0], [0, math.pow(2*sigma_r, 2)]]
it = np.nditer(measures.shape[1])
born = []
for i in np.ndindex(measures.shape[1]):
born.append(GmphdComponent(GMPHD.birth_w, measures[:,i], R))
return born