Apollo 11.0
自动驾驶开放平台
ccrf_type_fusion.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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
17#include <limits>
18
19#include "cyber/common/file.h"
20#include "cyber/common/log.h"
21
25
27#include "modules/perception/lidar_tracking/tracker/type_fusion/proto/ccrf_type_filter_config.pb.h"
28
29namespace apollo {
30namespace perception {
31namespace lidar {
32
33using ObjectPtr = std::shared_ptr<apollo::perception::base::Object>;
35
37 std::string config_file = "ccrf_type_fusion.conf";
38 if (!options.config_file.empty()) {
39 config_file = options.config_file;
40 }
41 config_file = GetConfigFile(options.config_path, config_file);
42
44 ACHECK(cyber::common::GetProtoFromFile(config_file, &config));
45 std::string classifiers_property_file_path
46 = GetConfigFile(options.config_path,
48 debug_log_ = options.debug_log;
49 ACHECK(type_util::LoadMultipleMatricesFile(classifiers_property_file_path,
51
52 for (auto& pair : smooth_matrices_) {
53 type_util::NormalizeRow(&pair.second);
54 pair.second.transposeInPlace();
55 AINFO << "Source: " << pair.first << std::endl << pair.second;
56 }
57
58 confidence_smooth_matrix_ = Matrixd::Identity();
59 auto iter = smooth_matrices_.find("Confidence");
60 if (iter != smooth_matrices_.end()) {
61 confidence_smooth_matrix_ = iter->second;
62 smooth_matrices_.erase(iter);
63 }
64 AINFO << "ConfSmoothMatrix: " << std::endl << confidence_smooth_matrix_;
65
66 return true;
67}
68
70 TrackedObjectPtr new_object) {
71 Vectord log_prob;
72 if (!FuseOneShotTypeProbs(new_object, &log_prob)) {
73 return false;
74 }
75 type_util::ToExp(&log_prob);
76 type_util::Normalize(&log_prob);
77 type_util::FromEigenToVector(log_prob, &(new_object->type_probs));
78 new_object->type = static_cast<ObjectType>(std::distance(
79 new_object->type_probs.begin(),
80 std::max_element(new_object->type_probs.begin(),
81 new_object->type_probs.end())));
82 return true;
83}
84
86 const TrackedObjectConstPtr& new_object, Vectord* log_prob) {
87 const auto& object = new_object->object_ptr;
88 if (log_prob == nullptr) {
89 return false;
90 }
91 const auto& vecs = object->lidar_supplement.raw_probs;
92 const auto& names = object->lidar_supplement.raw_classification_methods;
93 if (vecs.empty()) {
94 return false;
95 }
96
97 log_prob->setZero();
98
99 Vectord single_prob;
100 static const Vectord epsilon = Vectord::Ones() * 1e-6;
101 float conf = object->confidence;
102 for (size_t i = 0; i < vecs.size(); ++i) {
103 auto& vec = vecs[i];
104 // avoid parsing-wrong-type-probs
105 size_t type_count = static_cast<size_t>(ObjectType::MAX_OBJECT_TYPE);
106 if (vec.size() != type_count) {
107 AERROR << "[TypeFilter] Encounter Unknown type_probs";
108 continue;
109 }
110 type_util::FromStdToVector(vec, &single_prob);
111 auto iter = smooth_matrices_.find(names[i]);
112 if (iter == smooth_matrices_.end()) {
113 single_prob = single_prob + epsilon;
114 } else {
115 single_prob = iter->second * single_prob + epsilon;
116 }
117 type_util::Normalize(&single_prob);
118 // p(c|x) = p(c|x,o)p(o|x)+ p(c|x,~o)p(~o|x)
119 Vectord tmp = confidence_smooth_matrix_ * single_prob;
121 single_prob = conf * single_prob + (1.0 - conf) * tmp;
122
123 type_util::Normalize(&single_prob);
124 type_util::ToLog(&single_prob);
125 *log_prob += single_prob;
126 }
127
128 return true;
129}
130
132 TypeFilterInitOption one_shot_fuser_options;
133 one_shot_fuser_options.config_path = options.config_path;
134 ACHECK(one_shot_fuser_.Init(one_shot_fuser_options));
135
136 std::string config_file = "ccrf_type_fusion.conf";
137 if (!options.config_file.empty()) {
138 config_file = options.config_file;
139 }
140 config_file = GetConfigFile(options.config_path, config_file);
142 ACHECK(cyber::common::GetProtoFromFile(config_file, &config));
143
144 std::string transition_property_file_path
145 = GetConfigFile(options.config_path,
148 debug_log_ = options.debug_log;
149 ACHECK(type_util::LoadSingleMatrixFile(transition_property_file_path,
151 transition_matrix_ += Matrixd::Ones() * 1e-6;
152 for (std::size_t i = 0; i < VALID_OBJECT_TYPE; ++i) {
154 }
155 AINFO << "TypeTransitionMatrix: " << std::endl << transition_matrix_;
156
157 for (std::size_t i = 0; i < VALID_OBJECT_TYPE; ++i) {
158 for (std::size_t j = 0; j < VALID_OBJECT_TYPE; ++j) {
159 transition_matrix_(i, j) = log(transition_matrix_(i, j));
160 }
161 }
162 AINFO << "TypeTransitionMatrix(Log): " << std::endl << transition_matrix_;
163 return true;
164}
165
167 const TypeFilterOption& option,
168 const std::vector<TrackedObjectConstPtr>& tracked_objects,
169 TrackedObjectPtr new_object) {
170 return FuseWithConditionalProbabilityInference(tracked_objects,
171 new_object);
172}
173
175 const std::vector<TrackedObjectConstPtr>& tracked_objects,
176 TrackedObjectPtr new_object) {
177 fused_oneshot_probs_.resize(tracked_objects.size() + 1);
178 std::size_t i = 0;
179 for (const auto& obj : tracked_objects) {
180 // ObjectPtr& object = pair.second;
182 &fused_oneshot_probs_[i++])) {
183 AERROR << "Failed to fuse one short probs in sequence.";
184 return false;
185 }
186 }
188 &fused_oneshot_probs_[i++])) {
189 AERROR << "Failed to fuse one short probs for new object.";
190 return false;
191 }
192 // Use viterbi algorithm to infer the state
193 std::size_t length = fused_oneshot_probs_.size();
194 fused_sequence_probs_.resize(length);
195 state_back_trace_.resize(length);
196
198 // Add priori knowledge to suppress the sudden-appeared object types.
199 // fused_sequence_probs_[0] += transition_matrix_.row(0).transpose();
200
201 for (std::size_t i = 1; i < length; ++i) {
202 for (std::size_t right = 0; right < VALID_OBJECT_TYPE; ++right) {
203 double prob = 0.0;
204 double max_prob = -std::numeric_limits<double>::max();
205 std::size_t id = 0;
206 for (std::size_t left = 0; left < VALID_OBJECT_TYPE; ++left) {
207 prob = fused_sequence_probs_[i - 1](left)
208 + transition_matrix_(left, right) * s_alpha_
209 + fused_oneshot_probs_[i](right);
210 if (prob > max_prob) {
211 max_prob = prob;
212 id = left;
213 }
214 }
215 fused_sequence_probs_[i](right) = max_prob;
216 state_back_trace_[i](right) = static_cast<int>(id);
217 }
221 }
222 // ObjectPtr object = tracked_objects->rbegin()->second;
224 &new_object->type_probs, &new_object->type);
225 if (debug_log_) {
226 std::stringstream sstr;
227 sstr << " total_size: " << fused_oneshot_probs_.size();
228 for (size_t i = 0; i < fused_oneshot_probs_.size(); i++) {
232 sstr << " After-Smooth[" << std::to_string(i) << "]: ";
233 for (size_t j = 0; j < 4; j++) {
234 sstr << tmp(j) << ", ";
235 }
236 }
237 ADEBUG << sstr.str();
238 }
239 return true;
240}
241
243 std::vector<float>* dst, ObjectType* type) {
247 *type = static_cast<ObjectType>(std::distance(dst->begin(),
248 std::max_element(dst->begin(), dst->end())));
249 return true;
250}
251
254
255} // namespace lidar
256} // namespace perception
257} // namespace apollo
bool TypeFusion(const TypeFilterOption &option, const std::vector< TrackedObjectConstPtr > &tracked_objects, TrackedObjectPtr new_object) override
apollo::common::EigenVector< Vectord > fused_sequence_probs_
bool RecoverFromLogProbability(Vectord *prob, std::vector< float > *dst, perception::base::ObjectType *type)
EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool Init(const TypeFilterInitOption &option) override
bool FuseWithConditionalProbabilityInference(const std::vector< TrackedObjectConstPtr > &tracked_objects, TrackedObjectPtr new_object)
apollo::common::EigenVector< Vectori > state_back_trace_
apollo::common::EigenVector< Vectord > fused_oneshot_probs_
bool TypeFusion(const TypeFilterOption &option, TrackedObjectPtr new_object) override
bool FuseOneShotTypeProbs(const TrackedObjectConstPtr &new_object, Vectord *log_prob)
EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool Init(const TypeFilterInitOption &option) override
apollo::common::EigenMap< std::string, Matrixd > smooth_matrices_
#define PERCEPTION_REGISTER_ONESHOTTYPEFUSION(name)
#define PERCEPTION_REGISTER_SEQUENCETYPEFUSION(name)
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
void ToExpStable(Vectord *prob)
Compute stable exponential of Vectord
Definition util.cc:58
void ToExp(Vectord *prob)
Compute exponential of Vectord
Definition util.cc:52
bool LoadSingleMatrixFile(const std::string &filename, Matrixd *matrix)
Load single matrix from file
Definition util.cc:94
void FromStdToVector(const std::vector< float > &src_prob, Vectord *dst_prob)
From std to vector
Definition util.cc:29
void NormalizeRow(Matrixd *prob)
Compute normalize row of Matrixd
Definition util.cc:71
void ToLog(Vectord *prob)
Compute log of Vectord
Definition util.cc:46
void Normalize(Vectord *prob)
Compute normalize of Vectord
Definition util.cc:65
bool LoadMultipleMatricesFile(const std::string &filename, EigenMap< std::string, Matrixd > *matrices)
Definition util.cc:108
void FromEigenToVector(const Vectord &src_prob, std::vector< float > *dst_prob)
Transfrom Eigen to vector
Definition util.cc:37
std::shared_ptr< const TrackedObject > TrackedObjectConstPtr
Eigen::Matrix< double, VALID_OBJECT_TYPE, 1 > Vectord
Definition util.h:38
std::shared_ptr< apollo::perception::base::Object > ObjectPtr
std::shared_ptr< TrackedObject > TrackedObjectPtr
std::string GetConfigFile(const std::string &config_path, const std::string &config_file)
Definition util.cc:80
class register implement
Definition arena_queue.h:37