-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinception_init.m
More file actions
238 lines (207 loc) · 6.61 KB
/
inception_init.m
File metadata and controls
238 lines (207 loc) · 6.61 KB
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
function net = inception_init(varargin)
opts.scale = 1 ;
opts.initBias = 0.1 ;
opts.weightDecay = 1 ;
opts.cudnnWorkspaceLimit = 1024*1024*1204 ; % 1GB
opts = vl_argparse(opts, varargin) ;
net = dagnn.DagNN() ;
net.meta.inputSize = [32 32 3 1] ;
net.meta.normalization.imageSize = net.meta.inputSize(1:3) ;
stack = {} ;
function dup()
stack{end+1} = stack{end} ;
end
function swap()
stack([end-1 end]) = stack([end end-1]) ;
end
% 默认 feature map size 不变,可以通过输入改变 stride, pad
function Conv(name, ksize, out, varargin)
copts.stride = [1 1] ;
copts.pad = (ksize-1)/2 ;
copts = vl_argparse(copts, varargin) ;
if isempty(stack)
inputVar = 'input' ;
in = 3 ;
else
prev = stack{end} ;
stack(end) = [] ; % 对 cell 单元级别操作,使用 ()
i = net.getLayerIndex(prev) ;
inputVar = net.layers(i).outputs{1} ;
sizes = net.getVarSizes({'input', net.meta.inputSize}) ;
j = net.getVarIndex(inputVar) ;
in = sizes{j}(3) ;
end
if numel(ksize) == 1, ksize = [ksize ksize] ; end
net.addLayer(name , ...
dagnn.Conv('size', [ksize in out], ...
'stride', copts.stride, ....
'pad', copts.pad, ...
'opts', {'cudnnworkspacelimit', opts.cudnnWorkspaceLimit}), ...
inputVar, ...
[name '_conv'], ...
{[name '_f'], [name '_b']}) ;
net.addLayer([name '_bn'], ...
dagnn.BatchNorm('numChannels', out), ...
[name '_conv'], ...
[name '_bn'], ...
{[name '_bn_w'], [name '_bn_b'], [name '_bn_m']}) ;
net.addLayer([name '_relu'] , ...
dagnn.ReLU(), ...
[name '_bn'], ...
name) ;
stack{end+1} = [name '_relu'] ;
end
% 默认 feature map size 不变,可以通过改变 stride, pad 进行调整
function Pool(name, ksize, varargin)
copts.stride = [1 1] ;
copts.pad = (ksize-1)/2 ;
copts.method = 'max' ;
copts = vl_argparse(copts, varargin) ;
prev = stack{end} ;
stack(end) = [] ;
i = net.getLayerIndex(prev) ;
inputVar = net.layers(i).outputs{1} ;
if numel(ksize) == 1, ksize = [ksize ksize] ; end
net.addLayer(name , ...
dagnn.Pooling('poolSize', ksize, ...
'method', copts.method, ...
'stride', copts.stride, ....
'pad', copts.pad), ...
inputVar, ...
[name '_pool']) ;
stack{end+1} = name ;
end
function Concat(name, num)
inputVars = {} ;
for layer = stack(end-num+1:end)
prev = char(layer) ;
i = net.getLayerIndex(prev) ;
inputVars{end+1} = net.layers(i).outputs{1} ;
end
stack(end-num+1:end) = [] ;
net.addLayer(name , ...
dagnn.Concat(), ...
inputVars, ...
name) ;
stack{end+1} = name ;
end
function Pred(name, out, varargin)
prev = stack{end} ;
stack(end) = [] ;
i = net.getLayerIndex(prev) ;
inputVar = net.layers(i).outputs{1} ;
sizes = net.getVarSizes({'input', net.meta.inputSize}) ;
j = net.getVarIndex(inputVar) ;
in = sizes{j}(3) ;
net.addLayer([name '_dropout'] , ...
dagnn.DropOut('rate', 0.2), ...
inputVar, ...
[name '_dropout']) ;
net.addLayer(name, ...
dagnn.Conv('size', [1 1 in out]), ...
[name '_dropout'], ...
name, ...
{[name '_f'], [name '_b']}) ;
net.addLayer([name '_loss'], ...
dagnn.Loss('loss', 'softmaxlog'), ...
{name, 'label'}, ...
'objective') ;
net.addLayer([name '_top1error'], ...
dagnn.Loss('loss', 'classerror'), ...
{name, 'label'}, ...
[name '_top1error']) ;
net.addLayer([name '_top5error'], ...
dagnn.Loss('loss', 'topkerror', 'opts', {'topK', 5}), ...
{name, 'label'}, ...
[name '_top5error']) ;
end
% Stem
Conv('conv1',3,32); % 32 32 32
Conv('conv2',3,64); % 32 32 64
pfx=sprintf('stem_1');
dup();
Conv([pfx '_a1'],3,64,'stride',2,'pad',[0 1 0 1]); % 16 16 64
swap();
Pool([pfx '_b1'],3,'method','max','stride',2,'pad',[0 1 0 1]); % 16 16 64
Concat(pfx,2); % 16 16 128
% Inception fig. 5 x 2
for t=1:4
pfx=sprintf('inception5_%d',t);
dup();
Conv([pfx '_a1'],1,48);
swap(); dup();
Conv([pfx '_b1'],1,24);
Conv([pfx '_b2'],[1 5],32);
Conv([pfx '_b3'],[5 1],32);
swap(); dup();
Conv([pfx '_c1'],1,24);
Conv([pfx '_c2'],[1 5],28);
Conv([pfx '_c3'],[5 1],28);
Conv([pfx '_c4'],[1 5],32);
Conv([pfx '_c5'],[5 1],32);
swap();
Pool([pfx '_d1'],3,'method','avg');
Conv([pfx '_d2'],1,16);
Concat(pfx,4); % 16 16 128
end
% Inception fig. 5 down
pfx=sprintf('inception5_5');
dup();
Conv([pfx '_a1'],1,64);
Conv([pfx '_a2'],3,64,'stride',2,'pad',[0 1 0 1]);
swap(); dup();
Conv([pfx '_b1'],1,48);
Conv([pfx '_b2'],[1 5],48);
Conv([pfx '_b3'],[5 1],78);
Conv([pfx '_b4'],3,78,'stride',2,'pad',[0 1 0 1]);
swap();
Pool([pfx '_c1'],3,'method','max','stride',2,'pad',[0 1 0 1]);
Concat(pfx,3); % 8 8 270
% Inception fig. 6 x 1
pfx=sprintf('inception6_1');
dup(); % stack{'concat','concat'}
Conv([pfx '_a1'],1,45); % stack{'concat','a1'}
swap(); dup(); % stack{'a1','concat','concat'}
Conv([pfx '_b1'],1,90); % stack{'a1','concat','b1'}
dup(); % stack{'a1','concat','b1','b1'}
Conv([pfx '_b1_1'],[1 3],45); % stack{'a1','concat','b1','b1_1'}
swap(); % stack{'a1','concat','b1_1','b1'}
Conv([pfx '_b1_2'],[3 1],45); % stack{'a1','concat','b1_1','b1_2'}
stack{end+1}=stack{end-2}; % 待验证
stack(end-3)=[]; % stack{'a1','b1_1','b1_2','concat','concat'}
dup();
Conv([pfx '_c1'],1,90);
Conv([pfx '_c2'],[1 3],90);
Conv([pfx '_c3'],[3 1],90); % stack{'a1','b1_1','b1_2','concat','c3'}
dup();
Conv([pfx '_c3_1'],[1 3],45); % stack{'a1','b1_1','b1_2','concat','c3','c3_1'}
swap();
Conv([pfx '_c3_2'],[3 1],45); % stack{'a1','b1_1','b1_2','concat','c3_1','c3_2'}
stack{end+1}=stack{end-2};
stack(end-3)=[]; % stack{'a1','b1_1','b1_2','c3_1','c3_2','concat'}
Pool([pfx '_d1'],3,'method','avg');
Conv([pfx '_d2'],1,45);
Concat(pfx,6); % 8 8 270
% Prediction
% Average pooling and loss
Pool('Pool2',8,'method','avg','pad',0); % 1 1 270
Pred('prediction',10); % 1 1 10
% Meta parameters
net.meta.normalization.fullImageSize = 32 ;
net.meta.normalization.averageImage = [] ;
net.meta.augmentation.rgbSqrtCovariance = zeros(3,'single') ;
net.meta.augmentation.jitter = true ;
net.meta.augmentation.jitterLight = 0.1 ;
net.meta.augmentation.jitterBrightness = 0.4 ;
net.meta.augmentation.jitterSaturation = 0.4 ;
net.meta.augmentation.jitterContrast = 0.4 ;
net.meta.inputSize = {'input', [net.meta.normalization.imageSize 32]} ;
lr=logspace(-1,-3,80); % 60 -> 100
net.meta.trainOpts.learningRate = lr ;
net.meta.trainOpts.numEpochs = numel(lr) ;
net.meta.trainOpts.batchSize = 256 ; % 256 -> 128
net.meta.trainOpts.numSubBatches = 3 ;
net.meta.trainOpts.weightDecay = 0.0025 ;
% Init parameters randomly
net.initParams() ;
end