forked from jianboyang/CNNHAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_train.m
370 lines (331 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
function [net, info, predictions] = cnn_train(net, imdb, getBatch, varargin)
% CNN_TRAIN Demonstrates training a CNN
% CNN_TRAIN() is an example learner implementing stochastic gradient
% descent with momentum to train a CNN for image classification.
% It can be used with different datasets by providing a suitable
% getBatch function.
opts.train = [] ;
opts.val = [] ;
opts.numEpochs = 300 ;
opts.batchSize = 256 ;
opts.useGpu = false ;
opts.learningRate = 0.001 ;
opts.continue = false ;
opts.expDir = 'data/exp' ;
opts.conserveMemory = false ;
opts.sync = true ;
opts.prefetch = false ;
opts.weightDecay = 0.0005 ;
opts.momentum = 0.9 ;
opts.errorType = 'multiclass' ;
opts.plotDiagnostics = false ;
opts.outputfea = [];
opts = vl_argparse(opts, varargin) ;
if ~exist(opts.expDir), 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 opts.outputfea, xtrn = []; xtst = []; ytrn = []; ytst = []; xtrn = single(xtrn); xtst = single(xtst); end
% opts.val = [];
% -------------------------------------------------------------------------
% Network initialization
% -------------------------------------------------------------------------
for i=1:numel(net.layers)
if ~strcmp(net.layers{i}.type,'conv'), continue; end
net.layers{i}.filtersMomentum = zeros('like',net.layers{i}.filters) ;
net.layers{i}.biasesMomentum = zeros('like',net.layers{i}.biases) ;
if ~isfield(net.layers{i}, 'filtersLearningRate')
net.layers{i}.filtersLearningRate = 1 ;
end
if ~isfield(net.layers{i}, 'biasesLearningRate')
net.layers{i}.biasesLearningRate = 1 ;
end
if ~isfield(net.layers{i}, 'filtersWeightDecay')
net.layers{i}.filtersWeightDecay = 1 ;
end
if ~isfield(net.layers{i}, 'biasesWeightDecay')
net.layers{i}.biasesWeightDecay = 1 ;
end
end
if opts.useGpu
net = vl_simplenn_move(net, 'gpu') ;
for i=1:numel(net.layers)
if ~strcmp(net.layers{i}.type,'conv'), continue; end
net.layers{i}.filtersMomentum = gpuArray(net.layers{i}.filtersMomentum) ;
net.layers{i}.biasesMomentum = gpuArray(net.layers{i}.biasesMomentum) ;
end
end
% -------------------------------------------------------------------------
% Train and validate
% -------------------------------------------------------------------------
rng(0) ;
if opts.useGpu
one = gpuArray(single(1)) ;
else
one = single(1) ;
end
info.train.objective = [] ;
info.train.error = [] ;
info.train.topFiveError = [] ;
info.train.speed = [] ;
info.val.objective = [] ;
info.val.error = [] ;
info.val.topFiveError = [] ;
info.val.speed = [] ;
lr = 0 ;
res = [] ;
tttrain = 0;
for epoch=1:opts.numEpochs
tt1 = cputime;
% fprintf('--------------- epoch %d -----------\n',epoch);
prevLr = lr ;
lr = opts.learningRate(min(epoch, numel(opts.learningRate))) ;
% fast-forward to where we stopped
modelPath = [opts.expDir, 'net-epoch-%d.mat'] ;
modelFigPath = fullfile(opts.expDir, 'net-train.pdf') ;
if opts.continue
if exist(sprintf(modelPath, epoch),'file'), continue ; end
if epoch > 1
fprintf('\n resuming by loading epoch %d\n', epoch-1) ;
load(sprintf(modelPath, epoch-1), 'net', 'info') ;
end
end
train = opts.train(randperm(numel(opts.train))) ;
val = opts.val ;
% train(end) = 92;
info.train.objective(end+1) = 0 ;
info.train.error(end+1) = 0 ;
info.train.topFiveError(end+1) = 0 ;
info.train.speed(end+1) = 0 ;
info.val.objective(end+1) = 0 ;
info.val.error(end+1) = 0 ;
info.val.topFiveError(end+1) = 0 ;
info.val.speed(end+1) = 0 ;
% reset momentum if needed
if prevLr ~= lr
fprintf('learning rate changed (%f --> %f): resetting momentum\n', prevLr, lr) ;
for l=1:numel(net.layers)
if ~strcmp(net.layers{l}.type, 'conv'), continue ; end
net.layers{l}.filtersMomentum = 0 * net.layers{l}.filtersMomentum ;
net.layers{l}.biasesMomentum = 0 * net.layers{l}.biasesMomentum ;
end
end
for t=1:opts.batchSize:numel(train)
% get next image batch and labels
batch = train(t:min(t+opts.batchSize-1, numel(train))) ;
batch_time = tic ;
% fprintf('training: epoch %02d: processing batch %3d of %3d ...', epoch, ...
% fix(t/opts.batchSize)+1, ceil(numel(train)/opts.batchSize)) ;
% fprintf('training: batch %3d', fix(t/opts.batchSize)+1) ;
[im, labels] = getBatch(imdb, batch) ;
if opts.prefetch
nextBatch = train(t+opts.batchSize:min(t+2*opts.batchSize-1, numel(train))) ;
getBatch(imdb, nextBatch) ;
end
if opts.useGpu
im = gpuArray(im) ;
end
% backprop
net.layers{end}.class = labels ;
res = vl_simplenn(net, im, one, res, ...
'conserveMemory', opts.conserveMemory, ...
'sync', opts.sync) ;
% jby: Save traning feature
if epoch == opts.numEpochs && strcmp(opts.outputfea, 'true')
xtrn = [xtrn; squeeze(res(end-2).x)'];
ytrn = [ytrn; labels'];
end
% gradient step
for l=1:numel(net.layers)
if ~strcmp(net.layers{l}.type, 'conv'), continue ; end
net.layers{l}.filtersMomentum = ...
opts.momentum * net.layers{l}.filtersMomentum ...
- (lr * net.layers{l}.filtersLearningRate) * ...
(opts.weightDecay * net.layers{l}.filtersWeightDecay) * net.layers{l}.filters ...
- (lr * net.layers{l}.filtersLearningRate) / numel(batch) * res(l).dzdw{1} ;
net.layers{l}.biasesMomentum = ...
opts.momentum * net.layers{l}.biasesMomentum ...
- (lr * net.layers{l}.biasesLearningRate) * ....
(opts.weightDecay * net.layers{l}.biasesWeightDecay) * net.layers{l}.biases ...
- (lr * net.layers{l}.biasesLearningRate) / numel(batch) * res(l).dzdw{2} ;
net.layers{l}.filters = net.layers{l}.filters + net.layers{l}.filtersMomentum ;
net.layers{l}.biases = net.layers{l}.biases + net.layers{l}.biasesMomentum ;
end
% print information
batch_time = toc(batch_time) ;
speed = numel(batch)/batch_time ;
info.train = updateError(opts, info.train, net, res, batch_time) ;
n = t + numel(batch) - 1 ;
% fprintf(' %.2f s (%.1f images/s)', batch_time, speed) ;
% fprintf(' err %.1f err5 %.1f', ...
% info.train.error(end)/n*100, info.train.topFiveError(end)/n*100) ;
% fprintf('\n') ;
% debug info
if opts.plotDiagnostics
figure(2) ; vl_simplenn_diagnose(net,res) ; drawnow ;
end
% predictions = gather(res(end-1).x) ;
% switch opts.errorType
% case 'multiclass'
% [~,predictions] = sort(predictions, 3, 'descend') ;
% sz = size(predictions);
% predictions = reshape(predictions,[sz(3),sz(4)]);
% predictions = predictions(1,:);
% case 'binary'
% predictions = predictions;
% end
% yclass = unique(predictions)
end % next batch
predictions = gather(res(end-1).x) ;
switch opts.errorType
case 'multiclass'
[~,predictions] = sort(predictions, 3, 'descend') ;
sz = size(predictions);
if length(sz) < 4
predictions = reshape(predictions,[sz(3),1]);
else
predictions = reshape(predictions,[sz(3),sz(4)]);
end
predictions = predictions(1,:);
case 'binary'
predictions = predictions;
end
% yclass = unique(predictions)
tt2 = cputime;
tttrain = tttrain + tt2 - tt1;
% evaluation on validation set
ypredictions = [];
for t=1:opts.batchSize:numel(val)+opts.batchSize
batch_time = tic ;
batch = val(t:min(t+opts.batchSize-1, numel(val))) ;
if ~isempty(batch)
% fprintf('validation: epoch %02d: processing batch %3d of %3d ...', epoch, ...
% fix(t/opts.batchSize)+1, ceil(numel(val)/opts.batchSize)) ;
% fprintf('validation: batch %3d', fix(t/opts.batchSize)+1) ;
[im, labels] = getBatch(imdb, batch) ;
if opts.prefetch
nextBatch = val(t+opts.batchSize:min(t+2*opts.batchSize-1, numel(val))) ;
getBatch(imdb, nextBatch) ;
end
if opts.useGpu
im = gpuArray(im) ;
end
net.layers{end}.class = labels ;
res = vl_simplenn(net, im, [], res, ...
'disableDropout', true, ...
'conserveMemory', opts.conserveMemory, ...
'sync', opts.sync) ;
% jby: Save testing feature
predictions = gather(res(end-1).x) ;
switch opts.errorType
case 'multiclass'
[~,predictions] = sort(predictions, 3, 'descend') ;
sz = size(predictions);
if length(sz) < 4
predictions = reshape(predictions,[sz(3),1]);
else
predictions = reshape(predictions,[sz(3),sz(4)]);
end
predictions = predictions(1,:);
case 'binary'
predictions = predictions;
end
ypredictions = [ypredictions predictions];
if epoch == opts.numEpochs && strcmp(opts.outputfea, 'true')
xtst = [xtst; squeeze(res(end-2).x)'];
ytst = [ytst; labels'];
end
% print information
batch_time = toc(batch_time) ;
speed = numel(batch)/batch_time ;
info.val = updateError(opts, info.val, net, res, batch_time) ;
n = t + numel(batch) - 1 ;
% fprintf(' %.2f s (%.1f images/s)', batch_time, speed) ;
% fprintf(' err %.1f err5 %.1f', ...
% info.val.error(end)/n*100, info.val.topFiveError(end)/n*100) ;
% fprintf('\n') ;
end
end
tttest = cputime - tt2;
% save
info.train.objective(end) = info.train.objective(end) / numel(train) ;
info.train.error(end) = info.train.error(end) / numel(train) ;
info.train.topFiveError(end) = info.train.topFiveError(end) / numel(train) ;
info.train.speed(end) = numel(val) / info.train.speed(end) ;
info.val.objective(end) = info.val.objective(end) / numel(val) ;
info.val.error(end) = info.val.error(end) / numel(val) ;
info.val.topFiveError(end) = info.val.topFiveError(end) / numel(val) ;
info.val.speed(end) = numel(val) / info.val.speed(end) ;
save(sprintf(modelPath,epoch), 'net', 'info') ;
% figure(1) ; clf ;
% subplot(1,2,1) ;
% semilogy(1:epoch, info.train.objective, 'k') ; hold on ;
% semilogy(1:epoch, info.val.objective, 'b') ;
% xlabel('training epoch') ; ylabel('energy') ;
% grid on ;
% h=legend('train', 'val') ;
% set(h,'color','none');
% title('objective') ;
% subplot(1,2,2) ;
% switch opts.errorType
% case 'multiclass'
% plot(1:epoch, info.train.error, 'k') ; hold on ;
% plot(1:epoch, info.train.topFiveError, 'k--') ;
% plot(1:epoch, info.val.error, 'b') ;
% plot(1:epoch, info.val.topFiveError, 'b--') ;
% h=legend('train','train-5','val','val-5') ;
% case 'binary'
% plot(1:epoch, info.train.error, 'k') ; hold on ;
% plot(1:epoch, info.val.error, 'b') ;
% h=legend('train','val') ;
% end
% grid on ;
% xlabel('training epoch') ; ylabel('error') ;
% set(h,'color','none') ;
% title('error') ;
% drawnow ;
% print(1, modelFigPath, '-dpdf') ;
end
% val = opts.val ;
% batch = val;
% [im, labels] = getBatch(imdb, batch) ;
% net.layers{end}.class = labels ;
% res = vl_simplenn(net, im, [], res, ...
% 'disableDropout', true, ...
% 'conserveMemory', opts.conserveMemory, ...
% 'sync', opts.sync) ;
%
% predictions = gather(res(end-1).x) ;
% switch opts.errorType
% case 'multiclass'
% [~,predictions] = sort(predictions, 3, 'descend') ;
% sz = size(predictions);
% predictions = reshape(predictions,[sz(3),sz(4)]);
% predictions = predictions(1,:);
% case 'binary'
% predictions = predictions;
% end
predictions = ypredictions;
if opts.outputfea, info.xtrn = xtrn; info.xtst = xtst; info.ytrn = ytrn; info.ytst = ytst; end
fprintf('\ntttrain = %d and tttest = %d\n',tttrain,tttest);
% -------------------------------------------------------------------------
function info = updateError(opts, info, net, res, speed)
% -------------------------------------------------------------------------
predictions = gather(res(end-1).x) ;
sz = size(predictions) ;
n = prod(sz(1:2)) ;
labels = net.layers{end}.class ;
info.objective(end) = info.objective(end) + sum(double(gather(res(end).x))) ;
info.speed(end) = info.speed(end) + speed ;
switch opts.errorType
case 'multiclass'
[~,predictions] = sort(predictions, 3, 'descend') ;
error = ~bsxfun(@eq, predictions, reshape(labels, 1, 1, 1, [])) ;
info.error(end) = info.error(end) +....
sum(sum(sum(error(:,:,1,:))))/n ;
info.topFiveError(end) = info.topFiveError(end) + ...
sum(sum(sum(min(error(:,:,1:5,:),[],3))))/n ;
case 'binary'
error = bsxfun(@times, predictions, labels) < 0 ;
info.error(end) = info.error(end) + sum(error(:))/n ;
end