Apollo 11.0
自动驾驶开放平台
net_layer.h
浏览该文件的文档.
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
17#include <string>
18#include <vector>
19
21
22#pragma once
23
28namespace apollo {
29namespace prediction {
30namespace network {
31
38class Layer {
39 public:
43 Layer() = default;
44
48 virtual ~Layer() = default;
49
55 virtual bool Load(const apollo::prediction::LayerParameter& layer_pb);
56
60 virtual void ResetState() {}
61
66 virtual void SetState(const std::vector<Eigen::MatrixXf>& states) {}
67
72 virtual void State(std::vector<Eigen::MatrixXf>* states) const {}
73
79 virtual void Run(const std::vector<Eigen::MatrixXf>& inputs,
80 Eigen::MatrixXf* output) = 0;
81
86 std::string Name() const { return name_; }
87
92 int OrderNumber() const { return order_number_; }
93
94 private:
95 std::string name_;
96 int order_number_ = -1;
97};
98
108class Dense : public Layer {
109 public:
115 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
116
122 bool Load(const apollo::prediction::DenseParameter& layer_pb);
123
129 void Run(const std::vector<Eigen::MatrixXf>& inputs,
130 Eigen::MatrixXf* output) override;
131
132 private:
133 int units_;
134 bool use_bias_;
135 Eigen::MatrixXf weights_;
136 Eigen::VectorXf bias_;
137 std::function<float(float)> kactivation_;
138};
139
149class Conv1d : public Layer {
150 public:
156 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
157
163 bool Load(const apollo::prediction::Conv1dParameter& conv1d_pb);
164
170 void Run(const std::vector<Eigen::MatrixXf>& inputs,
171 Eigen::MatrixXf* output) override;
172
173 private:
174 std::vector<int> shape_;
175 bool use_bias_;
176 std::vector<Eigen::MatrixXf> kernel_;
177 Eigen::VectorXf bias_;
178 int stride_;
179};
180
185class MaxPool1d : public Layer {
186 public:
192 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
193
199 bool Load(const apollo::prediction::MaxPool1dParameter& maxpool1d_pb);
200
206 void Run(const std::vector<Eigen::MatrixXf>& inputs,
207 Eigen::MatrixXf* output) override;
208
209 private:
210 int kernel_size_;
211 int stride_;
212};
213
218class AvgPool1d : public Layer {
219 public:
225 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
226
232 bool Load(const apollo::prediction::AvgPool1dParameter& avgpool1d_pb);
233
239 void Run(const std::vector<Eigen::MatrixXf>& inputs,
240 Eigen::MatrixXf* output) override;
241
242 private:
243 int kernel_size_;
244 int stride_;
245};
246
255class Activation : public Layer {
256 public:
262 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
263
269 bool Load(const apollo::prediction::ActivationParameter& activation_pb);
270
276 void Run(const std::vector<Eigen::MatrixXf>& inputs,
277 Eigen::MatrixXf* output) override;
278
279 private:
280 std::function<float(float)> kactivation_;
281};
282
288class BatchNormalization : public Layer {
289 public:
295 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
296
302 void Run(const std::vector<Eigen::MatrixXf>& inputs,
303 Eigen::MatrixXf* output) override;
304
305 private:
306 Eigen::VectorXf mu_;
307 Eigen::VectorXf sigma_;
308 Eigen::VectorXf gamma_;
309 Eigen::VectorXf beta_;
310 float epsilon_ = 0.0f;
311 float momentum_ = 0.0f;
312 int axis_ = 0;
313 bool center_ = false;
314 bool scale_ = false;
315};
316
322class LSTM : public Layer {
323 public:
329 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
330
336 void Run(const std::vector<Eigen::MatrixXf>& inputs,
337 Eigen::MatrixXf* output) override;
338
342 void ResetState() override;
343
348 void SetState(const std::vector<Eigen::MatrixXf>& states) override;
349
354 void State(std::vector<Eigen::MatrixXf>* states) const override;
355
356 private:
364 void Step(const Eigen::MatrixXf& input, Eigen::MatrixXf* output,
365 Eigen::MatrixXf* ht_1, Eigen::MatrixXf* ct_1);
366
367 Eigen::MatrixXf wi_;
368 Eigen::MatrixXf wf_;
369 Eigen::MatrixXf wc_;
370 Eigen::MatrixXf wo_;
371 Eigen::VectorXf bi_;
372 Eigen::VectorXf bf_;
373 Eigen::VectorXf bc_;
374 Eigen::VectorXf bo_;
375
376 Eigen::MatrixXf r_wi_;
377 Eigen::MatrixXf r_wf_;
378 Eigen::MatrixXf r_wc_;
379 Eigen::MatrixXf r_wo_;
380
381 Eigen::MatrixXf ht_1_;
382 Eigen::MatrixXf ct_1_;
383 std::function<float(float)> kactivation_;
384 std::function<float(float)> krecurrent_activation_;
385 int units_ = 0;
386 bool return_sequences_ = false;
387 bool stateful_ = false;
388 bool use_bias_ = false;
389 bool unit_forget_bias_ = false;
390};
391
395class Flatten : public Layer {
396 public:
402 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
403
409 void Run(const std::vector<Eigen::MatrixXf>& inputs,
410 Eigen::MatrixXf* output) override;
411};
412
417class Input : public Layer {
418 public:
424 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
425
431 void Run(const std::vector<Eigen::MatrixXf>& inputs,
432 Eigen::MatrixXf* output) override;
433
434 private:
435 std::vector<int> input_shape_;
436 std::string dtype_;
437 bool sparse_ = false;
438};
439
444class Concatenate : public Layer {
445 public:
451 bool Load(const apollo::prediction::LayerParameter& layer_pb) override;
452
458 void Run(const std::vector<Eigen::MatrixXf>& inputs,
459 Eigen::MatrixXf* output) override;
460
461 private:
462 int axis_ = 0;
463};
464
465} // namespace network
466} // namespace prediction
467} // namespace apollo
Activation is an activation network layer.
Definition net_layer.h:255
bool Load(const apollo::prediction::ActivationParameter &activation_pb)
Load the parameter from a pb message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
Definition net_layer.cc:263
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the parameter from a pb message
AvgPool1d is the average Pool 1d network layer.
Definition net_layer.h:218
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
Definition net_layer.cc:219
bool Load(const apollo::prediction::AvgPool1dParameter &avgpool1d_pb)
Load the avg pool 1d layer parameter from a pb message
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
Definition net_layer.cc:304
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the parameter from a pb message
Definition net_layer.cc:269
concatenates a vector of inputs, return a single matrix.
Definition net_layer.h:444
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
Definition net_layer.cc:541
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
Definition net_layer.cc:555
Conv1d is the convolution 1d network layer.
Definition net_layer.h:149
bool Load(const apollo::prediction::Conv1dParameter &conv1d_pb)
Load the conv1d layer 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
Definition net_layer.cc:129
Dense is the forward fully connected network layer.
Definition net_layer.h:108
bool Load(const apollo::prediction::DenseParameter &layer_pb)
Load the dense layer 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
Definition net_layer.cc:82
network layer to flatten a matrix into vector.
Definition net_layer.h:395
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
Definition net_layer.cc:495
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the dense layer parameter from a pb message
Definition net_layer.cc:487
the input layer for a network, which specified the shape of input matrix
Definition net_layer.h:417
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
Definition net_layer.cc:504
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
Definition net_layer.cc:534
Long-Short Term Memory unit - Hochreiter 1997.
Definition net_layer.h:322
void SetState(const std::vector< Eigen::MatrixXf > &states) override
Set the internal state and memory cell state
Definition net_layer.cc:477
void ResetState() override
Reset the internal state and memory cell state as zero-matrix
Definition net_layer.cc:464
void State(std::vector< Eigen::MatrixXf > *states) const override
Access to the internal state and memory cell state
Definition net_layer.cc:471
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
Definition net_layer.cc:319
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
Definition net_layer.cc:448
Layer is a base class for specific network layers It contains a pure virtual function Run which must ...
Definition net_layer.h:38
virtual void State(std::vector< Eigen::MatrixXf > *states) const
Access to the internal state of a layer
Definition net_layer.h:72
virtual void ResetState()
Reset the internal state of a layer such as LSTM, GRU
Definition net_layer.h:60
int OrderNumber() const
Order number of a layer in a network
Definition net_layer.h:92
virtual ~Layer()=default
Destructor
virtual void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output)=0
Compute the layer output from inputs
virtual bool Load(const apollo::prediction::LayerParameter &layer_pb)
Load layer parameters from a protobuf message
Definition net_layer.cc:32
std::string Name() const
Name of a layer
Definition net_layer.h:86
virtual void SetState(const std::vector< Eigen::MatrixXf > &states)
Set the internal state of a layer
Definition net_layer.h:66
MaxPool1d is the max Pool 1d network layer.
Definition net_layer.h:185
void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output) override
Compute the layer output from inputs
Definition net_layer.cc:176
bool Load(const apollo::prediction::MaxPool1dParameter &maxpool1d_pb)
Load the max pool 1d layer parameter from a pb message
bool Load(const apollo::prediction::LayerParameter &layer_pb) override
Load the layer parameter from a pb message
class register implement
Definition arena_queue.h:37