-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_api.cpp
394 lines (318 loc) · 11.5 KB
/
cnn_api.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* Copyright 2016-2017 The MathWorks, Inc. */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdexcept>
#include <string>
#include <stdarg.h>
#include <cassert>
#include <vector>
#include "cnn_api.hpp"
#include "MWCNNLayerImpl.hpp"
#include "MWTargetNetworkImpl.hpp"
MWCNNLayer::MWCNNLayer() :
m_impl( NULL)
{
}
MWCNNLayer::~MWCNNLayer()
{
if (m_impl)
{
delete m_impl;
m_impl = 0;
}
}
void MWCNNLayer::predict()
{
if (m_impl)
{
m_impl->predict();
}
}
void MWCNNLayer::cleanup()
{
if (m_impl)
{
m_impl->cleanup();
}
for(int idx = 0; idx < getNumOutputs(); idx++)
{
MWTensor* op = getOutputTensor(idx);
delete op;
op = 0;
}
}
void MWCNNLayer::allocate()
{
if(m_impl)
{
m_impl->allocate();
}
}
// open filename
// if file is not found, look in current directory
FILE* MWCNNLayer::openBinaryFile(const char* fileName)
{
FILE* fp = fopen(fileName, "rb");
if (!fp)
{
#if defined(_WIN32) || defined(_WIN64)
char delim[] = "\\";
#else
char delim[] = "/";
#endif
std::string fileS(fileName);
size_t pos = 0;
while((pos = fileS.find(delim)) != std::string::npos)
{
if (pos == (fileS.size() - 1))
{
fileS = "";
break;
}
fileS = fileS.substr(pos+1);
}
if (!fileS.empty())
{
fp = fopen(fileS.c_str(), "rb");
}
if (!fp)
{
std::string errmsg = std::string("Error opening binary file ") + fileS;
printf("Error! Unable to open file %s\n", fileS.c_str());
throw std::runtime_error(errmsg.c_str());
}
}
return fp;
}
std::runtime_error MWCNNLayer::getFileOpenError(const char* filename)
{
const std::string message = std::string("Error! Unable to open file ") +
std::string(filename);
return std::runtime_error(message);
}
//Name of each layer is not set nor used for now.
void MWCNNLayer::setName(const char* n)
{
m_name = n;
return;
}
void MWTensor::setData(float* data) {
m_data = data;
}
MWTensor* MWCNNLayer::getInputTensor(int index)
{
std::map<int, MWTensor*>::iterator it = m_input.find(index);
assert(it != m_input.end());
return it->second;
}
MWTensor* MWCNNLayer::getOutputTensor(int index)
{
std::map<int, MWTensor*>::iterator it = m_output.find(index);
assert(it != m_output.end());
return it->second;
}
void MWCNNLayer::setInputTensor(MWTensor * other, int index)
{
m_input[index] = other;
}
int MWCNNLayer::getHeight(int index)
{
return getOutputTensor(index)->getHeight();
}
int MWCNNLayer::getBatchSize()
{
// return batch size from the output tensor
return getOutputTensor(0)->getBatchSize();
}
int MWCNNLayer::getWidth(int index)
{
return getOutputTensor(index)->getWidth();
}
int MWCNNLayer::getNumInputFeatures(int index)
{
return getInputTensor(index)->getChannels();
}
int MWCNNLayer::getNumOutputFeatures(int index)
{
return getOutputTensor(index)->getChannels();
}
float* MWCNNLayer::getData(int index)
{
float* data = getOutputTensor(index)->getData();
assert(data);
return data;
}
void MWCNNLayer::allocateOutputTensor(int numHeight, int numWidth, int numChannels, int batchSize, float* data, int index)
{
MWTensor* op = new MWTensor(numHeight, numWidth, numChannels, batchSize, data, this, index);
assert(op != NULL);
std::map<int, MWTensor*>::iterator it = m_output.find(index);
assert(it == m_output.end());
m_output[index] = op;
}
MWTensor::MWTensor(int height, int width, int channels, int batchsize, float* data, MWCNNLayer* owner, int srcport) :
m_height(height),
m_width(width),
m_channels(channels),
m_batchSize(batchsize),
m_data(data),
m_owner(owner),
m_srcport(srcport),
opBufIndex(-1)
{
}
MWTensor::~MWTensor()
{
}
//Creating the ImageInputLayer
//InputSize should be [h w n].
//If normalization is 'zerocenter', withAvg should be true.
//g1429526: currently AverageImage is not accessible.
//Will have to update the codegen to generate avg binary file once the geck is complete.
//And 'zerocenter' is the only supported transformation for this layer.
void MWInputLayer::createInputLayer(MWTargetNetworkImpl* ntwk_impl, int m_n, int m_h, int m_w, int m_c, int m_withAvg, const char* avg_file_name, int outbufIdx)
{
// populate output tensor
allocateOutputTensor(m_h, m_w, m_c, m_n, NULL);
m_impl = new MWInputLayerImpl(this, ntwk_impl, m_n, m_h, m_w, m_c,
m_withAvg, avg_file_name, outbufIdx);
/*Setting the data pointer */
MWTensor *opTensor = getOutputTensor();
opTensor->setData(m_impl->getData());
return;
}
//Create ReLULayer
void MWReLULayer::createReLULayer(MWTargetNetworkImpl* ntwk_impl, MWTensor* m_in, int inPlace, int outbufIdx)
{
setInputTensor(m_in);
// allocate output, reusing input tensor's data buffer
int numOutputFeatures = getInputTensor()->getChannels();
allocateOutputTensor(getInputTensor()->getHeight(), getInputTensor()->getWidth(), numOutputFeatures, getInputTensor()->getBatchSize(), NULL);
m_impl = new MWReLULayerImpl(this, ntwk_impl, inPlace, outbufIdx);
MWTensor *opTensor = getOutputTensor();
opTensor->setData(m_impl->getData());
return;
}
//Create CrossChannelNormalizationLayer
//Parameters here are the same naming as NNT.
void MWNormLayer::createNormLayer(MWTargetNetworkImpl* ntwk_impl, MWTensor* m_in, unsigned m_WindowChannelSize, double m_Alpha, double m_Beta, double m_K, int outbufIdx)
{
setInputTensor(m_in);
int numOutputFeatures = getInputTensor()->getChannels();
allocateOutputTensor(getInputTensor()->getHeight(), getInputTensor()->getWidth(), numOutputFeatures, getInputTensor()->getBatchSize(), NULL);
m_impl = new MWNormLayerImpl(this, ntwk_impl, m_WindowChannelSize, m_Alpha, m_Beta, m_K, outbufIdx);
MWTensor *opTensor = getOutputTensor();
opTensor->setData(m_impl->getData());
return;
}
//Create MaxPooling2DLayer with PoolSize = [ PoolH PoolW ]
// Stride = [ StrideH StrideW ]
// Padding = [ PaddingH_T PaddingH_B PaddingW_L PaddingW_R ]
void MWMaxPoolingLayer::createMaxPoolingLayer(MWTargetNetworkImpl* ntwk_impl, MWTensor* m_in, int m_PoolH, int m_PoolW, int m_StrideH, int m_StrideW, int m_PaddingH_T,int m_PaddingH_B, int m_PaddingW_L, int m_PaddingW_R, bool m_hasIndices, int numOutputs, ...)
{
setInputTensor(m_in);
int outputH = ((getInputTensor()->getHeight()- m_PoolH + m_PaddingH_T + m_PaddingH_B)/m_StrideH) + 1;
int outputW = ((getInputTensor()->getWidth()- m_PoolW + m_PaddingW_L + m_PaddingW_R)/m_StrideW) + 1;
int numOutputFeatures = getInputTensor()->getChannels();
allocateOutputTensor(outputH, outputW, numOutputFeatures, getInputTensor()->getBatchSize(), NULL, 0);
if (m_hasIndices)
{
// allocate index tensor
allocateOutputTensor(outputH, outputW, numOutputFeatures, getInputTensor()->getBatchSize(), NULL, 1);
}
{
va_list args;
va_start(args, numOutputs);
std::vector<int> bufIndices(numOutputs, -1);
for(int i = 0; i < numOutputs; i++)
{
bufIndices[i] = va_arg(args, int);
}
m_impl = new MWMaxPoolingLayerImpl(this, ntwk_impl, m_PoolH, m_PoolW, m_StrideH, m_StrideW, m_PaddingH_T, m_PaddingH_B, m_PaddingW_L, m_PaddingW_R, m_hasIndices, numOutputs, bufIndices);
}
/*Setting the MWTensor pointer */
MWTensor *opTensor = getOutputTensor(0);
opTensor->setData(m_impl->getData());
if (m_hasIndices)
{
MWTensor *indexOpTensor = getOutputTensor(1);
indexOpTensor->setData(dynamic_cast<MWMaxPoolingLayerImpl*>(m_impl)->getIndexData());
}
}
//Create FullyConnectedLayer with corresponding InputSize and OutputSize.
//This implementation uses SGEMV for m_BatchSize = 1 and SGEMM for others.
void MWFCLayer::createFCLayer(MWTargetNetworkImpl* ntwk_impl, MWTensor* m_in, int m_InputSize, int m_OutputSize, const char* m_weights_file, const char* m_bias_file, int outbufIdx)
{
setInputTensor(m_in);
allocateOutputTensor(1, 1, m_OutputSize, getInputTensor()->getBatchSize(), NULL);
m_impl = new MWFCLayerImpl(this, ntwk_impl, m_InputSize, m_weights_file, m_bias_file, outbufIdx);
/*Setting the MWTensor pointer */
MWTensor *opTensor = getOutputTensor();
opTensor->setData(m_impl->getData());
return;
}
//Create SoftmaxLayer
void MWSoftmaxLayer::createSoftmaxLayer(MWTargetNetworkImpl* ntwk_impl, MWTensor* m_in, int outbufIdx)
{
setInputTensor(m_in);
allocateOutputTensor(getInputTensor()->getHeight(), getInputTensor()->getWidth(), getInputTensor()->getChannels(), getInputTensor()->getBatchSize(), NULL);
m_impl = new MWSoftmaxLayerImpl(this, ntwk_impl, outbufIdx);
/*Setting the MWTensor pointer */
MWTensor *opTensor = getOutputTensor();
opTensor->setData(m_impl->getData());
return;
}
//Create ClassificationOutputLayer
//We are doing inference only so LossFunction is not needed.
//This layer would do nothing but point the data to previous layer.
void MWOutputLayer::createOutputLayer(MWTargetNetworkImpl* ntwk_impl, MWTensor* m_in, int outbufIdx)
{
setInputTensor(m_in);
allocateOutputTensor(getInputTensor()->getHeight(),
getInputTensor()->getWidth(),
getInputTensor()->getChannels(),
getInputTensor()->getBatchSize(),
getInputTensor()->getData());
m_impl = new MWOutputLayerImpl(this, ntwk_impl, outbufIdx);
/*Setting the MWTensor pointer */
MWTensor* opTensor = getOutputTensor();
opTensor->setData(m_impl->getData());
return;
}
void MWOutputLayer::predict()
{
m_impl->predict();
}
//Create pass through
//This layer would do nothing but point the data to previous layer.
void MWPassthroughLayer::createPassthroughLayer(MWTargetNetworkImpl* /*ntwk_impl*/, MWTensor* m_in, int /* outbufIdx */)
{
setInputTensor(m_in);
int numOutputFeatures = getInputTensor()->getChannels();
allocateOutputTensor(getInputTensor()->getHeight(),
getInputTensor()->getWidth(),
numOutputFeatures,
getInputTensor()->getBatchSize(),
getInputTensor()->getData());
return;
}
void MWPassthroughLayer::predict()
{
return;
}
//Create AvgPooling2DLayer with PoolSize = [ PoolH PoolW ]
// Stride = [ StrideH StrideW ]
// Padding = [ PaddingH PaddingW ]
void MWAvgPoolingLayer::createAvgPoolingLayer(MWTargetNetworkImpl* ntwk_impl, MWTensor* m_in, int m_PoolH, int m_PoolW, int m_StrideH, int m_StrideW, int m_PaddingH, int m_PaddingW, int outbufIdx)
{
setInputTensor(m_in);
int outputH = ((getInputTensor()->getHeight()- m_PoolH +2*m_PaddingH)/m_StrideH) + 1;
int outputW = ((getInputTensor()->getWidth()- m_PoolW + 2*m_PaddingW)/m_StrideW) + 1;
int numOutputFeatures = getInputTensor()->getChannels();
allocateOutputTensor(outputH, outputW, numOutputFeatures, getInputTensor()->getBatchSize(), NULL);
m_impl = new MWAvgPoolingLayerImpl(this, ntwk_impl, m_PoolH, m_PoolW, m_StrideH, m_StrideW, m_PaddingH, m_PaddingW, outbufIdx);
/*Setting the MWTensor pointer */
MWTensor *opTensor = getOutputTensor();
opTensor->setData(m_impl->getData());
}