-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
374 lines (324 loc) · 11.9 KB
/
evaluation.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
from math import sqrt
from model import (
get_gt_rmis,
get_rmi_batch,
get_rmis,
)
from training import valid_loop
from utils import unflatten_pose, flatten_pose
from losses import pose_loss
import torch
from torch.utils.data import DataLoader
from pylie.torch import SO3
from dataset import RmiDataset
def imu_dead_reckoning(filename):
g_a = torch.Tensor([0, 0, -9.80665]).reshape((-1, 1))
dataset = RmiDataset(filename, window_size=2, stride=1, use_cache=False)
_, _, poses = dataset.get_item_with_poses(0)
r, v, C = unflatten_pose(poses[0])
g = torch.Tensor([0, 0, -9.80665]).view((-1, 1))
t_data = [torch.Tensor([0.0])]
r_data = [r]
v_data = [v]
C_data = [C]
for x, _ in dataset:
t = x[0, :]
w = x[1:4, 0].reshape((-1, 1))
a = x[4:, 0].reshape((-1, 1))
dt = t[1] - t[0]
r = r + v * dt + 0.5 * g * (dt ** 2) + 0.5 * C @ a * (dt ** 2)
v = v + g * dt + C @ a * dt
C = C @ SO3.Exp(dt * w).squeeze()
t_data.append(t[1])
r_data.append(r)
v_data.append(v)
C_data.append(C)
t_data = torch.hstack(t_data)
r_data = torch.hstack(r_data)
v_data = torch.hstack(v_data)
C_data = torch.stack(C_data, 0)
return {"t": t_data, "r": r_data, "v": v_data, "C": C_data}
def rminet_test(net, filename):
g_a = torch.Tensor([0, 0, -9.80665]).reshape((-1, 1))
N = net._window_size
dataset = RmiDataset(filename, window_size=N, stride=N - 1)
with torch.no_grad():
net.eval()
_, _, poses_gt = dataset.get_item_with_poses(0)
r_i, v_i, C_i = unflatten_pose(poses_gt[0])
t_data = [torch.Tensor([0.0])]
r_data = [r_i]
v_data = [v_i]
C_data = [C_i]
r_gt_data = [r_i]
v_gt_data = [v_i]
C_gt_data = [C_i]
for i in range(len(dataset)):
imu_window, rmis_gt, poses_gt = dataset.get_item_with_poses(i)
# Get RMIs from neural network
imu_window = imu_window.unsqueeze(0)
rmis = net(imu_window[:, 1:, :])
DR, DV, DC = unflatten_pose(rmis[0, :])
t_i = imu_window[0, 0, 0]
t_j = imu_window[0, 0, -1]
DT = t_j - t_i
# ground truth poses at endpoints
r_gt_i, v_gt_i, C_gt_i = unflatten_pose(poses_gt[0])
r_gt_j, v_gt_j, C_gt_j = unflatten_pose(poses_gt[1])
r_gt_data.append(r_gt_j)
v_gt_data.append(v_gt_j)
C_gt_data.append(C_gt_j)
# Get RMIs from ground truth poses
DR_gt, DV_gt, DC_gt = get_gt_rmis(
r_gt_i, v_gt_i, C_gt_i, r_gt_j, v_gt_j, C_gt_j, DT
)
# Get RMIs from measurements
DR_dr, DV_dr, DC_dr = unflatten_pose(get_rmis(imu_window[0, :, :]))
# Use RMIs to predict motion forward
C_j = C_i @ DC
v_j = v_i + g_a * DT + C_i @ DV
r_j = r_i + v_i * DT + 0.5 * g_a * (DT ** 2) + C_i @ DR
r_i = r_j
v_i = v_j
C_i = C_j
t_data.append(t_j)
r_data.append(r_j)
v_data.append(v_j)
C_data.append(C_j)
t_data = torch.hstack(t_data)
r_data = torch.hstack(r_data)
v_data = torch.hstack(v_data)
r_gt_data = torch.hstack(r_gt_data)
v_gt_data = torch.hstack(v_gt_data)
return {
"t": t_data,
"r": r_data,
"v": v_data,
"C": C_data,
"r_gt": r_gt_data,
"v_gt": v_gt_data,
"C_gt": C_gt_data,
}
def drminet_test(net, filename):
g_a = torch.Tensor([0, 0, -9.80665]).reshape((-1, 1))
N = net._window_size
dataset = RmiDataset(filename, window_size=N, stride=N - 1, with_model=True)
with torch.no_grad():
net.eval()
_, _, poses_gt = dataset.get_item_with_poses(0)
r_i, v_i, C_i = unflatten_pose(poses_gt[0])
t_data = [torch.Tensor([0.0])]
r_data = [r_i]
v_data = [v_i]
C_data = [C_i]
r_gt_data = [r_i]
v_gt_data = [v_i]
C_gt_data = [C_i]
for i in range(len(dataset)):
imu_window, rmis, poses_gt = dataset.get_item_with_poses(i)
# Get RMIs from neural network
imu_window = imu_window.unsqueeze(0)
delta = net(imu_window)
# print(delta_rmi_loss(delta, rmis.unsqueeze(0)))
t_i = imu_window[0, 0, 0]
t_j = imu_window[0, 0, -1]
DT = t_j - t_i
DR_meas, DV_meas, DC_meas = unflatten_pose(rmis[15:])
dphi = delta[0, 0:3].reshape((-1, 1))
dv = delta[0, 3:6].reshape((-1, 1))
dr = delta[0, 6:9].reshape((-1, 1))
DC = torch.matmul(DC_meas, SO3.Exp(dphi).squeeze())
DV = DV_meas + dv
DR = DR_meas + dr
# ground truth poses at endpoints
r_gt_i, v_gt_i, C_gt_i = unflatten_pose(poses_gt[0])
r_gt_j, v_gt_j, C_gt_j = unflatten_pose(poses_gt[1])
r_gt_data.append(r_gt_j)
v_gt_data.append(v_gt_j)
C_gt_data.append(C_gt_j)
# Get RMIs from ground truth poses
DR_gt, DV_gt, DC_gt = get_gt_rmis(
r_gt_i, v_gt_i, C_gt_i, r_gt_j, v_gt_j, C_gt_j, DT
)
# Use RMIs to predict motion forward
C_j = C_i @ DC
v_j = v_i + g_a * DT + C_i @ DV
r_j = r_i + v_i * DT + 0.5 * g_a * (DT ** 2) + C_i @ DR
r_i = r_j
v_i = v_j
C_i = C_j
t_data.append(t_j)
r_data.append(r_j)
v_data.append(v_j)
C_data.append(C_j)
net_loss = pose_loss(
flatten_pose(DR, DV, DC).unsqueeze(0),
flatten_pose(DR_gt, DV_gt, DC_gt).unsqueeze(0),
)
model_loss = pose_loss(
flatten_pose(DR_meas, DV_meas, DC_meas).unsqueeze(0),
flatten_pose(DR_gt, DV_gt, DC_gt).unsqueeze(0),
)
print("Net Loss: %.6f, Model Loss: %.6f" % (net_loss, model_loss))
t_data = torch.hstack(t_data)
r_data = torch.hstack(r_data)
v_data = torch.hstack(v_data)
r_gt_data = torch.hstack(r_gt_data)
v_gt_data = torch.hstack(v_gt_data)
return {
"t": t_data,
"r": r_data,
"v": v_data,
"C": C_data,
"r_gt": r_gt_data,
"v_gt": v_gt_data,
"C_gt": C_gt_data,
}
def seperated_nets_test(trans_net, rot_net, filename):
g_a = torch.Tensor([0, 0, -9.80665]).reshape((-1, 1))
N = trans_net._window_size
dataset = RmiDataset(
filename, window_size=N, stride=N - 1, with_model=True, use_cache=False
)
with torch.no_grad():
trans_net.eval()
rot_net.eval()
_, _, poses_gt = dataset.get_item_with_poses(0)
r_i, v_i, C_i = unflatten_pose(poses_gt[0])
t_data = [torch.Tensor([0.0])]
r_data = [r_i]
v_data = [v_i]
C_data = [C_i]
r_gt_data = [r_i]
v_gt_data = [v_i]
C_gt_data = [C_i]
for i in range(len(dataset)):
imu_window, rmis, poses_gt = dataset.get_item_with_poses(i)
# Get RMIs from neural network
imu_window = imu_window.unsqueeze(0)
out_rot = rot_net(imu_window)
out_trans = trans_net(imu_window)
t_i = imu_window[0, 0, 0]
t_j = imu_window[0, 0, -1]
DT = t_j - t_i
DR_meas, DV_meas, DC_meas = unflatten_pose(rmis[15:])
dphi = out_rot[0, :].reshape((-1, 1))
dv = out_trans[0, 0:3].reshape((-1, 1))
dr = out_trans[0, 3:].reshape((-1, 1))
DC = torch.matmul(DC_meas, SO3.Exp(dphi).squeeze())
DV = DV_meas + dv
DR = DR_meas + dr
# ground truth poses at endpoints
r_gt_i, v_gt_i, C_gt_i = unflatten_pose(poses_gt[0])
r_gt_j, v_gt_j, C_gt_j = unflatten_pose(poses_gt[1])
r_gt_data.append(r_gt_j)
v_gt_data.append(v_gt_j)
C_gt_data.append(C_gt_j)
# Get RMIs from ground truth poses
DR_gt, DV_gt, DC_gt = get_gt_rmis(
r_gt_i, v_gt_i, C_gt_i, r_gt_j, v_gt_j, C_gt_j, DT
)
# Use RMIs to predict motion forward
C_j = C_i @ DC_meas
v_j = v_i + g_a * DT + C_i @ DV
r_j = r_i + v_i * DT + 0.5 * g_a * (DT ** 2) + C_i @ DR
r_i = r_j
v_i = v_j
C_i = C_j
t_data.append(t_j)
r_data.append(r_j)
v_data.append(v_j)
C_data.append(C_j)
net_loss = pose_loss(
flatten_pose(DR, DV, DC_gt).unsqueeze(0),
flatten_pose(DR_gt, DV_gt, DC_gt).unsqueeze(0),
)
model_loss = pose_loss(
flatten_pose(DR_meas, DV_meas, DC_gt).unsqueeze(0),
flatten_pose(DR_gt, DV_gt, DC_gt).unsqueeze(0),
)
print("Net Loss: %.6f, Model Loss: %.6f" % (net_loss, model_loss))
t_data = torch.hstack(t_data)
r_data = torch.hstack(r_data)
v_data = torch.hstack(v_data)
C_data = torch.stack(C_data, 0)
r_gt_data = torch.hstack(r_gt_data)
v_gt_data = torch.hstack(v_gt_data)
C_gt_data = torch.stack(C_gt_data, 0)
return {
"t": t_data,
"r": r_data,
"v": v_data,
"C": C_data,
"r_gt": r_gt_data,
"v_gt": v_gt_data,
"C_gt": C_gt_data,
}
def trans_net_violin(net, filename):
N = net._window_size
dataset = RmiDataset(
filename, window_size=N, stride=20, with_model=True, use_cache=True
)
loader = DataLoader(dataset, batch_size=1)
net.to("cpu")
net.eval()
r_rmse = []
v_rmse = []
r_meas_rmse = []
v_meas_rmse = []
with torch.no_grad():
for i, validation_sample in enumerate(loader, 0):
x, y = validation_sample
y_hat = net(x)
loss, info = delta_trans_rmi_loss(y_hat, y, with_info=True)
r_rmse.append(sqrt(info["r_loss"] / 3))
r_meas_rmse.append(sqrt(info["r_loss_meas"] / 3))
v_rmse.append(sqrt(info["v_loss"] / 3))
v_meas_rmse.append(sqrt(info["v_loss_meas"] / 3))
return torch.vstack(
[
torch.Tensor(r_rmse),
torch.Tensor(r_meas_rmse),
torch.Tensor(v_rmse),
torch.Tensor(v_meas_rmse),
]
)
def rmi_estimator_test(net, filename, window_size, output_window =400):
dataset = RmiDataset(
filename, window_size=window_size, stride=100, with_model=False, use_cache=False, output_window=output_window
)
loader = DataLoader(dataset, batch_size=len(dataset))
net.to("cpu")
net.eval()
r_rmse = []
v_rmse = []
C_rmse = []
r_meas_rmse = []
v_meas_rmse = []
C_meas_rmse = []
with torch.no_grad():
x, y = next(iter(loader))
y_hat = net(x)
y_test = get_rmi_batch(x)
for i in range(y_hat.shape[0]):
y_hat_sample = y_hat[i,:].unsqueeze(0)
y_test_sample = y_test[i,:].unsqueeze(0)
y_sample = y[i,:].unsqueeze(0)
loss, info = pose_loss(y_hat_sample, y_sample, with_info=True)
r_rmse.append(sqrt(info["r_se"] / 3))
v_rmse.append(sqrt(info["v_se"] / 3))
C_rmse.append(sqrt(info["C_se"] / 3))
loss, info = pose_loss(y_test_sample, y_sample, with_info=True)
r_meas_rmse.append(sqrt(info["r_se"] / 3))
v_meas_rmse.append(sqrt(info["v_se"] / 3))
C_meas_rmse.append(sqrt(info["C_se"] / 3))
return torch.vstack(
[
torch.Tensor(r_rmse),
torch.Tensor(r_meas_rmse),
torch.Tensor(v_rmse),
torch.Tensor(v_meas_rmse),
torch.Tensor(C_rmse),
torch.Tensor(C_meas_rmse),
]
)