-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_train.m
422 lines (379 loc) · 14.1 KB
/
cnn_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
function [net, info] = cnn_train(net, imdb, getBatch, varargin)
%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.batchSize = 256 ;
opts.numSubBatches = 1 ;
opts.train = [] ;
opts.val = [] ;
opts.numEpochs = 300 ;
opts.gpus = [] ; % which GPU devices to use (none, one, or more)
opts.learningRate = 0.001 ;
opts.continue = false ;
opts.expDir = fullfile('data','exp') ;
opts.conserveMemory = false ;
opts.backPropDepth = +inf ;
opts.sync = false ;
opts.prefetch = false ;
opts.cudnn = true ;
opts.weightDecay = 0.0005 ;
opts.momentum = 0.9 ;
opts.errorFunction = 'multiclass' ;
opts.errorLabels = {} ;
opts.plotDiagnostics = false ;
opts.memoryMapFile = fullfile(tempdir, 'matconvnet.bin') ;
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
% -------------------------------------------------------------------------
% Network initialization
% -------------------------------------------------------------------------
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
% Legacy code: will be removed
if isfield(net.layers{i}, 'filters')
net.layers{i}.momentum{1} = zeros(size(net.layers{i}.filters), 'single') ;
net.layers{i}.momentum{2} = zeros(size(net.layers{i}.biases), 'single') ;
if ~isfield(net.layers{i}, 'learningRate')
net.layers{i}.learningRate = ones(1, 2, 'single') ;
end
if ~isfield(net.layers{i}, 'weightDecay')
net.layers{i}.weightDecay = single([1 0]) ;
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
if isstr(opts.errorFunction)
switch opts.errorFunction
case 'none'
opts.errorFunction = @error_none ;
case 'multiclass'
opts.errorFunction = @error_multiclass ;
if isempty(opts.errorLabels), opts.errorLabels = {'top1e', 'top5e'} ; end
case 'binary'
opts.errorFunction = @error_binary ;
if isempty(opts.errorLabels), opts.errorLabels = {'bine'} ; end
otherwise
error('Uknown 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('resuming by loading epoch %d\n', start) ;
load(modelPath(start), 'net', 'info') ;
end
for epoch=start+1:opts.numEpochs
% train one epoch and validate
learningRate = opts.learningRate(min(epoch, numel(opts.learningRate))) ;
train = opts.train(randperm(numel(opts.train))) ; % shuffle
val = opts.val ;
if numGpus <= 1
[net,stats.train] = process_epoch(opts, getBatch, epoch, train, learningRate, imdb, net) ;
[~,stats.val] = process_epoch(opts, getBatch, epoch, val, 0, imdb, net) ;
else
spmd(numGpus)
[net_, stats_train_] = 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) ;
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, save(modelPath(epoch), 'net', 'info') ; end
% figure(1) ; clf ;
% hasError = isa(opts.errorFunction, 'function_handle') ;
% 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
% -------------------------------------------------------------------------
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
error = ~bsxfun(@eq, predictions, labels) ;
err(1,1) = sum(sum(sum(mass .* error(:,:,1,:)))) ;
err(2,1) = sum(sum(sum(mass .* min(error(:,:,1:5,:),[],3)))) ;
% -------------------------------------------------------------------------
function err = error_binaryclass(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 CNN to GPU as needed
numGpus = numel(opts.gpus) ;
if numGpus >= 1
net = vl_simplenn_move(net_cpu, 'gpu') ;
else
net = net_cpu ;
net_cpu = [] ;
end
% validation mode if learning rate is zero
training = learningRate > 0 ;
if training, mode = 'training' ; else, mode = 'validation' ; end
if nargout > 2, mpiprofile on ; end
numGpus = numel(opts.gpus) ;
if numGpus >= 1
one = gpuArray(single(1)) ;
else
one = single(1) ;
end
res = [] ;
mmap = [] ;
stats = [] ;
start = tic ;
for t=1:opts.batchSize:numel(subset)
fprintf('%s: epoch %02d: batch %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 CNN
net.layers{end}.class = labels ;
if training, dzdy = one; else, dzdy = [] ; end
res = vl_simplenn(net, im, dzdy, res, ...
'accumulate', s ~= 1, ...
'disableDropout', ~training, ...
'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
% 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
% print learning statistics
time = toc(start) ;
stats = sum([stats,[0 ; error]],2); % works even when stats=[]
stats(1) = time ;
n = (t + batchSize - 1) / max(1,numlabs) ;
speed = n/time ;
fprintf('%.1f Hz%s\n', speed) ;
fprintf(' obj:%.3g', stats(2)/n) ;
for i=1:numel(opts.errorLabels)
fprintf(' %s:%.3g', opts.errorLabels{i}, stats(i+2)/n) ;
end
fprintf(' [%d/%d]', numDone, batchSize);
fprintf('\n') ;
% debug info
if opts.plotDiagnostics && numGpus <= 1
figure(2) ; vl_simplenn_diagnose(net,res) ; drawnow ;
end
end
if nargout > 2
prof = mpiprofile('info');
mpiprofile off ;
end
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)
% -------------------------------------------------------------------------
for l=numel(net.layers):-1:1
for j=1:numel(res(l).dzdw)
thisDecay = opts.weightDecay * net.layers{l}.weightDecay(j) ;
thisLR = lr * net.layers{l}.learningRate(j) ;
% accumualte from multiple labs (GPUs) if needed
if nargin >= 6
tag = sprintf('l%d_%d',l,j) ;
tmp = zeros(size(mmap.Data(labindex).(tag)), 'single') ;
for g = setdiff(1:numel(mmap.Data), labindex)
tmp = tmp + mmap.Data(g).(tag) ;
end
res(l).dzdw{j} = res(l).dzdw{j} + tmp ;
end
if isfield(net.layers{l}, 'weights')
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} ;
else
% Legacy code: to be removed
if j == 1
net.layers{l}.momentum{j} = ...
opts.momentum * net.layers{l}.momentum{j} ...
- thisDecay * net.layers{l}.filters ...
- (1 / batchSize) * res(l).dzdw{j} ;
net.layers{l}.filters = net.layers{l}.filters + thisLR * net.layers{l}.momentum{j} ;
else
net.layers{l}.momentum{j} = ...
opts.momentum * net.layers{l}.momentum{j} ...
- thisDecay * net.layers{l}.biases ...
- (1 / batchSize) * res(l).dzdw{j} ;
net.layers{l}.biases = net.layers{l}.biases + thisLR * net.layers{l}.momentum{j} ;
end
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]) ;