33 if (!layer_pb.has_name()) {
34 ADEBUG <<
"Set name at default";
37 name_ = layer_pb.name();
39 if (!layer_pb.has_order_number()) {
40 ADEBUG <<
"Set order number at default";
43 order_number_ = layer_pb.order_number();
50 AERROR <<
"Fail to Load LayerParameter!";
54 return Load(dense_pb);
59 AERROR <<
"Fail to Load weights!";
62 if (!dense_pb.has_bias() || !
LoadTensor(dense_pb.
bias(), &bias_)) {
63 AERROR <<
"Fail to Load bias!";
66 if (!dense_pb.has_use_bias()) {
67 AWARN <<
"Set use_bias as false.";
72 if (!dense_pb.has_activation()) {
73 ADEBUG <<
"Set activation as linear function";
78 units_ = dense_pb.
units();
82void Dense::Run(
const std::vector<Eigen::MatrixXf>& inputs,
83 Eigen::MatrixXf* output) {
84 CHECK_EQ(inputs.size(), 1U);
85 Eigen::MatrixXf prod =
static_cast<Eigen::MatrixXf
>(inputs[0] * weights_);
87 Eigen::MatrixXf sum = prod.rowwise() + bias_.transpose();
90 *output = prod.unaryExpr(kactivation_);
91 CHECK_EQ(output->cols(), units_);
96 AERROR <<
"Fail to Load LayerParameter!";
100 return Load(conv1d_pb);
105 AERROR <<
"Fail to Load kernel!";
108 if (!conv1d_pb.has_bias() || !
LoadTensor(conv1d_pb.
bias(), &bias_)) {
109 AERROR <<
"Fail to Load bias!";
112 if (!conv1d_pb.has_use_bias()) {
113 AWARN <<
"Set use_bias as false.";
118 for (
int sz : conv1d_pb.shape()) {
119 shape_.push_back(sz);
121 if (conv1d_pb.has_stride()) {
122 stride_ = conv1d_pb.
stride();
130 Eigen::MatrixXf* output) {
131 CHECK_EQ(inputs.size(), 1U);
132 CHECK_GT(kernel_.size(), 0U);
133 CHECK_EQ(kernel_[0].rows(), inputs[0].rows());
134 int kernel_size =
static_cast<int>(kernel_[0].cols());
136 static_cast<int>((inputs[0].cols() - kernel_size) / stride_) + 1;
137 int output_num_row =
static_cast<int>(kernel_.size());
138 output->resize(output_num_row, output_num_col);
139 for (
int i = 0; i < output_num_row; ++i) {
140 for (
int j = 0; j < output_num_col; ++j) {
141 float output_i_j_unbiased = 0.0f;
142 for (
int p = 0; p < inputs[0].rows(); ++p) {
143 for (
int q = j * stride_; q < j * stride_ + kernel_size; ++q) {
144 output_i_j_unbiased +=
145 inputs[0](p, q) * kernel_[i](p, q - j * stride_);
149 (*output)(i, j) = output_i_j_unbiased + bias_(i);
156 AERROR <<
"Fail to Load LayerParameter!";
160 return Load(maxpool1d_pb);
164 ACHECK(maxpool1d_pb.has_kernel_size());
165 CHECK_GT(maxpool1d_pb.has_kernel_size(), 0);
167 if (maxpool1d_pb.has_stride() && maxpool1d_pb.
stride() > 0) {
168 stride_ = maxpool1d_pb.
stride();
170 ADEBUG <<
"No valid stride found, use kernel size, instead";
177 Eigen::MatrixXf* output) {
178 CHECK_EQ(inputs.size(), 1U);
180 static_cast<int>((inputs[0].cols() - kernel_size_) / stride_) + 1;
181 int output_num_row =
static_cast<int>(inputs[0].rows());
182 output->resize(output_num_row, output_num_col);
184 for (
int j = 0; j < output_num_col; ++j) {
185 CHECK_LE(input_index + kernel_size_, inputs[0].cols());
186 for (
int i = 0; i < output_num_row; ++i) {
187 float output_i_j = -std::numeric_limits<float>::infinity();
188 for (
int k = input_index; k < input_index + kernel_size_; ++k) {
189 output_i_j = std::max(output_i_j, inputs[0](i, k));
191 (*output)(i, j) = output_i_j;
193 input_index += stride_;
199 AERROR <<
"Fail to Load LayerParameter!";
203 return Load(avgpool1d_pb);
207 ACHECK(avgpool1d_pb.has_kernel_size());
208 CHECK_GT(avgpool1d_pb.has_kernel_size(), 0);
210 if (avgpool1d_pb.has_stride() && avgpool1d_pb.
stride() > 0) {
211 stride_ = avgpool1d_pb.
stride();
213 ADEBUG <<
"No valid stride found, use kernel size, instead";
220 Eigen::MatrixXf* output) {
221 CHECK_EQ(inputs.size(), 1U);
223 static_cast<int>((inputs[0].cols() - kernel_size_) / stride_) + 1;
224 int output_num_row =
static_cast<int>(inputs[0].rows());
225 output->resize(output_num_row, output_num_col);
227 for (
int j = 0; j < output_num_col; ++j) {
228 CHECK_LE(input_index + kernel_size_, inputs[0].cols());
229 for (
int i = 0; i < output_num_row; ++i) {
230 float output_i_j_sum = 0.0f;
231 for (
int k = input_index; k < input_index + kernel_size_; ++k) {
232 output_i_j_sum += inputs[0](i, k);
234 (*output)(i, j) = output_i_j_sum /
static_cast<float>(kernel_size_);
236 input_index += stride_;
242 AERROR <<
"Fail to Load the layer parameters!";
245 if (!layer_pb.has_activation()) {
255 if (!activation_pb.has_activation()) {
264 Eigen::MatrixXf* output) {
265 CHECK_EQ(inputs.size(), 1U);
266 *output = inputs[0].unaryExpr(kactivation_);
271 AERROR <<
"Fail to Load the layer parameters!";
275 auto bn_pb = layer_pb.batch_normalization();
276 epsilon_ =
static_cast<float>(bn_pb.epsilon());
277 axis_ = bn_pb.axis();
278 center_ = bn_pb.center();
279 scale_ = bn_pb.scale();
280 momentum_ = bn_pb.momentum();
281 if (!bn_pb.has_mu() || !
LoadTensor(bn_pb.mu(), &mu_)) {
282 AERROR <<
"Fail to Load mu!";
285 if (!bn_pb.has_sigma() || !
LoadTensor(bn_pb.sigma(), &sigma_)) {
286 AERROR <<
"Fail to Load sigma!";
290 if (!bn_pb.has_gamma() || !
LoadTensor(bn_pb.gamma(), &gamma_)) {
291 AERROR <<
"Fail to Load gamma!";
296 if (!bn_pb.has_beta() || !
LoadTensor(bn_pb.beta(), &beta_)) {
297 AERROR <<
"Fail to Load beta!";
305 Eigen::MatrixXf* output) {
306 CHECK_EQ(inputs.size(), 1U);
307 Eigen::MatrixXf temp = (inputs[0].rowwise() - mu_.transpose());
308 Eigen::MatrixXf norm =
309 temp.array().rowwise() / (sigma_.array().sqrt() + epsilon_).transpose();
311 norm = norm.array().rowwise() * gamma_.transpose().array();
314 norm = norm.rowwise() + beta_.transpose();
321 AERROR <<
"Fail to Load the layer parameters!";
324 LSTMParameter lstm_pb = layer_pb.lstm();
325 if (!lstm_pb.has_units()) {
326 ADEBUG <<
"Fail to Load the number of units.";
329 units_ = lstm_pb.units();
331 if (!lstm_pb.has_return_sequences()) {
332 ADEBUG <<
"Set return_sequences at default.";
333 return_sequences_ =
false;
335 return_sequences_ = lstm_pb.return_sequences();
337 if (!lstm_pb.has_stateful()) {
338 ADEBUG <<
"Set stateful at default.";
341 stateful_ = lstm_pb.stateful();
343 if (!lstm_pb.has_activation()) {
344 ADEBUG <<
"Set activation function as tanh.";
349 if (!lstm_pb.has_recurrent_activation()) {
350 ADEBUG <<
"Set recurrent_activation function as hard_tanh.";
353 krecurrent_activation_ =
356 if (!lstm_pb.has_use_bias()) {
357 ADEBUG <<
"Set use_bias as true.";
360 use_bias_ = lstm_pb.use_bias();
362 if (!lstm_pb.has_unit_forget_bias()) {
363 ADEBUG <<
"Set unit forget bias as true.";
364 unit_forget_bias_ =
true;
366 unit_forget_bias_ = lstm_pb.unit_forget_bias();
368 if (!lstm_pb.has_weights_input() ||
370 AERROR <<
"Fail to Load input weights!";
373 if (!lstm_pb.has_weights_forget() ||
374 !
LoadTensor(lstm_pb.weights_forget(), &wf_)) {
375 AERROR <<
"Fail to Load forget weights!";
378 if (!lstm_pb.has_weights_cell() ||
380 AERROR <<
"Fail to Load cell weights!";
383 if (!lstm_pb.has_weights_output() ||
384 !
LoadTensor(lstm_pb.weights_output(), &wo_)) {
385 AERROR <<
"Fail to Load output weights!";
388 if (!lstm_pb.has_bias_input() || !
LoadTensor(lstm_pb.bias_input(), &bi_)) {
389 AERROR <<
"Fail to Load input bias!";
392 if (!lstm_pb.has_bias_forget() || !
LoadTensor(lstm_pb.bias_forget(), &bf_)) {
393 AERROR <<
"Fail to Load forget bias!";
396 if (!lstm_pb.has_bias_cell() || !
LoadTensor(lstm_pb.bias_cell(), &bc_)) {
397 AERROR <<
"Fail to Load cell bias!";
400 if (!lstm_pb.has_bias_output() || !
LoadTensor(lstm_pb.bias_output(), &bo_)) {
401 AERROR <<
"Fail to Load output bias!";
404 if (!lstm_pb.has_recurrent_weights_input() ||
405 !
LoadTensor(lstm_pb.recurrent_weights_input(), &r_wi_)) {
406 AERROR <<
"Fail to Load recurrent input weights!";
409 if (!lstm_pb.has_recurrent_weights_forget() ||
410 !
LoadTensor(lstm_pb.recurrent_weights_forget(), &r_wf_)) {
411 AERROR <<
"Fail to Load recurrent forget weights!";
414 if (!lstm_pb.has_recurrent_weights_cell() ||
415 !
LoadTensor(lstm_pb.recurrent_weights_cell(), &r_wc_)) {
416 AERROR <<
"Fail to Load recurrent cell weights!";
419 if (!lstm_pb.has_recurrent_weights_output() ||
420 !
LoadTensor(lstm_pb.recurrent_weights_output(), &r_wo_)) {
421 AERROR <<
"Fail to Load recurrent output weights!";
428void LSTM::Step(
const Eigen::MatrixXf& input, Eigen::MatrixXf* output,
429 Eigen::MatrixXf* ht_1, Eigen::MatrixXf* ct_1) {
430 Eigen::MatrixXf x_i = input * wi_ + bi_.transpose();
431 Eigen::MatrixXf x_f = input * wf_ + bf_.transpose();
432 Eigen::MatrixXf x_c = input * wc_ + bc_.transpose();
433 Eigen::MatrixXf x_o = input * wo_ + bo_.transpose();
435 Eigen::MatrixXf i = (x_i + (*ht_1) * r_wi_).unaryExpr(krecurrent_activation_);
436 Eigen::MatrixXf
f = (x_f + (*ht_1) * r_wf_).unaryExpr(krecurrent_activation_);
438 f.array() * ct_1->array() +
439 i.array() * ((x_c + (*ht_1) * r_wc_).unaryExpr(kactivation_)).array();
440 Eigen::MatrixXf o = (x_o + (*ht_1) * r_wo_).unaryExpr(krecurrent_activation_);
441 Eigen::MatrixXf h = o.array() * (c.unaryExpr(kactivation_)).array();
448void LSTM::Run(
const std::vector<Eigen::MatrixXf>& inputs,
449 Eigen::MatrixXf* output) {
450 CHECK_EQ(inputs.size(), 1U);
451 Eigen::MatrixXf sequences(inputs[0].rows(), units_);
452 Eigen::MatrixXf temp;
453 for (
int i = 0; i < inputs[0].rows(); ++i) {
454 Step(inputs[0].row(i), &temp, &ht_1_, &ct_1_);
455 sequences.row(i) = temp.row(0);
457 if (return_sequences_) {
460 *output = temp.row(0);
465 ht_1_.resize(1, units_);
466 ct_1_.resize(1, units_);
473 states->at(0) = ht_1_;
474 states->at(1) = ct_1_;
478 CHECK_EQ(states.size(), 2U);
479 CHECK_EQ(states[0].rows(), 1);
480 CHECK_EQ(states[1].rows(), 1);
481 CHECK_EQ(states[0].cols(), units_);
482 CHECK_EQ(states[1].cols(), units_);
489 AERROR <<
"Fail to Load the layer parameters!";
496 Eigen::MatrixXf* output) {
497 CHECK_EQ(inputs.size(), 1U);
498 Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> inp(
500 inp.resize(1, inp.size());
506 AERROR <<
"Fail to Load the layer parameters!";
510 if (input_pb.input_shape_size() < 1) {
511 AERROR <<
"Fail to Load input shape of InputLayer!";
514 input_shape_.resize(input_pb.input_shape_size());
515 for (
int i = 0; i < input_pb.input_shape_size(); ++i) {
519 if (!input_pb.has_dtype()) {
520 ADEBUG <<
"Set the type of input as float!";
523 dtype_ = input_pb.
dtype();
525 if (!input_pb.has_sparse()) {
526 ADEBUG <<
"set the sparse of input as false!";
529 sparse_ = input_pb.
sparse();
535 Eigen::MatrixXf* output) {
536 CHECK_EQ(inputs.size(), 1U);
537 CHECK_EQ(inputs[0].cols(), input_shape_.back());
543 AERROR <<
"Fail to Load the layer parameters!";
546 ConcatenateParameter concat_pb = layer_pb.concatenate();
547 if (!concat_pb.has_axis()) {
548 AERROR <<
"Fail to Load the concatenate!";
551 axis_ = concat_pb.axis();
556 Eigen::MatrixXf* output) {
557 CHECK_EQ(inputs.size(), 2U);
558 CHECK_EQ(inputs[0].rows(), inputs[1].rows());
559 output->resize(inputs[0].rows(), inputs[0].cols() + inputs[1].cols());
560 *output << inputs[0], inputs[1];
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the parameter from a pb message
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the dense layer parameter from a pb message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the parameter from a pb message
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the dense layer parameter from a pb message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the dense layer parameter from a pb message
void SetState(const std::vector< Eigen::MatrixXf > &states) override
Set the internal state and memory cell state
void ResetState() override
Reset the internal state and memory cell state as zero-matrix
void State(std::vector< Eigen::MatrixXf > *states) const override
Access to the internal state and memory cell state
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
virtual bool Load(const apollo::prediction::LayerParameter &layer_pb)
Load layer parameters from a protobuf message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
std::function< float(float)> serialize_to_function(const std::string &str)
translate a string into a network activation function
bool LoadTensor(const TensorParameter &tensor_pb, Eigen::MatrixXf *matrix)
load matrix value from a protobuf message
optional string activation
optional int32 kernel_size
optional TensorParameter bias
optional TensorParameter kernel
optional string activation
optional TensorParameter weights
optional TensorParameter bias
optional int32 kernel_size