-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mpc_extended_bev_carla_world_model.py
265 lines (216 loc) · 8.71 KB
/
test_mpc_extended_bev_carla_world_model.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
from carla_env.carla_env_mpc_extended_bev_traffic import CarlaEnvironment
from carla_env.mpc.mpc_extended_bev import ModelPredictiveControl
from carla_env.models.dynamic.vehicle import KinematicBicycleModel
from carla_env.models.world.world import WorldBEVModel
from carla_env.cost.masked_cost_batched_mpc_extended_bev import Cost
import torch
import time
import logging
import wandb
import math
import argparse
from collections import deque
from utils.kinematic_utils import acceleration_to_throttle_brake
from utils.model_utils import (
load_world_model_from_wandb_run,
load_ego_model_from_checkpoint,
fetch_checkpoint_from_wandb_link,
convert_standard_bev_to_model_bev,
)
from utils.train_utils import seed_everything
logging.basicConfig(level=logging.INFO)
def main(config):
# seed_everything(seed=config.seed)
# ---------------------------------------------------------------------------- #
# Cost initialization #
# ---------------------------------------------------------------------------- #
cost = Cost(
image_width=192,
image_height=192,
device=config.world_device,
)
# ---------------------------------------------------------------------------- #
# Pretrained ego forward model #
# ---------------------------------------------------------------------------- #
ego_model_run = wandb.Api().run(config.ego_forward_model_wandb_link)
checkpoint = fetch_checkpoint_from_wandb_link(
wandb_link=config.ego_forward_model_wandb_link,
checkpoint_number=config.ego_forward_model_checkpoint_number,
)
ego_forward_model = KinematicBicycleModel.load_model_from_wandb_run(
run=ego_model_run, checkpoint=checkpoint, device=config.ego_device
)
ego_forward_model = ego_forward_model.to(device=config.ego_device).eval()
# ---------------------------------------------------------------------------- #
# Pretrained world forward model #
# ---------------------------------------------------------------------------- #
world_model_run = wandb.Api().run(config.world_forward_model_wandb_link)
checkpoint = fetch_checkpoint_from_wandb_link(
config.world_forward_model_wandb_link,
config.world_forward_model_checkpoint_number,
)
world_forward_model = WorldBEVModel.load_model_from_wandb_run(
run=world_model_run, checkpoint=checkpoint, device=config.world_device
)
world_forward_model = world_forward_model.to(device=config.world_device).eval()
mpc_module = ModelPredictiveControl(
device=config.mpc_device,
batch_size=config.batch_size,
rollout_length=config.rollout_length,
action_size=config.action_size,
number_of_optimization_iterations=40,
cost=cost,
ego_model=ego_forward_model,
init_action="zeros",
world_model=world_forward_model,
render_cost=True,
)
c = CarlaEnvironment(
config={
"render": True,
"save": True,
"save_video": True,
"fixed_delta_seconds": 0.1,
}
)
bev_tensor_deque = deque(maxlen=world_forward_model.num_time_step_previous)
(
current_transform,
current_velocity,
target_waypoint,
navigational_command,
) = c.step()
data = c.get_data()
bev = data["bev"]
for i in range(world_forward_model.num_time_step_previous):
bev_tensor_deque.append(
convert_standard_bev_to_model_bev(
bev,
device=config.world_device,
agent_channel=7,
vehicle_channel=6,
selected_channels=[0, 1, 2, 3, 4, 5, 6, 11],
calculate_offroad=False,
)
)
frame_counter = 0
skip_counter = 0
repeat_counter = 0
while not c.is_done:
t0 = time.time()
# Set the current state of the ego vehicle for the kinematic model
current_state = torch.zeros(
size=(config.batch_size, 1, 4), device=config.ego_device
)
current_state[..., 0] = current_transform.location.x
current_state[..., 1] = current_transform.location.y
current_state[..., 2] = current_transform.rotation.yaw * torch.pi / 180.0
current_state[..., 3] = (
math.sqrt(current_velocity.x**2 + current_velocity.y**2) + 1e-2
)
current_state.requires_grad_(True)
logging.debug(f"Current state: {current_state}")
target_state = torch.zeros(
size=(config.batch_size, 1, 4), device=config.ego_device
)
target_state[..., 0] = target_waypoint.transform.location.x
target_state[..., 1] = target_waypoint.transform.location.y
target_state[..., 2] = target_waypoint.transform.rotation.yaw * torch.pi / 180.0
target_state[..., 3] = 0.0
target_state.requires_grad_(True)
logging.debug(f"Target state: {target_state}")
# Get the control from the ModelPredictiveControl module
# Convert bev tensor deque to torch tensor
bev_tensor = torch.cat(list(bev_tensor_deque), dim=0).unsqueeze(0)
if (skip_counter == 0) and (repeat_counter == 0):
(control, location_predicted, cost, cost_canvas) = mpc_module.step(
initial_state=current_state,
target_state=target_state,
bev=bev_tensor.detach(),
)
control_selected = control[0][skip_counter].copy()
throttle, brake = acceleration_to_throttle_brake(
acceleration=control_selected[0]
)
control_ = [throttle, control_selected[1], brake]
(
current_transform,
current_velocity,
target_waypoint,
navigational_command,
) = c.step(action=control_)
data = c.get_data()
bev = data["bev"]
bev_tensor_deque.append(
convert_standard_bev_to_model_bev(
bev,
device=config.world_device,
agent_channel=7,
vehicle_channel=6,
selected_channels=[0, 1, 2, 3, 4, 5, 6, 11],
calculate_offroad=False,
)
)
t1 = time.time()
c.render(
predicted_location=location_predicted,
bev=bev,
cost_canvas=cost_canvas,
cost=cost,
control=control_selected,
current_state=current_state,
target_state=target_state,
frame_counter=frame_counter,
skip_counter=skip_counter,
repeat_counter=repeat_counter,
sim_fps=1 / (t1 - t0),
ego_forward_model_wandb_link=config.ego_forward_model_wandb_link,
ego_forward_model_checkpoint_number=config.ego_forward_model_checkpoint_number,
world_forward_model_wandb_link=config.world_forward_model_wandb_link,
world_forward_model_checkpoint_number=config.world_forward_model_checkpoint_number,
)
mpc_module.reset()
frame_counter += 1
skip_counter = (
skip_counter + (repeat_counter + 1 == (config.repeat_frames))
) % config.skip_frames
repeat_counter = (repeat_counter + 1) % config.repeat_frames
c.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Collect data from the CARLA simulator"
)
parser.add_argument("--seed", type=int, default=333)
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--rollout_length", type=int, default=5)
parser.add_argument("--action_size", type=int, default=2)
parser.add_argument("--skip_frames", type=int, default=1)
parser.add_argument("--repeat_frames", type=int, default=2)
parser.add_argument(
"--ego_forward_model_wandb_link", type=str, default="vaydingul/mbl/ssifa1go"
)
parser.add_argument("--ego_forward_model_checkpoint_number", type=int, default=459)
parser.add_argument(
"--world_forward_model_wandb_link", type=str, default="vaydingul/mbl/3bzai68t"
)
parser.add_argument("--world_forward_model_checkpoint_number", type=int, default=47)
parser.add_argument(
"--ego_device",
type=str,
default="cuda:0",
help="Device to use for the forward model",
)
parser.add_argument(
"--world_device",
type=str,
default="cuda:0",
help="Device to use for the world model",
)
parser.add_argument(
"--mpc_device",
type=str,
default="cuda:0",
help="Device to use for the MPC module",
)
config = parser.parse_args()
main(config)