feature_output.cc 5.2 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"
22
#include "modules/prediction/common/prediction_system_gflags.h"
23 24 25 26

namespace apollo {
namespace prediction {

27
Features FeatureOutput::features_;
P
panjiacheng 已提交
28
ListDataForLearning FeatureOutput::list_data_for_learning_;
29
ListPredictionResult FeatureOutput::list_prediction_result_;
K
kechxu 已提交
30
ListFrameEnv FeatureOutput::list_frame_env_;
31 32
std::size_t FeatureOutput::idx_feature_ = 0;
std::size_t FeatureOutput::idx_learning_ = 0;
33
std::size_t FeatureOutput::idx_prediction_result_ = 0;
K
kechxu 已提交
34
std::size_t FeatureOutput::idx_frame_env_ = 0;
35

36 37
void FeatureOutput::Close() {
  ADEBUG << "Close feature output";
38 39 40 41 42 43 44 45 46 47
  switch (FLAGS_prediction_offline_mode) {
    case 1: {
      WriteFeatureProto();
    }
    case 2: {
      WriteDataForLearning();
    }
    case 3: {
      WritePredictionResult();
    }
K
kechxu 已提交
48 49 50
    case 4: {
      WriteFrameEnv();
    }
51
  }
K
kechxu 已提交
52 53 54 55
  Clear();
}

void FeatureOutput::Clear() {
56 57
  idx_feature_ = 0;
  idx_learning_ = 0;
K
kechxu 已提交
58
  features_.Clear();
P
panjiacheng 已提交
59
  list_data_for_learning_.Clear();
60
}
61

C
Calvin Miao 已提交
62 63 64 65
bool FeatureOutput::Ready() {
  Clear();
  return true;
}
K
kechxu 已提交
66

67
void FeatureOutput::InsertFeatureProto(const Feature& feature) {
68 69 70
  features_.add_feature()->CopyFrom(feature);
}

P
panjiacheng 已提交
71
void FeatureOutput::InsertDataForLearning(
72
    const Feature& feature, const std::vector<double>& feature_values,
73
    const std::string& category) {
P
panjiacheng 已提交
74 75 76 77 78 79 80
  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]);
  }
81
  data_for_learning->set_category(category);
82
  ADEBUG << "Insert [" << category << "] data for learning";
83 84
}

85 86 87 88 89 90 91 92 93 94 95 96 97
void FeatureOutput::InsertPredictionResult(
    const int obstacle_id,
    const PredictionObstacle& prediction_obstacle) {
  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 已提交
98 99 100 101
void FeatureOutput::InsertFrameEnv(const FrameEnv& frame_env) {
  list_frame_env_.add_frame_env()->CopyFrom(frame_env);
}

102
void FeatureOutput::WriteFeatureProto() {
K
kechxu 已提交
103 104
  if (features_.feature_size() <= 0) {
    ADEBUG << "Skip writing empty feature.";
105 106 107
  } else {
    const std::string file_name =
        FLAGS_prediction_data_dir + "/feature." +
108
        std::to_string(idx_feature_) + ".bin";
109
    cyber::common::SetProtoToBinaryFile(features_, file_name);
110
    features_.Clear();
111
    ++idx_feature_;
112
  }
113
}
114

115 116
void FeatureOutput::WriteDataForLearning() {
  if (list_data_for_learning_.data_for_learning_size() <= 0) {
117 118 119 120
    ADEBUG << "Skip writing empty data_for_learning.";
  } else {
    const std::string file_name =
        FLAGS_prediction_data_dir + "/datalearn." +
121
        std::to_string(idx_learning_) + ".bin";
122
    cyber::common::SetProtoToBinaryFile(list_data_for_learning_, file_name);
P
panjiacheng 已提交
123
    list_data_for_learning_.Clear();
124
    ++idx_learning_;
K
kechxu 已提交
125
  }
126
}
127

128 129 130 131 132 133 134
void FeatureOutput::WritePredictionResult() {
  if (list_prediction_result_.prediction_result_size() <= 0) {
    ADEBUG << "Skip writing empty prediction_result.";
  } else {
    const std::string file_name =
        FLAGS_prediction_data_dir + "/prediction_result." +
        std::to_string(idx_prediction_result_) + ".bin";
135
    cyber::common::SetProtoToBinaryFile(list_prediction_result_, file_name);
136 137 138 139 140
    list_prediction_result_.Clear();
    ++idx_prediction_result_;
  }
}

K
kechxu 已提交
141 142 143 144 145 146 147
void FeatureOutput::WriteFrameEnv() {
  if (list_frame_env_.frame_env_size() <= 0) {
    ADEBUG << "Skip writing empty prediction_result.";
  } else {
    const std::string file_name =
        FLAGS_prediction_data_dir + "/frame_env." +
        std::to_string(idx_frame_env_) + ".bin";
K
kechxu 已提交
148
    cyber::common::SetProtoToBinaryFile(list_frame_env_, file_name);
K
kechxu 已提交
149 150 151 152 153
    list_frame_env_.Clear();
    ++idx_frame_env_;
  }
}

154 155
int FeatureOutput::Size() { return features_.feature_size(); }

156 157 158 159
int FeatureOutput::SizeOfDataForLearning() {
  return list_data_for_learning_.data_for_learning_size();
}

160 161 162 163
int FeatureOutput::SizeOfPredictionResult() {
  return list_prediction_result_.prediction_result_size();
}

K
kechxu 已提交
164 165 166 167
int FeatureOutput::SizeOfFrameEnv() {
  return list_frame_env_.frame_env_size();
}

168 169
}  // namespace prediction
}  // namespace apollo