51 torch::Tensor* ptr_target_obs_pos,
52 torch::Tensor* ptr_target_obs_pos_step,
53 torch::Tensor* ptr_vector_mask,
54 torch::Tensor* ptr_all_obstacle_pos,
55 torch::Tensor* ptr_all_obs_p_id,
56 torch::Tensor* ptr_obs_length) {
59 std::vector<std::pair<double, double>> target_pos_history(20, {0.0, 0.0});
60 std::vector<std::pair<double, double>> all_obs_length;
61 std::vector<std::vector<std::pair<double, double>>> all_obs_pos_history;
64 &target_pos_history, &all_obs_length,
65 &all_obs_pos_history, ptr_vector_mask)) {
66 AERROR <<
"Obstacle [" << obstacle_ptr->
id()
67 <<
"] failed to extract obstacle history";
71 for (
int j = 0; j < 20; ++j) {
72 ptr_target_obs_pos->index_put_({19 - j, 0}, target_pos_history[j].first);
73 ptr_target_obs_pos->index_put_({19 - j, 1}, target_pos_history[j].second);
74 if (j == 19 || (j > 0 && target_pos_history[j + 1].first == 0.0)) {
77 ptr_target_obs_pos_step->index_put_(
79 target_pos_history[j].first - target_pos_history[j + 1].first);
80 ptr_target_obs_pos_step->index_put_(
82 target_pos_history[j].second - target_pos_history[j + 1].second);
87 for (
int i = 0; i < obs_num; ++i) {
88 std::vector<double> obs_p_id{std::numeric_limits<float>::max(),
89 std::numeric_limits<float>::max()};
90 for (
int j = 0; j < 20; ++j) {
92 if (obs_p_id[0] > all_obs_pos_history[i][j].first) {
93 obs_p_id[0] = all_obs_pos_history[i][j].first;
95 if (obs_p_id[1] > all_obs_pos_history[i][j].second) {
96 obs_p_id[1] = all_obs_pos_history[i][j].second;
99 ptr_all_obstacle_pos->index_put_(
101 all_obs_pos_history[i][j].first);
102 ptr_all_obstacle_pos->index_put_(
104 all_obs_pos_history[i][j].second);
107 ptr_all_obs_p_id->index_put_({i, 0}, obs_p_id[0]);
108 ptr_all_obs_p_id->index_put_({i, 1}, obs_p_id[1]);
111 for (
int i = 0; i < obs_num; ++i) {
112 ptr_obs_length->index_put_({i, 0}, all_obs_length[i].first);
113 ptr_obs_length->index_put_({i, 1}, all_obs_length[i].second);
171 omp_set_num_threads(1);
176 CHECK_NOTNULL(obstacle_ptr);
177 int id = obstacle_ptr->
id();
179 AERROR <<
"Obstacle [" <<
id <<
"] has no latest feature.";
183 CHECK_NOTNULL(latest_feature_ptr);
185 if (adc_trajectory_container ==
nullptr) {
186 AERROR <<
"Null adc trajectory container";
193 auto start_time_obs = std::chrono::system_clock::now();
195 torch::Tensor target_obstacle_pos = torch::zeros({20, 2});
196 torch::Tensor target_obstacle_pos_step = torch::zeros({20, 2});
197 torch::Tensor vector_mask = torch::zeros({450, 50});
198 torch::Tensor all_obstacle_pos = torch::zeros({obs_num, 20, 2});
199 torch::Tensor all_obs_p_id = torch::zeros({obs_num, 2});
200 torch::Tensor obs_length_tmp = torch::zeros({obs_num, 2});
204 &target_obstacle_pos,
205 &target_obstacle_pos_step,
210 AERROR <<
"Obstacle [" <<
id <<
"] processing obstacle position fails.";
214 auto end_time_obs = std::chrono::system_clock::now();
215 std::chrono::duration<double> diff_obs = end_time_obs - start_time_obs;
216 ADEBUG <<
"obstacle vectors used time: " << diff_obs.count() * 1000 <<
" ms.";
222 double pos_x = latest_feature_ptr->
position().
x();
223 double pos_y = latest_feature_ptr->
position().
y();
225 center_point.set_x(pos_x);
226 center_point.set_y(pos_y);
229 auto start_time_query = std::chrono::system_clock::now();
231 if (!vector_net_.
query(center_point, heading, &map_feature, &map_p_id)) {
235 auto end_time_query = std::chrono::system_clock::now();
236 std::chrono::duration<double> diff_query = end_time_query - start_time_query;
237 ADEBUG <<
"vectors query used time: " << diff_query.count() * 1000 <<
" ms.";
240 int map_polyline_num = map_feature.size();
242 ((obs_num + map_polyline_num) < 450) ? (obs_num + map_polyline_num) : 450;
245 auto start_time_data_prep = std::chrono::system_clock::now();
246 int map_polyline_num_valid =
247 ((obs_num + map_polyline_num) < 450) ? map_polyline_num : (450 - obs_num);
248 map_polyline_num_valid =
249 map_polyline_num_valid > 0 ? map_polyline_num_valid : 0;
250 torch::Tensor map_data = torch::zeros({map_polyline_num_valid, 50, 9});
251 torch::Tensor all_map_p_id = torch::zeros({map_polyline_num_valid, 2});
259 AERROR <<
"Obstacle [" <<
id <<
"] processing map data fails.";
264 torch::Tensor polyline_mask = torch::zeros({450});
265 if (data_length < 450) {
266 polyline_mask.index_put_(
267 {torch::indexing::Slice(data_length, torch::indexing::None)}, 1);
271 torch::Tensor obs_pos_data = torch::cat(
272 {all_obstacle_pos.index(
273 {torch::indexing::Slice(),
274 torch::indexing::Slice(torch::indexing::None, -1),
275 torch::indexing::Slice()}),
276 all_obstacle_pos.index({torch::indexing::Slice(),
277 torch::indexing::Slice(1, torch::indexing::None),
278 torch::indexing::Slice()})},
281 torch::Tensor obs_length = obs_length_tmp.unsqueeze(1).repeat({1, 19, 1});
283 torch::Tensor obs_attr_agent =
284 torch::tensor({11.0, 4.0}).unsqueeze(0).unsqueeze(0).repeat({1, 19, 1});
285 torch::Tensor obs_attr_other =
286 torch::tensor({10.0, 4.0}).unsqueeze(0).unsqueeze(0).repeat(
287 {(obs_num - 1), 19, 1});
288 torch::Tensor obs_attr = torch::cat({obs_attr_agent, obs_attr_other}, 0);
291 torch::Tensor obs_id =
292 torch::arange(500, obs_num + 500).unsqueeze(1).repeat(
293 {1, 19}).unsqueeze(2);
295 torch::Tensor obs_data_with_len = torch::cat({obs_pos_data, obs_length}, 2);
296 torch::Tensor obs_data_with_attr =
297 torch::cat({obs_data_with_len, obs_attr}, 2);
298 torch::Tensor obs_data_with_id = torch::cat({obs_data_with_attr, obs_id}, 2);
299 torch::Tensor obs_data_final =
300 torch::cat({torch::zeros({obs_num, (50 - 19), 9}), obs_data_with_id}, 1);
303 torch::Tensor data_tmp = torch::cat({obs_data_final, map_data}, 0);
304 torch::Tensor p_id_tmp = torch::cat({all_obs_p_id, all_map_p_id}, 0);
305 torch::Tensor vector_data;
306 torch::Tensor polyline_id;
307 if (data_length < 450) {
308 torch::Tensor data_zeros = torch::zeros({(450 - data_length), 50, 9});
309 torch::Tensor p_id_zeros = torch::zeros({(450 - data_length), 2});
310 vector_data = torch::cat({data_tmp, data_zeros}, 0);
311 polyline_id = torch::cat({p_id_tmp, p_id_zeros}, 0);
313 vector_data = data_tmp;
314 polyline_id = p_id_tmp;
318 auto rand_mask = torch::zeros({450}).toType(at::kBool);
320 auto bool_vector_mask = vector_mask.toType(at::kBool);
321 auto bool_polyline_mask = polyline_mask.toType(at::kBool);
324 std::vector<std::pair<double, double>> adc_traj_curr_pos(30, {0.0, 0.0});
325 torch::Tensor adc_trajectory = torch::zeros({1, 30, 6});
326 const auto& adc_traj = adc_trajectory_container->adc_trajectory();
327 size_t adc_traj_points_num = adc_traj.trajectory_point().size();
328 if (adc_traj_points_num < 1) {
329 AERROR <<
"adc_traj points num is " << adc_traj_points_num
330 <<
" adc traj points are not enough";
333 std::vector<TrajectoryPoint> adc_traj_points;
336 double time_interval = obstacle_ptr->latest_feature().timestamp() -
337 adc_traj.header().timestamp_sec();
338 for (
size_t i = 0; i < adc_traj_points_num - 1; ++i) {
339 double delta_time = time_interval -
340 adc_traj.trajectory_point(0).relative_time();
341 adc_traj_points.emplace_back(
343 adc_traj.trajectory_point(i),
344 adc_traj.trajectory_point(i + 1), delta_time));
346 if (!ExtractADCTrajectory(&adc_traj_points,
347 obstacle_ptr, &adc_traj_curr_pos)) {
348 AERROR <<
"Failed to extract adc trajectory";
351 size_t traj_points_num = adc_traj_points.size();
352 for (
size_t j = 0; j < 30; ++j) {
353 if (j > traj_points_num - 1) {
354 adc_trajectory[0][j][0] =
355 adc_traj_curr_pos[traj_points_num - 1].first;
356 adc_trajectory[0][j][1] =
357 adc_traj_curr_pos[traj_points_num - 1].second;
358 adc_trajectory[0][j][2] =
359 adc_traj_points[traj_points_num - 1].path_point().theta();
360 adc_trajectory[0][j][3] =
361 adc_traj_points[traj_points_num - 1].v();
362 adc_trajectory[0][j][4] =
363 adc_traj_points[traj_points_num - 1].a();
364 adc_trajectory[0][j][5] =
365 adc_traj_points[traj_points_num - 1].path_point().kappa();
367 adc_trajectory[0][j][0] =
368 adc_traj_curr_pos[j].first;
369 adc_trajectory[0][j][1] =
370 adc_traj_curr_pos[j].second;
371 adc_trajectory[0][j][2] =
372 adc_traj_points[j].path_point().theta();
373 adc_trajectory[0][j][3] =
374 adc_traj_points[j].v();
375 adc_trajectory[0][j][4] =
376 adc_traj_points[j].a();
377 adc_trajectory[0][j][5] =
378 adc_traj_points[j].path_point().kappa();
383 std::vector<torch::jit::IValue> torch_inputs;
384 auto X_value = c10::ivalue::Tuple::create(
385 {std::move(target_obstacle_pos.unsqueeze(0).to(device_)),
386 std::move(target_obstacle_pos_step.unsqueeze(0).to(device_)),
387 std::move(vector_data.unsqueeze(0).to(device_)),
388 std::move(bool_vector_mask.unsqueeze(0).to(device_)),
389 std::move(bool_polyline_mask.unsqueeze(0).to(device_)),
390 std::move(rand_mask.unsqueeze(0).to(device_)),
391 std::move(polyline_id.unsqueeze(0).to(device_))});
392 torch_inputs.push_back(c10::ivalue::Tuple::create(
393 {X_value, std::move(adc_trajectory.to(device_))}));
395 auto end_time_data_prep = std::chrono::system_clock::now();
396 std::chrono::duration<double> diff_data_prep =
397 end_time_data_prep - start_time_data_prep;
398 ADEBUG <<
"vectornet input tensor prepration used time: "
399 << diff_data_prep.count() * 1000 <<
" ms.";
402 auto start_time_inference = std::chrono::system_clock::now();
403 at::Tensor torch_output_tensor = torch_default_output_tensor_;
404 torch_output_tensor =
405 torch_vehicle_model_.forward(torch_inputs).toTensor().to(torch::kCPU);
407 auto end_time_inference = std::chrono::system_clock::now();
408 std::chrono::duration<double> diff_inference =
409 end_time_inference - start_time_inference;
410 ADEBUG <<
"vectornet-interaction inference used time: "
411 << diff_inference.count() * 1000
415 auto torch_output = torch_output_tensor.accessor<float, 3>();
416 Trajectory* trajectory = latest_feature_ptr->add_predicted_trajectory();
417 trajectory->set_probability(1.0);
419 for (
int i = 0; i < 30; ++i) {
420 double prev_x = pos_x;
421 double prev_y = pos_y;
423 const auto& last_point = trajectory->trajectory_point(i - 1).path_point();
424 prev_x = last_point.x();
425 prev_y = last_point.y();
427 TrajectoryPoint* point = trajectory->add_trajectory_point();
428 double dx =
static_cast<double>(torch_output[0][i][0]);
429 double dy =
static_cast<double>(torch_output[0][i][1]);
431 double heading = latest_feature_ptr->velocity_heading();
432 Vec2d offset(dx, dy);
433 Vec2d rotated_offset = offset.rotate(heading - (M_PI / 2));
434 double point_x = pos_x + rotated_offset.x();
435 double point_y = pos_y + rotated_offset.y();
436 point->mutable_path_point()->set_x(point_x);
437 point->mutable_path_point()->set_y(point_y);
440 point->mutable_path_point()->set_theta(
441 latest_feature_ptr->velocity_heading());
443 point->mutable_path_point()->set_theta(
444 std::atan2(trajectory->trajectory_point(i).path_point().y() -
445 trajectory->trajectory_point(i - 1).path_point().y(),
446 trajectory->trajectory_point(i).path_point().x() -
447 trajectory->trajectory_point(i - 1).path_point().x()));
449 point->set_relative_time(
static_cast<double>(i) *
450 FLAGS_prediction_trajectory_time_resolution);
452 point->set_v(latest_feature_ptr->speed());
454 double diff_x = point_x - prev_x;
455 double diff_y = point_y - prev_y;
456 point->set_v(std::hypot(diff_x, diff_y) /
457 FLAGS_prediction_trajectory_time_resolution);
464bool JointlyPredictionPlanningEvaluator::ExtractObstaclesHistory(
466 std::vector<std::pair<double, double>>* target_pos_history,
467 std::vector<std::pair<double, double>>* all_obs_length,
468 std::vector<std::vector<std::pair<double, double>>>* all_obs_pos_history,
469 torch::Tensor* vector_mask) {
472 std::pair<double, double> obs_curr_pos = std::make_pair(
475 for (std::size_t i = 0; i < obstacle_ptr->
history_size() && i < 20; ++i) {
477 if (!target_feature.IsInitialized()) {
480 target_pos_history->at(i) =
481 WorldCoordToObjCoordNorth(std::make_pair(target_feature.
position().
x(),
483 obs_curr_pos, obs_curr_heading);
485 all_obs_length->emplace_back(
486 std::make_pair(obs_curr_feature.
length(), obs_curr_feature.
width()));
487 all_obs_pos_history->emplace_back(*target_pos_history);
490 std::vector<std::pair<double, double>> pos_history(20, {0.0, 0.0});
496 int target_id = obstacle_ptr->
id();
497 if (
id == target_id) {
501 all_obs_length->emplace_back(std::make_pair(
502 other_obs_curr_feature.
length(), other_obs_curr_feature.
width()));
505 obs_his_size = obs_his_size <= 20 ? obs_his_size : 20;
506 int cur_idx = all_obs_pos_history->size();
507 if (obs_his_size > 1) {
508 vector_mask->index_put_({cur_idx,
509 torch::indexing::Slice(torch::indexing::None,
510 -(obs_his_size - 1))}, 1);
512 vector_mask->index_put_({cur_idx,
513 torch::indexing::Slice(torch::indexing::None,
517 for (
size_t i = 0; i < obs_his_size; ++i) {
519 if (!feature.IsInitialized()) {
522 pos_history[i] = WorldCoordToObjCoordNorth(
524 obs_curr_pos, obs_curr_heading);
526 all_obs_pos_history->emplace_back(pos_history);