Apollo 10.0
自动驾驶开放平台
feature_output.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include "absl/strings/str_cat.h"
20#include "cyber/common/file.h"
23
24namespace apollo {
25namespace prediction {
26
28
29Features FeatureOutput::features_;
30ListDataForLearning FeatureOutput::list_data_for_learning_;
31ListPredictionResult FeatureOutput::list_prediction_result_;
32ListFrameEnv FeatureOutput::list_frame_env_;
33ListDataForTuning FeatureOutput::list_data_for_tuning_;
34std::size_t FeatureOutput::idx_feature_ = 0;
35std::size_t FeatureOutput::idx_learning_ = 0;
36std::size_t FeatureOutput::idx_prediction_result_ = 0;
37std::size_t FeatureOutput::idx_frame_env_ = 0;
38std::size_t FeatureOutput::idx_tuning_ = 0;
39std::mutex FeatureOutput::mutex_feature_;
40
42 ADEBUG << "Close feature output";
43 switch (FLAGS_prediction_offline_mode) {
44 case 1: {
46 break;
47 }
48 case 2: {
50 break;
51 }
52 case 3: {
54 break;
55 }
56 case 4: {
58 break;
59 }
60 case 5: {
62 break;
63 }
64 default: {
65 // No data dump
66 break;
67 }
68 }
69 Clear();
70}
71
73 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
74 idx_feature_ = 0;
75 idx_learning_ = 0;
76 idx_prediction_result_ = 0;
77 idx_frame_env_ = 0;
78 idx_tuning_ = 0;
79 features_.Clear();
80 list_data_for_learning_.Clear();
81 list_prediction_result_.Clear();
82 list_frame_env_.Clear();
83 list_data_for_tuning_.Clear();
84}
85
87 Clear();
88 return true;
89}
90
92 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
93 features_.add_feature()->CopyFrom(feature);
94}
95
97 const Feature& feature, const std::vector<double>& feature_values,
98 const std::string& category, const LaneSequence* lane_sequence_ptr) {
99 const std::vector<std::string> dummy_string_feature_values;
100 InsertDataForLearning(feature, feature_values, dummy_string_feature_values,
101 category, lane_sequence_ptr);
102}
103
105 const Feature& feature, const std::vector<double>& feature_values,
106 const std::vector<std::string>& string_feature_values,
107 const std::string& category, const LaneSequence* lane_sequence_ptr) {
108 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
109 DataForLearning* data_for_learning =
110 list_data_for_learning_.add_data_for_learning();
111 data_for_learning->set_id(feature.id());
112 data_for_learning->set_timestamp(feature.timestamp());
113 *(data_for_learning->mutable_features_for_learning()) = {
114 feature_values.begin(), feature_values.end()};
115 *(data_for_learning->mutable_string_features_for_learning()) = {
116 string_feature_values.begin(), string_feature_values.end()};
117 data_for_learning->set_category(category);
118 ADEBUG << "Insert [" << category
119 << "] data for learning with size = " << feature_values.size();
120 if (lane_sequence_ptr != nullptr) {
121 data_for_learning->set_lane_sequence_id(
122 lane_sequence_ptr->lane_sequence_id());
123 }
124}
125
127 const Obstacle* obstacle, const PredictionObstacle& prediction_obstacle,
128 const ObstacleConf& obstacle_conf, const Scenario& scenario) {
129 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
130 PredictionResult* prediction_result =
131 list_prediction_result_.add_prediction_result();
132 prediction_result->set_id(obstacle->id());
133 prediction_result->set_timestamp(prediction_obstacle.timestamp());
134 for (int i = 0; i < prediction_obstacle.trajectory_size(); ++i) {
135 prediction_result->add_trajectory()->CopyFrom(
136 prediction_obstacle.trajectory(i));
137 prediction_result->mutable_obstacle_conf()->CopyFrom(obstacle_conf);
138 }
139 // Insert the scenario that the single obstacle is in
140 if (scenario.type() == Scenario::JUNCTION &&
141 obstacle->IsInJunction(scenario.junction_id())) {
142 prediction_result->mutable_scenario()->set_type(Scenario::JUNCTION);
143 } else if (obstacle->IsOnLane()) {
144 prediction_result->mutable_scenario()->set_type(Scenario::CRUISE);
145 }
146}
147
149 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
150 list_frame_env_.add_frame_env()->CopyFrom(frame_env);
151}
152
154 const Feature& feature, const std::vector<double>& feature_values,
155 const std::string& category, const LaneSequence& lane_sequence,
156 const std::vector<TrajectoryPoint>& adc_trajectory) {
157 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
158 DataForTuning* data_for_tuning = list_data_for_tuning_.add_data_for_tuning();
159 data_for_tuning->set_id(feature.id());
160 data_for_tuning->set_timestamp(feature.timestamp());
161 *data_for_tuning->mutable_values_for_tuning() = {feature_values.begin(),
162 feature_values.end()};
163 data_for_tuning->set_category(category);
164 ADEBUG << "Insert [" << category
165 << "] data for tuning with size = " << feature_values.size();
166 data_for_tuning->set_lane_sequence_id(lane_sequence.lane_sequence_id());
167 for (const auto& adc_traj_point : adc_trajectory) {
168 data_for_tuning->add_adc_trajectory_point()->CopyFrom(adc_traj_point);
169 }
170}
171
173 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
174 if (features_.feature().empty()) {
175 ADEBUG << "Skip writing empty feature.";
176 } else {
177 const std::string file_name = absl::StrCat(
178 FLAGS_prediction_data_dir, "/feature.", idx_feature_, ".bin");
179 cyber::common::SetProtoToBinaryFile(features_, file_name);
180 features_.Clear();
181 ++idx_feature_;
182 }
183}
184
186 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
187 if (list_data_for_learning_.data_for_learning().empty()) {
188 ADEBUG << "Skip writing empty data_for_learning.";
189 } else {
190 const std::string file_name = absl::StrCat(
191 FLAGS_prediction_data_dir, "/datalearn.", idx_learning_, ".bin");
192 cyber::common::SetProtoToBinaryFile(list_data_for_learning_, file_name);
193 list_data_for_learning_.Clear();
194 ++idx_learning_;
195 }
196}
197
199 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
200 if (list_prediction_result_.prediction_result().empty()) {
201 ADEBUG << "Skip writing empty prediction_result.";
202 } else {
203 const std::string file_name =
204 absl::StrCat(FLAGS_prediction_data_dir, "/prediction_result.",
205 idx_prediction_result_, ".bin");
206 cyber::common::SetProtoToBinaryFile(list_prediction_result_, file_name);
207 list_prediction_result_.Clear();
208 ++idx_prediction_result_;
209 }
210}
211
213 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
214 if (list_frame_env_.frame_env().empty()) {
215 ADEBUG << "Skip writing empty prediction_result.";
216 } else {
217 const std::string file_name = absl::StrCat(
218 FLAGS_prediction_data_dir, "/frame_env.", idx_frame_env_, ".bin");
219 cyber::common::SetProtoToBinaryFile(list_frame_env_, file_name);
220 list_frame_env_.Clear();
221 ++idx_frame_env_;
222 }
223}
224
226 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
227 if (list_data_for_tuning_.data_for_tuning().empty()) {
228 ADEBUG << "Skip writing empty data_for_tuning.";
229 return;
230 }
231 const std::string file_name = absl::StrCat(
232 FLAGS_prediction_data_dir, "/datatuning.", idx_tuning_, ".bin");
233 cyber::common::SetProtoToBinaryFile(list_data_for_tuning_, file_name);
234 list_data_for_tuning_.Clear();
235 ++idx_tuning_;
236}
237
239 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
240 return features_.feature_size();
241}
242
244 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
245 return list_data_for_learning_.data_for_learning_size();
246}
247
249 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
250 return list_prediction_result_.prediction_result_size();
251}
252
254 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
255 return list_frame_env_.frame_env_size();
256}
257
259 UNIQUE_LOCK_MULTITHREAD(mutex_feature_);
260 return list_data_for_tuning_.data_for_tuning_size();
261}
262
263} // namespace prediction
264} // namespace apollo
static void InsertFeatureProto(const Feature &feature)
Insert a feature
static void InsertDataForLearning(const Feature &feature, const std::vector< double > &feature_values, const std::string &category, const LaneSequence *lane_sequence_ptr)
Insert a data_for_learning
static void WriteDataForLearning()
Write DataForLearning features to a file
static int SizeOfPredictionResult()
Get the size of prediction results.
static bool Ready()
Check if output is ready
static int Size()
Get feature size
static int SizeOfFrameEnv()
Get the size of frame env.
static void WriteDataForTuning()
Write DataForTuning features to a file
static int SizeOfDataForLearning()
Get the size of data_for_learning features.
static void WriteFrameEnv()
Write frame env to a file
static int SizeOfDataForTuning()
Get the size of data for tuning.
static void Close()
Close the output stream
static void WriteFeatureProto()
Write features to a file
static void InsertFrameEnv(const FrameEnv &frame_env)
Insert a frame env
static void InsertPredictionResult(const Obstacle *obstacle, const PredictionObstacle &prediction_obstacle, const ObstacleConf &obstacle_conf, const Scenario &scenario)
Insert a prediction result with predicted trajectories
static void InsertDataForTuning(const Feature &feature, const std::vector< double > &feature_values, const std::string &category, const LaneSequence &lane_sequence, const std::vector< apollo::common::TrajectoryPoint > &adc_trajectory)
Insert a data_for_tuning
static void WritePredictionResult()
Write PredictionResult to a file
Prediction obstacle.
Definition obstacle.h:52
bool IsInJunction(const std::string &junction_id) const
Check if the obstacle is a junction.
Definition obstacle.cc:223
int id() const
Get the obstacle's ID.
Definition obstacle.cc:60
bool IsOnLane() const
Check if the obstacle is on any lane.
Definition obstacle.cc:107
#define ADEBUG
Definition log.h:41
Some util functions.
#define UNIQUE_LOCK_MULTITHREAD(mutex_type)
Definition util.h:162
bool SetProtoToBinaryFile(const google::protobuf::Message &message, const std::string &file_name)
Sets the content of the file specified by the file_name to be the binary representation of the input ...
Definition file.cc:111
class register implement
Definition arena_queue.h:37