forked from Tencent/ncnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.h
163 lines (142 loc) · 4.04 KB
/
layer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef NCNN_LAYER_H
#define NCNN_LAYER_H
#include <stdio.h>
#include <string>
#include <vector>
#include "mat.h"
#include "platform.h"
namespace ncnn {
class Layer
{
public:
// empty
Layer();
// virtual destructor
virtual ~Layer();
#if NCNN_STDIO
#if NCNN_STRING
// load layer specific parameter from plain param file
// return 0 if success
virtual int load_param(FILE* paramfp);
#endif // NCNN_STRING
// load layer specific parameter from binary param file
// return 0 if success
virtual int load_param_bin(FILE* paramfp);
// load layer specific weight data from model file
// return 0 if success
virtual int load_model(FILE* binfp);
#endif // NCNN_STDIO
// load layer specific parameter from memory
// memory pointer is 32-bit aligned
// return 0 if success
virtual int load_param(const unsigned char*& mem);
// load layer specific weight data from memory
// memory pointer is 32-bit aligned
// return 0 if success
virtual int load_model(const unsigned char*& mem);
public:
// one input and one output blob
bool one_blob_only;
// support inplace inference
bool support_inplace;
public:
// implement inference
// return 0 if success
virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs) const;
virtual int forward(const Mat& bottom_blob, Mat& top_blob) const;
// implement inplace inference
// return 0 if success
virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs) const;
virtual int forward_inplace(Mat& bottom_top_blob) const;
public:
#if NCNN_STRING
// layer type name
std::string type;
// layer name
std::string name;
#endif // NCNN_STRING
// blob index which this layer needs as input
std::vector<int> bottoms;
// blob index which this layer produces as output
std::vector<int> tops;
};
namespace LayerType {
enum
{
AbsVal = 0,
ArgMax = 1,
BatchNorm = 2,
Bias = 3,
BNLL = 4,
Concat = 5,
Convolution = 6,
Crop = 7,
Deconvolution = 8,
Dropout = 9,
ELU = 10,
Eltwise = 11,
Embed = 12,
Exp = 13,
Flatten = 14,
InnerProduct = 15,
Input = 16,
Log = 17,
LRN = 18,
MemoryData = 19,
MVN = 20,
Pooling = 21,
Power = 22,
PReLU = 23,
Proposal = 24,
Reduction = 25,
ReLU = 26,
Reshape = 27,
ROIPooling = 28,
Scale = 29,
Sigmoid = 30,
Slice = 31,
Softmax = 32,
Split = 33,
SPP = 34,
TanH = 35,
Threshold = 36,
Tile = 37,
RNN = 38,
LSTM = 39,
CustomBit = (1<<8),
};
} // namespace LayerType
// layer factory function
typedef Layer* (*layer_creator_func)();
struct layer_registry_entry
{
#if NCNN_STRING
// layer type name
const char* name;
#endif // NCNN_STRING
// layer factory entry
layer_creator_func creator;
};
#if NCNN_STRING
// get layer type from type name
int layer_to_index(const char* type);
#endif // NCNN_STRING
// create layer from layer type
Layer* create_layer(int index);
#define DEFINE_LAYER_CREATOR(name) \
Layer* name##_layer_creator() { return new name; }
} // namespace ncnn
#endif // NCNN_LAYER_H