ivan@152
|
1 %% Dictionary Learning for Image Denoising - KSVD vs Recursive Least Squares
|
ivan@152
|
2 %
|
ivan@152
|
3 % This file contains an example of how SMALLbox can be used to test different
|
ivan@152
|
4 % dictionary learning techniques in Image Denoising problem.
|
ivan@152
|
5 % It calls generateImageDenoiseProblem that will let you to choose image,
|
ivan@152
|
6 % add noise and use noisy image to generate training set for dictionary
|
ivan@152
|
7 % learning.
|
ivan@152
|
8 % Two dictionary learning techniques were compared:
|
ivan@152
|
9 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
|
ivan@152
|
10 % Implementation of the K-SVD Algorithm using Batch Orthogonal
|
ivan@152
|
11 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
|
ivan@152
|
12 % - RLS-DLA - Skretting, K.; Engan, K.; , "Recursive Least Squares
|
ivan@152
|
13 % Dictionary Learning Algorithm," Signal Processing, IEEE Transactions on,
|
ivan@152
|
14 % vol.58, no.4, pp.2121-2130, April 2010
|
ivan@152
|
15 %
|
ivan@152
|
16
|
ivan@152
|
17
|
ivan@152
|
18 % Centre for Digital Music, Queen Mary, University of London.
|
ivan@152
|
19 % This file copyright 2011 Ivan Damnjanovic.
|
ivan@152
|
20 %
|
ivan@152
|
21 % This program is free software; you can redistribute it and/or
|
ivan@152
|
22 % modify it under the terms of the GNU General Public License as
|
ivan@152
|
23 % published by the Free Software Foundation; either version 2 of the
|
ivan@152
|
24 % License, or (at your option) any later version. See the file
|
ivan@152
|
25 % COPYING included with this distribution for more information.
|
ivan@152
|
26 %
|
ivan@152
|
27 %%
|
ivan@152
|
28
|
ivan@152
|
29
|
ivan@152
|
30
|
ivan@152
|
31 % If you want to load the image outside of generateImageDenoiseProblem
|
ivan@152
|
32 % function uncomment following lines. This can be useful if you want to
|
ivan@152
|
33 % denoise more then one image for example.
|
ivan@152
|
34 % Here we are loading test_image.mat that contains structure with 5 images : lena,
|
ivan@152
|
35 % barbara,boat, house and peppers.
|
ivan@152
|
36 clear;
|
ivan@152
|
37 TMPpath=pwd;
|
ivan@152
|
38 FS=filesep;
|
ivan@152
|
39 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
|
ivan@152
|
40 cd([pathstr1,FS,'data',FS,'images']);
|
ivan@152
|
41 load('test_image.mat');
|
ivan@152
|
42 cd(TMPpath);
|
ivan@152
|
43
|
ivan@152
|
44 % Deffining the noise levels that we want to test
|
ivan@152
|
45
|
ivan@152
|
46 noise_level=[10 20 25 50 100];
|
ivan@152
|
47
|
ivan@152
|
48 % Here we loop through different noise levels and images
|
ivan@152
|
49
|
ivan@153
|
50 for noise_ind=4:4
|
ivan@152
|
51 for im_num=1:1
|
ivan@152
|
52
|
ivan@152
|
53 % Defining Image Denoising Problem as Dictionary Learning
|
ivan@152
|
54 % Problem. As an input we set the number of training patches.
|
ivan@152
|
55
|
ivan@152
|
56 SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind));
|
ivan@152
|
57 SMALL.Problem.name=int2str(im_num);
|
ivan@152
|
58
|
ivan@152
|
59 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
|
ivan@152
|
60 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
|
ivan@152
|
61
|
ivan@152
|
62 % results structure is to store all results
|
ivan@152
|
63
|
ivan@152
|
64 results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr;
|
ivan@152
|
65
|
ivan@152
|
66 %%
|
ivan@152
|
67 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
|
ivan@152
|
68
|
ivan@152
|
69 % Initialising Dictionary structure
|
ivan@152
|
70 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
ivan@152
|
71 % to zero values
|
ivan@152
|
72
|
ivan@152
|
73 SMALL.DL(1)=SMALL_init_DL();
|
ivan@152
|
74
|
ivan@152
|
75 % Defining the parameters needed for dictionary learning
|
ivan@152
|
76
|
ivan@152
|
77 SMALL.DL(1).toolbox = 'KSVD';
|
ivan@152
|
78 SMALL.DL(1).name = 'ksvd';
|
ivan@152
|
79
|
ivan@152
|
80 % Defining the parameters for KSVD
|
ivan@152
|
81 % In this example we are learning 256 atoms in 20 iterations, so that
|
ivan@152
|
82 % every patch in the training set can be represented with target error in
|
ivan@152
|
83 % L2-norm (Edata)
|
ivan@152
|
84 % Type help ksvd in MATLAB prompt for more options.
|
ivan@152
|
85
|
ivan@152
|
86
|
ivan@152
|
87 SMALL.DL(1).param=struct(...
|
ivan@152
|
88 'Edata', Edata,...
|
ivan@152
|
89 'initdict', SMALL.Problem.initdict,...
|
ivan@152
|
90 'dictsize', SMALL.Problem.p,...
|
ivan@152
|
91 'exact', 1, ...
|
ivan@152
|
92 'iternum', 20,...
|
ivan@152
|
93 'memusage', 'high');
|
ivan@152
|
94
|
ivan@152
|
95 % Learn the dictionary
|
ivan@152
|
96
|
ivan@152
|
97 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
|
ivan@152
|
98
|
ivan@152
|
99 % Set SMALL.Problem.A dictionary
|
ivan@152
|
100 % (backward compatiblity with SPARCO: solver structure communicate
|
ivan@152
|
101 % only with Problem structure, ie no direct communication between DL and
|
ivan@152
|
102 % solver structures)
|
ivan@152
|
103
|
ivan@152
|
104 SMALL.Problem.A = SMALL.DL(1).D;
|
ivan@152
|
105 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
|
ivan@152
|
106
|
ivan@152
|
107 %%
|
ivan@152
|
108 % Initialising solver structure
|
ivan@152
|
109 % Setting solver structure fields (toolbox, name, param, solution,
|
ivan@152
|
110 % reconstructed and time) to zero values
|
ivan@152
|
111
|
ivan@152
|
112 SMALL.solver(1)=SMALL_init_solver;
|
ivan@152
|
113
|
ivan@152
|
114 % Defining the parameters needed for image denoising
|
ivan@152
|
115
|
ivan@152
|
116 SMALL.solver(1).toolbox='ompbox';
|
ivan@152
|
117 SMALL.solver(1).name='omp2';
|
ivan@152
|
118 SMALL.solver(1).param=struct(...
|
ivan@152
|
119 'epsilon',Edata,...
|
ivan@152
|
120 'maxatoms', maxatoms);
|
ivan@152
|
121
|
ivan@152
|
122 % Denoising the image - find the sparse solution in the learned
|
ivan@152
|
123 % dictionary for all patches in the image and the end it uses
|
ivan@152
|
124 % reconstruction function to reconstruct the patches and put them into a
|
ivan@152
|
125 % denoised image
|
ivan@152
|
126
|
ivan@152
|
127 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
|
ivan@152
|
128
|
ivan@152
|
129 % Show PSNR after reconstruction
|
ivan@152
|
130
|
ivan@152
|
131 SMALL.solver(1).reconstructed.psnr
|
ivan@152
|
132
|
ivan@152
|
133 %%
|
ivan@152
|
134 % For comparison purposes we will denoise image with overcomplete DCT
|
ivan@152
|
135 % here
|
ivan@152
|
136 % Set SMALL.Problem.A dictionary to be oDCT (i.e. Problem.initdict -
|
ivan@152
|
137 % since initial dictionaruy is already set to be oDCT when generating the
|
ivan@152
|
138 % denoising problem
|
ivan@152
|
139
|
ivan@152
|
140
|
ivan@152
|
141 % Initialising solver structure
|
ivan@152
|
142 % Setting solver structure fields (toolbox, name, param, solution,
|
ivan@152
|
143 % reconstructed and time) to zero values
|
ivan@152
|
144
|
ivan@152
|
145 SMALL.solver(2)=SMALL_init_solver;
|
ivan@152
|
146
|
ivan@152
|
147 % Defining the parameters needed for image denoising
|
ivan@152
|
148
|
ivan@152
|
149 SMALL.solver(2).toolbox='ompbox';
|
ivan@152
|
150 SMALL.solver(2).name='omp2';
|
ivan@152
|
151 SMALL.solver(2).param=struct(...
|
ivan@152
|
152 'epsilon',Edata,...
|
ivan@152
|
153 'maxatoms', maxatoms);
|
ivan@152
|
154
|
ivan@152
|
155 % Initialising Dictionary structure
|
ivan@152
|
156 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
ivan@152
|
157 % to zero values
|
ivan@152
|
158
|
ivan@152
|
159 SMALL.DL(2)=SMALL_init_DL('TwoStepDL', 'MOD', '', 1);
|
ivan@152
|
160
|
ivan@152
|
161
|
ivan@152
|
162 % Defining the parameters for MOD
|
ivan@152
|
163 % In this example we are learning 256 atoms in 20 iterations, so that
|
ivan@152
|
164 % every patch in the training set can be represented with target error in
|
ivan@152
|
165 % L2-norm (EData)
|
ivan@152
|
166 % Type help ksvd in MATLAB prompt for more options.
|
ivan@152
|
167
|
ivan@152
|
168
|
ivan@152
|
169 SMALL.DL(2).param=struct(...
|
ivan@152
|
170 'solver', SMALL.solver(2),...
|
ivan@152
|
171 'initdict', SMALL.Problem.initdict,...
|
ivan@152
|
172 'dictsize', SMALL.Problem.p,...
|
ivan@152
|
173 'iternum', 40,...
|
ivan@152
|
174 'show_dict', 1);
|
ivan@152
|
175
|
ivan@152
|
176 % Learn the dictionary
|
ivan@152
|
177
|
ivan@152
|
178 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
|
ivan@152
|
179
|
ivan@152
|
180 % Set SMALL.Problem.A dictionary
|
ivan@152
|
181 % (backward compatiblity with SPARCO: solver structure communicate
|
ivan@152
|
182 % only with Problem structure, ie no direct communication between DL and
|
ivan@152
|
183 % solver structures)
|
ivan@152
|
184
|
ivan@152
|
185 SMALL.Problem.A = SMALL.DL(2).D;
|
ivan@152
|
186 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
|
ivan@152
|
187
|
ivan@152
|
188 % Denoising the image - find the sparse solution in the learned
|
ivan@152
|
189 % dictionary for all patches in the image and the end it uses
|
ivan@152
|
190 % reconstruction function to reconstruct the patches and put them into a
|
ivan@152
|
191 % denoised image
|
ivan@152
|
192
|
ivan@152
|
193 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
|
ivan@152
|
194
|
ivan@152
|
195 %%
|
ivan@152
|
196 % In the b1 field all patches from the image are stored. For RLS-DLA we
|
ivan@152
|
197 % will first exclude all the patches that have l2 norm smaller then
|
ivan@152
|
198 % threshold and then take min(40000, number_of_remaining_patches) in
|
ivan@152
|
199 % ascending order as our training set (SMALL.Problem.b)
|
ivan@152
|
200
|
ivan@152
|
201 X=SMALL.Problem.b1;
|
ivan@152
|
202 X_norm=sqrt(sum(X.^2, 1));
|
ivan@152
|
203 [X_norm_sort, p]=sort(X_norm);
|
ivan@152
|
204 p1=p(X_norm_sort>Edata);
|
ivan@152
|
205 if size(p1,2)>40000
|
ivan@152
|
206 p2 = randperm(size(p1,2));
|
ivan@152
|
207 p2=sort(p2(1:40000));
|
ivan@152
|
208 size(p2,2)
|
ivan@152
|
209 SMALL.Problem.b=X(:,p1(p2));
|
ivan@152
|
210 else
|
ivan@152
|
211 size(p1,2)
|
ivan@152
|
212 SMALL.Problem.b=X(:,p1);
|
ivan@152
|
213
|
ivan@152
|
214 end
|
ivan@152
|
215
|
ivan@152
|
216 % Forgetting factor for RLS-DLA algorithm, in this case we are using
|
ivan@152
|
217 % fixed value
|
ivan@152
|
218
|
ivan@152
|
219 lambda=0.9998
|
ivan@152
|
220
|
ivan@152
|
221 % Use Recursive Least Squares
|
ivan@152
|
222 % to Learn overcomplete dictionary
|
ivan@152
|
223
|
ivan@152
|
224 % Initialising Dictionary structure
|
ivan@152
|
225 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
ivan@152
|
226 % to zero values
|
ivan@152
|
227
|
ivan@152
|
228 SMALL.DL(3)=SMALL_init_DL();
|
ivan@152
|
229
|
ivan@152
|
230 % Defining fields needed for dictionary learning
|
ivan@152
|
231
|
ivan@152
|
232 SMALL.DL(3).toolbox = 'SMALL';
|
ivan@152
|
233 SMALL.DL(3).name = 'SMALL_rlsdla';
|
ivan@152
|
234 SMALL.DL(3).param=struct(...
|
ivan@152
|
235 'Edata', Edata,...
|
ivan@152
|
236 'initdict', SMALL.Problem.initdict,...
|
ivan@152
|
237 'dictsize', SMALL.Problem.p,...
|
ivan@152
|
238 'forgettingMode', 'FIX',...
|
ivan@152
|
239 'forgettingFactor', lambda,...
|
ivan@152
|
240 'show_dict', 1000);
|
ivan@152
|
241
|
ivan@152
|
242
|
ivan@152
|
243 SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3));
|
ivan@152
|
244
|
ivan@152
|
245 % Initialising solver structure
|
ivan@152
|
246 % Setting solver structure fields (toolbox, name, param, solution,
|
ivan@152
|
247 % reconstructed and time) to zero values
|
ivan@152
|
248
|
ivan@152
|
249 SMALL.Problem.A = SMALL.DL(3).D;
|
ivan@152
|
250 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
|
ivan@152
|
251
|
ivan@152
|
252 SMALL.solver(3)=SMALL_init_solver;
|
ivan@152
|
253
|
ivan@152
|
254 % Defining the parameters needed for image denoising
|
ivan@152
|
255
|
ivan@152
|
256 SMALL.solver(3).toolbox='ompbox';
|
ivan@152
|
257 SMALL.solver(3).name='omp2';
|
ivan@152
|
258 SMALL.solver(3).param=struct(...
|
ivan@152
|
259 'epsilon',Edata,...
|
ivan@152
|
260 'maxatoms', maxatoms);
|
ivan@152
|
261
|
ivan@152
|
262
|
ivan@152
|
263 SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3));
|
ivan@152
|
264
|
ivan@152
|
265 SMALL.solver(3).reconstructed.psnr
|
ivan@152
|
266
|
ivan@152
|
267
|
ivan@152
|
268 % show results %
|
ivan@152
|
269
|
ivan@152
|
270 SMALL_ImgDeNoiseResult(SMALL);
|
ivan@152
|
271
|
ivan@152
|
272 results(noise_ind,im_num).psnr.ksvd=SMALL.solver(1).reconstructed.psnr;
|
ivan@152
|
273 results(noise_ind,im_num).psnr.odct=SMALL.solver(2).reconstructed.psnr;
|
ivan@152
|
274 results(noise_ind,im_num).psnr.rlsdla=SMALL.solver(3).reconstructed.psnr;
|
ivan@152
|
275 results(noise_ind,im_num).vmrse.ksvd=SMALL.solver(1).reconstructed.vmrse;
|
ivan@152
|
276 results(noise_ind,im_num).vmrse.odct=SMALL.solver(2).reconstructed.vmrse;
|
ivan@152
|
277 results(noise_ind,im_num).vmrse.rlsdla=SMALL.solver(3).reconstructed.vmrse;
|
ivan@152
|
278 results(noise_ind,im_num).ssim.ksvd=SMALL.solver(1).reconstructed.ssim;
|
ivan@152
|
279 results(noise_ind,im_num).ssim.odct=SMALL.solver(2).reconstructed.ssim;
|
ivan@152
|
280 results(noise_ind,im_num).ssim.rlsdla=SMALL.solver(3).reconstructed.ssim;
|
ivan@152
|
281
|
ivan@152
|
282 results(noise_ind,im_num).time.ksvd=SMALL.solver(1).time+SMALL.DL(1).time;
|
ivan@152
|
283 results(noise_ind,im_num).time.rlsdla.time=SMALL.solver(3).time+SMALL.DL(3).time;
|
ivan@152
|
284 clear SMALL;
|
ivan@152
|
285 end
|
ivan@152
|
286 end
|
ivan@152
|
287 % save results.mat results
|