-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
371 lines (288 loc) · 14.5 KB
/
visualization.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
import pickle
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import multivariate_normal
import string
from matplotlib.patches import Ellipse
from utils.helper import cov_mat_generation, getCoef
import torch
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, mark_inset
def get_cmap(n, name='hsv'):
'''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name.'''
return plt.cm.get_cmap(name, n)
def plot_trajecotries(true_trajectories,pred_trajectories,obs_len,batch,
dist_param_seq,PedsList_seq,lookup,true_trajectories_veh=None,
VehsList_seq=None,lookup_seq_veh=None, grid_seq=None,
grid_seq_veh=None, is_train=False, frame_i=5):
num_ped = pred_trajectories.shape[1]
seq_len = pred_trajectories.shape[0]
if true_trajectories_veh is not None:
num_veh = true_trajectories_veh.shape[1]
else:
num_veh = 0
fig, ax = plt.subplots()
plt.ion()
ax.set_xlabel("x (m)", fontsize=12)
ax.set_ylabel("y (m)", fontsize=12)
ax.tick_params(axis='x', labelsize=12)
ax.tick_params(axis='y', labelsize=12)
min_x = pred_trajectories[:,:,0].min() - 10
max_x = pred_trajectories[:,:,0].max() + 10
min_y = pred_trajectories[:,:,1].min() - 10
max_y = pred_trajectories[:,:,1].max() + 10
# # scenario 500
# min_x = 55
# max_x = 78
# min_y = 35
# max_y = 55
# # scenario 621
# min_x = 48
# max_x = 78
# min_y = 25
# max_y = 55
# # scenario 621 (zoomed on the stationary ped)
# min_x = 57
# max_x = 63
# min_y = 43
# max_y = 48
# # scenario 730
# min_x = 42
# max_x = 62
# min_y = 25
# max_y = 45
# scenario 874
min_x = 43
max_x = 66
min_y = 27 # 26
max_y = 50 # 49
# # scenario 871
# min_x = 42.5
# max_x = 67.5
# min_y = 26 # 26
# max_y = 53 # 49
ax.set_xlim(min_x, max_x)
ax.set_ylim(min_y, max_y)
mux, muy, sx, sy, corr = getCoef(dist_param_seq)
scaled_param_dist = torch.stack((mux, muy, sx, sy, corr), 2)
cov_matrix = cov_mat_generation(scaled_param_dist)
for agent_index in range(num_ped): # for each agent plotting its trajecotry for the frames the agent is present
# finding the id of the agent usign the lookup dictionary,
# this is the opposite of what we were usally doing (geting the index from the agent_id in ped_list_seq)
id_list = list(lookup.keys())
index_list = list(lookup.values())
position = index_list.index(agent_index)
agent_id = id_list[position]
# Plotting the observation part
# frames that the agent is present
obs_frame_nums = []
for frame in range(0,obs_len):
if agent_id in PedsList_seq[frame]:
obs_frame_nums.append(frame)
pred_frame_nums = []
for frame in range(obs_len,seq_len):
if agent_id in PedsList_seq[frame]:
pred_frame_nums.append(frame)
### Ground truth:
alpha_val = 1.0 # transparancy value
# observed traj
ax.plot(true_trajectories[obs_frame_nums,agent_index,0], true_trajectories[obs_frame_nums,agent_index,1],
c='g', linewidth=2.0, alpha=alpha_val, ls='--')
# ground truth predcition
ax.plot(true_trajectories[pred_frame_nums,agent_index,0], true_trajectories[pred_frame_nums,agent_index,1],
c='0.0', linewidth=2.0, alpha=alpha_val, ls='-')
### Predictions
max_size = 6
min_size = 1
# prediction of CollisionGrid
for f in pred_frame_nums:
marker_size = min_size + ((max_size-min_size)/seq_len * f)
# plot also the current positions 1 sigma confidence interval
mean = dist_param_seq[f,agent_index,:2]
cov = cov_matrix[f,agent_index]
plot_bivariate_gaussian3(mean, cov, ax, 1)
ax.plot(pred_trajectories[pred_frame_nums,agent_index,0], pred_trajectories[pred_frame_nums,agent_index,1],
c='r', linestyle = (0,(1,0.7)), linewidth=2)
zoomed_ped_id = 3
if agent_index == zoomed_ped_id:
# ============ zoomed in plot ============
# Define the inset axis position
ax_inset = inset_axes(ax, width='35%', height='35%', loc='upper left')
# Plot the zoomed-in data
for f in pred_frame_nums:
marker_size = min_size + ((max_size-min_size)/seq_len * f)
mean = dist_param_seq[f,agent_index,:2]
cov = cov_matrix[f,agent_index]
plot_bivariate_gaussian3(mean, cov, ax_inset, 1)
ax_inset.plot(pred_trajectories[pred_frame_nums,agent_index,0],
pred_trajectories[pred_frame_nums,agent_index,1],
c='r', linestyle = (0,(1,0.7)), linewidth=2.5,
label='Zoomed In')
# observed traj
ax_inset.plot(true_trajectories[obs_frame_nums,agent_index,0],
true_trajectories[obs_frame_nums,agent_index,1],
c='g', linewidth=2.0, alpha=alpha_val, ls='--')
# ground truth predcition
ax_inset.plot(true_trajectories[pred_frame_nums,agent_index,0],
true_trajectories[pred_frame_nums,agent_index,1],
c='0.0', linewidth=2.0, alpha=alpha_val, ls='-')
# current position
ax_inset.plot(true_trajectories[frame_i, agent_index,0],
true_trajectories[frame_i, agent_index,1],
color='k', marker="*", markersize=6)
# sceanrio 874
ax_inset.set_xlim(50.5, 51.8) # Set x-axis range
ax_inset.set_ylim(37.7, 39) # Set y-axis range
# # sceanrio 871
# ax_inset.set_xlim(50.5, 51.5) # Set x-axis range
# ax_inset.set_ylim(38, 39) # Set y-axis range
# # # sceanrio 871 whole cov
# # ax_inset.set_xlim(50, 52.5) # Set x-axis range
# # ax_inset.set_ylim(36, 40.5) # Set y-axis range
# Remove the axes labels in the zoomed-in plot
ax_inset.set_xticks([])
ax_inset.set_yticks([])
ax_inset.set_xlabel('')
ax_inset.set_ylabel('')
# Mark the area of the inset in the main plot
mark_inset(ax, ax_inset, loc1=3, loc2=1, fc="none", ec="0.5")
max_size_veh = 6
min_size_veh = 2
for veh_ind in range(num_veh):
id_list_veh = list(lookup_seq_veh.keys())
index_list_veh = list(lookup_seq_veh.values())
position_veh = index_list_veh.index(veh_ind)
veh_id = id_list_veh[position_veh]
pres_frame_nums = []
for frame in range(seq_len):
if veh_id in VehsList_seq[frame]:
pres_frame_nums.append(frame)
marker_size = min_size_veh + ((max_size_veh-min_size_veh)/seq_len * frame)
ax.plot(true_trajectories_veh[frame,veh_ind,0], true_trajectories_veh[frame,veh_ind,1],
c='0.5', marker='o', markersize=marker_size)
ax.plot(true_trajectories_veh[pres_frame_nums,veh_ind,0], true_trajectories_veh[pres_frame_nums,veh_ind,1],
c='0.5', linewidth=2.0)
# ===================================================================
# ============================= Neigbors ============================
# ===================================================================
alphabet = list(string.ascii_uppercase)
label_font_size = 12
if grid_seq != None:
ego_agent_indx_in_pedlist = 0
for indx, nodes_pre in enumerate(PedsList_seq[frame_i]):
agent_i = lookup[nodes_pre]
label = "Ped " + alphabet[indx]
# ax.plot(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
# color='k', marker="*", markersize=9)
# ax.text(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
# label, fontsize = label_font_size, color ="k")
if agent_i < 7:
if indx in [1]:
ax.plot(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
color='k', marker="*", markersize=9)
ax.text(true_trajectories[frame_i, agent_i,0]+0.5, true_trajectories[frame_i, agent_i,1]+0.7,
label, fontsize = label_font_size, color ="k")
elif indx in [2]:
ax.plot(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
color='k', marker="*", markersize=9)
ax.text(true_trajectories[frame_i, agent_i,0]+0.4, true_trajectories[frame_i, agent_i,1]+0.4,
label, fontsize = label_font_size, color ="k")
elif indx in [3]:
ax.plot(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
color='k', marker="*", markersize=9)
ax.text(true_trajectories[frame_i, agent_i,0]-0.4, true_trajectories[frame_i, agent_i,1]+1,
label, fontsize = label_font_size, color ="k")
elif indx in [4]:
ax.plot(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
color='k', marker="*", markersize=9)
ax.text(true_trajectories[frame_i, agent_i,0]+0.5, true_trajectories[frame_i, agent_i,1],
label, fontsize = label_font_size, color ="k")
elif indx in [5]:
ax.plot(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
color='k', marker="*", markersize=9)
ax.text(true_trajectories[frame_i, agent_i,0]-1.4, true_trajectories[frame_i, agent_i,1]+0.5,
label, fontsize = label_font_size, color ="k")
else:
ax.plot(true_trajectories[frame_i, agent_i,0], true_trajectories[frame_i, agent_i,1],
color='k', marker="*", markersize=9)
ax.text(true_trajectories[frame_i, agent_i,0]-0.5, true_trajectories[frame_i, agent_i,1]+0.7,
label, fontsize = label_font_size, color ="k")
for indx, nodes_pre in enumerate(VehsList_seq[frame_i]):
label_veh = "Veh " + alphabet[indx]
agent_i = lookup_seq_veh[nodes_pre]
ax.plot(true_trajectories_veh[frame_i, agent_i,0], true_trajectories_veh[frame_i, agent_i,1],
color='0.3', marker="*", markersize=11)
ax.text(true_trajectories_veh[frame_i, agent_i,0]-0.5, true_trajectories_veh[frame_i, agent_i,1]-1.5,
label_veh, fontsize = label_font_size, color ="k")
# legends
ax.plot(-100,-100, c='b', ls='-', label='$1\sigma$ std. pred')
ax.plot(-100,-100, c='r', ls=':', label='UAW-PCG mean pred.')
# ax.plot(-100,-100, c='r', ls=':', label='PCG mean pred.')
ax.plot(-100,-100, c='k', ls='-', label='Ground truth')
ax.plot(-100,-100, c='g', ls='--', label='Observed traj.')
ax.legend(loc="lower right", prop={'size': 12}, ncol=2)
if is_train:
plt.savefig("Store_Results/plot/train/plt/%d.png"%batch, dpi=200)
else:
plt.savefig("Store_Results/plot/test/plt/%d.png"%batch, dpi=200)
plt.close()
def plot_bivariate_gaussian3(mean, cov, ax, max_nstd=3, c='b'):
def eigsorted(cov):
vals, vecs = np.linalg.eigh(cov)
order = vals.argsort()[::-1]
return vals[order], vecs[:,order]
vals, vecs = eigsorted(cov)
theta = np.degrees(np.arctan2(*vecs[:,0][::-1]))
for j in range(1, max_nstd+1):
# Width and height are "full" widths, not radius
width, height = 2 * j * np.sqrt(vals)
ellip = Ellipse(xy=mean, width=width, height=height, angle=theta, edgecolor=c, fill=False,
linewidth=1.0)
ax.add_artist(ellip)
return ellip
def Loss_Plot(train_batch_num, error_batch, loss_batch, file_name, x_axis_label,
NLL_loss_batch=None, uncertainty_loss_batch=None):
plt.subplot(2,1,1)
plt.plot(train_batch_num, error_batch, 'b', linewidth=2.0, label="error")
plt.ylabel("error")
plt.subplot(2,1,2)
plt.plot(train_batch_num, loss_batch, 'k', linewidth=2.0, label="combination_loss")
if NLL_loss_batch is not None:
plt.plot(train_batch_num, NLL_loss_batch, 'g', linewidth=2.0, label="NLL_loss")
if uncertainty_loss_batch is not None:
plt.plot(train_batch_num, uncertainty_loss_batch, 'r', linewidth=2.0, label="uncertainty_loss")
plt.xlabel(x_axis_label)
plt.ylabel("loss")
plt.legend()
plt.savefig("Store_Results/plot/train/"+file_name+".png")
plt.close()
def main():
file_path_PCG = "Store_Results/plot/test/PCG/test_results.pkl"
file_path_UAWPCG = "Store_Results/plot/test/UAWPCG/test_results.pkl"
file_path = file_path_UAWPCG # file_path_PCG OR file_path_UAWPCG
try:
f = open(file_path, 'rb')
except FileNotFoundError:
print("File not found: %s"%file_path)
results = pickle.load(f)
print("====== The total number of data in the test set is: " + str(len(results)) + ' ========')
for i in [874]: # range(0, len(results), 10): # plotting the samples in the test set
results_i = results[i]
true_trajectories = results_i[0]
pred_trajectories = results_i[1]
PedsList_seq = results_i[2]
lookup_seq = results_i[3]
obs_length = results_i[5]
dist_param_seq = results_i[6]
true_trajectories_veh = results_i[7]
VehsList_seq = results_i[8]
lookup_seq_veh = results_i[9]
grid_seq = results_i[10]
grid_seq_veh = results_i[11]
# covnert dist_param_seq to a torch tensor
dist_param_seq = torch.from_numpy(dist_param_seq)
plot_trajecotries(true_trajectories, pred_trajectories, obs_length,i,
dist_param_seq, PedsList_seq, lookup_seq, true_trajectories_veh,
VehsList_seq, lookup_seq_veh, grid_seq, grid_seq_veh)
if __name__ == '__main__':
main()