42 {
43
44 omp_set_num_threads(1);
45
47
49 CHECK_NOTNULL(obstacle_ptr);
50 int id = obstacle_ptr->id();
51 if (!obstacle_ptr->latest_feature().IsInitialized()) {
52 AERROR <<
"Obstacle [" <<
id <<
"] has no latest feature.";
53 return false;
54 }
55 Feature* latest_feature_ptr = obstacle_ptr->mutable_latest_feature();
56 CHECK_NOTNULL(latest_feature_ptr);
57
58
59 if (!latest_feature_ptr->has_junction_feature() ||
60 latest_feature_ptr->junction_feature().junction_exit_size() < 2) {
61 ADEBUG <<
"Obstacle [" <<
id <<
"] has less than two junction_exits.";
62 return false;
63 }
64
65 std::vector<double> feature_values;
67 ADEBUG <<
"Obstacle [" <<
id <<
"] failed to extract junction exit mask";
68 return false;
69 }
70
71 if (!FLAGS_enable_semantic_map) {
72 ADEBUG <<
"Not enable semantic map, exit junction_map_evaluator.";
73 return false;
74 }
75 cv::Mat feature_map;
76 if (!semantic_map_->
GetMapById(
id, &feature_map)) {
77 return false;
78 }
79
80
81 std::vector<torch::jit::IValue> torch_inputs;
82
83 cv::cvtColor(feature_map, feature_map, cv::COLOR_BGR2RGB);
84 cv::Mat img_float;
85 feature_map.convertTo(img_float, CV_32F, 1.0 / 255);
86 torch::Tensor img_tensor = torch::from_blob(img_float.data, {1, 224, 224, 3});
87 img_tensor = img_tensor.permute({0, 3, 1, 2});
88 img_tensor[0][0] = img_tensor[0][0].sub(0.485).div(0.229);
89 img_tensor[0][1] = img_tensor[0][1].sub(0.456).div(0.224);
90 img_tensor[0][2] = img_tensor[0][2].sub(0.406).div(0.225);
91
92 torch::Tensor junction_exit_mask =
93 torch::zeros({1, static_cast<int>(feature_values.size())});
94 for (size_t i = 0; i < feature_values.size(); ++i) {
95 junction_exit_mask[0][i] = static_cast<float>(feature_values[i]);
96 }
97
98 torch_inputs.push_back(
99 c10::ivalue::Tuple::create({std::move(img_tensor.to(device_)),
100 std::move(junction_exit_mask.to(device_))}));
101
102
103 std::vector<double> probability;
104 at::Tensor torch_output_tensor =
105 torch_model_.forward(torch_inputs).toTensor().to(torch::kCPU);
106 auto torch_output = torch_output_tensor.accessor<float, 2>();
107 for (int i = 0; i < torch_output.size(1); ++i) {
108 probability.push_back(static_cast<double>(torch_output[0][i]));
109 }
110
111 std::unordered_map<std::string, double> junction_exit_prob;
112 for (const JunctionExit& junction_exit :
113 latest_feature_ptr->junction_feature().junction_exit()) {
114 double x =
115 junction_exit.exit_position().x() - latest_feature_ptr->position().x();
116 double y =
117 junction_exit.exit_position().y() - latest_feature_ptr->position().y();
118 double angle =
119 std::atan2(y, x) - std::atan2(latest_feature_ptr->raw_velocity().y(),
120 latest_feature_ptr->raw_velocity().x());
121 double d_idx = (angle / (2.0 * M_PI) + 1.0 / 24.0) * 12.0;
122 int idx = static_cast<int>(floor(d_idx >= 0 ? d_idx : d_idx + 12));
123 junction_exit_prob[junction_exit.exit_lane_id()] = probability[idx];
124 }
125
126
127 LaneGraph* lane_graph_ptr =
128 latest_feature_ptr->mutable_lane()->mutable_lane_graph();
129 CHECK_NOTNULL(lane_graph_ptr);
130 if (lane_graph_ptr->lane_sequence().empty()) {
131 AERROR <<
"Obstacle [" <<
id <<
"] has no lane sequences.";
132 return false;
133 }
134 for (int i = 0; i < lane_graph_ptr->lane_sequence_size(); ++i) {
135 LaneSequence* lane_sequence_ptr = lane_graph_ptr->mutable_lane_sequence(i);
136 CHECK_NOTNULL(lane_sequence_ptr);
137 for (const LaneSegment& lane_segment : lane_sequence_ptr->lane_segment()) {
138 if (junction_exit_prob.find(lane_segment.lane_id()) !=
139 junction_exit_prob.end()) {
140 lane_sequence_ptr->set_probability(
141 junction_exit_prob[lane_segment.lane_id()]);
142 }
143 }
144 }
145 return true;
146}
void Clear()
Clear obstacle feature map
bool ExtractFeatureValues(Obstacle *obstacle_ptr, std::vector< double > *feature_values)
Extract feature vector
bool GetMapById(const int obstacle_id, cv::Mat *feature_map)