This repository has been archived by the owner on Mar 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathres_finetune.m
204 lines (175 loc) · 7.12 KB
/
res_finetune.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
function [net, info] = res_finetune(varargin)
% res_finetune('datasetName', 'minc', 'datafn',...
% @setup_imdb_minc, 'nClasses', 23, 'gpus',[1 2]);
% res_finetune('gpus',[2]);
setup;
opts.datasetName = 'reflectance';
opts.datafn = @setup_imdb_reflectance;
[opts,varargin] = vl_argparse(opts, varargin) ;
opts.dataDir = fullfile('data',opts.datasetName) ;
opts.imdb = [];
opts.expDir = fullfile('data','exp_ft', opts.datasetName) ;
opts.baseModel = 50;
opts.numEpochs = [10 55];
opts.networkType = 'resnet' ;
opts.batchNormalization = true ;
opts.nClasses = 21;
opts.bn = true;
opts.whitenData = true;
opts.contrastNormalization = true;
opts.meanType = 'pixel'; % 'pixel' | 'image'
opts.border = [4 4 4 4]; % tblr
opts.gpus = [];
opts.batchSize = 32;
opts.checkpointFn = [];
[opts, varargin] = vl_argparse(opts, varargin) ;
opts.train.learningRate = [0.05*ones(1,10) 0.01*ones(1,10) 0.001*ones(1,10)...
0.0001*ones(1,55)];
opts.train.momentum = 0.9;
opts.train.gpus = [];
opts.train = vl_argparse(opts.train, varargin) ;
if ~exist(opts.expDir, 'dir'), vl_xmkdir(opts.expDir) ; end
opts.numFetchThreads = 12 ;
opts = vl_argparse(opts, varargin) ;
% -------------------------------------------------------------------------
% Prepare data
% -------------------------------------------------------------------------
if isempty(opts.imdb),
imdb = get_imdb(opts.datasetName, 'func', opts.datafn);
else
imdb = opts.imdb;
end
opts.train.train = find(imdb.images.set==1);
opts.train.val = find(imdb.images.set==3);
% -------------------------------------------------------------------------
% Prepare model
% -------------------------------------------------------------------------
net = res_finetune_test(opts.baseModel, );%res_finetune_init(imdb, opts.baseModel);%
% Compute image statistics (mean, RGB covariances, etc.)
imageStatsPath = fullfile(opts.expDir, 'imageStats.mat') ;
if exist(imageStatsPath)
load(imageStatsPath, 'averageImage', 'rgbMean', 'rgbCovariance') ;
else
[averageImage, rgbMean, rgbCovariance] = getImageStats(opts, net.meta, imdb) ;
save(imageStatsPath, 'averageImage', 'rgbMean', 'rgbCovariance') ;
end
% Set the image average (use either an image or a color)
%net.meta.normalization.averageImage = averageImage ;
net.meta.normalization.averageImage = rgbMean ;
% Set data augmentation statistics
[v,d] = eig(rgbCovariance) ;
net.meta.augmentation.rgbVariance = 0.1*sqrt(d)*v' ;
clear v d ;
% -------------------------------------------------------------------------
% Learn
% -------------------------------------------------------------------------
func = @(a) ~isempty(a.params) ;
trainable_layers = find(arrayfun(@(l) func(l),net.layers));
p = arrayfun(@(l) net.layers(l) , trainable_layers);
p = arrayfun(@(l) net.params(net.getParamIndex(l.params(1))), p, 'UniformOutput',false);
lr = cellfun(@(l) l.learningRate, p);
layers_for_update = {trainable_layers(end), trainable_layers};
% tune last layer --> tune all layers
trainfn = @cnn_train_dag_check;
for s = 1:numel(opts.numEpochs),
if opts.numEpochs(s)<1, continue; end
for i = 1:numel(trainable_layers),
l = trainable_layers(i);
if ismember(l,layers_for_update{s}),
net.layers(l).learningRate = lr(i);
else
net.layers(l).learningRate = lr(i)*0;
end
end
[net, info] = trainfn(net, imdb, getBatchFn(opts, net.meta), ...
'expDir', opts.expDir, ...
opts.train, ...
'gpus', opts.gpus, ...
'batchSize',opts.batchSize,...
'numEpochs', sum(opts.numEpochs(1:s)),...
'derOutputs', {'loss', 1}, ...
'checkpointFn', opts.checkpointFn) ;
end
% -------------------------------------------------------------------------
function fn = getBatchFn(opts, meta)
% -------------------------------------------------------------------------
useGpu = numel(opts.gpus) > 0 ;
bopts.numThreads = opts.numFetchThreads ;
bopts.imageSize = meta.normalization.imageSize ;
bopts.border = meta.normalization.border ;
bopts.averageImage = meta.normalization.averageImage ;
bopts.rgbVariance = meta.augmentation.rgbVariance ;
bopts.transformation = meta.augmentation.transformation ;
switch lower(opts.networkType)
case 'simplenn'
fn = @(x,y) getSimpleNNBatch(bopts,x,y) ;
case {'dagnn', 'resnet'}
fn = @(x,y) getDagNNBatch(bopts,useGpu,x,y) ;
end
% -------------------------------------------------------------------------
function [im,labels] = getSimpleNNBatch(opts, imdb, batch)
% -------------------------------------------------------------------------
images = strcat([imdb.imageDir filesep], imdb.images.name(batch)) ;
isVal = ~isempty(batch) && imdb.images.set(batch(1)) ~= 1 ;
if ~isVal
% training
im = cnn_imagenet_get_batch(images, opts, ...
'prefetch', nargout == 0) ;
else
% validation: disable data augmentation
im = cnn_imagenet_get_batch(images, opts, ...
'prefetch', nargout == 0, ...
'transformation', 'none') ;
end
if nargout > 0
labels = imdb.images.label(batch) ;
end
% -------------------------------------------------------------------------
function inputs = getDagNNBatch(opts, useGpu, imdb, batch)
% -------------------------------------------------------------------------
images = strcat([imdb.imageDir filesep], imdb.images.name(batch)) ;
isVal = ~isempty(batch) && imdb.images.set(batch(1)) ~= 1 ;
if ~isVal
% training
im = cnn_imagenet_get_batch(images, opts, ...
'prefetch', nargout == 0) ;
else
% validation: disable data augmentation
im = cnn_imagenet_get_batch(images, opts, ...
'prefetch', nargout == 0, ...
'transformation', 'none') ;
end
if nargout > 0
if useGpu
im = gpuArray(im) ;
end
labels = imdb.images.label(batch) ;
inputs = {'data', im, 'label', labels} ;
end
% -------------------------------------------------------------------------
function [averageImage, rgbMean, rgbCovariance] = getImageStats(opts, meta, imdb)
% -------------------------------------------------------------------------
train = find(imdb.images.set == 1) ;
train = train(1: 101: end);
bs = 256 ;
opts.networkType = 'simplenn' ;
fn = getBatchFn(opts, meta) ;
avg = {}; rgbm1 = {}; rgbm2 = {};
for t=1:bs:numel(train)
batch_time = tic ;
batch = train(t:min(t+bs-1, numel(train))) ;
fprintf('collecting image stats: batch starting with image %d ...', batch(1)) ;
temp = fn(imdb, batch) ;
z = reshape(permute(temp,[3 1 2 4]),3,[]) ;
n = size(z,2) ;
avg{end+1} = mean(temp, 4) ;
rgbm1{end+1} = sum(z,2)/n ;
rgbm2{end+1} = z*z'/n ;
batch_time = toc(batch_time) ;
fprintf(' %.2f s (%.1f images/s)\n', batch_time, numel(batch)/ batch_time) ;
end
averageImage = mean(cat(4,avg{:}),4) ;
rgbm1 = mean(cat(2,rgbm1{:}),2) ;
rgbm2 = mean(cat(3,rgbm2{:}),3) ;
rgbMean = rgbm1 ;
rgbCovariance = rgbm2 - rgbm1*rgbm1' ;