-
Notifications
You must be signed in to change notification settings - Fork 10
/
vl_simplenn.m
570 lines (515 loc) · 18.8 KB
/
vl_simplenn.m
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
function res = vl_simplenn(net, x, dzdy, res, ResultTest,varargin)
%VL_SIMPLENN Evaluate a SimpleNN network.
% RES = VL_SIMPLENN(NET, X) evaluates the convnet NET on data X.
% RES = VL_SIMPLENN(NET, X, DZDY) evaluates the convnent NET and its
% derivative on data X and output derivative DZDY (foward+bacwkard pass).
% RES = VL_SIMPLENN(NET, X, [], RES) evaluates the NET on X reusing the
% structure RES.
% RES = VL_SIMPLENN(NET, X, DZDY, RES) evaluates the NET on X and its
% derivatives reusing the structure RES.
%
% This function process networks using the SimpleNN wrapper
% format. Such networks are 'simple' in the sense that they consist
% of a linear sequence of computational layers. You can use the
% `dagnn.DagNN` wrapper for more complex topologies, or write your
% own wrapper around MatConvNet computational blocks for even
% greater flexibility.
%
% The format of the network structure NET and of the result
% structure RES are described in some detail below. Most networks
% expect the input data X to be standardized, for example by
% rescaling the input image(s) and subtracting a mean. Doing so is
% left to the user, but information on how to do this is usually
% contained in the `net.meta` field of the NET structure (see
% below).
%
% The NET structure needs to be updated as new features are
% introduced in MatConvNet; use the `VL_SIMPLENN_TIDY()` function
% to make an old network current, as well as to cleanup and check
% the structure of an existing network.
%
% Networks can run either on the CPU or GPU. Use VL_SIMPLENN_MOVE()
% to move the network parameters between these devices.
%
% To print or obtain summary of the network structure, use the
% VL_SIMPLENN_DISPLAY() function.
%
% VL_SIMPLENN(NET, X, DZDY, RES, 'OPT', VAL, ...) takes the following
% options:
%
% `Mode`:: `normal`
% Specifies the mode of operation. It can be either `'normal'` or
% `'test'`. In test mode, dropout and batch-normalization are
% bypassed. Note that, when a network is deployed, it may be
% preferable to *remove* such blocks altogether.
%
% `ConserveMemory`:: `false`
% Aggressively delete intermediate results. This in practice has
% a very small performance hit and allows training much larger
% models. However, it can be useful to disable it for
% debugging. Keeps the values in `res(1)` (input) and `res(end)`
% (output) with the outputs of `loss` and `softmaxloss` layers.
% It is also possible to preserve individual layer outputs
% by setting `net.layers{...}.precious` to `true`.
% For back-propagation, keeps only the derivatives with respect to
% weights.
%
% `CuDNN`:: `true`
% Use CuDNN when available.
%
% `Accumulate`:: `false`
% Accumulate gradients in back-propagation instead of rewriting
% them. This is useful to break the computation in sub-batches.
% The gradients are accumulated to the provided RES structure
% (i.e. to call VL_SIMPLENN(NET, X, DZDY, RES, ...).
%
% `BackPropDepth`:: `inf`
% Limit the back-propagation to top-N layers.
%
% `SkipForward`:: `false`
% Reuse the output values from the provided RES structure and compute
% only the derivatives (backward pass).
%
% ## The result format
%
% SimpleNN returns the result of its calculations in the RES
% structure array. RES(1) contains the input to the network, while
% RES(2), RES(3), ... contain the output of each layer, from first
% to last. Each entry has the following fields:
%
% - `res(i+1).x`: the output of layer `i`. Hence `res(1).x` is the
% network input.
%
% - `res(i+1).aux`: any auxiliary output data of layer i. For example,
% dropout uses this field to store the dropout mask.
%
% - `res(i+1).dzdx`: the derivative of the network output relative
% to the output of layer `i`. In particular `res(1).dzdx` is the
% derivative of the network output with respect to the network
% input.
%
% - `res(i+1).dzdw`: a cell array containing the derivatives of the
% network output relative to the parameters of layer `i`. It can
% be a cell array for multiple parameters.
%
% ## The network format
%
% The network is represented by the NET structure, which contains
% two fields:
%
% - `net.layers` is a cell array with the CNN layers.
%
% - `net.meta` is a grab-bag of auxiliary application-dependent
% information, including for example details on how to normalize
% input data, the class names for a classifiers, or details of
% the learning algorithm. The content of this field is ignored by
% VL_SIMPLENN().
%
% SimpleNN is aware of the following layers:
%
% Convolution layer::
% The convolution layer wraps VL_NNCONV(). It has fields:
%
% - `layer.type` contains the string `'conv'`.
% - `layer.weights` is a cell array with filters and biases.
% - `layer.stride` is the sampling stride (e.g. 1).
% - `layer.pad` is the padding (e.g. 0).
%
% Convolution transpose layer::
% The convolution transpose layer wraps VL_NNCONVT(). It has fields:
%
% - `layer.type` contains the string `'convt'`.
% - `layer.weights` is a cell array with filters and biases.
% - `layer.upsample` is the upsampling factor (e.g. 1).
% - `layer.crop` is the amount of output cropping (e.g. 0).
%
% Max pooling layer::
% The max pooling layer wraps VL_NNPOOL(). It has fields:
%
% - `layer.type` contains the string `'pool'`.
% - `layer.method` is the pooling method (either 'max' or 'avg').
% - `layer.pool` is the pooling size (e.g. 3).
% - `layer.stride` is the sampling stride (usually 1).
% - `layer.pad` is the padding (usually 0).
%
% Normalization (LRN) layer::
% The normalization layer wraps VL_NNNORMALIZE(). It has fields:
%
% - `layer.type` contains the string `'normalize'` or `'lrn'`.
% - `layer.param` contains the normalization parameters (see VL_NNNORMALIZE()).
%
% Spatial normalization layer::
% The spatial normalization layer wraps VL_NNSPNORM(). It has fields:
%
% - `layer.type` contains the string `'spnorm'`.
% - `layer.param` contains the normalization parameters (see VL_NNSPNORM()).
%
% Batch normalization layer::
% This layer wraps VL_NNBNORM(). It has fields:
%
% - `layer.type` contains the string `'bnorm'`.
% - `layer.weights` contains is a cell-array with, multiplier and
% biases, and moments parameters
%
% Note that moments are used only in `'test'` mode to bypass batch
% normalization.
%
% ReLU and Sigmoid layers::
% The ReLU layer wraps VL_NNRELU(). It has fields:
%
% - `layer.type` contains the string `'relu'`.
% - `layer.leak` is the leak factor (e.g. 0).
%
% The sigmoid layer is the same, but for the sigmoid function,
% with `relu` replaced by `sigmoid` and no leak factor.
%
% Dropout layer::
% The dropout layer wraps VL_NNDROPOUT(). It has fields:
%
% - `layer.type` contains the string `'dropout'`.
% - `layer.rate` is the dropout rate (e.g. 0.5).
%
% Note that the block is bypassed in `test` mode.
%
% Softmax layer::
% The softmax layer wraps VL_NNSOFTMAX(). It has fields
%
% - `layer.type` contains the string`'softmax'`.
%
% Log-loss layer and softmax-log-loss::
% The log-loss layer wraps VL_NNLOSS(). It has fields:
%
% - `layer.type` contains `'loss'`.
% - `layer.class` contains the ground-truth class labels.
%
% The softmax-log-loss layer wraps VL_NNSOFTMAXLOSS() instead. it
% has the same parameters, but `type` contains the `'softmaxloss'`
% string.
%
% P-dist layer::
% The p-dist layer wraps VL_NNPDIST(). It has fields:
%
% - `layer.type` contains the string `'pdist'`.
% - `layer.p` is the P parameter of the P-distance (e.g. 2).
% - `layer.noRoot` it tells whether to raise the distance to
% the P-th power (e.g. `false`).
% - `layer.epsilon` is the regularization parameter for the derivatives.
%
% Custom layer::
% This can be used to specify custom layers.
%
% - `layer.type` contains the string `'custom'`.
% - `layer.forward` is a function handle computing the block.
% - `layer.backward` is a function handle computing the block derivative.
%
% The first function is called as
%
% res(i+1) = layer.forward(layer, res(i), res(i+1))
%
% where RES is the structure array specified before. The second function is
% called as
%
% res(i) = layer.backward(layer, res(i), res(i+1))
%
% Note that the `layer` structure can contain additional custom
% fields if needed.
%
% See also: dagnn.DagNN, VL_SIMPLENN_TIDY(),
% VL_SIMPLENN_DISPLAY(), VL_SIMPLENN_MOVE().
% Copyright (C) 2014-15 Andrea Vedaldi.
% All rights reserved.
%
% This file is part of the VLFeat library and is made available under
% the terms of the BSD license (see the COPYING file).
opts.conserveMemory = false ;
opts.sync = false ;
opts.mode = 'normal' ;
opts.accumulate = false ;
opts.cudnn = true ;
opts.backPropDepth = +inf ;
opts.skipForward = false;
opts = vl_argparse(opts, varargin);
n = numel(net.layers) ;
assert(opts.backPropDepth > 0, 'Invalid `backPropDepth` value (!>0)');
backPropLim = max(n - opts.backPropDepth + 1, 1);
if (nargin <= 2) || isempty(dzdy)
doder = false ;
if opts.skipForward
error('simplenn:skipForwardNoBackwPass', ...
'`skipForward` valid only when backward pass is computed.');
end
else
doder = true ;
end
if opts.cudnn
cudnn = {'CuDNN'} ;
else
cudnn = {'NoCuDNN'} ;
end
switch lower(opts.mode)
case 'normal'
testMode = false ;
case 'test'
testMode = true ;
otherwise
error('Unknown mode ''%s''.', opts. mode) ;
end
gpuMode = isa(x, 'gpuArray') ;
if nargin <= 3 || isempty(res)
if opts.skipForward
error('simplenn:skipForwardEmptyRes', ...
'RES structure must be provided for `skipForward`.');
end
res = struct(...
'x', cell(1,n+1), ...
'dzdx', cell(1,n+1), ...
'dzdw', cell(1,n+1), ...
'aux', cell(1,n+1), ...
'stats', cell(1,n+1), ...
'time', num2cell(zeros(1,n+1)), ...
'backwardTime', num2cell(zeros(1,n+1))) ;
end
if ~opts.skipForward
res(1).x = x ;
end
% -------------------------------------------------------------------------
% Forward pass
% -------------------------------------------------------------------------
nBypassLayerBuf = {};
for i=1:n
if opts.skipForward, break; end;
l = net.layers{i} ;
res(i).time = tic ;
switch l.type
case 'conv'
res(i+1).x = vl_nnconv(res(i).x, l.weights{1}, l.weights{2}, ...
'pad', l.pad, ...
'stride', l.stride, ...
l.opts{:}, ...
cudnn{:}) ;
case 'convt'
res(i+1).x = vl_nnconvt(res(i).x, l.weights{1}, l.weights{2}, ...
'crop', l.crop, ...
'upsample', l.upsample, ...
'numGroups', l.numGroups, ...
l.opts{:}, ...
cudnn{:}) ;
case 'bypassCatch'
res(i+1).x = res(i).x + nBypassLayerBuf{l.passIDn}; %res(nBypassLayerNum).x;
nBypassLayerBuf{l.passIDn} = [];
case 'bypassToss'
res(i+1).x = res(i).x;
nBypassLayerBuf{l.passIDn} = res(i).x;
case 'unpool'
szz = size(res(i).x);
tsz = size(szz(:));
if tsz(1) ==3
szz(4)=1;
end
oneTbuf = gpuArray(ones(szz(1)*2,szz(2)*2, szz(3), szz(4),'single'));
res(i+1).x = vl_nnpool(oneTbuf, l.pool, res(i).x, ...
'pad', l.pad, 'stride', l.stride, ...
'method', l.method, ...
l.opts{:}, ...
cudnn{:}) ;
clear oneTbuf;
% oneTbuf=[];
case 'pool'
res(i+1).x = vl_nnpool(res(i).x, l.pool, ...
'pad', l.pad, 'stride', l.stride, ...
'method', l.method, ...
l.opts{:}, ...
cudnn{:}) ;
case {'normalize', 'lrn'}
res(i+1).x = vl_nnnormalize(res(i).x, l.param) ;
case 'softmax'
res(i+1).x = vl_nnsoftmax(res(i).x) ;
case 'loss'
res(i+1).x = vl_nnloss(res(i).x, l.class) ;
case 'softmaxloss'
res(i+1).x = vl_nnsoftmaxloss(res(i).x, l.class) ;
case 'dropInpaint'
if testMode
res(i+1).x = res(i).x ;
else
% tempSkipBuf = res(i).x;
[res(i+1).x, res(i+1).aux] = vl_nndropInpaint(res(i).x, 'rate', l.rate) ;
end
case 'euclideanloss'
if ResultTest ==0
res(i+1).x = vl_euclideanloss(res(i).x, l.class);
end
case 'relu'
if l.leak > 0, leak = {'leak', l.leak} ; else leak = {} ; end
res(i+1).x = vl_nnrelu(res(i).x,[],leak{:}) ;
case 'sigmoid'
res(i+1).x = vl_nnsigmoid(res(i).x) ;
case 'noffset'
res(i+1).x = vl_nnnoffset(res(i).x, l.param) ;
case 'spnorm'
res(i+1).x = vl_nnspnorm(res(i).x, l.param) ;
case 'dropout'
if testMode
res(i+1).x = res(i).x ;
else
[res(i+1).x, res(i+1).aux] = vl_nndropout(res(i).x, 'rate', l.rate) ;
end
case 'bnorm'
if testMode
res(i+1).x = vl_nnbnorm(res(i).x, l.weights{1}, l.weights{2}, 'moments', l.weights{3}) ;
else
res(i+1).x = vl_nnbnorm(res(i).x, l.weights{1}, l.weights{2}) ;
end
case 'pdist'
res(i+1).x = vl_nnpdist(res(i).x, l.class, l.p, ...
'noRoot', l.noRoot, ...
'epsilon', l.epsilon, ...
'aggregate', l.aggregate, ...
'instanceWeights', l.instanceWeights) ;
case 'custom'
res(i+1) = l.forward(l, res(i), res(i+1)) ;
otherwise
error('Unknown layer type ''%s''.', l.type) ;
end
% optionally forget intermediate results
needsBProp = doder && i >= backPropLim;
forget = opts.conserveMemory && ~needsBProp ;
if i > 1
lp = net.layers{i-1} ;
% forget RELU input, even for BPROP
forget = forget && (~needsBProp || (strcmp(l.type, 'relu') && ~lp.precious)) ;
forget = forget && ~(strcmp(lp.type, 'loss') || strcmp(lp.type, 'softmaxloss')) ;
forget = forget && ~lp.precious ;
end
if forget
res(i).x = [] ;
end
if gpuMode && opts.sync
wait(gpuDevice) ;
end
res(i).time = toc(res(i).time) ;
end
nBypassLayerBuf = [];
% wait(gpuDevice) ;
% -------------------------------------------------------------------------
% Backward pass
% -------------------------------------------------------------------------
if doder
res(n+1).dzdx = dzdy ;
for i=n:-1:backPropLim
l = net.layers{i} ;
res(i).backwardTime = tic ;
switch l.type
case 'conv'
[res(i).dzdx, dzdw{1}, dzdw{2}] = ...
vl_nnconv(res(i).x, l.weights{1}, l.weights{2}, res(i+1).dzdx, ...
'pad', l.pad, ...
'stride', l.stride, ...
l.opts{:}, ...
cudnn{:}) ;
case 'convt'
[res(i).dzdx, dzdw{1}, dzdw{2}] = ...
vl_nnconvt(res(i).x, l.weights{1}, l.weights{2}, res(i+1).dzdx, ...
'crop', l.crop, ...
'upsample', l.upsample, ...
'numGroups', l.numGroups, ...
l.opts{:}, ...
cudnn{:}) ;
case 'bypassCatch'
res(i).dzdx = res(i+1).dzdx;
% nBypassLayerBuf = res(i).dzdx;
nBypassLayerBuf{l.passIDn} = res(i).dzdx;
case 'bypassToss'
res(i).dzdx = res(i+1).dzdx + nBypassLayerBuf{l.passIDn};
nBypassLayerBuf{l.passIDn} = [];
case 'unpool'
res(i).dzdx = vl_nnpool(res(i+1).dzdx, l.pool, ...
'pad', l.pad, 'stride', l.stride, ...
'method', l.method, ...
l.opts{:}, ...
cudnn{:}) ;
case 'pool'
res(i).dzdx = vl_nnpool(res(i).x, l.pool, res(i+1).dzdx, ...
'pad', l.pad, 'stride', l.stride, ...
'method', l.method, ...
l.opts{:}, ...
cudnn{:}) ;
case {'normalize', 'lrn'}
res(i).dzdx = vl_nnnormalize(res(i).x, l.param, res(i+1).dzdx) ;
case 'softmax'
res(i).dzdx = vl_nnsoftmax(res(i).x, res(i+1).dzdx) ;
case 'loss'
res(i).dzdx = vl_nnloss(res(i).x, l.class, res(i+1).dzdx) ;
case 'softmaxloss'
res(i).dzdx = vl_nnsoftmaxloss(res(i).x, l.class, res(i+1).dzdx) ;
case 'euclideanloss'
res(i).dzdx = vl_euclideanloss(res(i).x, l.class, res(i+1).dzdx) ;
case 'relu'
if l.leak > 0, leak = {'leak', l.leak} ; else leak = {} ; end
if ~isempty(res(i).x)
res(i).dzdx = vl_nnrelu(res(i).x, res(i+1).dzdx, leak{:}) ;
else
% if res(i).x is empty, it has been optimized away, so we use this
% hack (which works only for ReLU):
res(i).dzdx = vl_nnrelu(res(i+1).x, res(i+1).dzdx, leak{:}) ;
end
case 'sigmoid'
res(i).dzdx = vl_nnsigmoid(res(i).x, res(i+1).dzdx) ;
case 'noffset'
res(i).dzdx = vl_nnnoffset(res(i).x, l.param, res(i+1).dzdx) ;
case 'spnorm'
res(i).dzdx = vl_nnspnorm(res(i).x, l.param, res(i+1).dzdx) ;
case 'dropInpaint'
res(i).dzdx = vl_nndropInpaint(res(i).x, res(i+1).dzdx, ...
'mask', res(i+1).aux) ;
case 'dropout'
if testMode
res(i).dzdx = res(i+1).dzdx ;
else
res(i).dzdx = vl_nndropout(res(i).x, res(i+1).dzdx, ...
'mask', res(i+1).aux) ;
end
case 'bnorm'
[res(i).dzdx, dzdw{1}, dzdw{2}, dzdw{3}] = ...
vl_nnbnorm(res(i).x, l.weights{1}, l.weights{2}, res(i+1).dzdx) ;
% multiply the moments update by the number of images in the batch
% this is required to make the update additive for subbatches
% and will eventually be normalized away
dzdw{3} = dzdw{3} * size(res(i).x,4) ;
case 'pdist'
res(i).dzdx = vl_nnpdist(res(i).x, l.class, ...
l.p, res(i+1).dzdx, ...
'noRoot', l.noRoot, ...
'epsilon', l.epsilon, ...
'aggregate', l.aggregate, ...
'instanceWeights', l.instanceWeights) ;
case 'custom'
res(i) = l.backward(l, res(i), res(i+1)) ;
end % layers
switch l.type
case {'conv', 'convt', 'bnorm'}
if ~opts.accumulate
res(i).dzdw = dzdw ;
else
for j=1:numel(dzdw)
res(i).dzdw{j} = res(i).dzdw{j} + dzdw{j} ;
end
end
dzdw = [] ;
end
if opts.conserveMemory && ~net.layers{i}.precious && i ~= n
res(i+1).dzdx = [] ;
res(i+1).x = [] ;
end
if gpuMode && opts.sync
wait(gpuDevice) ;
end
res(i).backwardTime = toc(res(i).backwardTime) ;
end
if i > 1 && i == backPropLim && opts.conserveMemory && ~net.layers{i}.precious
res(i).dzdx = [] ;
res(i).x = [] ;
end
end
nBypassLayerBuf = [];
% wait(gpuDevice) ;