Apollo 11.0
自动驾驶开放平台
net_util.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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
18
19#include <unordered_map>
20
21#include "cyber/common/log.h"
22
23namespace apollo {
24namespace prediction {
25namespace network {
26
27float sigmoid(const float x) { return 1.0f / (1.0f + std::exp(-x)); }
28
29float tanh(const float x) { return std::tanh(x); }
30
31float linear(const float x) { return x; }
32
33float hard_sigmoid(const float x) {
34 const float z = 0.2f * x + 0.5f;
35 return z <= 0.0f ? 0.0f : (z <= 1.0f ? z : 1.0f);
36}
37
38float relu(const float x) { return (x > 0.0f) ? x : 0.0f; }
39
40Eigen::MatrixXf FlattenMatrix(const Eigen::MatrixXf& matrix) {
41 CHECK_GT(matrix.rows(), 0);
42 CHECK_GT(matrix.cols(), 0);
43 int output_size = static_cast<int>(matrix.rows() * matrix.cols());
44 Eigen::MatrixXf output_matrix;
45 output_matrix.resize(1, output_size);
46 int output_index = 0;
47 for (int i = 0; i < matrix.rows(); ++i) {
48 for (int j = 0; j < matrix.cols(); ++j) {
49 output_matrix(0, output_index) = matrix(i, j);
50 ++output_index;
51 }
52 }
53 return output_matrix;
54}
55
56std::function<float(float)> serialize_to_function(const std::string& str) {
57 static const std::unordered_map<std::string, std::function<float(float)>>
58 func_map({{"linear", linear},
59 {"tanh", tanh},
60 {"sigmoid", sigmoid},
61 {"hard_sigmoid", hard_sigmoid},
62 {"relu", relu}});
63 return func_map.at(str);
64}
65
66bool LoadTensor(const TensorParameter& tensor_pb, Eigen::MatrixXf* matrix) {
67 if (tensor_pb.data().empty() || tensor_pb.shape().empty()) {
68 AERROR << "Fail to load the necessary fields!";
69 return false;
70 }
71 if (tensor_pb.shape_size() < 2) {
72 ADEBUG << "Load tensor size: (1, " << tensor_pb.shape(0) << ")";
73 matrix->resize(1, tensor_pb.shape(0));
74 for (int i = 0; i < tensor_pb.shape(0); ++i) {
75 (*matrix)(0, i) = static_cast<float>(tensor_pb.data(i));
76 }
77 return true;
78 }
79 ADEBUG << "Load tensor size: (" << tensor_pb.shape(0) << ", "
80 << tensor_pb.shape(1) << ")";
81 CHECK_EQ(tensor_pb.shape_size(), 2);
82 matrix->resize(tensor_pb.shape(0), tensor_pb.shape(1));
83 for (int i = 0; i < tensor_pb.shape(0); ++i) {
84 for (int j = 0; j < tensor_pb.shape(1); ++j) {
85 (*matrix)(i, j) =
86 static_cast<float>(tensor_pb.data(i * tensor_pb.shape(1) + j));
87 }
88 }
89 return true;
90}
91
92bool LoadTensor(const TensorParameter& tensor_pb, Eigen::VectorXf* vector) {
93 if (tensor_pb.data().empty() || tensor_pb.shape().empty()) {
94 AERROR << "Fail to load the necessary fields!";
95 return false;
96 }
97 ADEBUG << "Load tensor size: (" << tensor_pb.shape(0) << ", 1)";
98 CHECK_EQ(tensor_pb.shape_size(), 1);
99 if (tensor_pb.shape_size() == 1) {
100 vector->resize(tensor_pb.shape(0));
101 for (int i = 0; i < tensor_pb.shape(0); ++i) {
102 (*vector)(i) = static_cast<float>(tensor_pb.data(i));
103 }
104 }
105 return true;
106}
107
108bool LoadTensor(const TensorParameter& tensor_pb,
109 std::vector<Eigen::MatrixXf>* const tensor3d) {
110 if (tensor_pb.data().empty() || tensor_pb.shape_size() != 3) {
111 AERROR << "Fail to load the necessary fields!";
112 return false;
113 }
114 int num_depth = tensor_pb.shape(0);
115 int num_row = tensor_pb.shape(1);
116 int num_col = tensor_pb.shape(2);
117 CHECK_EQ(tensor_pb.data_size(), num_depth * num_row * num_col);
118 int tensor_pb_index = 0;
119 for (int k = 0; k < num_depth; ++k) {
120 Eigen::MatrixXf matrix = Eigen::MatrixXf::Zero(num_row, num_col);
121 for (int i = 0; i < num_row; ++i) {
122 for (int j = 0; j < num_col; ++j) {
123 matrix(i, j) = tensor_pb.data(tensor_pb_index);
124 ++tensor_pb_index;
125 }
126 }
127 tensor3d->push_back(matrix);
128 }
129 CHECK_EQ(tensor_pb_index, num_depth * num_row * num_col);
130 return true;
131}
132
133} // namespace network
134} // namespace prediction
135} // namespace apollo
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
float linear(const float x)
linear function: f(x) = x
Definition net_util.cc:31
std::function< float(float)> serialize_to_function(const std::string &str)
translate a string into a network activation function
Definition net_util.cc:56
float tanh(const float x)
hyperbolic tangent function: f(x) = (1 + exp(-2x)) / (1 - exp(-2x))
Definition net_util.cc:29
float hard_sigmoid(const float x)
"hard" sigmoid function: | 0.0 x in (-oo, 0) f(x) = | 0.2x + 0.5 x in [0, 2.5] | 1....
Definition net_util.cc:33
Eigen::MatrixXf FlattenMatrix(const Eigen::MatrixXf &matrix)
flatten a matrix to a row vector
Definition net_util.cc:40
float sigmoid(const float x)
sigmoid function: f(x) = 1 / (1 + exp(-x))
Definition net_util.cc:27
bool LoadTensor(const TensorParameter &tensor_pb, Eigen::MatrixXf *matrix)
load matrix value from a protobuf message
Definition net_util.cc:66
float relu(const float x)
relu function: | 0.0 x in (-oo, 0.0) f(x) = | | x x in [0.0, +oo)
Definition net_util.cc:38
class register implement
Definition arena_queue.h:37
repeated float data
repeated int32 shape