-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResNet_Block5_nh.m
210 lines (166 loc) · 7.72 KB
/
ResNet_Block5_nh.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
function [net,layer_out ,classifier_outs]=ResNet_Block5_nh(net,layer_in,nlayers, ker_size, nh0, nh1, nh2, newLr, nClass)
layer_prefix=['d','e','f','g','h','i','j','k','l','m'];
conv_in=layer_in;
bn_out0=layer_in;
skip_inputs={};
% nh0 the original input channel num, nh1 the new channel num, nh0 the
% hidden later channel num
if nh1~=nh0
conv_layer0 = sprintf('res5%s_branch1', layer_prefix(1));
bn_layer0 = sprintf('bn5%s_branch1', layer_prefix(1));
conv_out0 = sprintf('res5%s_branch1', layer_prefix(1));
bn_out0 = sprintf('res5%s_branch1x', layer_prefix(1));
%% conv layer
cw_param_f = sprintf('res5%s_branch1_filter', layer_prefix(1));
cw_f_shared = 1e-2*randn(1, 1, 2048, nh1, 'single');
net.addLayer(conv_layer0, ...
dagnn.Conv('size', [1 1 2048 nh1], 'pad', 0,'hasBias',0), ...
conv_in, conv_out0, {cw_param_f});
f = net.getParamIndex(cw_param_f) ;
net.params(f).value = cw_f_shared ;
net.params(f).learningRate = 1 * newLr ;
net.params(f).weightDecay = 1 ;
%% Batch Normalization
bn_param_f = sprintf('bn5%s_branch1_mult', layer_prefix(1));
bn_param_b = sprintf('bn5%s_branch1_bias', layer_prefix(1));
bn_param_m = sprintf('bn5%s_branch1_moments', layer_prefix(1));
net.addLayer(bn_layer0, ...
dagnn.BatchNorm(), ...
conv_out0, bn_out0, {bn_param_f, bn_param_b, bn_param_m});
f = net.getParamIndex(bn_param_f) ;
net.params(f).value = ones(nh1, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_b) ;
net.params(f).value = zeros(nh1, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_m) ;
net.params(f).value = zeros(nh1, 2, 'single') ;
net.params(f).learningRate = 0 ;
net.params(f).weightDecay = 0 ;
end
for i=1:nlayers
conv_layer = sprintf('res5%s_branch2a', layer_prefix(i));
bn_layer = sprintf('bn5%s_branch2a', layer_prefix(i));
relu_layer = sprintf('res5%s_branch2a_relu',layer_prefix(i));
conv_layer2 = sprintf('res5%s_branch2b', layer_prefix(i));
bn_layer2 = sprintf('bn5%s_branch2b', layer_prefix(i));
relu_layer2 = sprintf('res5%s_branch2b_relu',layer_prefix(i));
conv_layer3 = sprintf('res5%s_branch2c', layer_prefix(i));
bn_layer3 = sprintf('bn5%s_branch2c', layer_prefix(i));
sum_layer = sprintf('res5%s', layer_prefix(i));
relu_layer3 = sprintf('res5%s_relu', layer_prefix(i));
conv_out = sprintf('res5%s_branch2a', layer_prefix(i));
bn_out = sprintf('res5%s_branch2ax', layer_prefix(i));
relu_out = sprintf('res5%s_branch2axxx', layer_prefix(i));
conv_out2 = sprintf('res5%s_branch2b', layer_prefix(i));
bn_out2 = sprintf('res5%s_branch2bx', layer_prefix(i));
relu_out2 = sprintf('res5%s_branch2bxxx', layer_prefix(i));
conv_out3 = sprintf('res5%s_branch2c', layer_prefix(i));
bn_out3 = sprintf('res5%s_branch2cx', layer_prefix(i));
sum_out = sprintf('res5%s', layer_prefix(i));
relu_out3 = sprintf('res5%sx', layer_prefix(i));
%% conv layer
cw_param_f = sprintf('res5%s_branch2a_filter', layer_prefix(i));
cw_f_shared = 1e-2*randn(1, 1, nh0, nh2, 'single');
net.addLayer(conv_layer, ...
dagnn.Conv('size', [1 1 nh0 nh2], 'pad', 0,'hasBias',0), ...
conv_in, conv_out, {cw_param_f});
f = net.getParamIndex(cw_param_f) ;
net.params(f).value = cw_f_shared ;
net.params(f).learningRate = 1 * newLr ;
net.params(f).weightDecay = 1 ;
%% Batch Normalization
bn_param_f = sprintf('bn5%s_branch2a_mult', layer_prefix(i));
bn_param_b = sprintf('bn5%s_branch2a_bias', layer_prefix(i));
bn_param_m = sprintf('bn5%s_branch2a_moments', layer_prefix(i));
net.addLayer(bn_layer, ...
dagnn.BatchNorm(), ...
conv_out, bn_out, {bn_param_f, bn_param_b, bn_param_m});
f = net.getParamIndex(bn_param_f) ;
net.params(f).value = ones(nh2, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_b) ;
net.params(f).value = zeros(nh2, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_m) ;
net.params(f).value = zeros(nh2, 2, 'single') ;
net.params(f).learningRate = 0 ;
net.params(f).weightDecay = 0 ;
%% ReLU
net.addLayer(relu_layer,dagnn.ReLU(),bn_out, relu_out);
%% conv layer 2b
cw_param_f = sprintf('res5%s_branch2b_filter', layer_prefix(i));
cw_f_shared = 1e-2*randn(ker_size, ker_size, nh2, nh2, 'single');
net.addLayer(conv_layer2, ...
dagnn.Conv('size', [ker_size ker_size nh2 nh2], 'pad', floor(ker_size/2),'hasBias',0), ...
relu_out, conv_out2, {cw_param_f});
f = net.getParamIndex(cw_param_f) ;
net.params(f).value = cw_f_shared ;
net.params(f).learningRate = 1 * newLr ;
net.params(f).weightDecay = 1 ;
%% Batch Normalization 2b
bn_param_f = sprintf('bn5%s_branch2b_mult', layer_prefix(i));
bn_param_b = sprintf('bn5%s_branch2b_bias', layer_prefix(i));
bn_param_m = sprintf('bn5%s_branch2b_moments', layer_prefix(i));
net.addLayer(bn_layer2, ...
dagnn.BatchNorm(), ...
conv_out2, bn_out2, {bn_param_f, bn_param_b, bn_param_m});
f = net.getParamIndex(bn_param_f) ;
net.params(f).value = ones(nh2, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_b) ;
net.params(f).value = zeros(nh2, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_m) ;
net.params(f).value = zeros(nh2, 2, 'single') ;
net.params(f).learningRate = 0 ;
net.params(f).weightDecay = 0 ;
%% ReLU 2b
net.addLayer(relu_layer2,dagnn.ReLU(),bn_out2, relu_out2);
%% conv layer 2c
cw_param_f = sprintf('res5%s_branch2c_filter', layer_prefix(i));
cw_f_shared = 1e-2*randn(1, 1, nh2, nh1, 'single');
net.addLayer(conv_layer3, ...
dagnn.Conv('size', [1 1 nh2 nh1], 'pad', 0,'hasBias',0), ...
relu_out2, conv_out3, {cw_param_f});
f = net.getParamIndex(cw_param_f) ;
net.params(f).value = cw_f_shared ;
net.params(f).learningRate = 1 * newLr ;
net.params(f).weightDecay = 1 ;
%% Batch Normalization 2c
bn_param_f = sprintf('bn5%s_branch2c_mult', layer_prefix(i));
bn_param_b = sprintf('bn5%s_branch2c_bias', layer_prefix(i));
bn_param_m = sprintf('bn5%s_branch2c_moments', layer_prefix(i));
net.addLayer(bn_layer3, ...
dagnn.BatchNorm(), ...
conv_out3, bn_out3, {bn_param_f, bn_param_b, bn_param_m});
f = net.getParamIndex(bn_param_f) ;
net.params(f).value = ones(nh1, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_b) ;
net.params(f).value = zeros(nh1, 1, 'single') ;
net.params(f).learningRate = 1 * newLr;
net.params(f).weightDecay = 1 ;
f = net.getParamIndex(bn_param_m) ;
net.params(f).value = zeros(nh1, 2, 'single') ;
net.params(f).learningRate = 0 ;
net.params(f).weightDecay = 0 ;
%% sum
net.addLayer(sum_layer, dagnn.Sum(), {bn_out0, bn_out3}, sum_out) ;
%% ReLU
net.addLayer(relu_layer3,dagnn.ReLU(),sum_out, relu_out3);
conv_in=relu_out3;
skip_inputs=[skip_inputs,relu_out3];
nh0=nh1;
bn_out0=relu_out3;
end
layer_out=relu_out3;
[net, classifier_outs] = skipNetwork(net, skip_inputs, nh1, nh2, ...
nClass, newLr, 'skip_newblock5');