ivan@152
|
1 function DL=SMALL_two_step_DL(Problem, DL)
|
ivan@152
|
2
|
ivan@152
|
3 % determine which solver is used for sparse representation %
|
ivan@152
|
4
|
ivan@152
|
5 solver = DL.param.solver;
|
ivan@152
|
6
|
ivan@152
|
7 % determine which type of udate to use ('KSVD', 'MOD', 'ols' or 'mailhe') %
|
ivan@152
|
8
|
ivan@152
|
9 typeUpdate = DL.name;
|
ivan@152
|
10
|
ivan@152
|
11 sig = Problem.b;
|
ivan@152
|
12
|
ivan@152
|
13 % determine dictionary size %
|
ivan@152
|
14
|
ivan@152
|
15 if (isfield(DL.param,'initdict'))
|
ivan@152
|
16 if (any(size(DL.param.initdict)==1) && all(iswhole(DL.param.initdict(:))))
|
ivan@152
|
17 dictsize = length(DL.param.initdict);
|
ivan@152
|
18 else
|
ivan@152
|
19 dictsize = size(DL.param.initdict,2);
|
ivan@152
|
20 end
|
ivan@152
|
21 end
|
ivan@152
|
22 if (isfield(DL.param,'dictsize')) % this superceedes the size determined by initdict
|
ivan@152
|
23 dictsize = DL.param.dictsize;
|
ivan@152
|
24 end
|
ivan@152
|
25
|
ivan@152
|
26 if (size(sig,2) < dictsize)
|
ivan@152
|
27 error('Number of training signals is smaller than number of atoms to train');
|
ivan@152
|
28 end
|
ivan@152
|
29
|
ivan@152
|
30
|
ivan@152
|
31 % initialize the dictionary %
|
ivan@152
|
32
|
luis@190
|
33 % todo: check second if statement
|
luis@190
|
34 if (isfield(DL.param,'initdict')) && ~isempty(DL.param.initdict);
|
ivan@152
|
35 if (any(size(DL.param.initdict)==1) && all(iswhole(DL.param.initdict(:))))
|
ivan@152
|
36 dico = sig(:,DL.param.initdict(1:dictsize));
|
ivan@152
|
37 else
|
ivan@152
|
38 if (size(DL.param.initdict,1)~=size(sig,1) || size(DL.param.initdict,2)<dictsize)
|
ivan@152
|
39 error('Invalid initial dictionary');
|
ivan@152
|
40 end
|
ivan@152
|
41 dico = DL.param.initdict(:,1:dictsize);
|
ivan@152
|
42 end
|
ivan@152
|
43 else
|
ivan@152
|
44 data_ids = find(colnorms_squared(sig) > 1e-6); % ensure no zero data elements are chosen
|
ivan@152
|
45 perm = randperm(length(data_ids));
|
ivan@152
|
46 dico = sig(:,data_ids(perm(1:dictsize)));
|
ivan@152
|
47 end
|
ivan@152
|
48
|
ivan@152
|
49 % flow: 'sequential' or 'parallel'. If sequential, the residual is updated
|
ivan@152
|
50 % after each atom update. If parallel, the residual is only updated once
|
ivan@152
|
51 % the whole dictionary has been computed. Sequential works better, there
|
ivan@152
|
52 % may be no need to implement parallel. Not used with MOD.
|
ivan@152
|
53
|
ivan@152
|
54 if isfield(DL.param,'flow')
|
ivan@152
|
55 flow = DL.param.flow;
|
ivan@152
|
56 else
|
ivan@152
|
57 flow = 'sequential';
|
ivan@152
|
58 end
|
ivan@152
|
59
|
ivan@152
|
60 % learningRate. If the type is 'ols', it is the descent step of
|
ivan@152
|
61 % the gradient (typical choice: 0.1). If the type is 'mailhe', the
|
ivan@152
|
62 % descent step is the optimal step*rho (typical choice: 1, although 2
|
ivan@152
|
63 % or 3 seems to work better). Not used for MOD and KSVD.
|
ivan@152
|
64
|
ivan@152
|
65 if isfield(DL.param,'learningRate')
|
ivan@152
|
66 learningRate = DL.param.learningRate;
|
ivan@152
|
67 else
|
ivan@152
|
68 learningRate = 0.1;
|
ivan@152
|
69 end
|
ivan@152
|
70
|
ivan@152
|
71 % number of iterations (default is 40) %
|
ivan@152
|
72
|
ivan@152
|
73 if isfield(DL.param,'iternum')
|
ivan@152
|
74 iternum = DL.param.iternum;
|
ivan@152
|
75 else
|
ivan@152
|
76 iternum = 40;
|
ivan@152
|
77 end
|
ivan@152
|
78 % determine if we should do decorrelation in every iteration %
|
ivan@152
|
79
|
ivan@152
|
80 if isfield(DL.param,'coherence')
|
ivan@152
|
81 decorrelate = 1;
|
ivan@152
|
82 mu = DL.param.coherence;
|
ivan@152
|
83 else
|
ivan@152
|
84 decorrelate = 0;
|
ivan@152
|
85 end
|
ivan@152
|
86
|
ivan@152
|
87 % show dictonary every specified number of iterations
|
ivan@152
|
88
|
ivan@153
|
89 if isfield(DL.param,'show_dict')
|
ivan@152
|
90 show_dictionary=1;
|
ivan@152
|
91 show_iter=DL.param.show_dict;
|
ivan@152
|
92 else
|
ivan@152
|
93 show_dictionary=0;
|
ivan@152
|
94 show_iter=0;
|
ivan@152
|
95 end
|
ivan@152
|
96
|
ivan@152
|
97 % This is a small patch that needs to be resolved in dictionary learning we
|
ivan@152
|
98 % want sparse representation of training set, and in Problem.b1 in this
|
ivan@152
|
99 % version of software we store the signal that needs to be represented
|
ivan@152
|
100 % (for example the whole image)
|
daniele@195
|
101 global SMALL_path
|
ivan@152
|
102
|
ivan@152
|
103 tmpTraining = Problem.b1;
|
ivan@152
|
104 Problem.b1 = sig;
|
ivan@153
|
105 if isfield(Problem,'reconstruct')
|
ivan@153
|
106 Problem = rmfield(Problem, 'reconstruct');
|
ivan@153
|
107 end
|
ivan@152
|
108 solver.profile = 0;
|
ivan@152
|
109
|
ivan@152
|
110 % main loop %
|
ivan@152
|
111
|
ivan@152
|
112 for i = 1:iternum
|
ivan@153
|
113 Problem.A = dico;
|
ivan@152
|
114 solver = SMALL_solve(Problem, solver);
|
luis@190
|
115
|
luis@190
|
116 % configuration file
|
luis@190
|
117 run([SMALL_path '/config/SMALL_two_step_DL_config.m'])
|
luis@190
|
118
|
luis@190
|
119 % [dico, solver.solution] = dico_update(dico, sig, solver.solution, ...
|
luis@190
|
120 % typeUpdate, flow, learningRate);
|
luis@190
|
121 % if (decorrelate)
|
luis@190
|
122 % dico = dico_decorr(dico, mu, solver.solution);
|
luis@190
|
123 % end
|
ivan@153
|
124
|
ivan@152
|
125 if ((show_dictionary)&&(mod(i,show_iter)==0))
|
ivan@152
|
126 dictimg = SMALL_showdict(dico,[8 8],...
|
ivan@152
|
127 round(sqrt(size(dico,2))),round(sqrt(size(dico,2))),'lines','highcontrast');
|
ivan@152
|
128 figure(2); imagesc(dictimg);colormap(gray);axis off; axis image;
|
ivan@152
|
129 pause(0.02);
|
ivan@152
|
130 end
|
ivan@152
|
131 end
|
ivan@152
|
132
|
ivan@152
|
133 Problem.b1 = tmpTraining;
|
ivan@152
|
134 DL.D = dico;
|
ivan@152
|
135
|
ivan@153
|
136 end
|
ivan@153
|
137
|
ivan@153
|
138 function Y = colnorms_squared(X)
|
ivan@153
|
139
|
ivan@153
|
140 % compute in blocks to conserve memory
|
ivan@153
|
141 Y = zeros(1,size(X,2));
|
ivan@153
|
142 blocksize = 2000;
|
ivan@153
|
143 for i = 1:blocksize:size(X,2)
|
ivan@153
|
144 blockids = i : min(i+blocksize-1,size(X,2));
|
ivan@153
|
145 Y(blockids) = sum(X(:,blockids).^2);
|
ivan@153
|
146 end
|
ivan@153
|
147
|
daniele@195
|
148 end
|