Apollo 11.0
自动驾驶开放平台
net_model.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 <utility>
20
21#include "cyber/common/log.h"
22
23namespace apollo {
24namespace prediction {
25namespace network {
26
27bool NetModel::LoadModel(const NetParameter& net_parameter) {
28 net_parameter_.CopyFrom(net_parameter);
29 layers_.clear();
30
31 for (int i = 0; i < net_parameter_.layers_size(); ++i) {
32 LayerParameter layer_pb = net_parameter_.layers(i);
33 ADEBUG << i << "-th layer name: " << layer_pb.name().c_str();
34 std::unique_ptr<Layer> layer(nullptr);
35 switch (layer_pb.oneof_layers_case()) {
36 case LayerParameter::kInput:
37 layer = std::unique_ptr<Layer>(new Input());
38 break;
39 case LayerParameter::kActivation:
40 layer = std::unique_ptr<Layer>(new Activation());
41 break;
42 case LayerParameter::kDense:
43 layer = std::unique_ptr<Layer>(new Dense());
44 break;
45 case LayerParameter::kBatchNormalization:
46 layer = std::unique_ptr<Layer>(new BatchNormalization());
47 break;
48 case LayerParameter::kLstm:
49 layer = std::unique_ptr<Layer>(new LSTM());
50 break;
51 case LayerParameter::kFlatten:
52 layer = std::unique_ptr<Layer>(new Flatten());
53 break;
54 case LayerParameter::kConcatenate:
55 layer = std::unique_ptr<Layer>(new Concatenate());
56 break;
57 default:
58 AERROR << "Failed to load layer: " << layer_pb.type().c_str();
59 break;
60 }
61 if (!layer->Load(layer_pb)) {
62 AERROR << "Failed to load " << i << "-layer: " << layer_pb.name().c_str();
63 return false;
64 }
65 layers_.push_back(std::move(layer));
66 }
67 ok_ = true;
68 AINFO << "Success in loading the model!";
69 ADEBUG << "Its Performance:" << PerformanceString().c_str();
70 return true;
71}
72
73std::string NetModel::PerformanceString() const {
74 std::stringstream ss;
75 if (!net_parameter_.has_performance()) {
76 AWARN << "The protobuf does not contain performance values!";
77 return " ";
78 }
79 ss << "\n test: accuracy = ";
80 for (int i = 0; i < net_parameter_.performance().accuracy_size(); ++i) {
81 ss << net_parameter_.performance().accuracy(i) << ", ";
82 }
83 ss << "\n recall = ";
84 for (int i = 0; i < net_parameter_.performance().recall_size(); ++i) {
85 ss << net_parameter_.performance().recall(i) << ", ";
86 }
87 ss << "\n precision = ";
88 for (int i = 0; i < net_parameter_.performance().precision_size(); ++i) {
89 ss << net_parameter_.performance().precision(i) << ", ";
90 }
91 ss << std::endl;
92 return ss.str();
93}
94
95const std::string& NetModel::Name() const { return net_parameter_.name(); }
96
97int NetModel::Id() const { return net_parameter_.id(); }
98
99bool NetModel::IsOk() const { return ok_; }
100
101} // namespace network
102} // namespace prediction
103} // namespace apollo
Activation is an activation network layer.
Definition net_layer.h:255
concatenates a vector of inputs, return a single matrix.
Definition net_layer.h:444
Dense is the forward fully connected network layer.
Definition net_layer.h:108
network layer to flatten a matrix into vector.
Definition net_layer.h:395
the input layer for a network, which specified the shape of input matrix
Definition net_layer.h:417
Long-Short Term Memory unit - Hochreiter 1997.
Definition net_layer.h:322
const std::string & Name() const
Name of a network model
Definition net_model.cc:95
std::string PerformanceString() const
Shows the performance information of a network
Definition net_model.cc:73
bool IsOk() const
Indicate the state of a network model
Definition net_model.cc:99
bool LoadModel(const NetParameter &net_parameter)
Load network parameters from a protobuf message
Definition net_model.cc:27
int Id() const
Id of a network model
Definition net_model.cc:97
std::vector< std::unique_ptr< Layer > > layers_
Definition net_model.h:110
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
class register implement
Definition arena_queue.h:37