Apollo 10.0
自动驾驶开放平台
ccrf_type_fusion.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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
28namespace apollo {
29namespace perception {
30namespace radar4d {
31
32using ObjectPtr = std::shared_ptr<apollo::perception::base::Object>;
34
36 std::string config_file = "ccrf_type_fusion.pb.txt";
37 if (!options.config_file.empty()) {
38 config_file = options.config_file;
39 }
40 config_file = GetConfigFile(options.config_path, config_file);
42 ACHECK(cyber::common::GetProtoFromFile(config_file, &config));
43 std::string classifiers_property_file_path =
46 ACHECK(util::LoadMultipleMatricesFile(classifiers_property_file_path,
48
49 for (auto& pair : smooth_matrices_) {
50 util::NormalizeRow(&pair.second);
51 pair.second.transposeInPlace();
52 AINFO << "Source: " << pair.first;
53 AINFO << std::endl << pair.second;
54 }
55
56 confidence_smooth_matrix_ = Matrixd::Identity();
57 auto iter = smooth_matrices_.find("Confidence");
58 if (iter != smooth_matrices_.end()) {
59 confidence_smooth_matrix_ = iter->second;
60 smooth_matrices_.erase(iter);
61 }
62 AINFO << "Confidence: ";
63 AINFO << std::endl << confidence_smooth_matrix_;
64
65 return true;
66}
67
69 ObjectPtr object) {
70 if (object == nullptr) {
71 return false;
72 }
73 Vectord log_prob;
74 if (!FuseOneShotTypeProbs(object, &log_prob)) {
75 return false;
76 }
77 util::ToExp(&log_prob);
78 util::Normalize(&log_prob);
79 util::FromEigenToVector(log_prob, &(object->type_probs));
80 object->type = static_cast<ObjectType>(std::distance(
81 object->type_probs.begin(),
82 std::max_element(object->type_probs.begin(), object->type_probs.end())));
83 return true;
84}
85
87 Vectord* log_prob) {
88 if (object == nullptr) {
89 return false;
90 }
91 if (log_prob == nullptr) {
92 return false;
93 }
94 const auto& vecs = object->radar4d_supplement.raw_probs;
95 const auto& names = object->radar4d_supplement.raw_classification_methods;
96 if (vecs.empty()) {
97 return false;
98 }
99
100 log_prob->setZero();
101
102 Vectord single_prob;
103 static const Vectord epsilon = Vectord::Ones() * 1e-6;
104 float conf = object->confidence;
105 for (size_t i = 0; i < vecs.size(); ++i) {
106 auto& vec = vecs[i];
107 util::FromStdToVector(vec, &single_prob);
108 auto iter = smooth_matrices_.find(names[i]);
109 if (vecs.size() == 1 || iter == smooth_matrices_.end()) {
110 single_prob = single_prob + epsilon;
111 } else {
112 single_prob = iter->second * single_prob + epsilon;
113 }
114 util::Normalize(&single_prob);
115 // p(c|x) = p(c|x,o)p(o|x)+ p(c|x,~o)p(~o|x)
116 single_prob = conf * single_prob +
117 (1.0 - conf) * confidence_smooth_matrix_ * single_prob;
118 util::ToLog(&single_prob);
119 *log_prob += single_prob;
120 }
121
122 return true;
123}
124
126 TypeFusionInitOption one_shot_fuser_options;
127 one_shot_fuser_options.config_path = options.config_path;
128 ACHECK(one_shot_fuser_.Init(one_shot_fuser_options));
129
130 std::string config_file = "ccrf_type_fusion.pb.txt";
131 if (!options.config_file.empty()) {
132 config_file = options.config_file;
133 }
134 config_file = GetConfigFile(options.config_path, config_file);
136 ACHECK(cyber::common::GetProtoFromFile(config_file, &config));
137 std::string transition_property_file_path =
141 ACHECK(util::LoadSingleMatrixFile(transition_property_file_path,
143 transition_matrix_ += Matrixd::Ones() * 1e-6;
145 AINFO << "transition matrix";
146 AINFO << std::endl << transition_matrix_;
147 for (std::size_t i = 0; i < VALID_OBJECT_TYPE; ++i) {
148 for (std::size_t j = 0; j < VALID_OBJECT_TYPE; ++j) {
149 transition_matrix_(i, j) = log(transition_matrix_(i, j));
150 }
151 }
152 AINFO << std::endl << transition_matrix_;
153 return true;
154}
155
157 TrackedObjects* tracked_objects) {
158 if (tracked_objects == nullptr) {
159 return false;
160 }
161 if (tracked_objects->empty()) {
162 return false;
163 }
164 return FuseWithConditionalProbabilityInference(tracked_objects);
165}
166
168 TrackedObjects* tracked_objects) {
169 // AINFO << "Enter fuse with conditional probability inference";
170 fused_oneshot_probs_.resize(tracked_objects->size());
171
172 std::size_t i = 0;
173 for (auto& pair : *tracked_objects) {
174 ObjectPtr& object = pair.second;
176 &fused_oneshot_probs_[i++])) {
177 AERROR << "Failed to fuse one short probs in sequence.";
178 return false;
179 }
180 }
181
182 // Use viterbi algorithm to infer the state
183 std::size_t length = tracked_objects->size();
184 fused_sequence_probs_.resize(length);
185 state_back_trace_.resize(length);
186
188 // Add priori knowledge to suppress the sudden-appeared object types.
189 fused_sequence_probs_[0] += transition_matrix_.row(0).transpose();
190
191 for (std::size_t i = 1; i < length; ++i) {
192 for (std::size_t right = 0; right < VALID_OBJECT_TYPE; ++right) {
193 double prob = 0.0;
194 double max_prob = -std::numeric_limits<double>::max();
195 std::size_t id = 0;
196 for (std::size_t left = 0; left < VALID_OBJECT_TYPE; ++left) {
197 prob = fused_sequence_probs_[i - 1](left) +
198 transition_matrix_(left, right) * s_alpha_ +
199 fused_oneshot_probs_[i](right);
200 if (prob > max_prob) {
201 max_prob = prob;
202 id = left;
203 }
204 }
205 fused_sequence_probs_[i](right) = max_prob;
206 state_back_trace_[i](right) = static_cast<int>(id);
207 }
208 }
209 ObjectPtr object = tracked_objects->rbegin()->second;
210 RecoverFromLogProbability(&fused_sequence_probs_.back(), &object->type_probs,
211 &object->type);
212 return true;
213}
214
216 std::vector<float>* dst,
217 ObjectType* type) {
218 util::ToExpStable(prob);
219 util::Normalize(prob);
220 util::FromEigenToVector(*prob, dst);
221 *type = static_cast<ObjectType>(
222 std::distance(dst->begin(), std::max_element(dst->begin(), dst->end())));
223 return true;
224}
225
228
229} // namespace radar4d
230} // namespace perception
231} // namespace apollo
bool TypeFusion(const TypeFusionOption &option, std::shared_ptr< perception::base::Object > object) override
Type fusion of objects
EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool Init(const TypeFusionInitOption &option) override
INit type fusion
bool FuseOneShotTypeProbs(const std::shared_ptr< perception::base::Object > &object, Vectord *log_prob)
Get object type probabilities
apollo::common::EigenMap< std::string, Matrixd > smooth_matrices_
bool TypeFusion(const TypeFusionOption &option, TrackedObjects *tracked_objects) override
Type fusion of tracked object
bool RecoverFromLogProbability(Vectord *prob, std::vector< float > *dst, perception::base::ObjectType *type)
bool FuseWithConditionalProbabilityInference(TrackedObjects *tracked_objects)
apollo::common::EigenVector< Vectord > fused_sequence_probs_
apollo::common::EigenVector< Vectori > state_back_trace_
apollo::common::EigenVector< Vectord > fused_oneshot_probs_
EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool Init(const TypeFusionInitOption &option) override
Init type fusion
#define PERCEPTION_REGISTER_ONESHOTTYPEFUSION(name)
#define PERCEPTION_REGISTER_SEQUENCETYPEFUSION(name)
#define ACHECK(cond)
Definition log.h:80
#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
bool LoadSingleMatrixFile(const std::string &filename, Matrixd *matrix)
Load single matrix from file
Definition util.cc:94
void ToExp(Vectord *prob)
Compute exponential of Vectord
Definition util.cc:52
bool LoadMultipleMatricesFile(const std::string &filename, EigenMap< std::string, Matrixd > *matrices)
Definition util.cc:108
void Normalize(Vectord *prob)
Compute normalize of Vectord
Definition util.cc:65
void ToLog(Vectord *prob)
Compute log of Vectord
Definition util.cc:46
void NormalizeRow(Matrixd *prob)
Compute normalize row of Matrixd
Definition util.cc:71
void FromEigenToVector(const Vectord &src_prob, std::vector< float > *dst_prob)
Transfrom Eigen to vector
Definition util.cc:37
void FromStdToVector(const std::vector< float > &src_prob, Vectord *dst_prob)
From std to vector
Definition util.cc:29
std::shared_ptr< apollo::perception::base::Object > ObjectPtr
Eigen::Matrix< double, VALID_OBJECT_TYPE, 1 > Vectord
Definition util.h:38
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