Elffuss BraidHub

kikocis/ncnn public

master · 8.1 KiBRenderedRawPermalink
1# step1 create a new empty class
2```cpp
3// mylayer.h
4#include "layer.h"
5using namespace ncnn;
6
7// a new layer type called MyLayer
8class MyLayer : public Layer
9{
10};
11
12// mylayer.cpp
13#include "mylayer.h"
14DEFINE_LAYER_CREATOR(MyLayer)
15```
16
17# step2 declare layer parameters and weights
18```cpp
19// mylayer.h
20#include "layer.h"
21using namespace ncnn;
22
23class MyLayer : public Layer
24{
25private:
26 int channels;// new code
27 float gamma;// new code
28 Mat weight;// new code
29};
30
31// mylayer.cpp
32#include "mylayer.h"
33DEFINE_LAYER_CREATOR(MyLayer)
34```
35
36# step3 implement load functions for parameters and weights
37```cpp
38// mylayer.h
39#include "layer.h"
40using namespace ncnn;
41
42class MyLayer : public Layer
43{
44public:
45 virtual int load_param(const ParamDict& pd);// new code
46 virtual int load_model(const ModelBin& mb);// new code
47
48private:
49 int channels;
50 float eps;
51 Mat gamma_data;
52};
53
54// mylayer.cpp
55#include "mylayer.h"
56DEFINE_LAYER_CREATOR(MyLayer)
57
58// new routine for loading parameters
59int MyLayer::load_param(const ParamDict& pd)
60{
61 // details about the relations with param file
62 // https://github.com/Tencent/ncnn/wiki/param-and-model-file-structure
63 //
64 channels = pd.get(0, 0);// parse 0=<int value> entry, default value 0
65 eps = pd.get(1, 0.001f);// parse 1=<float value> entry, default value 0.001f
66
67 return 0;// return zero if success
68}
69
70// new routine for loading weights
71int MyLayer::load_model(const ModelBin& mb)
72{
73 // details about the relations with model file
74 // https://github.com/Tencent/ncnn/wiki/param-and-model-file-structure
75 //
76 // read weights with length of channels * sizeof(float)
77 // the second argument explains as follows
78 // 0 judge the value type automatically, you may get float or float16 or uint8 etc
79 // depends on the model storage and the supporting target hardware
80 // 1 read float values anyway
81 // 2 read float16 values anyway
82 // 3 read uint8 values anyway
83 gamma_data = mb.load(channels, 1);
84 if (gamma_data.empty())
85 return -100;// return non-zero on error, -100 indicates out-of-memory
86
87 return 0;// return zero if success
88}
89```
90
91# step4 determine forward behavior
92```cpp
93// mylayer.h
94#include "layer.h"
95using namespace ncnn;
96
97class MyLayer : public Layer
98{
99public:
100 MyLayer();// new code
101 virtual int load_param(const ParamDict& pd);
102 virtual int load_model(const ModelBin& mb);
103
104private:
105 int channels;
106 float eps;
107 Mat gamma_data;
108};
109
110// mylayer.cpp
111#include "mylayer.h"
112DEFINE_LAYER_CREATOR(MyLayer)
113
114// new routine for setting forward behavior
115MyLayer::MyLayer()
116{
117 // one input and one output
118 // typical one_blob_only type: Convolution, Pooling, ReLU, Softmax ...
119 // typical non-one_blob_only type: Eltwise, Split, Concat, Slice ...
120 one_blob_only = true;
121
122 // do not change the blob size, modify data in-place
123 // typical support_inplace type: ReLU, Sigmoid ...
124 // typical non-support_inplace type: Convolution, Pooling ...
125 support_inplace = true;
126}
127
128int MyLayer::load_param(const ParamDict& pd)
129{
130 channels = pd.get(0, 0);
131 eps = pd.get(1, 0.001f);
132
133 // you could alter the behavior based on loaded parameter
134 // if (eps == 0.001f)
135 // {
136 // one_blob_only = false;
137 // support_inplace = false;
138 // }
139
140 return 0;
141}
142
143int MyLayer::load_model(const ModelBin& mb)
144{
145 gamma_data = mb.load(channels, 1);
146 if (gamma_data.empty())
147 return -100;
148
149 // you could alter the behavior based on loaded weight
150 // if (gamma_data[0] == 0.f)
151 // {
152 // one_blob_only = false;
153 // support_inplace = false;
154 // }
155
156 return 0;
157}
158```
159
160# step5 choose proper interface based on forward behavior
161```cpp
162// The base class Layer defines four interfaces for each forward behavior combination
163
164// 1
165virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const;
166
167// 2
168virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const;
169
170// 3
171virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const;
172
173// 4
174virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;
175```
176**must** = layer must implement this function
177
178**optional** = layer may implement this function for optimal performance
179
180sometimes the graph inference path cannot call forward_inplace directly due to data sharing, in this situation the non-inplace forward routine will be used, which deep-copy the input blob and call inplace forward on it if the optional routine is not implemented. Thus, you could avoid this deep-copy by process input to output on-the-fly.
181
182|one_blob_only|support_inplace|1|2|3|4|
183|---|---|---|---|---|---|
184|false|false|must| | | |
185|false|true|optional| |must| |
186|true|false| |must| | |
187|true|true| |optional| |must|
188
189# step6 implement forward function
190```cpp
191// mylayer.h
192#include "layer.h"
193using namespace ncnn;
194
195class MyLayer : public Layer
196{
197public:
198 MyLayer();
199 virtual int load_param(const ParamDict& pd);
200 virtual int load_model(const ModelBin& mb);
201
202 virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const;// new code, optional
203 virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;// new code
204
205private:
206 int channels;
207 float eps;
208 Mat gamma_data;
209};
210
211// mylayer.cpp
212#include "mylayer.h"
213DEFINE_LAYER_CREATOR(MyLayer)
214
215MyLayer::MyLayer()
216{
217 one_blob_only = true;
218 support_inplace = true;
219}
220
221int MyLayer::load_param(const ParamDict& pd)
222{
223 channels = pd.get(0, 0);
224 eps = pd.get(1, 0.001f);
225
226 return 0;
227}
228
229int MyLayer::load_model(const ModelBin& mb)
230{
231 gamma_data = mb.load(channels, 1);
232 if (gamma_data.empty())
233 return -100;
234
235 return 0;
236}
237
238// optional new routine for layer forward function, non-inplace version
239int MyLayer::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
240{
241 // check input dims, return non-zero on error
242 if (bottom_blob.c != channels)
243 return -1;
244
245 // x = (x + eps) * gamma_per_channel
246
247 int w = bottom_blob.w;
248 int h = bottom_blob.h;
249 size_t elemsize = bottom_blob.elemsize;
250 int size = w * h;
251
252 top_blob.create(w, h, channels, elemsize, opt.blob_allocator);
253 if (top_blob.empty())
254 return -100;// return non-zero on error, -100 indicates out-of-memory
255
256 #pragma omp parallel for num_threads(opt.num_threads)
257 for (int q=0; q<channels; q++)
258 {
259 const float* ptr = bottom_blob.channel(q);
260 float* outptr = top_blob.channel(q);
261 const float gamma = gamma_data[q];
262
263 for (int i=0; i<size; i++)
264 {
265 outptr[i] = (ptr[i] + eps) * gamma ;
266 }
267 }
268
269 return 0;
270}
271
272// new routine for layer forward function
273int MyLayer::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
274{
275 // check input dims, return non-zero on error
276 if (bottom_top_blob.c != channels)
277 return -1;
278
279 // x = (x + eps) * gamma_per_channel
280
281 int w = bottom_top_blob.w;
282 int h = bottom_top_blob.h;
283 int size = w * h;
284
285 #pragma omp parallel for num_threads(opt.num_threads)
286 for (int q=0; q<channels; q++)
287 {
288 float* ptr = bottom_top_blob.channel(q);
289 const float gamma = gamma_data[q];
290
291 for (int i=0; i<size; i++)
292 {
293 ptr[i] = (ptr[i] + eps) * gamma ;
294 }
295 }
296
297 return 0;
298}
299```
300
301# step7 integrate with ncnn library
302you may probably need to modify caffe2ncnn or mxnet2ncnn etc. to write your layer specific parameters and weights into ncnn param and model file
303
304the param and model file structure [param-and-model-file-structure](param-and-model-file-structure)
305
306```
307// example param file content
308Input input 0 1 input
309Convolution conv2d 1 1 input conv2d 0=32 1=1 2=1 3=1 4=0 5=0 6=768
310MyLayer mylayer 1 1 conv2d mylayer0
311Pooling maxpool 1 1 mylayer0 maxpool 0=0 1=3 2=2 3=-233 4=0
312```
313
314```cpp
315ncnn::Net net;
316
317// register custom layer before load param and model
318// the layer creator function signature is always XYZ_layer_creator, which defined in DEFINE_LAYER_CREATOR macro
319net.register_custom_layer("MyLayer", MyLayer_layer_creator);
320
321net.load_param("model.param");
322net.load_model("model.bin");
323```