Apollo  6.0
Open source self driving car software
net_layer.h
Go to the documentation of this file.
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 
28 namespace apollo {
29 namespace prediction {
30 namespace network {
31 
38 class 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 
108 class 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 
149 class 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 
185 class 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 
218 class 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 
255 class 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 
288 class 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 
322 class 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 
395 class 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 
417 class 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 
444 class 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
virtual void ResetState()
Reset the internal state of a layer such as LSTM, GRU.
Definition: net_layer.h:60
Definition: net_layer.h:444
Activation is an activation network layer. Activation layer output is y = f(x), where x is the input...
Definition: net_layer.h:255
AvgPool1d is the average Pool 1d network layer.
Definition: net_layer.h:218
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual void State(std::vector< Eigen::MatrixXf > *states) const
Access to the internal state of a layer.
Definition: net_layer.h:72
Conv1d is the convolution 1d network layer. Conv1d layer output is y = Conv(x, w), where x is the input, w the weight.
Definition: net_layer.h:149
virtual ~Layer()=default
Destructor.
Layer()=default
Constructor.
For a step-by-step description of the algorithm, see this tutorial.
Definition: net_layer.h:322
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
Definition: net_layer.h:395
virtual bool Load(const apollo::prediction::LayerParameter &layer_pb)
Load layer parameters from a protobuf message.
int OrderNumber() const
Order number of a layer in a network.
Definition: net_layer.h:92
std::string Name() const
Name of a layer.
Definition: net_layer.h:86
Definition: net_layer.h:417
virtual void Run(const std::vector< Eigen::MatrixXf > &inputs, Eigen::MatrixXf *output)=0
Compute the layer output from inputs.
Layer is a base class for specific network layers It contains a pure virtual function Run which must ...
Definition: net_layer.h:38
Dense is the forward fully connected network layer. Dense layer output is y = f(x*w + b)...
Definition: net_layer.h:108