-
Notifications
You must be signed in to change notification settings - Fork 10
/
examples.py
402 lines (338 loc) · 15 KB
/
examples.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
import matplotlib.pyplot as plt
import time
from gmphd import *
def process_model_for_example_1():
# This is the model for the example in "Bayesian Multiple Target Filtering Using Random Finite Sets" by Vo, Vo, Clark
# The implementation almost analog to Matlab code provided by Vo in http://ba-tuong.vo-au.com/codes.html
model = {}
# Sampling time, time step duration
T_s = 1.
model['T_s'] = T_s
# number of scans, number of iterations in our simulation
model['num_scans'] = 100
# Surveillance region
x_min = -1000
x_max = 1000
y_min = -1000
y_max = 1000
model['surveillance_region'] = np.array([[x_min, x_max], [y_min, y_max]])
# TRANSITION MODEL
# Probability of survival
model['p_s'] = 0.99
# Transition matrix
I_2 = np.eye(2)
# F = [[I_2, T_s*I_2], [02, I_2]
F = np.zeros((4, 4))
F[0:2, 0:2] = I_2
F[0:2, 2:] = I_2 * T_s
F[2:, 2:] = I_2
model['F'] = F
# Process noise covariance matrix
Q = np.zeros((4, 4))
Q[0:2, 0:2] = (T_s ** 4) / 4 * I_2
Q[0:2, 2:] = (T_s ** 3) / 2 * I_2
Q[2:, 0:2] = (T_s ** 3) / 2 * I_2
Q[2:, 2:] = (T_s ** 2) * I_2
# standard deviation of the process noise
sigma_w = 5.
Q = Q * (sigma_w ** 2)
model['Q'] = Q
# Parameters for the spawning model: beta(x|ksi) = sum(w[i]*Normal(x,F_spawn[i]*ksi+d_spawn[i],Q_spawn[i]))
model['F_spawn'] = []
model['d_spawn'] = []
model['Q_spawn'] = []
model['w_spawn'] = []
# Parameters of the new born targets Gaussian mixture
w = [0.03] * 4
m = [np.array([0., 0., 0., 0.]), np.array([400., -600., 0., 0.]), np.array([-800., -200., 0., 0.]),
np.array([-200., 800., 0., 0.])]
P_pom_ = np.diag([100., 100., 100., 100.])
P = [P_pom_.copy(), P_pom_.copy(), P_pom_.copy(), P_pom_.copy()]
model['birth_GM'] = GaussianMixture(w, m, P)
# MEASUREMENT MODEL
# probability of detection
model['p_d'] = 0.98
# measurement matrix z = Hx + v = N(z; Hx, R)
model['H'] = np.zeros((2, 4))
model['H'][:, 0:2] = np.eye(2)
# measurement noise covariance matrix
sigma_v = 10 # m
model['R'] = I_2 * (sigma_v ** 2)
# the reference to clutter intensity function
model['lc'] = 50
model['clutt_int_fun'] = lambda z: clutter_intensity_function(z, model['lc'], model['surveillance_region'])
# pruning and merging parameters:
model['T'] = 1e-5
model['U'] = 4.
model['Jmax'] = 100
return model
def process_model_for_example_2():
"""
This is the model of the process for the example in "Bayesian Multiple Target Filtering Using Random Finite Sets" by
Vo, Vo, Clark. The model code is analog to Matlab code provided by
Vo in http://ba-tuong.vo-au.com/codes.html
:returns
- model: dictionary containing the necessary parameters, read through code to understand it better
"""
model = {}
# Sampling time, time step duration
T_s = 1.
model['T_s'] = T_s
# number of scans, number of iterations in our simulation
model['num_scans'] = 100
# Surveillance region
x_min = -1000
x_max = 1000
y_min = -1000
y_max = 1000
model['surveillance_region'] = np.array([[x_min, x_max], [y_min, y_max]])
# TRANSITION MODEL
# Probability of survival
model['p_s'] = 0.99
# Transition matrix
I_2 = np.eye(2)
# F = [[I_2, T_s*I_2], [02, I_2]
F = np.zeros((4, 4))
F[0:2, 0:2] = I_2
F[0:2, 2:] = I_2 * T_s
F[2:, 2:] = I_2
model['F'] = F
# Process noise covariance matrix
Q = np.zeros((4, 4))
Q[0:2, 0:2] = (T_s ** 4) / 4 * I_2
Q[0:2, 2:] = (T_s ** 3) / 2 * I_2
Q[2:, 0:2] = (T_s ** 3) / 2 * I_2
Q[2:, 2:] = (T_s ** 2) * I_2
# standard deviation of the process noise
sigma_w = 5.
Q = Q * (sigma_w ** 2)
model['Q'] = Q
# Parameters for the spawning model: beta(x|ksi) = sum(w[i]*Normal(x,F_spawn[i]*ksi+d_spawn[i],Q_spawn[i]))
model['w_spawn'] = [0.05]
model['F_spawn'] = [np.eye(4)]
model['d_spawn'] = [0.0]
Q_spawn = np.eye(4) * 100
Q_spawn[[2, 3], [2, 3]] = 400
model['Q_spawn'] = [Q_spawn]
# Parameters of the new born targets Gaussian mixture
w = [0.1, 0.1]
m = [np.array([250., 250., 0., 0.]), np.array([-250., -250., 0., 0.])]
P = [np.diag([100., 100., 25., 25.]), np.diag([100., 100., 25., 25.])]
model['birth_GM'] = GaussianMixture(w, m, P)
# MEASUREMENT MODEL
# probability of detection
model['p_d'] = 0.98
# measurement matrix z = Hx + v = N(z; Hx, R)
model['H'] = np.zeros((2, 4))
model['H'][:, 0:2] = np.eye(2)
# measurement noise covariance matrix
sigma_v = 10 # m
model['R'] = I_2 * (sigma_v ** 2)
# the reference to clutter intensity function
model['lc'] = 50
model['clutt_int_fun'] = lambda z: clutter_intensity_function(z, model['lc'], model['surveillance_region'])
# pruning and merging parameters:
model['T'] = 1e-5
model['U'] = 4.
model['Jmax'] = 100
return model
def extract_positions_of_targets(X_collection):
X_pos = []
for X_set in X_collection:
x = []
for state in X_set:
x.append(state[0:2])
X_pos.append(x)
return X_pos
def example1(num_of_scans=100):
targets_birth_time = [1, 1, 1, 20, 20, 20, 40, 40, 60, 60, 80, 80]
targets_birth_time = (np.array(targets_birth_time) - 1).tolist()
targets_death_time = [70, num_of_scans, 70, num_of_scans, num_of_scans, num_of_scans,
num_of_scans, num_of_scans, num_of_scans, num_of_scans, num_of_scans,
num_of_scans]
targets_start = [np.array([0., 0., 0., -10.]),
np.array([400., -600., -10., 5.]),
np.array([-800., -200., 20., -5.]),
np.array([400., -600., -7., -4.]),
np.array([400., -600., -2.5, 10.]),
np.array([0., 0., 7.5, -5.]),
np.array([-800., -200., 12., 7.]),
np.array([-200., 800., 15., -10.]),
np.array([-800., -200., 3., 15.]),
np.array([-200., 800., -3., -15.]),
np.array([0., 0., -20., -15.]),
np.array([-200., 800., 15., -5.])]
return targets_birth_time, targets_death_time, targets_start
def example2(num_of_scans=100):
targets_birth_time = [1, 1]
targets_birth_time = (np.array(targets_birth_time) - 1).tolist()
targets_death_time = [num_of_scans, num_of_scans]
targets_start = [np.array([250., 250., 2.5, -11.5]),
np.array([-250., -250., 11.5, -2.5])]
# for spawning targets, there is birth time, death time, initial velocity and target from which it spawns
targets_spw_time_brttgt_vel = [(66, num_of_scans, np.array([-20., 4.]), 0)]
return targets_birth_time, targets_death_time, targets_start, targets_spw_time_brttgt_vel
def generate_trajectories(model, targets_birth_time, targets_death_time, targets_start, targets_spw_time_brttgt_vel=[],
noise=False):
num_of_scans = model['num_scans']
trajectories = []
for i in range(num_of_scans):
trajectories.append([])
targets_tracks = {}
for i, start in enumerate(targets_start):
target_state = start
targets_tracks[i] = []
for k in range(targets_birth_time[i], min(targets_death_time[i], num_of_scans)):
target_state = model['F'] @ target_state
if noise:
target_state += np.random.multivariate_normal(np.zeros(target_state.size), model['Q'])
if target_state[0] < model['surveillance_region'][0][0] or target_state[0] > \
model['surveillance_region'][0][1] or target_state[1] < model['surveillance_region'][1][0] or \
target_state[1] > model['surveillance_region'][1][1]:
targets_death_time[i] = k - 1
break
trajectories[k].append(target_state)
targets_tracks[i].append(target_state)
# next part is only for spawning targets. In examples, this part is often omitted.
for i, item in enumerate(targets_spw_time_brttgt_vel):
(target_birth_time, target_death_time, velocity, parent) = item
target_state = np.zeros(4)
if target_birth_time - targets_birth_time[parent] < 0 or target_death_time - targets_death_time[parent] > 0:
continue
target_state[0:2] = targets_tracks[parent][target_birth_time - targets_birth_time[parent]][0:2]
target_state[2:] = velocity
targets_birth_time.append(target_birth_time)
targets_death_time.append(target_death_time)
targets_start.append(target_state)
# trajectories.append([])
targets_tracks[len(targets_birth_time) - 1] = []
for k in range(target_birth_time, min(target_death_time, num_of_scans)):
target_state = model['F'] @ target_state
if noise:
target_state += np.random.multivariate_normal(np.zeros(target_state.size), model['Q'])
if target_state[0] < model['surveillance_region'][0][0] or target_state[0] > \
model['surveillance_region'][0][1] or target_state[1] < model['surveillance_region'][1][0] or \
target_state[1] > model['surveillance_region'][1][1]:
targets_death_time[-1] = k - 1
break
trajectories[k].append(target_state)
targets_tracks[len(targets_birth_time) - 1].append(target_state)
return trajectories, targets_tracks
def generate_measurements(model, trajectories):
data = []
surveillanceRegion = model['surveillance_region']
for X in trajectories:
m = []
for state in X:
if np.random.rand() <= model['p_d']:
meas = model['H'] @ state + np.random.multivariate_normal(np.zeros(model['H'].shape[0]), model['R'])
m.append(meas)
for i in range(np.random.poisson(model['lc'])):
x = (surveillanceRegion[0][1] - surveillanceRegion[0][0]) * np.random.rand() + surveillanceRegion[0][0]
y = (surveillanceRegion[1][1] - surveillanceRegion[1][0]) * np.random.rand() + surveillanceRegion[1][0]
m.append(np.array([x, y]))
data.append(m)
return data
def true_trajectory_tracks_plots(targets_birth_time, targets_tracks, delta):
for_plot = {}
for i, birth in enumerate(targets_birth_time):
brojac = birth
x = []
y = []
time = []
for state in targets_tracks[i]:
x.append(state[0])
y.append(state[1])
time.append(brojac)
brojac += delta
for_plot[i] = (time, x, y)
return for_plot
def extract_axis_for_plot(X_collection, delta):
time = []
x = []
y = []
k = 0
for X in X_collection:
for state in X:
x.append(state[0])
y.append(state[1])
time.append(k)
k += delta
return time, x, y
if __name__ == '__main__':
# For example 1, uncomment the following code.
# =================================================Example 1========================================================
model = process_model_for_example_1()
targets_birth_time, targets_death_time, targets_start = example1(model['num_scans'])
trajectories, targets_tracks = generate_trajectories(model, targets_birth_time, targets_death_time, targets_start,
noise=False)
# ==================================================================================================================
# For example 2, uncomment the following code.
# =================================================Example 2========================================================
# model = process_model_for_example_2()
# targets_birth_time, targets_death_time, targets_start, targets_spw_time_brttgt_vel = example2(model['num_scans'])
# trajectories, targets_tracks = generate_trajectories(model, targets_birth_time, targets_death_time, targets_start,
# targets_spw_time_brttgt_vel, noise=False)
# ==================================================================================================================
# Collections of observations for each time step
data = generate_measurements(model, trajectories)
# Call of the gmphd filter for the created observations collections
gmphd = GmphdFilter(model)
a = time.time()
X_collection = gmphd.filter_data(data)
print('Filtration time: ' + str(time.time() - a) + ' sec')
# Plot the results of filtration saved in X_collection file
tracks_plot = true_trajectory_tracks_plots(targets_birth_time, targets_tracks, model['T_s'])
plt.figure()
for key in tracks_plot:
t, x, y = tracks_plot[key]
plt.plot(x[0], y[0], 'o', c='k', mfc='none')
plt.plot(x[-1], y[-1], 's', c='k', mfc='none')
plt.plot(x, y)
plt.axis(model['surveillance_region'].flatten())
plt.gca().set_aspect('equal', adjustable='box')
plt.xlabel('x')
plt.ylabel('y')
plt.title(r"Targets movement in surveilance region. Circle represents the starting point and"
r" square represents the end point.", loc='center', wrap=True)
# Plot measurements, true trajectories and estimations
meas_time, meas_x, meas_y = extract_axis_for_plot(data, model['T_s'])
estim_time, estim_x, estim_y = extract_axis_for_plot(X_collection, model['T_s'])
plt.figure()
plt.plot(meas_time, meas_x, 'x', c='C0')
for key in tracks_plot:
t, x, y = tracks_plot[key]
plt.plot(t, x, 'r')
plt.plot(estim_time, estim_x, 'o', c='k', markersize=3)
plt.xlabel('time[$sec$]')
plt.ylabel('x')
plt.title('X axis in time. Blue x are measurements(50 in each time step), '
'black dots are estimations and the red lines are actual trajectories of targets', loc='center',
wrap=True)
plt.figure()
plt.plot(meas_time, meas_y, 'x', c='C0')
for key in tracks_plot:
t, x, y = tracks_plot[key]
plt.plot(t, y, 'r')
plt.plot(estim_time, estim_y, 'o', c='k', markersize=3)
plt.xlabel('time[$sec$]')
plt.ylabel('y')
plt.title('Y axis in time. Blue x are measurements(50 in each time step), '
'black dots are estimations and the red lines are actual trajectories of targets', loc='center',
wrap=True)
num_targets_truth = []
num_targets_estimated = []
for x_set in trajectories:
num_targets_truth.append(len(x_set))
for x_set in X_collection:
num_targets_estimated.append(len(x_set))
plt.figure()
(markerline, stemlines, baseline) = plt.stem(num_targets_estimated, label='estimated number of targets')
plt.setp(baseline, color='k') # visible=False)
plt.setp(stemlines, visible=False) # visible=False)
plt.setp(markerline, markersize=3.0)
plt.step(num_targets_truth, 'r', label='actual number of targets')
plt.xlabel('time[$sec$]')
plt.legend()
plt.title('Estimated cardinality VS actual cardinality', loc='center', wrap=True)
plt.show()