-
Notifications
You must be signed in to change notification settings - Fork 119
/
init_MSLapSRN_model.m
341 lines (249 loc) · 11 KB
/
init_MSLapSRN_model.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
function net = init_MSLapSRN_model(opts, mode)
% -------------------------------------------------------------------------
% Description:
% initialize MS-LapSRN model
%
% Input:
% - opts : options generated from init_MSLapSRN_opts()
%
% Output:
% - net : dagnn model
%
% Citation:
% Fast and Accurate Image Super-Resolution with Deep Laplacian Pyramid Networks
% Wei-Sheng Lai, Jia-Bin Huang, Narendra Ahuja, and Ming-Hsuan Yang
% arXiv, 2017
%
% Contact:
% Wei-Sheng Lai
% University of California, Merced
% -------------------------------------------------------------------------
%% parameters
rng('default');
rng(0) ;
f = opts.conv_f;
n = opts.conv_n;
pad = floor(f/2);
if( f == 3 )
crop = [0, 1, 0, 1];
elseif( f == 5 )
crop = [1, 2, 1, 2];
else
error('Need to specify crop in deconvolution for f = %d\n', f);
end
%% initialize model
net = dagnn.DagNN;
%% multiscale training
for s = 1:length(opts.scales)
scale = opts.scales(s);
level = ceil(log(scale) / log(2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Feature extraction branch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% input conv
inputs = { sprintf('x%dSR_LR', scale) };
outputs = { sprintf('x%dSR_input_conv', scale) };
params = { 'input_conv_f', 'input_conv_b' };
net.addLayer(outputs{1}, ...
dagnn.Conv('size', [f, f, 1, n], ...
'pad', pad, ...
'stride', 1), ...
inputs, outputs, params);
level_input = outputs{1};
%% feature embedding sub-network
for l = 1 : level
block_input = {};
block_output = {};
for r = 1:opts.recursive
if r == 1
block_input{r} = level_input;
else
block_input{r} = block_output{r-1};
end
%% recursive block
for d = 1:opts.depth
if d == 1
next_input = block_input{r};
end
% ReLU
inputs = { next_input };
outputs = { sprintf('x%dSR_level%d_R%d_relu%d', scale, l, r, d) };
net.addLayer(outputs{1}, ...
dagnn.ReLU('leak', 0.2), ...
inputs, outputs);
next_input = outputs{1};
% conv
inputs = { next_input };
outputs = { sprintf('x%dSR_level%d_R%d_conv%d', scale, l, r, d) };
params = { sprintf('conv%d_f', d), ...
sprintf('conv%d_b', d)};
net.addLayer(outputs{1}, ...
dagnn.Conv('size', [f, f, n, n], ...
'pad', pad, ...
'stride', 1), ...
inputs, outputs, params);
next_input = outputs{1};
end %% end of recursive block
%% local skip connection
if strcmp(opts.LRL, 'NS')
% no skip connection
block_output{r} = next_input;
elseif strcmp(opts.LRL, 'DS')
% next_input + block_input
inputs = { next_input, block_input{r} };
outputs = { sprintf('x%dSR_level%d_R%d_output', scale, l, r) };
net.addLayer(outputs{1}, ...
dagnn.Sum(), ...
inputs, outputs);
block_output{r} = outputs{1};
elseif strcmp(opts.LRL, 'SS')
% next_input + level_input
inputs = { next_input, level_input };
outputs = { sprintf('x%dSR_level%d_R%d_output', scale, l, r) };
net.addLayer(outputs{1}, ...
dagnn.Sum(), ...
inputs, outputs);
block_output{r} = outputs{1};
else
error('Unknown local skip connection %s.', opts.LRL);
end %% end of local skip connection
end %% end of recursive
%% features upsample layers
% ReLU
inputs = { block_output{opts.recursive} };
outputs = { sprintf('x%dSR_level%d_uprelu', scale, l) };
net.addLayer(outputs{1}, ...
dagnn.ReLU('leak', 0.2), ...
inputs, outputs);
next_input = outputs{1};
% conv
inputs = { next_input };
outputs = { sprintf('x%dSR_level%d_upconv', scale, l) };
params = { 'upconv_f', 'upconv_b' };
net.addLayer(outputs{1}, ...
dagnn.ConvTranspose(...
'size', [f, f, n, n], ...
'upsample', 2, ...
'crop', crop, ...
'numGroups', 1, ...
'hasBias', true), ...
inputs, outputs, params) ;
level_input = outputs{1};
end %% end of level (feature extraction)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Image reconstruction branch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for l = 1 : level
if l == 1
next_input = sprintf('x%dSR_LR', scale);
else
next_input = sprintf('x%dSR_%dx_output', scale, 2^(l-1));
end
%% image upsample layer
inputs = { next_input };
outputs = { sprintf('x%dSR_level%d_img_up', scale, l) };
params = { 'img_up_f' };
net.addLayer(outputs{1}, ...
dagnn.ConvTranspose(...
'size', [4, 4, 1, 1], ...
'upsample', 2, ...
'crop', 1, ...
'numGroups', 1, ...
'hasBias', false), ...
inputs, outputs, params) ;
%% residual prediction layer (f x f x n x 1)
inputs = { sprintf('x%dSR_level%d_upconv', scale, l) };
outputs = { sprintf('x%dSR_level%d_residual', scale, l) };
params = { 'residual_conv_f', 'residual_conv_b' };
net.addLayer(outputs{1}, ...
dagnn.Conv('size', [f, f, n, 1], ...
'pad', pad, ...
'stride', 1), ...
inputs, outputs, params);
%% addition layer
inputs = { sprintf('x%dSR_level%d_img_up', scale, l), ...
sprintf('x%dSR_level%d_residual', scale, l) };
outputs = { sprintf('x%dSR_%dx_output', scale, 2^l) };
net.addLayer(outputs{1}, ...
dagnn.Sum(), ...
inputs, outputs);
next_input = outputs{1};
%% Loss layer
inputs = { next_input, ...
sprintf('x%dSR_%dx_HR', scale, 2^l) };
outputs = { sprintf('x%dSR_%dx_%s_loss', scale, 2^l, opts.loss) };
net.addLayer(outputs{1}, ...
dagnn.vllab_dag_loss(...
'loss_type', opts.loss), ...
inputs, outputs);
end %% end of level (image reconstruction)
end %% end of multiscale
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% initialize parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(mode, 'train')
%% input conv
params = { 'input_conv_f', 'input_conv_b' };
sigma = opts.init_sigma;
filters = sigma * randn(f, f, 1, n, 'single');
biases = zeros(1, n, 'single');
idx = net.getParamIndex(params{1});
net.params(idx).value = filters;
net.params(idx).learningRate = 1;
net.params(idx).weightDecay = 1;
idx = net.getParamIndex(params{2});
net.params(idx).value = biases;
net.params(idx).learningRate = 0.1;
net.params(idx).weightDecay = 1;
%% deep conv
for d = 1:opts.depth
params = { sprintf('conv%d_f', d), ...
sprintf('conv%d_b', d)};
sigma = sqrt( 2 / (f * f * n) );
filters = sigma * randn(f, f, n, n, 'single');
biases = zeros(1, n, 'single');
idx = net.getParamIndex(params{1});
net.params(idx).value = filters;
net.params(idx).learningRate = 1;
net.params(idx).weightDecay = 1;
idx = net.getParamIndex(params{2});
net.params(idx).value = biases;
net.params(idx).learningRate = 0.1;
net.params(idx).weightDecay = 1;
end
%% feature upsample
params = { 'upconv_f', 'upconv_b' };
sigma = sqrt( 2 / (f * f * n) );
filters = sigma * randn(f, f, n, n, 'single');
biases = zeros(1, n, 'single');
idx = net.getParamIndex(params{1});
net.params(idx).value = filters;
net.params(idx).learningRate = 1;
net.params(idx).weightDecay = 1;
idx = net.getParamIndex(params{2});
net.params(idx).value = biases;
net.params(idx).learningRate = 0.1;
net.params(idx).weightDecay = 1;
%% image upsample
params = { 'img_up_f' };
filters = single(bilinear_kernel(4, 1, 1));
idx = net.getParamIndex(params{1});
net.params(idx).value = filters;
net.params(idx).learningRate = 1;
net.params(idx).weightDecay = 1;
%% residual prediction
params = { 'residual_conv_f', 'residual_conv_b' };
sigma = sqrt(2 / (f * f * n));
filters = sigma * randn(f, f, n, 1, 'single');
biases = zeros(1, 1, 'single');
idx = net.getParamIndex(params{1});
net.params(idx).value = filters;
net.params(idx).learningRate = 1;
net.params(idx).weightDecay = 1;
idx = net.getParamIndex(params{2});
net.params(idx).value = biases;
net.params(idx).learningRate = 0.1;
net.params(idx).weightDecay = 1;
end
end