-
Notifications
You must be signed in to change notification settings - Fork 133
/
cnn_shape_train.m
520 lines (466 loc) · 16.5 KB
/
cnn_shape_train.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
function [net, info] = cnn_shape_train(net, imdb, getBatch, varargin)
%CNN_SHAPE_TRAIN A modified cnn_train
% Options added:
% 'maxIterPerEpoch'
% 'balancingFunction'
% Hang Su
%
%CNN_TRAIN An example implementation of SGD for training CNNs
% CNN_TRAIN() is an example learner implementing stochastic
% gradient descent with momentum to train a CNN. It can be used
% with different datasets and tasks by providing a suitable
% getBatch function.
%
% The function automatically restarts after each training epoch by
% checkpointing.
%
% The function supports training on CPU or on one or more GPUs
% (specify the list of GPU IDs in the `gpus` option). Multi-GPU
% support is relatively primitive but sufficient to obtain a
% noticable speedup.
% 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.expDir = fullfile('data','exp') ;
opts.continue = true ;
opts.batchSize = 256 ;
opts.numSubBatches = 1 ;
opts.train = [] ;
opts.val = [] ;
opts.gpus = [] ;
opts.prefetch = false ;
opts.numEpochs = 300 ;
opts.learningRate = 0.001 ;
opts.weightDecay = 0.0005 ;
opts.momentum = 0.9 ;
opts.memoryMapFile = fullfile(tempdir, 'matconvnet.bin') ;
opts.profile = false ;
opts.conserveMemory = true ;
opts.backPropDepth = +inf ;
opts.sync = false ;
opts.cudnn = true ;
opts.errorFunction = 'multiclass' ;
opts.errorLabels = {} ;
opts.plotDiagnostics = false ;
opts.plotStatistics = true;
opts.maxIterPerEpoch = [Inf Inf];
opts.balancingFunction = @(v) v;
opts = vl_argparse(opts, varargin) ;
if ~exist(opts.expDir, 'dir'), mkdir(opts.expDir) ; end
if isempty(opts.train), opts.train = find(imdb.images.set==1) ; end
if isempty(opts.val), opts.val = find(imdb.images.set==2) ; end
if isnan(opts.train), opts.train = [] ; end
if isnan(opts.val), opts.val = [] ; end
if numel(opts.maxIterPerEpoch)==1, opts.maxIterPerEpoch = opts.maxIterPerEpoch*[1 1]; end
if ~iscell(opts.balancingFunction), opts.balancingFunction = {opts.balancingFunction}; end
if numel(opts.balancingFunction)==1, opts.balancingFunction = repmat(opts.balancingFunction,[1 2]); end
% -------------------------------------------------------------------------
% Network initialization
% -------------------------------------------------------------------------
net = vl_simplenn_tidy(net); % fill in some eventually missing values
net.layers{end-1}.precious = 1; % do not remove predictions, used for error
vl_simplenn_display(net, 'batchSize', opts.batchSize) ;
evaluateMode = isempty(opts.train) ;
if ~evaluateMode
for i=1:numel(net.layers)
if isfield(net.layers{i}, 'weights')
J = numel(net.layers{i}.weights) ;
for j=1:J
net.layers{i}.momentum{j} = zeros(size(net.layers{i}.weights{j}), 'single') ;
end
if ~isfield(net.layers{i}, 'learningRate')
net.layers{i}.learningRate = ones(1, J, 'single') ;
end
if ~isfield(net.layers{i}, 'weightDecay')
net.layers{i}.weightDecay = ones(1, J, 'single') ;
end
end
end
end
% setup GPUs
numGpus = numel(opts.gpus) ;
if numGpus > 1
if isempty(gcp('nocreate')),
parpool('local',numGpus) ;
spmd, gpuDevice(opts.gpus(labindex)), end
end
elseif numGpus == 1
gpuDevice(opts.gpus)
end
if exist(opts.memoryMapFile), delete(opts.memoryMapFile) ; end
% setup error calculation function
hasError = true ;
if isstr(opts.errorFunction)
switch opts.errorFunction
case 'none'
opts.errorFunction = @error_none ;
hasError = false ;
case 'multiclass'
opts.errorFunction = @error_multiclass ;
if isempty(opts.errorLabels), opts.errorLabels = {'top1err', 'top5err'} ; end
case 'binary'
opts.errorFunction = @error_binary ;
if isempty(opts.errorLabels), opts.errorLabels = {'binerr'} ; end
otherwise
error('Unknown error function ''%s''.', opts.errorFunction) ;
end
end
% -------------------------------------------------------------------------
% Train and validate
% -------------------------------------------------------------------------
modelPath = @(ep) fullfile(opts.expDir, sprintf('net-epoch-%d.mat', ep));
modelFigPath = fullfile(opts.expDir, 'net-train.pdf') ;
start = opts.continue * findLastCheckpoint(opts.expDir) ;
if start >= 1
fprintf('%s: resuming by loading epoch %d\n', mfilename, start) ;
load(modelPath(start), 'net', 'info') ;
net = vl_simplenn_tidy(net) ; % just in case MatConvNet was updated
end
trainQueue = [];
valQueue = [];
for epoch=start+1:opts.numEpochs
% train one epoch and validate
learningRate = opts.learningRate(min(epoch, numel(opts.learningRate))) ;
[train, trainQueue] = next_samples(opts.train, trainQueue, imdb.images.class(opts.train), opts.batchSize*opts.maxIterPerEpoch(1), opts.balancingFunction{1});
[val, valQueue] = next_samples(opts.val, valQueue, imdb.images.class(opts.val), opts.batchSize*opts.maxIterPerEpoch(2), opts.balancingFunction{2});
if numGpus <= 1
[net,stats.train,prof] = process_epoch(opts, getBatch, epoch, train, learningRate, imdb, net) ;
[~,stats.val] = process_epoch(opts, getBatch, epoch, val, 0, imdb, net) ;
if opts.profile
profile('viewer') ;
keyboard ;
end
else
fprintf('%s: sending model to %d GPUs\n', mfilename, numGpus) ;
spmd(numGpus)
[net_, stats_train_,prof_] = process_epoch(opts, getBatch, epoch, train, learningRate, imdb, net) ;
[~, stats_val_] = process_epoch(opts, getBatch, epoch, val, 0, imdb, net_) ;
end
net = net_{1} ;
stats.train = sum([stats_train_{:}],2) ;
stats.val = sum([stats_val_{:}],2) ;
if opts.profile
mpiprofile('viewer', [prof_{:,1}]) ;
keyboard ;
end
clear net_ stats_train_ stats_val_ ;
end
% save
if evaluateMode, sets = {'val'} ; else sets = {'train', 'val'} ; end
for f = sets
f = char(f) ;
n = numel(eval(f)) ;
info.(f).speed(epoch) = n / stats.(f)(1) * max(1, numGpus) ;
info.(f).objective(epoch) = stats.(f)(2) / n ;
info.(f).error(:,epoch) = stats.(f)(3:end) / n ;
end
if ~evaluateMode
fprintf('%s: saving model for epoch %d\n', mfilename, epoch) ;
tic ;
save(modelPath(epoch), 'net', 'info') ;
fprintf('%s: model saved in %.2g s\n', mfilename, toc) ;
end
if opts.plotStatistics
switchfigure(1) ; clf ;
subplot(1,1+hasError,1) ;
if ~evaluateMode
semilogy(1:epoch, info.train.objective, '.-', 'linewidth', 2) ;
hold on ;
end
semilogy(1:epoch, info.val.objective, '.--') ;
xlabel('training epoch') ; ylabel('energy') ;
grid on ;
h=legend(sets) ;
set(h,'color','none');
title('objective') ;
if hasError
subplot(1,2,2) ; leg = {} ;
if ~evaluateMode
plot(1:epoch, info.train.error', '.-', 'linewidth', 2) ;
hold on ;
leg = horzcat(leg, strcat('train ', opts.errorLabels)) ;
end
plot(1:epoch, info.val.error', '.--') ;
leg = horzcat(leg, strcat('val ', opts.errorLabels)) ;
set(legend(leg{:}),'color','none') ;
grid on ;
xlabel('training epoch') ; ylabel('error') ;
title('error') ;
end
drawnow ;
print(1, modelFigPath, '-dpdf') ;
end
end
% -------------------------------------------------------------------------
function err = error_multiclass(opts, labels, res)
% -------------------------------------------------------------------------
predictions = gather(res(end-1).x) ;
[~,predictions] = sort(predictions, 3, 'descend') ;
% be resilient to badly formatted labels
if numel(labels) == size(predictions, 4)
labels = reshape(labels,1,1,1,[]) ;
end
% skip null labels
mass = single(labels(:,:,1,:) > 0) ;
if size(labels,3) == 2
% if there is a second channel in labels, used it as weights
mass = mass .* labels(:,:,2,:) ;
labels(:,:,2,:) = [] ;
end
m = min(5, size(predictions,3)) ;
error = ~bsxfun(@eq, predictions, labels) ;
err(1,1) = sum(sum(sum(mass .* error(:,:,1,:)))) ;
err(2,1) = sum(sum(sum(mass .* min(error(:,:,1:m,:),[],3)))) ;
% -------------------------------------------------------------------------
function err = error_binary(opts, labels, res)
% -------------------------------------------------------------------------
predictions = gather(res(end-1).x) ;
error = bsxfun(@times, predictions, labels) < 0 ;
err = sum(error(:)) ;
% -------------------------------------------------------------------------
function err = error_none(opts, labels, res)
% -------------------------------------------------------------------------
err = zeros(0,1) ;
% -------------------------------------------------------------------------
function [net_cpu,stats,prof] = process_epoch(opts, getBatch, epoch, subset, learningRate, imdb, net_cpu)
% -------------------------------------------------------------------------
% move the CNN to GPU (if needed)
numGpus = numel(opts.gpus) ;
if numGpus >= 1
net = vl_simplenn_move(net_cpu, 'gpu') ;
one = gpuArray(single(1)) ;
else
net = net_cpu ;
net_cpu = [] ;
one = single(1) ;
end
% assume validation mode if the learning rate is zero
training = learningRate > 0 ;
if training
mode = 'train' ;
evalMode = 'normal' ;
else
mode = 'val' ;
evalMode = 'test' ;
end
% turn on the profiler (if needed)
if opts.profile
if numGpus <= 1
prof = profile('info') ;
profile clear ;
profile on ;
else
prof = mpiprofile('info') ;
mpiprofile reset ;
mpiprofile on ;
end
end
res = [] ;
mmap = [] ;
stats = [] ;
start = tic ;
for t=1:opts.batchSize:numel(subset)
fprintf('%s: epoch %02d: %3d/%3d: ', mode, epoch, ...
fix(t/opts.batchSize)+1, ceil(numel(subset)/opts.batchSize)) ;
batchSize = min(opts.batchSize, numel(subset) - t + 1) ;
numDone = 0 ;
error = [] ;
for s=1:opts.numSubBatches
% get this image batch and prefetch the next
batchStart = t + (labindex-1) + (s-1) * numlabs ;
batchEnd = min(t+opts.batchSize-1, numel(subset)) ;
batch = subset(batchStart : opts.numSubBatches * numlabs : batchEnd) ;
[im, labels] = getBatch(imdb, batch) ;
if opts.prefetch
if s==opts.numSubBatches
batchStart = t + (labindex-1) + opts.batchSize ;
batchEnd = min(t+2*opts.batchSize-1, numel(subset)) ;
else
batchStart = batchStart + numlabs ;
end
nextBatch = subset(batchStart : opts.numSubBatches * numlabs : batchEnd) ;
getBatch(imdb, nextBatch) ;
end
if numGpus >= 1
im = gpuArray(im) ;
end
% evaluate the CNN
net.layers{end}.class = labels ;
if training, dzdy = one; else, dzdy = [] ; end
res = vl_simplenn(net, im, dzdy, res, ...
'accumulate', s ~= 1, ...
'mode', evalMode, ...
'conserveMemory', opts.conserveMemory, ...
'backPropDepth', opts.backPropDepth, ...
'sync', opts.sync, ...
'cudnn', opts.cudnn) ;
% accumulate training errors
error = sum([error, [...
sum(double(gather(res(end).x))) ;
reshape(opts.errorFunction(opts, labels, res),[],1) ; ]],2) ;
numDone = numDone + numel(batch) ;
end % next sub-batch
% gather and accumulate gradients across labs
if training
if numGpus <= 1
[net,res] = accumulate_gradients(opts, learningRate, batchSize, net, res) ;
else
if isempty(mmap)
mmap = map_gradients(opts.memoryMapFile, net, res, numGpus) ;
end
write_gradients(mmap, net, res) ;
labBarrier() ;
[net,res] = accumulate_gradients(opts, learningRate, batchSize, net, res, mmap) ;
end
end
% collect and print learning statistics
time = toc(start) ;
stats = sum([stats,[0 ; error]],2); % works even when stats=[]
stats(1) = time ;
n = t + batchSize - 1 ; % number of images processed overall
speed = n/time ;
fprintf('%.1f Hz%s\n', speed) ;
m = n / max(1,numlabs) ; % num images processed on this lab only
fprintf(' obj:%.3g', stats(2)/m) ;
for i=1:numel(opts.errorLabels)
fprintf(' %s:%.3g', opts.errorLabels{i}, stats(i+2)/m) ;
end
fprintf(' [%d/%d]', numDone, batchSize);
fprintf('\n') ;
% collect diagnostic statistics
if training & opts.plotDiagnostics
switchfigure(2) ; clf ;
diag = [res.stats] ;
barh(horzcat(diag.variation)) ;
set(gca,'TickLabelInterpreter', 'none', ...
'YTickLabel',horzcat(diag.label), ...
'YDir', 'reverse', ...
'XScale', 'log', ...
'XLim', [1e-5 1]) ;
drawnow ;
end
end
% switch off the profiler
if opts.profile
if numGpus <= 1
prof = profile('info') ;
profile off ;
else
prof = mpiprofile('info');
mpiprofile off ;
end
else
prof = [] ;
end
% bring the network back to CPU
if numGpus >= 1
net_cpu = vl_simplenn_move(net, 'cpu') ;
else
net_cpu = net ;
end
% -------------------------------------------------------------------------
function [net,res] = accumulate_gradients(opts, lr, batchSize, net, res, mmap)
% -------------------------------------------------------------------------
if nargin >= 6
numGpus = numel(mmap.Data) ;
else
numGpus = 1 ;
end
for l=numel(net.layers):-1:1
for j=1:numel(res(l).dzdw)
% accumualte gradients from multiple labs (GPUs) if needed
if numGpus > 1
tag = sprintf('l%d_%d',l,j) ;
tmp = zeros(size(mmap.Data(labindex).(tag)), 'single') ;
for g = setdiff(1:numGpus, labindex)
tmp = tmp + mmap.Data(g).(tag) ;
end
res(l).dzdw{j} = res(l).dzdw{j} + tmp ;
end
if j == 3 && strcmp(net.layers{l}.type, 'bnorm')
% special case for learning bnorm moments
thisLR = net.layers{l}.learningRate(j) ;
net.layers{l}.weights{j} = ...
(1-thisLR) * net.layers{l}.weights{j} + ...
(thisLR/batchSize) * res(l).dzdw{j} ;
else
% standard gradient training
thisDecay = opts.weightDecay * net.layers{l}.weightDecay(j) ;
thisLR = lr * net.layers{l}.learningRate(j) ;
net.layers{l}.momentum{j} = ...
opts.momentum * net.layers{l}.momentum{j} ...
- thisDecay * net.layers{l}.weights{j} ...
- (1 / batchSize) * res(l).dzdw{j} ;
net.layers{l}.weights{j} = net.layers{l}.weights{j} + ...
thisLR * net.layers{l}.momentum{j} ;
end
% if requested, collect some useful stats for debugging
if opts.plotDiagnostics
variation = [] ;
label = '' ;
switch net.layers{l}.type
case {'conv','convt'}
variation = thisLR * mean(abs(net.layers{l}.momentum{j}(:))) ;
if j == 1 % fiters
base = mean(abs(net.layers{l}.weights{j}(:))) ;
label = 'filters' ;
else % biases
base = mean(abs(res(l+1).x(:))) ;
label = 'biases' ;
end
variation = variation / base ;
label = sprintf('%s_%s', net.layers{l}.name, label) ;
end
res(l).stats.variation(j) = variation ;
res(l).stats.label{j} = label ;
end
end
end
% -------------------------------------------------------------------------
function mmap = map_gradients(fname, net, res, numGpus)
% -------------------------------------------------------------------------
format = {} ;
for i=1:numel(net.layers)
for j=1:numel(res(i).dzdw)
format(end+1,1:3) = {'single', size(res(i).dzdw{j}), sprintf('l%d_%d',i,j)} ;
end
end
format(end+1,1:3) = {'double', [3 1], 'errors'} ;
if ~exist(fname) && (labindex == 1)
f = fopen(fname,'wb') ;
for g=1:numGpus
for i=1:size(format,1)
fwrite(f,zeros(format{i,2},format{i,1}),format{i,1}) ;
end
end
fclose(f) ;
end
labBarrier() ;
mmap = memmapfile(fname, 'Format', format, 'Repeat', numGpus, 'Writable', true) ;
% -------------------------------------------------------------------------
function write_gradients(mmap, net, res)
% -------------------------------------------------------------------------
for i=1:numel(net.layers)
for j=1:numel(res(i).dzdw)
mmap.Data(labindex).(sprintf('l%d_%d',i,j)) = gather(res(i).dzdw{j}) ;
end
end
% -------------------------------------------------------------------------
function epoch = findLastCheckpoint(modelDir)
% -------------------------------------------------------------------------
list = dir(fullfile(modelDir, 'net-epoch-*.mat')) ;
tokens = regexp({list.name}, 'net-epoch-([\d]+).mat', 'tokens') ;
epoch = cellfun(@(x) sscanf(x{1}{1}, '%d'), tokens) ;
epoch = max([epoch 0]) ;
% -------------------------------------------------------------------------
function switchfigure(n)
% -------------------------------------------------------------------------
if get(0,'CurrentFigure') ~= n
try
set(0,'CurrentFigure',n) ;
catch
figure(n) ;
end
end