feature_output.cc 5.4 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 35
std::size_t FeatureOutput::idx_feature_ = 0;
std::size_t FeatureOutput::idx_learning_ = 0;
36
std::size_t FeatureOutput::idx_prediction_result_ = 0;
K
kechxu 已提交
37
std::size_t FeatureOutput::idx_frame_env_ = 0;
38

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

void FeatureOutput::Clear() {
67 68
  idx_feature_ = 0;
  idx_learning_ = 0;
K
kechxu 已提交
69
  features_.Clear();
P
panjiacheng 已提交
70
  list_data_for_learning_.Clear();
71
}
72

C
Calvin Miao 已提交
73 74 75 76
bool FeatureOutput::Ready() {
  Clear();
  return true;
}
K
kechxu 已提交
77

78
void FeatureOutput::InsertFeatureProto(const Feature& feature) {
79 80 81
  features_.add_feature()->CopyFrom(feature);
}

P
panjiacheng 已提交
82
void FeatureOutput::InsertDataForLearning(
83
    const Feature& feature, const std::vector<double>& feature_values,
84
    const std::string& category) {
P
panjiacheng 已提交
85 86 87 88 89 90 91
  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]);
  }
92
  data_for_learning->set_category(category);
93
  ADEBUG << "Insert [" << category << "] data for learning";
94 95
}

96 97 98 99 100 101 102 103 104 105 106 107 108
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 已提交
109 110 111 112
void FeatureOutput::InsertFrameEnv(const FrameEnv& frame_env) {
  list_frame_env_.add_frame_env()->CopyFrom(frame_env);
}

113
void FeatureOutput::WriteFeatureProto() {
K
kechxu 已提交
114 115
  if (features_.feature_size() <= 0) {
    ADEBUG << "Skip writing empty feature.";
116
  } else {
K
kechxu 已提交
117 118
    const std::string file_name = StrCat(FLAGS_prediction_data_dir,
        "/feature.", std::to_string(idx_feature_), ".bin");
119
    cyber::common::SetProtoToBinaryFile(features_, file_name);
120
    features_.Clear();
121
    ++idx_feature_;
122
  }
123
}
124

125
void FeatureOutput::WriteDataForLearning() {
K
kechxu 已提交
126
  if (list_data_for_learning_.data_for_learning().empty()) {
127 128
    ADEBUG << "Skip writing empty data_for_learning.";
  } else {
K
kechxu 已提交
129 130
    const std::string file_name = StrCat(FLAGS_prediction_data_dir,
        "/datalearn.", std::to_string(idx_learning_), ".bin");
131
    cyber::common::SetProtoToBinaryFile(list_data_for_learning_, file_name);
P
panjiacheng 已提交
132
    list_data_for_learning_.Clear();
133
    ++idx_learning_;
K
kechxu 已提交
134
  }
135
}
136

137
void FeatureOutput::WritePredictionResult() {
K
kechxu 已提交
138
  if (list_prediction_result_.prediction_result().empty()) {
139 140
    ADEBUG << "Skip writing empty prediction_result.";
  } else {
K
kechxu 已提交
141 142
    const std::string file_name = StrCat(FLAGS_prediction_data_dir,
        "/prediction_result.", std::to_string(idx_prediction_result_), ".bin");
143
    cyber::common::SetProtoToBinaryFile(list_prediction_result_, file_name);
144 145 146 147 148
    list_prediction_result_.Clear();
    ++idx_prediction_result_;
  }
}

K
kechxu 已提交
149
void FeatureOutput::WriteFrameEnv() {
K
kechxu 已提交
150
  if (list_frame_env_.frame_env().empty()) {
K
kechxu 已提交
151 152
    ADEBUG << "Skip writing empty prediction_result.";
  } else {
K
kechxu 已提交
153 154
    const std::string file_name = StrCat(FLAGS_prediction_data_dir,
        "/frame_env.", std::to_string(idx_frame_env_), ".bin");
K
kechxu 已提交
155
    cyber::common::SetProtoToBinaryFile(list_frame_env_, file_name);
K
kechxu 已提交
156 157 158 159 160
    list_frame_env_.Clear();
    ++idx_frame_env_;
  }
}

161 162
int FeatureOutput::Size() { return features_.feature_size(); }

163 164 165 166
int FeatureOutput::SizeOfDataForLearning() {
  return list_data_for_learning_.data_for_learning_size();
}

167 168 169 170
int FeatureOutput::SizeOfPredictionResult() {
  return list_prediction_result_.prediction_result_size();
}

K
kechxu 已提交
171 172 173 174
int FeatureOutput::SizeOfFrameEnv() {
  return list_frame_env_.frame_env_size();
}

175 176
}  // namespace prediction
}  // namespace apollo