-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_scotopic_init.m
247 lines (219 loc) · 9.12 KB
/
cnn_scotopic_init.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
function net = cnn_scotopic_init(varargin)
% Initialize scotopic classifier
% (c) 2016 Bo Chen
opts.dataset = 'cifar';
opts.networkType = 'simplenn' ;
opts.adaptLayer = 'none';
opts.sp = [];
opts = vl_argparse(opts, varargin) ;
switch opts.dataset
case 'cifar'
opts.size = [32 32 64 64];
opts.lr = [.1 2];
case 'mnist'
opts.size = [20 50 500];
opts.lr = [1 1];
otherwise
error('Unknown dataset %s', opts.dataset);
end
opts = vl_argparse(opts, varargin); % read opts.size again
switch opts.dataset
case 'cifar'
net = cifar_skeleton(opts.size, opts.lr);
net.meta.trainOpts.learningRate = [0.05*ones(1,30) 0.005*ones(1,25) 0.0005*ones(1,20)];
if strcmp(opts.adaptLayer, 'wb-first')
net.meta.trainOpts.learningRate = net.meta.trainOpts.learningRate*4;
end
net.meta.trainOpts.weightDecay = 0.0001 ;
case 'mnist'
net = mnist_skeleton(opts.size, opts.lr);
if strcmp(opts.adaptLayer, 'wb-first')
net.meta.trainOpts.learningRate = 0.004*ones(1,80);% 4x learning rate in waldnet mode
else
net.meta.trainOpts.learningRate = 0.001*ones(1,80);
end
end
net.meta.trainOpts.batchSize = 100 ;
net.meta.trainOpts.numEpochs = numel(net.meta.trainOpts.learningRate) ;
net = net_insertBNorm(net);
net = net_adapt(net, opts.adaptLayer, opts.sp);
% Fill in default values
net = vl_simplenn_tidy(net) ;
% Switch to DagNN if requested
switch lower(opts.networkType)
case 'simplenn'
% done
case 'dagnn'
net = dagnn.DagNN.fromSimpleNN(net, 'canonicalNames', true) ;
net.addLayer('error', dagnn.Loss('loss', 'classerror'), ...
{'prediction','label'}, 'error') ;
otherwise
assert(false) ;
end
function net = cifar_skeleton(siz, lr)
% Define network CIFAR10-quick
net.layers = {} ;
% Block 1
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{0.01*randn(5,5,3,siz(1), 'single'), zeros(1, siz(1), 'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 2) ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [3 3], ...
'stride', 2, ...
'pad', [0 1 0 1]) ;
net.layers{end+1} = struct('type', 'relu') ;
% Block 2
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{0.05*randn(5,5,siz(1),siz(2), 'single'), zeros(1,siz(2),'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 2) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'avg', ...
'pool', [3 3], ...
'stride', 2, ...
'pad', [0 1 0 1]) ; % Emulate caffe
% Block 3
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{0.05*randn(5,5,siz(2),siz(3), 'single'), zeros(1,siz(3),'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 2) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'avg', ...
'pool', [3 3], ...
'stride', 2, ...
'pad', [0 1 0 1]) ; % Emulate caffe
% Block 4
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{0.05*randn(4,4,siz(3),siz(4), 'single'), zeros(1,siz(4),'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
% Block 5
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{0.05*randn(1,1,siz(4),10, 'single'), zeros(1,10,'single')}}, ...
'learningRate', .1*lr, ...
'stride', 1, ...
'pad', 0) ;
% Loss layer
net.layers{end+1} = struct('type', 'softmaxloss') ;
% Meta parameters
net.meta.inputSize = [32 32 3] ;
function net = mnist_skeleton(siz, lr)
f = 0.01;
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(5,5,1,siz(1), 'single'), zeros(1, siz(1), 'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(5,5,siz(1),siz(2), 'single'),zeros(1,siz(2),'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(4,4,siz(2),siz(3), 'single'), zeros(1,siz(3),'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,siz(3),10, 'single'), zeros(1,10,'single')}}, ...
'learningRate', lr, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'softmaxloss') ;
% Meta parameters
net.meta.inputSize = [28 28 1] ;
function net = net_adapt(net, adaptLayer, sp)
switch adaptLayer
case 'none' % done
case 'first' % only adapt the first layer
net.layers{1} = adaptConv(net.layers{1}, sp);
case 'all' % adapt all layers
for l=1:length(net.layers)
if strcmp(net.layers{l}.type, 'conv')
net.layers{l} = adaptConv(net.layers{l}, sp);
end
end
case 'wb-first' % adapt the first layer, both weight and bias
% batch normalization should take care of the scaling and offset
net.layers{2} = adaptBnorm(net.layers{2}, sp);
case 'wb-all' % adapt all layers
for l=1:length(net.layers)
% if strcmp(net.layers{l}.type, 'conv')
% net.layers{l}.learningRate(2) = 0;
% else
if strcmp(net.layers{l}.type, 'bnorm')
net.layers{l} = adaptBnorm(net.layers{l}, sp);
end
end
otherwise
assert(false);
end
function l = adaptConv(l, sp)
l.PPPArray = sp.PPPArray;
ppp_N = length(l.PPPArray);
nout = size(l.weights{2}, 2);
l.type = 'custom';
l.original_type = 'conv';
l.forward = @(a, res1, res2, testMode) vl_timed_conv('forward', res1, [], a);
l.backward = @(a, res1, res2) vl_timed_conv('backward',res1, res2.dzdx, a);
l.weights{2} = zeros(ppp_N, nout, 'single');
l.opts = {};
function l = adaptBnorm(l, sp)
l.PPPArray = sp.PPPArray;
ppp_N = length(l.PPPArray);
nout = size(l.weights{1}, 1);
l.type = 'custom';
l.original_type = 'bnorm';
l.forward = @(a, res1, res2, testMode) vl_timed_Bnorm('forward', res1, [], a, testMode);
l.backward = @(a, res1, res2) vl_timed_Bnorm('backward',res1, res2.dzdx, a);
l.weights{1} = ones(nout, ppp_N, 'single');
l.weights{2} = zeros(nout, ppp_N, 'single');
l.weights{3} = zeros(nout, 2, ppp_N, 'single');
l.weightDecay = [0 0 0];
function net = net_insertBNorm(net)
insertLayers = [];
for l = 1:length(net.layers)
if isfield(net.layers{l}, 'weights'),
insertLayers = cat(2, insertLayers, l);
end
end
k = 0;
for l = insertLayers
net = insertBnorm(net, l+k);
k = k + 1;
end
function net = insertBnorm(net, l)
% --------------------------------------------------------------------
assert(isfield(net.layers{l}, 'weights'));
% disable bias of the conv layer below (it will be modeled by BNorm)
net.layers{l}.weights{2}(:) = 0;
net.layers{l}.learningRate(2) = 0;
ndim = size(net.layers{l}.weights{1}, 4);
layer = struct('type', 'bnorm', ...
'weights', {{ones(ndim, 1, 'single'), zeros(ndim, 1, 'single')}}, ...
'learningRate', [1 1 0.05], ...
'weightDecay', [0 0]) ;
net.layers{l}.biases = [] ;
net.layers = horzcat(net.layers(1:l), layer, net.layers(l+1:end)) ;