forked from davidrmiller/neural2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseTopologyConfig.cpp
532 lines (461 loc) · 17.2 KB
/
parseTopologyConfig.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
This is parseTopologyConfig.cpp, part of neural2d.
This is the part that parses the topology config file.
David R. Miller, 2015
https://github.com/davidrmiller/neural2d
Also see neural2d.h for more information.
*/
#include <fstream>
#include <iostream>
#include <vector>
#include "neural2d.h"
namespace NNet {
// One of these gets created for each layer specification line in the topology config file:
//
topologyConfigSpec_t::topologyConfigSpec_t(void)
{
fromLayerName = "";
fromLayerIndex = 0;
sizeSpecified = false;
colorChannelSpecified = false;
radiusSpecified = false;
tfSpecified = false;
size.depth = size.x = size.y = 0;
channel = NNet::BW;
radius.x = radius.y = 0.0;
transferFunctionName.clear();
transferFunctionName = "tanh";
flatConvolveMatrix.clear();
isRegularLayer = false;
isConvolutionFilterLayer = false; // Equivalent to (convolveMatrix.size() == 1)
isConvolutionNetworkLayer = false; // Equivalent to (convolveMatrix.size() > 1)
isPoolingLayer = false; // Equivalent to (poolSize.x != 0)
kernelSize.x = kernelSize.y = 0; // Used only for convolution filter and conv. network layers
poolSize.x = poolSize.y = 0; // Used only for convolution network layers
poolMethod = POOL_NONE;
}
void configErrorThrow(topologyConfigSpec_t ¶ms, const string &msg)
{
err << "There's a problem in the topology config file at line " << params.configLineNum << ":";
if (params.layerName.size() > 0) {
err << "(layer \"" << params.layerName << "\")";
}
err << endl;
err << msg << endl;
throw exceptionConfigFile();
}
/*
Convolution filter matrix example formats:
{0, 1,2}
{ {0,1,2}, {1,2,1}, {0, 1, 0}}
*/
void extractConvolveFilterMatrix(topologyConfigSpec_t ¶ms, std::istringstream &ss)
{
char c;
enum state_t { INIT, LEFTBRACE, RIGHTBRACE, COMMA, NUM };
enum action_t { SKIP, ILL, PLINC, PLDECX, STONYINC, STONXINC, ACCUM };
state_t lastState = INIT;
state_t newState = INIT;
int braceLevel = 0;
vector<float> row;
vector<vector<float>> mat;
float num = 0.0;
action_t table[5][5] = {
/* INIT LEFTBRACE RIGHTBRACE COMMA NUM */
/* INIT */ { ILL, PLINC, ILL, ILL, ILL },
/* LEFTBRACE */ { ILL, PLINC, ILL, ILL, ACCUM },
/* RIGHTBRACE */ { ILL, ILL, PLDECX, SKIP, ILL },
/* COMMA */ { ILL, PLINC, ILL, ILL, ACCUM },
/* DIGIT */ { ILL, ILL, STONYINC, STONXINC, ACCUM },
};
bool done = false;
while (!done && ss) {
ss >> c;
if (isspace(c)) {
continue;
} else if (c == '{') {
newState = LEFTBRACE;
} else if (c == '}') {
newState = RIGHTBRACE;
} else if (c == ',') {
newState = COMMA;
} else if (c == '-' || c == '+' || c == '.' || isdigit(c)) {
newState = NUM;
} else {
configErrorThrow(params, "Warning: Internal error in parsing convolve filter matrix spec");
}
action_t action = table[lastState][newState];
switch(action) {
case SKIP:
break;
case ILL:
configErrorThrow(params, "Syntax error in convolve filter matrix spec");
break;
case PLINC:
++braceLevel;
break;
case PLDECX:
--braceLevel;
if (braceLevel != 0) {
configErrorThrow(params, "Syntax error in convolve filter matrix spec");
}
done = true;
break;
case STONYINC:
row.push_back(num); // Add the element to the row
mat.push_back(row); // Add the row to the matrix
row.clear();
num = 0.0; // Start a new number after this
if (--braceLevel == 0) {
done = true;
}
break;
case STONXINC:
row.push_back(num); // Add the element to the row
num = 0.0; // Start a new number after this
break;
case ACCUM:
// We've got the first char of the number in c, which can be -, +, ., or a digit.
// Now gather the rest of the numeric string:
string numstr;
numstr.clear();
numstr.push_back(c);
while (ss.peek() == '.' || isdigit(ss.peek())) {
char cc;
ss >> cc;
numstr.push_back(cc);
}
num = strtod(numstr.c_str(), NULL);
break;
}
lastState = newState;
}
// Check that all rows have the same size:
unsigned firstRowSize = mat[0].size();
if (0 != count_if(mat.begin() + 1, mat.end(), [firstRowSize](vector<float> row) {
return row.size() != firstRowSize; })) {
configErrorThrow(params, "Error in topology config file: inconsistent row size in convolve filter matrix spec");
}
// We'll create (or recreate) a one-element flatConvolveMatrix in the params structure:
// Convolution filtering only needs a single convolve matrix, so only element zero is
// used in params.flatConvolveMatrix. That one element will be a flattened
// container of the 2D convolve matrix:
params.flatConvolveMatrix.clear();
params.flatConvolveMatrix.push_back(vector<float>()); // Start with one empty container for one kernel
params.flatConvolveMatrix.back().assign(mat.size() * mat[0].size(), 0);
// for (auto &row : mat) {
// for (auto val : row) {
for (uint32_t row = 0; row < mat.size(); ++row) {
for (uint32_t col = 0; col < mat[row].size(); ++col) {
params.flatConvolveMatrix.back()[flattenXY(col, row, mat.size())] = mat[row][col];
}
}
// The matrix is arranged so that we can access elements as [x][y]:
params.kernelSize.x = mat.size();
params.kernelSize.y = mat[0].size();
}
// format: [depth *] X [x Y]
// Depth, if omitted, defaults to zero.
// Y, if omitted, defaults to zero.
//
dxySize extractDxySize(std::istringstream &ss)
{
char ch;
dxySize size;
size.depth = 1; // Default is 1 unless otherwise specified
ss >> size.x; // This may actually be the depth, we'll see
auto pos = ss.tellg();
ss >> ch; // Test the next non-space char
if (ch == '*') {
// Depth dimension
size.depth = size.x; // That was the depth we read, not X
size.x = 0;
ss >> size.x;
pos = ss.tellg();
ss >> ch; // Test the next non-space char
}
if (ch == 'x') {
ss >> size.y;
} else {
ss.seekg(pos); // Put back what is not ours
size.y = 1; // E.g., "8" means 8x1
}
return size;
}
// format: X [x Y]
// Y, if omitted, defaults to zero.
//
xySize extractXySize(std::istringstream &ss)
{
char ch;
xySize size;
ss >> size.x;
auto pos = ss.tellg();
ss >> ch; // Test the next non-space char
if (ch == 'x') {
ss >> size.y;
} else {
ss.seekg(pos); // Put back what is not ours
size.y = 1; // E.g., "8" means 8x1
}
return size;
}
// Throws for any error.
//
void extractChannel(topologyConfigSpec_t ¶ms, std::istringstream &ss)
{
string stoken;
ss >> stoken;
if (stoken == "R") params.channel = NNet::R;
else if (stoken == "G") params.channel = NNet::G;
else if (stoken == "B") params.channel = NNet::B;
else if (stoken == "BW") params.channel = NNet::BW;
else {
configErrorThrow(params, "Unknown color channel");
}
params.colorChannelSpecified = true;
}
// Throws for any error.
// Modifies params and leaves ss pointing to the next char after the pool method:
//
void extractPoolMethod(topologyConfigSpec_t ¶ms, std::istringstream &ss)
{
string stoken;
ss >> stoken;
if (stoken == "max") {
params.poolMethod = POOL_MAX;
} else if (stoken == "avg") {
params.poolMethod = POOL_AVG;
} else {
configErrorThrow(params, "Expected pool method \"max\" or \"avg\"");
}
}
// Topology config grammar:
//
// layer-name parameters
// parameters := parameter [ parameters ]
// parameter :=
// input | output | layer-name
// size dxy-spec
// from layer-name
// channel channel-spec
// radius xy-spec
// tf transfer-function-spec
// convolve filter-spec
// convolve xy-spec
// pool { max | avg } xy-spec
// dxy-spec := integer * xy-spec
// xy-spec := integer [ x integer ]
// channel-spec := R|G|B|BW
// filter-spec := max|avg
//
// Returns true if we successfully extracted params, else returns
// false for any error or if the line is a comment or blank line:
//
bool extractOneLayerParams(topologyConfigSpec_t ¶ms, const string &line)
{
string stoken;
char ctoken;
std::istringstream ss(line);
ss >> stoken; // First token is always the layer name
if (stoken == "" || stoken[0] == '#') {
return false;
}
params.layerName = stoken;
// Extract the rest of the parameters:
bool done = false;
while (!done && !ss.eof() && ss.tellg() != -1) {
string stoken;
ss >> stoken;
if (stoken.size() == 0) {
break;
}
if (stoken == "size") {
params.size = extractDxySize(ss);
params.sizeSpecified = true;
} else if (stoken == "from") {
ss >> params.fromLayerName;
} else if (stoken == "channel") {
extractChannel(params, ss);
params.colorChannelSpecified = true;
} else if (stoken == "radius") {
params.radius = extractXySize(ss);
params.radiusSpecified = true;
} else if (stoken == "tf") {
ss >> params.transferFunctionName;
params.tfSpecified = true;
} else if (stoken == "convolve") {
// The next non-space char determines whether this is a
// conv-filter or conv-network parameter:
auto pos = ss.tellg();
ss >> ctoken;
if (ctoken == '{') {
// Convolution filter spec, expects: {{},{}}
ss.seekg(pos);
extractConvolveFilterMatrix(params, ss); // Allocates and initializes the matrix
params.isConvolutionFilterLayer = true;
} else {
extern float randomFloat(void);
// Convolution network layer expects: kernel size to be defined
ss.seekg(pos);
params.kernelSize = extractXySize(ss);
params.isConvolutionNetworkLayer = true;
}
} else if (stoken == "pool") {
extractPoolMethod(params, ss);
params.poolSize = extractXySize(ss);
params.isPoolingLayer = true;
} else {
configErrorThrow(params, "Unknown parameter");
}
params.isRegularLayer =
!params.isConvolutionFilterLayer
&& !params.isConvolutionNetworkLayer
&& !params.isPoolingLayer;
}
return true;
}
// Fix-ups and consistency checks go here after all the records have
// been collected:
//
void consistency(vector<topologyConfigSpec_t> ¶ms)
{
if (params.size() < 2) {
err << "Topology config spec needs at least an input and output layer" << endl;
throw exceptionConfigFile();
}
// Specific only to input layer:
if (params[0].layerName != "input") {
err << "First layer must be named input" << endl;
throw exceptionConfigFile();
}
if (params[0].fromLayerName.size() > 0) {
warn << "Input layer cannot have a from parameter" << endl;
}
if (!params[0].isRegularLayer) {
err << "Input layer cannot have a convolve or pool parameter" << endl;
throw exceptionConfigFile();
}
if (params[0].radiusSpecified) {
err << "Input layer cannot have a radius parameter" << endl;
throw exceptionConfigFile();
}
if (params[0].tfSpecified) {
err << "Input layer cannot have a tf parameter" << endl;
throw exceptionConfigFile();
}
// In common to hidden layer and output layer specs:
for (auto it = params.begin() + 1; it != params.end(); ++it) {
auto &spec = *it;
// If no tf parameter was specified, set a default.
// A transfer function is permitted on all layers except the input
// and convolution filter layers:
if (!spec.tfSpecified) {
if (spec.isConvolutionFilterLayer) {
spec.transferFunctionName = "linear";
} else {
spec.transferFunctionName = "tanh";
}
}
// Check from parameter:
if (spec.fromLayerName.size() == 0) {
err << "All hidden and output layers need a from parameter" << endl;
throw exceptionConfigFile();
}
// Verify from layer and compute its index
auto iti = find_if(params.begin(), params.end() - 1, [spec](topologyConfigSpec_t &pspec) {
return pspec.layerName == spec.fromLayerName; });
if (iti == params.end() - 1) {
err << "Undefined from-layer:" << spec.fromLayerName << endl;
throw exceptionConfigFile();
} else {
spec.fromLayerIndex = distance(params.begin(), iti);
}
// If a size param was not specified, copy the size from the from-layer:
if (!spec.sizeSpecified) {
spec.size = params[spec.fromLayerIndex].size;
}
// Ensure that if a layer name is repeated, the size must match the size of
// the previous spec:
for (auto itp = it - 1; itp != params.begin() - 1; --itp) {
if (itp->layerName == spec.layerName) {
if (itp->size.depth != spec.size.depth
|| itp->size.x != spec.size.x
|| itp->size.y != spec.size.y) {
err << "Repeated layer spec for \"" << spec.layerName
<< "\" must have the same size" << endl;
throw exceptionConfigFile();
}
}
}
// Check that radius is not specified at the same with with a convolve or pool parameter:
if (spec.radiusSpecified && !spec.isRegularLayer) {
err << "Radius cannot be specified on a convolve or pool layer." << endl;
throw exceptionConfigFile();
}
// Check convolve kernel size:
if ((spec.isConvolutionFilterLayer || spec.isConvolutionNetworkLayer)
&& (spec.kernelSize.x == 0 || spec.kernelSize.y == 0)) {
err << "Error in topology config file: Convolve kernel dimension cannot be zero" << endl;
throw exceptionConfigFile();
}
// Initialize convolution network kernels if needed: Construct a matrix of the
// correct size, and make depth copies
if (spec.isConvolutionNetworkLayer) {
extern float randomFloat();
vector<float> flatMatrix(spec.kernelSize.x * spec.kernelSize.y);
std::for_each(flatMatrix.begin(), flatMatrix.end(), [](float &w) {
w = randomFloat() / 100.0; // Do something more intelligent here
});
spec.flatConvolveMatrix.assign(spec.size.depth, flatMatrix);
}
}
// Specific only to hidden layers:
for (auto it = params.begin() + 1; it != params.end() - 1; ++it) {
auto &spec = *it;
// Verify that if a depth was specified > 1, then there must be a convolve or pool param:
if (spec.size.depth > 1 && !(spec.isConvolutionNetworkLayer || spec.isPoolingLayer)) {
err << "A layer with depth > 1 must be a convolution networking or pooling layer" << endl;
throw exceptionConfigFile();
}
}
// Specific only to output layer:
if (params.back().layerName != "output") {
err << "Last layer must be named output" << endl;
throw exceptionConfigFile();
}
if (params.back().isConvolutionNetworkLayer || params.back().size.depth > 1) {
err << "Output layer cannot be a convolution network layer" << endl;
throw exceptionConfigFile();
}
}
// Returns an array (vector) of topologyConfigSpec_t objects containing all
// the layer parameters for all the layers, extracted or derived from the
// topology config file.
// See the GitHub wiki (https://github.com/davidrmiller/neural2d)
// for more information about the format of the topology config file.
// Throws an exception for any error.
//
vector<topologyConfigSpec_t> Net::parseTopologyConfig(std::istream &cfg)
{
if (!cfg) {
err << "Error reading topology config stream" << endl;
throw exceptionConfigFile();
}
vector<topologyConfigSpec_t> allLayers;
unsigned lineNum = 0;
string line;
while (!cfg.eof() && getline(cfg, line)) {
++lineNum;
if (line[0] != '\n' && line[0] != '\0' && line[0] != '#') { // this check may not be needed here any longer?
topologyConfigSpec_t params;
params.configLineNum = lineNum;
if (extractOneLayerParams(params, line)) { // Get what we can from the topology config file
allLayers.push_back(params);
}
}
}
consistency(allLayers); // Add missing fields
return allLayers;
}
} // End namespace NNet