feature_output.cc 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/prediction/common/feature_output.h"

P
panjiacheng 已提交
19
#include <vector>
20

21
#include "cyber/common/file.h"
K
kechxu 已提交
22
#include "modules/common/util/string_util.h"
23
#include "modules/prediction/common/prediction_system_gflags.h"
24 25 26 27

namespace apollo {
namespace prediction {

K
kechxu 已提交
28 29
using apollo::common::util::StrCat;

30
Features FeatureOutput::features_;
P
panjiacheng 已提交
31
ListDataForLearning FeatureOutput::list_data_for_learning_;
32
ListPredictionResult FeatureOutput::list_prediction_result_;
K
kechxu 已提交
33
ListFrameEnv FeatureOutput::list_frame_env_;
34
ListDataForTuning FeatureOutput::list_data_for_tuning_;
35 36
std::size_t FeatureOutput::idx_feature_ = 0;
std::size_t FeatureOutput::idx_learning_ = 0;
37
std::size_t FeatureOutput::idx_prediction_result_ = 0;
K
kechxu 已提交
38
std::size_t FeatureOutput::idx_frame_env_ = 0;
39
std::size_t FeatureOutput::idx_tuning_ = 0;
40

41 42
void FeatureOutput::Close() {
  ADEBUG << "Close feature output";
43 44 45
  switch (FLAGS_prediction_offline_mode) {
    case 1: {
      WriteFeatureProto();
K
kechxu 已提交
46
      break;
47 48 49
    }
    case 2: {
      WriteDataForLearning();
K
kechxu 已提交
50
      break;
51 52 53
    }
    case 3: {
      WritePredictionResult();
K
kechxu 已提交
54
      break;
55
    }
K
kechxu 已提交
56 57
    case 4: {
      WriteFrameEnv();
K
kechxu 已提交
58 59
      break;
    }
60 61 62 63
    case 5: {
      WriteDataForTuning();
      break;
    }
K
kechxu 已提交
64 65 66
    default: {
      // No data dump
      break;
K
kechxu 已提交
67
    }
68
  }
K
kechxu 已提交
69 70 71 72
  Clear();
}

void FeatureOutput::Clear() {
73 74
  idx_feature_ = 0;
  idx_learning_ = 0;
K
kechxu 已提交
75
  features_.Clear();
P
panjiacheng 已提交
76
  list_data_for_learning_.Clear();
77
}
78

C
Calvin Miao 已提交
79 80 81 82
bool FeatureOutput::Ready() {
  Clear();
  return true;
}
K
kechxu 已提交
83

84
void FeatureOutput::InsertFeatureProto(const Feature& feature) {
85 86 87
  features_.add_feature()->CopyFrom(feature);
}

P
panjiacheng 已提交
88
void FeatureOutput::InsertDataForLearning(
89
    const Feature& feature, const std::vector<double>& feature_values,
90
    const std::string& category, const LaneSequence* lane_sequence_ptr) {
91 92 93 94 95 96 97 98 99
  std::vector<std::string> dummy_string_feature_values;
  InsertDataForLearning(feature, feature_values, dummy_string_feature_values,
                        category, lane_sequence_ptr);
}

void FeatureOutput::InsertDataForLearning(const Feature& feature,
      const std::vector<double>& feature_values,
      const std::vector<std::string>& string_feature_values,
      const std::string& category, const LaneSequence* lane_sequence_ptr) {
P
panjiacheng 已提交
100 101 102 103 104 105 106
  DataForLearning* data_for_learning =
      list_data_for_learning_.add_data_for_learning();
  data_for_learning->set_id(feature.id());
  data_for_learning->set_timestamp(feature.timestamp());
  for (size_t i = 0; i < feature_values.size(); ++i) {
    data_for_learning->add_features_for_learning(feature_values[i]);
  }
107 108 109 110
  for (size_t i = 0; i < string_feature_values.size(); ++i) {
    data_for_learning->add_string_features_for_learning(
        string_feature_values[i]);
  }
111
  data_for_learning->set_category(category);
X
 
Xiangquan Xiao 已提交
112 113
  ADEBUG << "Insert [" << category
         << "] data for learning with size = " << feature_values.size();
114 115 116 117
  if (lane_sequence_ptr != nullptr) {
    data_for_learning->set_lane_sequence_id(
        lane_sequence_ptr->lane_sequence_id());
  }
118 119
}

120
void FeatureOutput::InsertPredictionResult(
121
    const int obstacle_id, const PredictionObstacle& prediction_obstacle) {
122 123 124 125 126 127 128 129 130 131
  PredictionResult* prediction_result =
      list_prediction_result_.add_prediction_result();
  prediction_result->set_id(obstacle_id);
  prediction_result->set_timestamp(prediction_obstacle.timestamp());
  for (int i = 0; i < prediction_obstacle.trajectory_size(); ++i) {
    prediction_result->add_trajectory()->CopyFrom(
        prediction_obstacle.trajectory(i));
  }
}

K
kechxu 已提交
132 133 134 135
void FeatureOutput::InsertFrameEnv(const FrameEnv& frame_env) {
  list_frame_env_.add_frame_env()->CopyFrom(frame_env);
}

136 137
void FeatureOutput::InsertDataForTuning(
    const Feature& feature, const std::vector<double>& feature_values,
138
    const std::string& category, const LaneSequence& lane_sequence) {
A
Aaron Xiao 已提交
139
  DataForTuning* data_for_tuning = list_data_for_tuning_.add_data_for_tuning();
140 141
  data_for_tuning->set_id(feature.id());
  data_for_tuning->set_timestamp(feature.timestamp());
A
Aaron Xiao 已提交
142 143
  *data_for_tuning->mutable_values_for_tuning() = {feature_values.begin(),
                                                   feature_values.end()};
144 145 146
  data_for_tuning->set_category(category);
  ADEBUG << "Insert [" << category
         << "] data for tuning with size = " << feature_values.size();
147
  data_for_tuning->set_lane_sequence_id(lane_sequence.lane_sequence_id());
148 149
}

150
void FeatureOutput::WriteFeatureProto() {
K
kechxu 已提交
151 152
  if (features_.feature_size() <= 0) {
    ADEBUG << "Skip writing empty feature.";
153
  } else {
154 155
    const std::string file_name = StrCat(FLAGS_prediction_data_dir, "/feature.",
                                         std::to_string(idx_feature_), ".bin");
156
    cyber::common::SetProtoToBinaryFile(features_, file_name);
157
    features_.Clear();
158
    ++idx_feature_;
159
  }
160
}
161

162
void FeatureOutput::WriteDataForLearning() {
K
kechxu 已提交
163
  if (list_data_for_learning_.data_for_learning().empty()) {
164 165
    ADEBUG << "Skip writing empty data_for_learning.";
  } else {
166 167 168
    const std::string file_name =
        StrCat(FLAGS_prediction_data_dir, "/datalearn.",
               std::to_string(idx_learning_), ".bin");
169
    cyber::common::SetProtoToBinaryFile(list_data_for_learning_, file_name);
P
panjiacheng 已提交
170
    list_data_for_learning_.Clear();
171
    ++idx_learning_;
K
kechxu 已提交
172
  }
173
}
174

175
void FeatureOutput::WritePredictionResult() {
K
kechxu 已提交
176
  if (list_prediction_result_.prediction_result().empty()) {
177 178
    ADEBUG << "Skip writing empty prediction_result.";
  } else {
179 180 181
    const std::string file_name =
        StrCat(FLAGS_prediction_data_dir, "/prediction_result.",
               std::to_string(idx_prediction_result_), ".bin");
182
    cyber::common::SetProtoToBinaryFile(list_prediction_result_, file_name);
183 184 185 186 187
    list_prediction_result_.Clear();
    ++idx_prediction_result_;
  }
}

K
kechxu 已提交
188
void FeatureOutput::WriteFrameEnv() {
K
kechxu 已提交
189
  if (list_frame_env_.frame_env().empty()) {
K
kechxu 已提交
190 191
    ADEBUG << "Skip writing empty prediction_result.";
  } else {
192 193 194
    const std::string file_name =
        StrCat(FLAGS_prediction_data_dir, "/frame_env.",
               std::to_string(idx_frame_env_), ".bin");
K
kechxu 已提交
195
    cyber::common::SetProtoToBinaryFile(list_frame_env_, file_name);
K
kechxu 已提交
196 197 198 199 200
    list_frame_env_.Clear();
    ++idx_frame_env_;
  }
}

201 202 203 204 205 206 207 208 209 210 211 212 213
void FeatureOutput::WriteDataForTuning() {
  if (list_data_for_tuning_.data_for_tuning().empty()) {
    ADEBUG << "Skip writing empty data_for_tuning.";
    return;
  }
  const std::string file_name =
      StrCat(FLAGS_prediction_data_dir, "/datatuning.",
             std::to_string(idx_tuning_), ".bin");
  cyber::common::SetProtoToBinaryFile(list_data_for_tuning_, file_name);
  list_data_for_tuning_.Clear();
  ++idx_tuning_;
}

214 215
int FeatureOutput::Size() { return features_.feature_size(); }

216 217 218 219
int FeatureOutput::SizeOfDataForLearning() {
  return list_data_for_learning_.data_for_learning_size();
}

220 221 222 223
int FeatureOutput::SizeOfPredictionResult() {
  return list_prediction_result_.prediction_result_size();
}

224
int FeatureOutput::SizeOfFrameEnv() { return list_frame_env_.frame_env_size(); }
K
kechxu 已提交
225

226 227 228 229
int FeatureOutput::SizeOfDataForTuning() {
  return list_data_for_tuning_.data_for_tuning_size();
}

230 231
}  // namespace prediction
}  // namespace apollo