-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCDL_multiscale.m
255 lines (222 loc) · 6.83 KB
/
CDL_multiscale.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
function [X,D, res] = CDL_multiscale(D, S, lamb, Kls, Mls, opts)
% Efficient algorithm for onvolutional dictionary learning (CDL) (with multiscale filters)
%
% Inputs:
% D: initial dictionary of K filters of size m1 by m2
% S: P stacked image of size H by W (S is H by W by P)
% Kls: list of number of filters of each size
% Mls: list of filter sizes (Mls and Kls must be equal)
% lamb: l1-norm regularization parameter
% opt: optional parameters
%
% Outputs:
% X: sparse coefficient maps
% D: the learned dictionary
% res: results (iterations details)
%
% Optional parameters:
%
% opts.MaxItr: maximum number of iterations (default 500)
% opts.csc_iters: number of internal csc step iterations (default 1)
% opts.cdl_iters: number of internal dict update step iterations (default 1)
% opts.rho: ADMM penalty parameter for csc step (default 10)
% opts.sig: ADMM penalty parameter for cdl step (default 10)
% opts.AutoRho: varying penalty parameter (ADMM extention) (default 0 (disabled))
% opts.AutoSig: varying penalty parameter (ADMM extention) (default 0 (disabled))
% opts.RhoUpdateCycle: rho update cycle (default 10)
% opts.SigUpdateCycle: sig update cycle (default 10)
% opts.relaxParam: relaxation parameter (ADMM extention) (default 1.8)
% opts.Xinit: initial sparse codes (default zeros)
% opts.Uinit: initial lagrange variables (default zeros)
% opts.Vinit: initial lagrange variables (default zeros)
% opts.dcfilter: the first filter is DC filter and is kept unchanges (default 0)
%
% reference:
%
% Farsad G. Veshki and Sergiy Vorobyov. Efficient ADMM-based Algorithms for
% Convolutional Sparse Coding. 2021
%%
if length(Mls) ~= length(Kls)
error('Numbers of elements in K and M are diffrent.')
end
%% parameters
[H,W,P] = size(S);
[m,~,K] = size(D);
S = reshape(S,[H W 1 P]);
if nargin < 4
opts = [];
end
if ~isfield(opts,'MaxIter')
opts.MaxIter = 200;
end
if ~isfield(opts,'csc_iters')
opts.csc_iters = 1;
end
if ~isfield(opts,'cdl_iters')
opts.cdl_iters = 1;
end
if ~isfield(opts,'rho')
opts.rho = 10;
end
if ~isfield(opts,'sig')
opts.sig = 10;
end
if ~isfield(opts,'AutoRho')
opts.AutoRho = 1;
end
if ~isfield(opts,'AutoSig')
opts.AutoSig = 1;
end
if ~isfield(opts,'AutoSig')
opts.AutoSig = 1;
end
if ~isfield(opts,'RhoUpdateCycle')
opts.RhoUpdateCycle = 1;
end
if ~isfield(opts,'SigUpdateCycle')
opts.SigUpdateCycle = 1;
end
if ~isfield(opts,'Xinit')
opts.Xinit = zeros(H,W,K,P,'single');
end
if ~isfield(opts,'Uinit')
opts.Uinit = zeros(H,W,K,P,'single');
end
if ~isfield(opts,'Vinit')
opts.Vinit = zeros(H,W,K,P,'single');
end
if ~isfield(opts,'relaxParam')
opts.relaxParam = 1.8;
end
if ~isfield(opts,'eAbs')
opts.eAbs = 1e-4;
end
if ~isfield(opts,'eRel')
opts.eRel = 1e-4;
end
if ~isfield(opts,'dcfilter')
opts.dcfilter = 0;
end
%% initialization
alpha = opts.relaxParam;
Sf = fft2(S);
X = opts.Xinit; % sparse code
U = opts.Uinit; % scaled dual variable
V = opts.Vinit;
rho = opts.rho;
sig = opts.sig;
RhoUpdateCycle = opts.RhoUpdateCycle;
SigUpdateCycle = opts.SigUpdateCycle;
eAbs = opts.eAbs;
eRel = opts.eRel;
eprix = 0;
eduax = 0;
eprid = 0;
eduad = 0;
MaxIter = opts.MaxIter;
csc_iters = opts.csc_iters;
cdl_iters = opts.cdl_iters;
r_csc = inf; s_csc = inf; r_cdl= inf; s_cdl = inf;
res.iterinf = [];
mu = 5; % varying rho parameter
tau = 1.2; % varying rho parameter
vec = @(x) x(:);
itr = 1;
%% CDL CYCLES
tsrt = tic;
D = padarray(D,[H-m W-m],'post');
Df = fft2(D);
Nx = numel(X);
Nd = numel(D);
while itr<=MaxIter && (r_csc > eprix || s_csc > eduax || r_cdl > eprid || s_cdl > eduad)
%%% ADMM iterations
%% CSC
for ttt = 1:csc_iters % default = 1
Xprv = X;
Z = Z_update(fft2(X-U),Df,Sf,rho) ; % X update
Zr = alpha * Z + (1-alpha)*X; % relaxation
X = sfthrsh(Zr+U, lamb/rho); % Z update
U = Zr - X + U; % U update
end
%% CDL
for ttt = 1:cdl_iters % default = 1
Dprv = D;
G = G_update(fft2(X),Sf,sig,fft2(D-V));
Gr = alpha * G + (1-alpha)*D; % relaxation
frst = 1;
for id = 1:length(Mls)
D(:,:,frst:frst+Kls(id)-1) = D_proj( sum(Gr(:,:,frst:frst+Kls(id)-1,:)+V(:,:,frst:frst+Kls(id)-1,:),4)/P ,Mls(id),H,W); % projection on constraint set
frst = frst+Kls(id);
end
if opts.dcfilter == 1
D(:,:,1) = Dprv(:,:,1);
end
V = Gr - D + V;
end
%%
Df = fft2(D);
titer = toc(tsrt);
%%
%_________________________residuals CSC_____________________________
nZ = norm(Z(:)); nX = norm(X(:)); nU = norm(U(:));
r_csc = norm(vec(Z-X)); % primal residulal
s_csc = rho*norm(vec(Xprv-X)); % dual residual
eprix = sqrt(Nx)*eAbs+max(nX,nZ)*eRel;
eduax = sqrt(Nx)*eAbs+rho*nU*eRel;
%_________________________residuals CDL_____________________________
nG = norm(G(:)); nD = norm(D(:))*sqrt(P); nV = norm(V(:));
r_cdl = norm(vec(G-D)); % primal residulal
s_cdl = sqrt(P)*sig*norm(vec(Dprv-D)); % dual residual
eprid = sqrt(Nd)*eAbs+max(nD,nG)*eRel;
eduad = sqrt(Nd)*eAbs+rho*nV*eRel;
%_________________________rho update_____________________________
if opts.AutoRho && rem(itr,RhoUpdateCycle)==0
[rho,U] = rho_update(rho,r_csc,s_csc,mu,tau,U);
end
%_________________________sig update_____________________________
if opts.AutoSig && rem(itr,SigUpdateCycle)==0
[sig,V] = rho_update(sig,r_cdl,s_cdl,mu,tau,V);
end
%_________________________progress_______________________________
rPow = sum(vec(abs(sum(Df.*fft2(X),3)-Sf).^2))/(2*H*W); % residual power
L1 = sum(abs(X(:))); % l1-norm
fval = rPow + lamb*L1; % functional value
res.iterinf = [res.iterinf; [itr fval rPow L1 r_csc s_csc r_cdl s_cdl rho sig titer]];
itr = itr+1;
end
D = D(1:m,1:m,:);
end
function y = sfthrsh(x, kappa) % shrinkage
y = sign(x).*max(0, abs(x) - kappa);
end
function Z = Z_update(Wf,Df,Sf,rho)
C = conj(Df)./(sum(abs(Df).^2,3)+rho);
Rf = Sf - sum(Wf.*Df,3); % residual update
Zf = Wf + C.*Rf; % X update
Z = ifft2(Zf,'symmetric');
end
function G = G_update(Xf,Sf,sig,Wf)
C = conj(Xf)./(sum(abs(Xf).^2,3)+sig);
Rf = Sf - sum(Xf.*Wf,3); % residual update
Gf = Wf + C.*Rf;
G = ifft2(Gf,'symmetric');
end
function D = D_proj(D,m,H,W) % projection on unit ball
D = padarray(D(1:m,1:m,:,:),[H-m W-m],'post');
D = D./max(sqrt(sum(D.^2,1:2)),0);
end
function [rho,U] = rho_update(rho,r,s,mu,tau,U)
% varying penalty parameter
a = 1;
if r > mu*s
a = tau;
end
if s > mu*r
a = 1/tau;
end
rho_ = a*rho;
if rho_>1e-4
rho = rho_;
U = U/a;
end
end