Apollo 10.0
自动驾驶开放平台
util.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 "cyber/common/log.h"
18
20
21namespace apollo {
22namespace perception {
23namespace radar4d {
24namespace util {
25
28
29void FromStdToVector(const std::vector<float>& src_prob, Vectord* dst_prob) {
30 (*dst_prob)(0) = src_prob[0];
31 for (size_t i = 3; i < static_cast<size_t>(ObjectType::MAX_OBJECT_TYPE);
32 ++i) {
33 (*dst_prob)(i - 2) = static_cast<double>(src_prob[i]);
34 }
35}
36
37void FromEigenToVector(const Vectord& src_prob, std::vector<float>* dst_prob) {
38 dst_prob->assign(static_cast<int>(ObjectType::MAX_OBJECT_TYPE), 0);
39 dst_prob->at(0) = static_cast<float>(src_prob(0));
40 for (size_t i = 3; i < static_cast<size_t>(ObjectType::MAX_OBJECT_TYPE);
41 ++i) {
42 dst_prob->at(i) = static_cast<float>(src_prob(i - 2));
43 }
44}
45
46void ToLog(Vectord* prob) {
47 for (size_t i = 0; i < VALID_OBJECT_TYPE; ++i) {
48 (*prob)(i) = log((*prob)(i));
49 }
50}
51
52void ToExp(Vectord* prob) {
53 for (size_t i = 0; i < VALID_OBJECT_TYPE; ++i) {
54 (*prob)(i) = exp((*prob)(i));
55 }
56}
57
58void ToExpStable(Vectord* prob) {
59 double min_value = prob->minCoeff();
60 for (size_t i = 0; i < VALID_OBJECT_TYPE; ++i) {
61 (*prob)(i) = exp((*prob)(i)-min_value);
62 }
63}
64
65void Normalize(Vectord* prob) {
66 double sum = prob->sum();
67 sum = sum < 1e-9 ? 1e-9 : sum;
68 *prob /= sum;
69}
70
71void NormalizeRow(Matrixd* prob) {
72 double sum = 0.0;
73 for (size_t row = 0; row < VALID_OBJECT_TYPE; ++row) {
74 sum = 0.0;
75 for (size_t col = 0; col < VALID_OBJECT_TYPE; ++col) {
76 sum += (*prob)(row, col);
77 }
78 sum = sum < 1e-9 ? 1e-9 : sum;
79 for (size_t col = 0; col < VALID_OBJECT_TYPE; ++col) {
80 (*prob)(row, col) /= sum;
81 }
82 }
83}
84
85bool LoadSingleMatrix(std::ifstream& fin, Matrixd* matrix) {
86 for (size_t row = 0; row < VALID_OBJECT_TYPE; ++row) {
87 for (size_t col = 0; col < VALID_OBJECT_TYPE; ++col) {
88 fin >> (*matrix)(row, col);
89 }
90 }
91 return true;
92}
93
94bool LoadSingleMatrixFile(const std::string& filename, Matrixd* matrix) {
95 if (matrix == nullptr) {
96 return false;
97 }
98 std::ifstream fin(filename);
99 if (!fin.is_open()) {
100 AERROR << "Fail to open file: " << filename;
101 return false;
102 }
103 LoadSingleMatrix(fin, matrix);
104 fin.close();
105 return true;
106}
107
108bool LoadMultipleMatricesFile(const std::string& filename,
109 EigenMap<std::string, Matrixd>* matrices) {
110 if (matrices == nullptr) {
111 return false;
112 }
113 std::ifstream fin(filename);
114 if (!fin.is_open()) {
115 AERROR << "Fail to open file: " << filename;
116 return false;
117 }
118 matrices->clear();
119 size_t num = 0;
120 fin >> num;
121 if (num > 100) {
122 fin.close();
123 return false;
124 }
125 for (size_t i = 0; i < num; ++i) {
126 std::string name;
127 fin >> name;
128 Matrixd matrix;
129 LoadSingleMatrix(fin, &matrix);
130 matrices->emplace(name, matrix);
131 }
132 fin.close();
133 return true;
134}
135
136} // namespace util
137} // namespace radar4d
138} // namespace perception
139} // namespace apollo
#define AERROR
Definition log.h:44
std::map< T, EigenType, std::less< T >, Eigen::aligned_allocator< std::pair< const T, EigenType > > > EigenMap
Definition eigen_defs.h:40
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
bool LoadSingleMatrix(std::ifstream &fin, Matrixd *matrix)
Load single matrix
Definition util.cc:85
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
Eigen::Matrix< double, VALID_OBJECT_TYPE, VALID_OBJECT_TYPE > Matrixd
Definition util.h:40
Eigen::Matrix< double, VALID_OBJECT_TYPE, 1 > Vectord
Definition util.h:38
class register implement
Definition arena_queue.h:37