-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmotion_predictor.py
405 lines (282 loc) · 16.7 KB
/
motion_predictor.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python3.8
# -*- coding: utf-8 -*-
"""
Created on Mon July 03 17:46:19 2023
@author: Carlos Gómez Huélamo
Scenarios of interest:
<route id="25" town="Town05"> Test
"""
# General purpose imports
import argparse
import os
import git
import sys
import pdb
import os
import csv
from importlib import import_module
from types import SimpleNamespace
# DL & Math imports
import torch
import numpy as np
# ROS imports
import rospy
from visualization_msgs.msg import Marker, MarkerArray
from geometry_msgs.msg import Point
# Custom imports
from av2.datasets.motion_forecasting.data_schema import ObjectType
repo = git.Repo(__file__, search_parent_directories=True)
BASE_DIR = repo.working_tree_dir
sys.path.append(BASE_DIR)
from model.utils.utils import load_pretrain
from preprocess.data import from_numpy, get_object_type
# Global variables
#######################################
# Main class
class Motion_Predictor():
def __init__(self, get_model=False):
"""
"""
self.NUM_MODES = 6
self.OBS_LEN = 50
self.PRED_LEN = 60
self.required_variables = 5 # id, obs_num, x, y, padding
self.VISUALIZE_EGO_TRAJECTORY = False
self.PREDICTION_TYPE = "multimodal"
self.TINY_VISUALIZATION = False # Represent only few future steps for visualization purposes
self.THRESHOLD_NUM_OBSERVATIONS = 3 # Minimum number of observations out of self.OBS_LEN (e.g. 20 out of 50),
# to start predicting an agent
self.NUM_STEPS = 10 # To obtain predictions every n-th STEP
self.NUM_PREDICTED_POSES = 4 # e.g. t+0, t+STEP, t+2*STEP, t+3*STEP
if get_model: self.prediction_network = self.get_prediction_model()
self.CONFIDENCE_THRESHOLD = 0.2 # The mode must have at least 0.2 out of 1.0 to be plotted
self.pub_predictions_marker = rospy.Publisher("/t4ac/perception/prediction/prediction_markers", MarkerArray, queue_size=10)
def get_prediction_model(self):
"""
"""
# Motion Prediction network configuration (Note, since this script
# is an auxiliary module, we avoid the use of ArgParse. Its use is preferred
# for main scripts, not for imported modules)
args = {}
args["model"] = "CGHNet"
args["exp_name"] = "results_student"
args["weight_dir"] = "stable_ckpts"
args["ckpt"] = "50.000.ckpt"
args["use_map"] = False
args["use_goal_areas"] = False
args["map_in_decoder"] = False
args["motion_refinement"] = False
args["distill"] = False
# Transform from dictionary to dot notation
args = SimpleNamespace(**args)
model = import_module("model.models.%s" % args.model)
config, Dataset, collate_fn, net, loss, post_process, opt = model.get_model(exp_name=args.exp_name,
distill=args.distill,
use_map=args.use_map,
use_goal_areas=args.use_goal_areas,
map_in_decoder=args.map_in_decoder)
# Load pretrained model
ckpt_path = os.path.join(BASE_DIR, args.weight_dir,
args.exp_name, args.ckpt)
ckpt = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
load_pretrain(net, ckpt["state_dict"])
net.eval()
return net
def preprocess_trackers(self, trajectories):
"""
"""
agents_info_array = np.zeros([0, self.required_variables])
for key, value in trajectories.items():
for num_obs in range(self.OBS_LEN):
agent_info = np.array([key,
num_obs,
value[num_obs,0],
value[num_obs,1],
value[num_obs,2]])
agents_info_array = np.vstack((agents_info_array, agent_info))
# Avoid storing full-padded agents
agents_id = np.unique(agents_info_array[:, 0], axis=0)
valid_agents_info = []
valid_agents_id = []
for agent_index, agent_id in enumerate(agents_id):
agent_info = agents_info_array[agents_info_array[:, 0] == agent_id]
# if not (agent_info[:, -1] == 0).all(): # Avoid storing full-padded agents
if np.sum(agent_info[:,-1] == 1) >= self.THRESHOLD_NUM_OBSERVATIONS: # Only consider those agents that have
# at least self.THRESHOLD_STEPS of observations
valid_agents_info.append(agent_info)
valid_agents_id.append(int(agent_info[0,0]))
return valid_agents_info, valid_agents_id
def predict_agents(self, valid_agents_info, file_id):
"""
"""
# Get agents of the scene (we assume the first one represents our ego-vehicle)
# (N agents * 50) x 5 (track_id, timestep, x, y, padding)
# Preprocess agents (relative displacements, orientation, etc.)
trajs, steps, track_ids, object_types = [], [], [], []
final_predictions, final_confidences = [], []
# TODO: Write here the corresponding object type, though at this moment it is not used
object_type = ObjectType.VEHICLE
# agent_info: 0 = track_id, 1 = timestep, 2 = x, 3 = y, 4 = padding
for agent_info in valid_agents_info:
non_padded_agent_info = agent_info[agent_info[:, -1] == 1]
trajs.append(non_padded_agent_info[:, 2:4])
steps.append(non_padded_agent_info[:, 1].astype(np.int64))
track_ids.append(non_padded_agent_info[0, 0])
object_types.append(get_object_type(object_type))
# Our ego-vehicle is always the first agent of the scenario
if len(trajs) > 0 and trajs[0].shape[0] > 1:
current_step_index = steps[0].tolist().index(self.OBS_LEN-1)
pre_current_step_index = current_step_index-1
orig = trajs[0][current_step_index][:2].copy().astype(np.float32)
curr_pos = trajs[0][current_step_index][:2]
pre_pos = trajs[0][pre_current_step_index][:2]
theta = np.arctan2(curr_pos[1] - pre_pos[1],
curr_pos[0] - pre_pos[0])
rot = np.asarray([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]], np.float32)
feats, ctrs, valid_track_ids, valid_object_types = [], [], [], []
for traj, step, track_id, object_type in zip(trajs, steps, track_ids, object_types):
if self.OBS_LEN-1 not in step:
continue
valid_track_ids.append(track_id)
valid_object_types.append(object_type)
obs_mask = step < self.OBS_LEN
step = step[obs_mask]
traj = traj[obs_mask]
idcs = step.argsort()
step = step[idcs]
traj = traj[idcs]
feat = np.zeros((self.OBS_LEN, 3), np.float32)
feat[step, :2] = np.matmul(rot, (traj[:, :2] - orig.reshape(-1, 2)).T).T
feat[step, 2] = 1.0
ctrs.append(feat[-1, :2].copy())
feat[1:, :2] -= feat[:-1, :2]
feat[step[0], :2] = 0
feats.append(feat)
feats = np.asarray(feats, np.float32)
ctrs = np.asarray(ctrs, np.float32)
data = dict()
# OBS: Our network must receive a list (batch) per value of the dictionary. In this case, we only want
# to analyze a single scenario, so the values must be introduced as lists of 1 element, indicating
# batch_size = 1
data['scenario_id'] = [file_id]
data['track_ids'] = [valid_track_ids]
data['object_types'] = [np.asarray(valid_object_types, np.float32)]
data['feats'] = [feats]
data['ctrs'] = [ctrs]
data['orig'] = [orig]
data['theta'] = [theta]
data['rot'] = [rot]
# Recursively transform numpy.ndarray to torch.Tensor
data = from_numpy(data)
output = self.prediction_network(data)
for agent_index in range(feats.shape[0]):
agent_mm_pred = output["reg"][0][agent_index,:,:,:].cpu().data.numpy()
agent_cls = output["cls"][0][agent_index,:].cpu().data.numpy()
if self.PREDICTION_TYPE == "unimodal": # Unimodal prediction (60 x 2)
most_probable_mode_index = np.argmax(agent_cls)
agent_um_pred = agent_mm_pred[most_probable_mode_index, :, :]
agent_um_pred = agent_um_pred[::self.NUM_STEPS,:][:self.NUM_PREDICTED_POSES, :]
final_predictions.append(agent_um_pred)
else:
final_predictions.append(agent_mm_pred)
final_confidences.append(agent_cls)
return final_predictions, final_confidences
def plot_predictions_ros_markers(self, predictions, confidences, valid_agents_id, timestamp, colours_palette, apply_colour=False, lifetime=0.5):
"""
"""
lifetime = 0.2
predictions_markers_list = MarkerArray()
assert self.NUM_MODES == len(confidences[0])
slope = (1 - 1/self.NUM_MODES) / (self.NUM_MODES - 1)
for num_agent, agent_predictions in enumerate(predictions):
# Avoid plotting the predictions of the ego-vehicle
if num_agent == 0 and not self.VISUALIZE_EGO_TRAJECTORY:
continue
sorted_confidences = np.sort(confidences[num_agent])
for num_mode in range(agent_predictions.shape[0]):
if confidences[num_agent][num_mode] < self.CONFIDENCE_THRESHOLD:
continue
agent_predictions_marker = Marker()
agent_predictions_marker.header.frame_id = "/map"
agent_predictions_marker.header.stamp = timestamp
agent_predictions_marker.ns = f"agent_{valid_agents_id[num_agent]}_predictions"
agent_predictions_marker.action = agent_predictions_marker.ADD
agent_predictions_marker.lifetime = rospy.Duration.from_sec(lifetime)
agent_predictions_marker.id = num_agent * agent_predictions.shape[0] + num_mode
agent_predictions_marker.type = Marker.LINE_STRIP
# agent_predictions_marker.type = Marker.POINTS
conf_index = np.where(confidences[num_agent][num_mode] == sorted_confidences)[0].item()
agent_predictions_marker.color.a = round(slope * (conf_index + 1),2)
if apply_colour:
colour = colours_palette[valid_agents_id[num_agent]%colours_palette.shape[0]]
agent_predictions_marker.color.r = colour[0]
agent_predictions_marker.color.g = colour[1]
agent_predictions_marker.color.b = colour[2]
else: # Green
agent_predictions_marker.color.r = 0.2
agent_predictions_marker.color.g = 0.4
agent_predictions_marker.color.b = 0.0
agent_predictions_marker.scale.x = 0.5
agent_predictions_marker.pose.orientation.w = 1.0
assert self.PRED_LEN == agent_predictions.shape[1]
if self.TINY_VISUALIZATION:
curr_agent_predictions = agent_predictions[num_mode,::self.NUM_STEPS,:]
else:
curr_agent_predictions = agent_predictions[num_mode,:,:]
for num_pred in range(curr_agent_predictions.shape[0]):
point = Point()
point.x = curr_agent_predictions[num_pred,0]
point.y = curr_agent_predictions[num_pred,1]
point.z = 0
agent_predictions_marker.points.append(point)
predictions_markers_list.markers.append(agent_predictions_marker)
# End-Point
agent_prediction_end_point_marker = Marker()
agent_prediction_end_point_marker.header.frame_id = "/map"
agent_prediction_end_point_marker.header.stamp = timestamp
agent_prediction_end_point_marker.ns = f"agent_{valid_agents_id[num_agent]}_predictions_end_points"
agent_prediction_end_point_marker.action = agent_prediction_end_point_marker.ADD
agent_prediction_end_point_marker.lifetime = rospy.Duration.from_sec(lifetime)
agent_prediction_end_point_marker.id = num_agent * agent_predictions.shape[0] + num_mode
agent_prediction_end_point_marker.type = Marker.SPHERE
conf_index = np.where(confidences[num_agent][num_mode] == sorted_confidences)[0].item()
agent_prediction_end_point_marker.color.a = round(slope * (conf_index + 1),2)
if apply_colour:
colour = colours_palette[valid_agents_id[num_agent]%colours_palette.shape[0]]
agent_prediction_end_point_marker.color.r = colour[0]
agent_prediction_end_point_marker.color.g = colour[1]
agent_prediction_end_point_marker.color.b = colour[2]
else:
agent_prediction_end_point_marker.color.r = 0.2
agent_prediction_end_point_marker.color.g = 0.4
agent_prediction_end_point_marker.color.b = 0.0
agent_prediction_end_point_marker.scale.x = 1.0
agent_prediction_end_point_marker.scale.y = 1.0
agent_prediction_end_point_marker.scale.z = 1.0
agent_prediction_end_point_marker.pose.orientation.w = 1.0
agent_prediction_end_point_marker.pose.position.x = curr_agent_predictions[-1,0]
agent_prediction_end_point_marker.pose.position.y = curr_agent_predictions[-1,1]
agent_prediction_end_point_marker.pose.position.z = 0
predictions_markers_list.markers.append(agent_prediction_end_point_marker)
# Confidence text
# agent_prediction_confidence_marker = Marker()
# agent_prediction_confidence_marker.header.frame_id = "/map"
# agent_prediction_confidence_marker.header.stamp = timestamp
# agent_prediction_confidence_marker.ns = f"agent_{valid_agents_id[num_agent]}_predictions_confidences"
# agent_prediction_confidence_marker.action = agent_prediction_confidence_marker.ADD
# agent_prediction_confidence_marker.lifetime = rospy.Duration.from_sec(lifetime)
# agent_prediction_confidence_marker.id = num_agent * agent_predictions.shape[0] + num_mode
# agent_prediction_confidence_marker.type = Marker.TEXT_VIEW_FACING
# agent_prediction_confidence_marker.text = str(round(confidences[num_agent][num_mode],2))
# agent_prediction_confidence_marker.color.a = 1.0
# agent_prediction_confidence_marker.color.r = 0
# agent_prediction_confidence_marker.color.g = 0
# agent_prediction_confidence_marker.color.b = 0
# agent_prediction_confidence_marker.scale.z = 2.0
# agent_prediction_confidence_marker.pose.orientation.w = 1.0
# agent_prediction_confidence_marker.pose.position.x = curr_agent_predictions[-1,0] + 1
# agent_prediction_confidence_marker.pose.position.y = curr_agent_predictions[-1,1] + 1
# agent_prediction_confidence_marker.pose.position.z = 0
# predictions_markers_list.markers.append(agent_prediction_confidence_marker)
self.pub_predictions_marker.publish(predictions_markers_list)