37void LaneAggregatingEvaluator::LoadModel() {
38 torch::set_num_threads(1);
39 torch_obstacle_encoding_ = torch::jit::load(
40 FLAGS_torch_lane_aggregating_obstacle_encoding_file, device_);
41 torch_lane_encoding_ = torch::jit::load(
42 FLAGS_torch_lane_aggregating_lane_encoding_file, device_);
43 torch_prediction_layer_ = torch::jit::load(
44 FLAGS_torch_lane_aggregating_prediction_layer_file, device_);
50 CHECK_NOTNULL(obstacle_ptr);
54 int id = obstacle_ptr->
id();
56 AERROR <<
"Obstacle [" <<
id <<
"] has no latest feature.";
60 CHECK_NOTNULL(latest_feature_ptr);
61 if (!latest_feature_ptr->has_lane() ||
62 !latest_feature_ptr->
lane().has_lane_graph_ordered()) {
63 AERROR <<
"Obstacle [" <<
id <<
"] has no lane graph.";
67 latest_feature_ptr->mutable_lane()->mutable_lane_graph_ordered();
68 CHECK_NOTNULL(lane_graph_ptr);
70 AERROR <<
"Obstacle [" <<
id <<
"] has no lane sequences.";
73 ADEBUG <<
"There are " << lane_graph_ptr->lane_sequence_size()
74 <<
" lane sequences to scan.";
78 std::vector<double> obstacle_feature_values;
79 if (!ExtractObstacleFeatures(obstacle_ptr, &obstacle_feature_values)) {
80 ADEBUG <<
"Failed to extract obstacle features for obs_id = " << id;
82 if (obstacle_feature_values.size() != OBSTACLE_FEATURE_SIZE) {
83 ADEBUG <<
"Obstacle [" <<
id <<
"] has fewer than "
84 <<
"expected obstacle feature_values "
85 << obstacle_feature_values.size() <<
".";
88 ADEBUG <<
"Obstacle feature size = " << obstacle_feature_values.size();
89 std::vector<torch::jit::IValue> obstacle_encoding_inputs;
90 torch::Tensor obstacle_encoding_inputs_tensor =
91 torch::zeros({1,
static_cast<int>(obstacle_feature_values.size())});
92 for (
size_t i = 0; i < obstacle_feature_values.size(); ++i) {
93 obstacle_encoding_inputs_tensor[0][i] =
94 static_cast<float>(obstacle_feature_values[i]);
96 obstacle_encoding_inputs.push_back(
97 std::move(obstacle_encoding_inputs_tensor));
98 torch::Tensor obstalce_encoding =
99 torch_obstacle_encoding_.forward(obstacle_encoding_inputs)
103 std::vector<std::vector<double>> lane_feature_values;
104 std::vector<int> lane_sequence_idx_to_remove;
105 if (!ExtractStaticEnvFeatures(obstacle_ptr, lane_graph_ptr,
106 &lane_feature_values,
107 &lane_sequence_idx_to_remove)) {
108 AERROR <<
"Failed to extract static environmental features around obs_id = "
111 std::vector<torch::Tensor> lane_encoding_list;
112 for (
const auto& single_lane_feature_values : lane_feature_values) {
113 if (single_lane_feature_values.size() !=
114 SINGLE_LANE_FEATURE_SIZE *
115 (LANE_POINTS_SIZE + BACKWARD_LANE_POINTS_SIZE)) {
116 AERROR <<
"Obstacle [" <<
id <<
"] has incorrect lane feature size.";
119 std::vector<torch::jit::IValue> single_lane_encoding_inputs;
120 torch::Tensor single_lane_encoding_inputs_tensor =
121 torch::zeros({1, SINGLE_LANE_FEATURE_SIZE * LANE_POINTS_SIZE});
122 for (
size_t i = 0; i < SINGLE_LANE_FEATURE_SIZE * LANE_POINTS_SIZE; ++i) {
123 single_lane_encoding_inputs_tensor[0][i] =
static_cast<float>(
124 single_lane_feature_values[i + SINGLE_LANE_FEATURE_SIZE *
125 BACKWARD_LANE_POINTS_SIZE]);
127 single_lane_encoding_inputs.push_back(
128 std::move(single_lane_encoding_inputs_tensor));
129 torch::Tensor single_lane_encoding =
130 torch_lane_encoding_.forward(single_lane_encoding_inputs)
133 lane_encoding_list.push_back(std::move(single_lane_encoding));
139 std::vector<double> prediction_scores;
140 for (
size_t i = 0; i < lane_encoding_list.size(); ++i) {
141 std::vector<torch::jit::IValue> prediction_layer_inputs;
142 torch::Tensor prediction_layer_inputs_tensor = torch::zeros(
143 {1, OBSTACLE_ENCODING_SIZE + SINGLE_LANE_ENCODING_SIZE + 1});
144 for (
size_t j = 0; j < OBSTACLE_ENCODING_SIZE; ++j) {
145 prediction_layer_inputs_tensor[0][j] = obstalce_encoding[0][j];
147 for (
size_t j = 0; j + 1 < SINGLE_LANE_ENCODING_SIZE; ++j) {
148 prediction_layer_inputs_tensor[0][OBSTACLE_ENCODING_SIZE + j] =
149 lane_encoding_list[i][0][j];
151 prediction_layer_inputs_tensor[0][OBSTACLE_ENCODING_SIZE +
152 SINGLE_LANE_ENCODING_SIZE - 1] =
153 static_cast<float>(lane_feature_values[i][SINGLE_LANE_FEATURE_SIZE *
154 BACKWARD_LANE_POINTS_SIZE]);
155 prediction_layer_inputs_tensor[0][OBSTACLE_ENCODING_SIZE +
156 SINGLE_LANE_ENCODING_SIZE] = 0.0;
162 prediction_layer_inputs.push_back(
163 std::move(prediction_layer_inputs_tensor));
164 torch::Tensor prediction_layer_output =
165 torch_prediction_layer_.forward(prediction_layer_inputs)
168 auto prediction_score = prediction_layer_output.accessor<float, 2>();
169 prediction_scores.push_back(
static_cast<double>(prediction_score[0][0]));
173 std::vector<double> output_probabilities = StableSoftmax(prediction_scores);
174 for (
int i = 0; i < lane_graph_ptr->lane_sequence_size(); ++i) {
175 lane_graph_ptr->mutable_lane_sequence(i)->set_probability(
176 output_probabilities[i]);
177 ADEBUG << output_probabilities[i];
180 *(latest_feature_ptr->mutable_lane()->mutable_lane_graph()) = *lane_graph_ptr;
184bool LaneAggregatingEvaluator::ExtractObstacleFeatures(
185 const Obstacle* obstacle_ptr, std::vector<double>* feature_values) {
187 CHECK_NOTNULL(obstacle_ptr);
188 feature_values->clear();
189 FLAGS_cruise_historical_frame_length = 20;
190 std::vector<double> has_history(FLAGS_cruise_historical_frame_length, 1.0);
191 std::vector<std::pair<double, double>> pos_history(
192 FLAGS_cruise_historical_frame_length, std::make_pair(0.0, 0.0));
193 std::vector<std::pair<double, double>> vel_history(
194 FLAGS_cruise_historical_frame_length, std::make_pair(0.0, 0.0));
195 std::vector<std::pair<double, double>> acc_history(
196 FLAGS_cruise_historical_frame_length, std::make_pair(0.0, 0.0));
197 std::vector<double> vel_heading_history(FLAGS_cruise_historical_frame_length,
199 std::vector<double> vel_heading_changing_rate_history(
200 FLAGS_cruise_historical_frame_length, 0.0);
205 std::pair<double, double> obs_curr_pos = std::make_pair(
207 double prev_timestamp = obs_curr_feature.
timestamp();
211 <<
" history timestamps.";
212 for (std::size_t i = 0; i < std::min(obstacle_ptr->
history_size(),
213 FLAGS_cruise_historical_frame_length);
216 if (!feature.IsInitialized()) {
217 has_history[i] = 0.0;
220 if (i != 0 && has_history[i - 1] == 0.0) {
221 has_history[i] = 0.0;
225 if (feature.has_position()) {
228 obs_curr_pos, obs_curr_heading);
230 has_history[i] = 0.0;
233 if (feature.has_velocity()) {
236 obs_curr_pos, obs_curr_heading);
238 obs_curr_pos, obs_curr_heading);
239 vel_history[i] = std::make_pair(vel_end.first - vel_begin.first,
240 vel_end.second - vel_begin.second);
242 has_history[i] = 0.0;
245 if (feature.has_acceleration()) {
249 obs_curr_pos, obs_curr_heading);
251 obs_curr_pos, obs_curr_heading);
252 acc_history[i] = std::make_pair(acc_end.first - acc_begin.first,
253 acc_end.second - acc_begin.second);
255 has_history[i] = 0.0;
258 if (feature.has_velocity_heading()) {
259 vel_heading_history[i] =
262 vel_heading_changing_rate_history[i] =
263 (vel_heading_history[i] - vel_heading_history[i - 1]) /
264 (feature.
timestamp() - prev_timestamp + FLAGS_double_precision);
268 has_history[i] = 0.0;
273 i < FLAGS_cruise_historical_frame_length; ++i) {
274 has_history[i] = 0.0;
278 for (std::size_t i = 0; i < FLAGS_cruise_historical_frame_length; i++) {
279 feature_values->push_back(has_history[i]);
280 feature_values->push_back(pos_history[i].first);
281 feature_values->push_back(pos_history[i].second);
282 feature_values->push_back(vel_history[i].first);
283 feature_values->push_back(vel_history[i].second);
284 feature_values->push_back(acc_history[i].first);
285 feature_values->push_back(acc_history[i].second);
286 feature_values->push_back(vel_heading_history[i]);
287 feature_values->push_back(vel_heading_changing_rate_history[i]);
293bool LaneAggregatingEvaluator::ExtractStaticEnvFeatures(
294 const Obstacle* obstacle_ptr,
const LaneGraph* lane_graph_ptr,
295 std::vector<std::vector<double>>* feature_values,
296 std::vector<int>* lane_sequence_idx_to_remove) {
298 CHECK_NOTNULL(lane_graph_ptr);
299 feature_values->clear();
302 const Feature& obs_curr_feature = obstacle_ptr->latest_feature();
304 std::pair<double, double> obs_curr_pos = std::make_pair(
305 obs_curr_feature.position().x(), obs_curr_feature.position().y());
309 for (
int i = 0; i < lane_graph_ptr->lane_sequence_size(); ++i) {
312 const LaneSequence& lane_sequence = lane_graph_ptr->lane_sequence(i);
313 std::vector<double> curr_feature_values;
317 std::vector<double> backward_feature_values;
318 for (
int j = lane_sequence.adc_lane_segment_idx(); j >= 0; --j) {
319 if (count >= SINGLE_LANE_FEATURE_SIZE * BACKWARD_LANE_POINTS_SIZE) {
322 const LaneSegment& lane_segment = lane_sequence.lane_segment(j);
323 int k_starting_idx = lane_segment.lane_point_size() - 1;
324 if (j == lane_sequence.adc_lane_segment_idx()) {
325 k_starting_idx = std::min(lane_segment.adc_lane_point_idx(),
326 lane_segment.lane_point_size() - 1);
328 for (
int k = k_starting_idx; k >= 0; --k) {
329 if (count >= SINGLE_LANE_FEATURE_SIZE * BACKWARD_LANE_POINTS_SIZE) {
332 const LanePoint& lane_point = lane_segment.lane_point(k);
333 std::pair<double, double> relative_s_l =
335 lane_point.position().y()),
336 obs_curr_pos, obs_curr_heading);
337 double relative_ang =
340 backward_feature_values.push_back(lane_point.kappa());
341 backward_feature_values.push_back(relative_ang);
342 backward_feature_values.push_back(relative_s_l.first);
343 backward_feature_values.push_back(relative_s_l.second);
349 while (count >= SINGLE_LANE_FEATURE_SIZE * 2 &&
350 count < SINGLE_LANE_FEATURE_SIZE * BACKWARD_LANE_POINTS_SIZE) {
351 std::size_t s = backward_feature_values.size();
352 double relative_l_new =
353 2 * backward_feature_values[s - 1] - backward_feature_values[s - 5];
354 double relative_s_new =
355 2 * backward_feature_values[s - 2] - backward_feature_values[s - 6];
356 double relative_ang_new = backward_feature_values[s - 3];
358 backward_feature_values.push_back(0.0);
359 backward_feature_values.push_back(relative_ang_new);
360 backward_feature_values.push_back(relative_s_new);
361 backward_feature_values.push_back(relative_l_new);
366 for (
int j =
static_cast<int>(backward_feature_values.size()) - 1; j >= 0;
368 curr_feature_values.push_back(backward_feature_values[j]);
373 for (
int j = lane_sequence.adc_lane_segment_idx();
374 j < lane_sequence.lane_segment_size(); ++j) {
375 if (count >= SINGLE_LANE_FEATURE_SIZE * LANE_POINTS_SIZE) {
378 const LaneSegment& lane_segment = lane_sequence.lane_segment(j);
379 int k_starting_idx = 0;
380 if (j == lane_sequence.adc_lane_segment_idx()) {
381 k_starting_idx = std::min(lane_segment.adc_lane_point_idx(),
382 lane_segment.lane_point_size() - 1);
384 for (
int k = k_starting_idx; k < lane_segment.lane_point_size(); ++k) {
385 if (count >= SINGLE_LANE_FEATURE_SIZE * LANE_POINTS_SIZE) {
388 const LanePoint& lane_point = lane_segment.lane_point(k);
389 std::pair<double, double> relative_s_l =
391 lane_point.position().y()),
392 obs_curr_pos, obs_curr_heading);
393 double relative_ang =
396 curr_feature_values.push_back(relative_s_l.second);
397 curr_feature_values.push_back(relative_s_l.first);
398 curr_feature_values.push_back(relative_ang);
399 curr_feature_values.push_back(lane_point.kappa());
404 while (count >= SINGLE_LANE_FEATURE_SIZE * 2 &&
405 count < SINGLE_LANE_FEATURE_SIZE * LANE_POINTS_SIZE) {
406 std::size_t s = curr_feature_values.size();
407 double relative_l_new =
408 2 * curr_feature_values[s - 4] - curr_feature_values[s - 8];
409 double relative_s_new =
410 2 * curr_feature_values[s - 3] - curr_feature_values[s - 7];
411 double relative_ang_new = curr_feature_values[s - 2];
413 curr_feature_values.push_back(relative_l_new);
414 curr_feature_values.push_back(relative_s_new);
415 curr_feature_values.push_back(relative_ang_new);
416 curr_feature_values.push_back(0.0);
420 feature_values->push_back(curr_feature_values);
426torch::Tensor LaneAggregatingEvaluator::AggregateLaneEncodings(
427 const std::vector<torch::Tensor>& lane_encoding_list) {
428 torch::Tensor output_tensor =
429 torch::zeros({1, SINGLE_LANE_ENCODING_SIZE * 2});
430 torch::Tensor max_pooling_tensor = LaneEncodingMaxPooling(lane_encoding_list);
431 torch::Tensor avg_pooling_tensor = LaneEncodingAvgPooling(lane_encoding_list);
432 for (
size_t i = 0; i < SINGLE_LANE_ENCODING_SIZE; ++i) {
433 output_tensor[0][i] = max_pooling_tensor[0][i];
435 for (
size_t i = 0; i < SINGLE_LANE_ENCODING_SIZE; ++i) {
436 output_tensor[0][SINGLE_LANE_ENCODING_SIZE + i] = avg_pooling_tensor[0][i];
438 return output_tensor;
441torch::Tensor LaneAggregatingEvaluator::LaneEncodingMaxPooling(
442 const std::vector<torch::Tensor>& lane_encoding_list) {
443 ACHECK(!lane_encoding_list.empty());
444 torch::Tensor output_tensor = lane_encoding_list[0];
446 for (
size_t i = 1; i < lane_encoding_list.size(); ++i) {
447 for (
size_t j = 0; j < SINGLE_LANE_ENCODING_SIZE; ++j) {
448 output_tensor[0][j] =
449 torch::max(output_tensor[0][j], lane_encoding_list[i][0][j]);
452 return output_tensor;
455torch::Tensor LaneAggregatingEvaluator::LaneEncodingAvgPooling(
456 const std::vector<torch::Tensor>& lane_encoding_list) {
457 ACHECK(!lane_encoding_list.empty());
458 torch::Tensor output_tensor = lane_encoding_list[0];
460 for (
size_t i = 1; i < lane_encoding_list.size(); ++i) {
461 for (
size_t j = 0; j < SINGLE_LANE_ENCODING_SIZE; ++j) {
462 output_tensor[0][j] += lane_encoding_list[i][0][j];
466 torch::Tensor lane_encoding_list_size = torch::zeros({1});
467 lane_encoding_list_size[0] =
static_cast<float>(lane_encoding_list.size());
468 for (
size_t i = 0; i < SINGLE_LANE_ENCODING_SIZE; ++i) {
469 output_tensor[0][i] /= lane_encoding_list_size[0];
471 return output_tensor;
474std::vector<double> LaneAggregatingEvaluator::StableSoftmax(
475 const std::vector<double>& prediction_scores) {
476 double max_score = prediction_scores[0];
477 for (
const auto& prediction_score : prediction_scores) {
478 max_score = std::fmax(max_score, prediction_score);
480 std::vector<double> output_probabilities;
481 for (
const auto& prediction_score : prediction_scores) {
482 output_probabilities.push_back(std::exp(prediction_score - max_score));
484 double denominator = 0.0;
485 for (
const auto& element : output_probabilities) {
486 denominator += element;
488 for (
auto& element : output_probabilities) {
489 element = element / denominator;
491 return output_probabilities;
double WorldAngleToObjAngle(double input_world_angle, double obj_world_angle)
std::pair< double, double > WorldCoordToObjCoord(std::pair< double, double > input_world_coord, std::pair< double, double > obj_world_coord, double obj_world_angle)
ObstacleConf::EvaluatorType evaluator_type_
LaneAggregatingEvaluator()
Constructor
bool Evaluate(Obstacle *obstacle_ptr, ObstaclesContainer *obstacles_container) override
Override Evaluate
Feature * mutable_latest_feature()
Get a pointer to the latest feature.
int id() const
Get the obstacle's ID.
size_t history_size() const
Get the number of historical features.
const Feature & feature(const size_t i) const
Get the ith feature from latest to earliest.
void SetEvaluatorType(const ObstacleConf::EvaluatorType &evaluator_type)
const Feature & latest_feature() const
Get the latest feature.
Use container manager to manage all containers
Define the lane aggregating evaluator class
optional double timestamp
optional apollo::common::Point3D velocity
optional double velocity_heading
optional apollo::common::Point3D position
optional apollo::common::Point3D acceleration
repeated LaneSequence lane_sequence
@ LANE_AGGREGATING_EVALUATOR