forked from phoenix104104/LapSRN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_LapSRN.m
247 lines (193 loc) · 7.88 KB
/
init_LapSRN.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
function net = init_LapSRN(opts)
% -------------------------------------------------------------------------
% Description:
% create initial LapSRN model
%
% Input:
% - opts : options generated from init_opts()
%
% Output:
% - net : dagnn model
%
% Citation:
% Deep Laplacian Pyramid Networks for Fast and Accurate Super-Resolution
% Wei-Sheng Lai, Jia-Bin Huang, Narendra Ahuja, and Ming-Hsuan Yang
% IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 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);
depth = opts.depth;
scale = opts.scale;
level = ceil(log(scale) / log(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
net = dagnn.DagNN;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Feature extraction branch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sigma = opts.init_sigma;
filters = sigma * randn(f, f, 1, n, 'single');
biases = zeros(1, n, 'single');
% conv
inputs = { 'LR' };
outputs = { 'input_conv' };
params = { 'input_conv_f', 'input_conv_b' };
net.addLayer(outputs{1}, ...
dagnn.Conv('size', size(filters), ...
'pad', pad, ...
'stride', 1), ...
inputs, outputs, params);
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;
% ReLU
inputs = { 'input_conv' };
outputs = { 'input_relu' };
net.addLayer(outputs{1}, ...
dagnn.ReLU('leak', 0.2), ...
inputs, outputs);
next_input = outputs{1};
%% deep conv layers (f x f x n x n)
sigma = sqrt( 2 / (f * f * n) );
for s = level : -1 : 1
% conv layers (f x f x n x n)
for d = 1:depth
filters = sigma * randn(f, f, n, n, 'single');
biases = zeros(1, n, 'single');
% conv
inputs = { next_input };
outputs = { sprintf('level%d_conv%d', s, d) };
params = { sprintf('level%d_conv%d_f', s, d), ...
sprintf('level%d_conv%d_b', s, d)};
net.addLayer(outputs{1}, ...
dagnn.Conv('size', size(filters), ...
'pad', pad, ...
'stride', 1), ...
inputs, outputs, params);
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;
% ReLU
inputs = { sprintf('level%d_conv%d', s, d) };
outputs = { sprintf('level%d_relu%d', s, d) };
net.addLayer(outputs{1}, ...
dagnn.ReLU('leak', 0.2), ...
inputs, outputs);
next_input = outputs{1};
end
%% features upsample layers
filters = sigma * randn(f, f, n, n, 'single');
biases = zeros(1, n, 'single');
inputs = { next_input };
outputs = { sprintf('level%d_upconv', s) };
params = { sprintf('level%d_upconv_f', s), ...
sprintf('level%d_upconv_b', s) };
net.addLayer(outputs{1}, ...
dagnn.ConvTranspose(...
'size', size(filters), ...
'upsample', 2, ...
'crop', crop, ...
'numGroups', 1, ...
'hasBias', true), ...
inputs, outputs, params) ;
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;
%% ReLU
inputs = { sprintf('level%d_upconv', s) };
outputs = { sprintf('level%d_uprelu', s) };
net.addLayer(outputs{1}, ...
dagnn.ReLU('leak', 0.2), ...
inputs, outputs);
next_input = outputs{1};
%% residual prediction layer (f x f x n x 1)
sigma = sqrt(2 / (f * f * n));
filters = sigma * randn(f, f, n, 1, 'single');
biases = zeros(1, 1, 'single');
inputs = { next_input };
outputs = { sprintf('level%d_residual', s) };
params = { sprintf('level%d_residual_conv_f', s), ...
sprintf('level%d_residual_conv_b', s) };
net.addLayer(outputs{1}, ...
dagnn.Conv('size', size(filters), ...
'pad', pad, ...
'stride', 1), ...
inputs, outputs, params);
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Image reconstruction branch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
next_input = 'LR';
for s = level : -1 : 1
%% image upsample layer
filters = single(bilinear_kernel(4, 1, 1));
inputs = { next_input };
outputs = { sprintf('level%d_img_up', s) };
params = { sprintf('level%d_img_up_f', s) };
net.addLayer(outputs{1}, ...
dagnn.ConvTranspose(...
'size', size(filters), ...
'upsample', 2, ...
'crop', 1, ...
'numGroups', 1, ...
'hasBias', false), ...
inputs, outputs, params) ;
idx = net.getParamIndex(params{1});
net.params(idx).value = filters;
net.params(idx).learningRate = 1;
net.params(idx).weightDecay = 1;
%% residual addition layer
inputs = { sprintf('level%d_img_up', s), ...
sprintf('level%d_residual', s) };
outputs = { sprintf('level%d_output', s) };
net.addLayer(outputs{1}, ...
dagnn.Sum(), ...
inputs, outputs);
next_input = outputs{1};
%% Loss layer
inputs = { next_input, ...
sprintf('level%d_HR', s) };
outputs = { sprintf('level%d_%s_loss', s, opts.loss) };
net.addLayer(outputs{1}, ...
dagnn.vllab_dag_loss(...
'loss_type', opts.loss), ...
inputs, outputs);
end
end