forked from Stanford-ILIAD/CARLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (35 loc) · 1.46 KB
/
utils.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
import torch
import numpy as np
def compare_trajectories(results1, results2):
"""Compare the trajectories of two runs and compute the MSE between the two trajectories (if two different sampling frequency, the higher frequency is downsampled).
"""
run1_x = results1["x_pos"][1]
run1_y = results1["y_pos"][1]
run1_collision = results1["collision"].astype(np.int32)
run1_t = results1["t"]
run2_x = results2["x_pos"][1]
run2_y = results2["y_pos"][1]
run2_collision = results2["collision"].astype(np.int32)
run2_t = results2["t"]
#downsample
if run1_t.shape[0] != run2_t.shape[0]:
if run1_t.shape[0] > run2_t.shape[0]:
t_common = run2_t
run1_x = np.interp(t_common,run1_t,run1_x)
run1_y = np.interp(t_common,run1_t,run1_y)
run1_collision = np.interp(t_common,run1_t,run1_collision)
else:
t_common = run1_t
run2_x = np.interp(t_common,run2_t,run2_x)
run2_y = np.interp(t_common,run2_t,run2_y)
run2_collision = np.interp(t_common,run2_t,run2_collision)
else:
t_common = run1_t
#compute MSE
run1_pos = np.stack([run1_x,run1_y])
run2_pos = np.stack([run2_x,run2_y])
#MSE for trajectories
MSE = (np.linalg.norm(run1_pos-run2_pos,axis=0)**2).mean()
#BCE for collision
BCE = torch.nn.functional.binary_cross_entropy(torch.Tensor(run1_collision),torch.Tensor(run2_collision))
return MSE,BCE