forked from MCZhi/DIPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_utils.py
172 lines (136 loc) · 6.91 KB
/
train_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
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
import torch
import logging
import glob
import random
import numpy as np
from torch.utils.data import Dataset
from torch.nn import functional as F
def initLogging(log_file: str, level: str = "INFO"):
logging.basicConfig(filename=log_file, filemode='w',
level=getattr(logging, level, None),
format='[%(levelname)s %(asctime)s] %(message)s',
datefmt='%m-%d %H:%M:%S')
logging.getLogger().addHandler(logging.StreamHandler())
def set_seed(CUR_SEED):
random.seed(CUR_SEED)
np.random.seed(CUR_SEED)
torch.manual_seed(CUR_SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class DrivingData(Dataset):
def __init__(self, data_dir):
self.data_list = glob.glob(data_dir)
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
data = np.load(self.data_list[idx])
ego = data['ego']
neighbors = data['neighbors']
ref_line = data['ref_line']
map_lanes = data['map_lanes']
map_crosswalks = data['map_crosswalks']
gt_future_states = data['gt_future_states']
return ego, neighbors, map_lanes, map_crosswalks, ref_line, gt_future_states
def MFMA_loss(plans, predictions, scores, ground_truth, weights, use_planning):
global best_mode
predictions = predictions * weights.unsqueeze(1)
prediction_distance = torch.norm(predictions[:, :, :, 9::10, :2] - ground_truth[:, None, 1:, 9::10, :2], dim=-1)
plan_distance = torch.norm(plans[:, :, 9::10, :2] - ground_truth[:, None, 0, 9::10, :2], dim=-1)
prediction_distance = prediction_distance.mean(-1).sum(-1)
plan_distance = plan_distance.mean(-1)
best_mode = torch.argmin(plan_distance+prediction_distance, dim=-1)
score_loss = F.cross_entropy(scores, best_mode)
best_mode_plan = torch.stack([plans[i, m] for i, m in enumerate(best_mode)])
best_mode_prediction = torch.stack([predictions[i, m] for i, m in enumerate(best_mode)])
prediction_loss: torch.tensor = 0
for i in range(10):
prediction_loss += F.smooth_l1_loss(best_mode_prediction[:, i], ground_truth[:, i+1, :, :3])
if not use_planning:
imitation_loss = F.smooth_l1_loss(best_mode_plan, ground_truth[:, 0, :, :3])
return 0.5 * prediction_loss + imitation_loss + score_loss
else:
return 0.5 * prediction_loss + score_loss
def select_future(plans, predictions, scores):
plan = torch.stack([plans[i, m] for i, m in enumerate(best_mode)])
prediction = torch.stack([predictions[i, m] for i, m in enumerate(best_mode)])
return plan, prediction
def motion_metrics(plan_trajectory, prediction_trajectories, ground_truth_trajectories, weights):
prediction_trajectories = prediction_trajectories * weights
plan_distance = torch.norm(plan_trajectory[:, :, :2] - ground_truth_trajectories[:, 0, :, :2], dim=-1)
prediction_distance = torch.norm(prediction_trajectories[:, :, :, :2] - ground_truth_trajectories[:, 1:, :, :2], dim=-1)
# planning
plannerADE = torch.mean(plan_distance)
plannerFDE = torch.mean(plan_distance[:, -1])
# prediction
predictorADE = torch.mean(prediction_distance, dim=-1)
predictorADE = torch.masked_select(predictorADE, weights[:, :, 0, 0])
predictorADE = torch.mean(predictorADE)
predictorFDE = prediction_distance[:, :, -1]
predictorFDE = torch.masked_select(predictorFDE, weights[:, :, 0, 0])
predictorFDE = torch.mean(predictorFDE)
return plannerADE.item(), plannerFDE.item(), predictorADE.item(), predictorFDE.item()
def project_to_frenet_frame(traj, ref_line):
distance_to_ref = torch.cdist(traj[:, :, :2], ref_line[:, :, :2])
k = torch.argmin(distance_to_ref, dim=-1).view(-1, traj.shape[1], 1).expand(-1, -1, 3)
ref_points = torch.gather(ref_line, 1, k)
x_r, y_r, theta_r = ref_points[:, :, 0], ref_points[:, :, 1], ref_points[:, :, 2]
x, y = traj[:, :, 0], traj[:, :, 1]
s = 0.1 * (k[:, :, 0] - 200)
l = torch.sign((y-y_r)*torch.cos(theta_r)-(x-x_r)*torch.sin(theta_r)) * torch.sqrt(torch.square(x-x_r)+torch.square(y-y_r))
sl = torch.stack([s, l], dim=-1)
return sl
def project_to_cartesian_frame(traj, ref_line):
k = (10 * traj[:, :, 0] + 200).long()
k = torch.clip(k, 0, 1200-1)
ref_points = torch.gather(ref_line, 1, k.view(-1, traj.shape[1], 1).expand(-1, -1, 3))
x_r, y_r, theta_r = ref_points[:, :, 0], ref_points[:, :, 1], ref_points[:, :, 2]
x = x_r - traj[:, :, 1] * torch.sin(theta_r)
y = y_r + traj[:, :, 1] * torch.cos(theta_r)
xy = torch.stack([x, y], dim=-1)
return xy
def bicycle_model(control, current_state):
dt = 0.1 # discrete time period [s]
max_delta = 0.6 # vehicle's steering limits [rad]
max_a = 5 # vehicle's accleration limits [m/s^2]
x_0 = current_state[:, 0] # vehicle's x-coordinate [m]
y_0 = current_state[:, 1] # vehicle's y-coordinate [m]
theta_0 = current_state[:, 2] # vehicle's heading [rad]
v_0 = torch.hypot(current_state[:, 3], current_state[:, 4]) # vehicle's velocity [m/s]
L = 3.089 # vehicle's wheelbase [m]
a = control[:, :, 0].clamp(-max_a, max_a) # vehicle's accleration [m/s^2]
delta = control[:, :, 1].clamp(-max_delta, max_delta) # vehicle's steering [rad]
# speed
v = v_0.unsqueeze(1) + torch.cumsum(a * dt, dim=1)
v = torch.clamp(v, min=0)
# angle
d_theta = v * delta / L # use delta to approximate tan(delta)
theta = theta_0.unsqueeze(1) + torch.cumsum(d_theta * dt, dim=-1)
theta = torch.fmod(theta, 2*torch.pi)
# x and y coordniate
x = x_0.unsqueeze(1) + torch.cumsum(v * torch.cos(theta) * dt, dim=-1)
y = y_0.unsqueeze(1) + torch.cumsum(v * torch.sin(theta) * dt, dim=-1)
# output trajectory
traj = torch.stack([x, y, theta, v], dim=-1)
return traj
def physical_model(control, current_state, dt=0.1):
dt = 0.1 # discrete time period [s]
max_d_theta = 0.5 # vehicle's change of angle limits [rad/s]
max_a = 5 # vehicle's accleration limits [m/s^2]
x_0 = current_state[:, 0] # vehicle's x-coordinate
y_0 = current_state[:, 1] # vehicle's y-coordinate
theta_0 = current_state[:, 2] # vehicle's heading [rad]
v_0 = torch.hypot(current_state[:, 3], current_state[:, 4]) # vehicle's velocity [m/s]
a = control[:, :, 0].clamp(-max_a, max_a) # vehicle's accleration [m/s^2]
d_theta = control[:, :, 1].clamp(-max_d_theta, max_d_theta) # vehicle's heading change rate [rad/s]
# speed
v = v_0.unsqueeze(1) + torch.cumsum(a * dt, dim=1)
v = torch.clamp(v, min=0)
# angle
theta = theta_0.unsqueeze(1) + torch.cumsum(d_theta * dt, dim=-1)
theta = torch.fmod(theta, 2*torch.pi)
# x and y coordniate
x = x_0.unsqueeze(1) + torch.cumsum(v * torch.cos(theta) * dt, dim=-1)
y = y_0.unsqueeze(1) + torch.cumsum(v * torch.sin(theta) * dt, dim=-1)
# output trajectory
traj = torch.stack([x, y, theta, v], dim=-1)
return traj