comparison examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsRLSDLA.m @ 42:623fcf3a69b1

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:41:53 +0000
parents
children 55faa9b5d1ac
comparison
equal deleted inserted replaced
41:83de4ea524df 42:623fcf3a69b1
1 %% DICTIONARY LEARNING FOR IMAGE DENOISING
2 % This file contains an example of how SMALLbox can be used to test different
3 % dictionary learning techniques in Image Denoising problem.
4 % It calls generateImageDenoiseProblem that will let you to choose image,
5 % add noise and use noisy image to generate training set for dictionary
6 % learning.
7 % Three dictionary learning techniques were compared:
8 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
9 % Implementation of the K-SVD Algorithm using Batch Orthogonal
10 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
11 % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse
12 % Dictionaries for Sparse Signal Approximation", Technical
13 % Report - CS, Technion, June 2009.
14 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
15 % Dictionary Learning for Sparse Coding. International
16 % Conference on Machine Learning,Montreal, Canada, 2009
17 %
18 %
19 % Ivan Damnjanovic 2010
20 %%
21
22
23
24 % If you want to load the image outside of generateImageDenoiseProblem
25 % function uncomment following lines. This can be useful if you want to
26 % denoise more then one image for example.
27 clear;
28 TMPpath=pwd;
29 FS=filesep;
30 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
31 cd([pathstr1,FS,'data',FS,'images']);
32 load('test_image.mat');
33 % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
34 % [pathstr, name, ext, versn] = fileparts(filename);
35 % test_image = imread(filename);
36 % test_image = double(test_image);
37 % cd(TMPpath);
38 % SMALL.Problem.name=name;
39
40 noise_level=[10 20 25 50 100];
41 % Defining Image Denoising Problem as Dictionary Learning
42 % Problem. As an input we set the number of training patches.
43 for noise_ind=1:1
44 for im_num=4:4
45 SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',512, noise_level(noise_ind));
46 SMALL.Problem.name=im_num;
47
48 results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr;
49
50 %%
51 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
52
53 % Initialising Dictionary structure
54 % Setting Dictionary structure fields (toolbox, name, param, D and time)
55 % to zero values
56
57 SMALL.DL(1)=SMALL_init_DL();
58
59 % Defining the parameters needed for dictionary learning
60
61 SMALL.DL(1).toolbox = 'KSVD';
62 SMALL.DL(1).name = 'ksvd';
63
64 % Defining the parameters for KSVD
65 % In this example we are learning 256 atoms in 20 iterations, so that
66 % every patch in the training set can be represented with target error in
67 % L2-norm (EData)
68 % Type help ksvd in MATLAB prompt for more options.
69
70 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
71 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
72 SMALL.DL(1).param=struct(...
73 'Edata', Edata,...
74 'initdict', SMALL.Problem.initdict,...
75 'dictsize', SMALL.Problem.p,...
76 'exact', 1, ...
77 'iternum', 20,...
78 'memusage', 'high');
79
80 % Learn the dictionary
81
82 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
83
84 % Set SMALL.Problem.A dictionary
85 % (backward compatiblity with SPARCO: solver structure communicate
86 % only with Problem structure, ie no direct communication between DL and
87 % solver structures)
88
89 SMALL.Problem.A = SMALL.DL(1).D;
90 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
91
92 %%
93 % Initialising solver structure
94 % Setting solver structure fields (toolbox, name, param, solution,
95 % reconstructed and time) to zero values
96
97 SMALL.solver(1)=SMALL_init_solver;
98
99 % Defining the parameters needed for image denoising
100
101 SMALL.solver(1).toolbox='ompbox';
102 SMALL.solver(1).name='omp2';
103 SMALL.solver(1).param=struct(...
104 'epsilon',Edata,...
105 'maxatoms', maxatoms);
106
107 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
108 % but backward compatible with KSVD definition of denoising
109
110 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
111 SMALL.solver(1).reconstructed.psnr
112 %%
113 % Use KSVDS Dictionary Learning Algorithm to denoise image
114
115 % Initialising solver structure
116 % Setting solver structure fields (toolbox, name, param, solution,
117 % reconstructed and time) to zero values
118 %
119 % SMALL.DL(2)=SMALL_init_DL();
120 %
121 % % Defining the parameters needed for dictionary learning
122 %
123 % SMALL.DL(2).toolbox = 'KSVDS';
124 % SMALL.DL(2).name = 'ksvds';
125 %
126 % % Defining the parameters for KSVDS
127 % % In this example we are learning 256 atoms in 20 iterations, so that
128 % % every patch in the training set can be represented with target error in
129 % % L2-norm (EDataS). We also impose "double sparsity" - dictionary itself
130 % % has to be sparse in the given base dictionary (Tdict - number of
131 % % nonzero elements per atom).
132 % % Type help ksvds in MATLAB prompt for more options.
133 %
134 %
135 % SMALL.DL(2).param=struct(...
136 % 'Edata', Edata, ...
137 % 'Tdict', 6,...
138 % 'stepsize', 1,...
139 % 'dictsize', SMALL.Problem.p,...
140 % 'iternum', 20,...
141 % 'memusage', 'high');
142 % SMALL.DL(2).param.initA = speye(SMALL.Problem.p);
143 % SMALL.DL(2).param.basedict{1} = odctdict(8,16);
144 % SMALL.DL(2).param.basedict{2} = odctdict(8,16);
145 %
146 % % Learn the dictionary
147 %
148 % SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
149
150 % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary
151 % (backward compatiblity with SPARCO: solver structure communicate
152 % only with Problem structure, ie no direct communication between DL and
153 % solver structures)
154
155 SMALL.Problem.A = SMALL.Problem.initdict;
156 % SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1};
157 % SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2};
158 SMALL.DL(2).D=SMALL.Problem.initdict;
159 SparseDict=0;
160 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem, SparseDict);
161
162 %%
163 % Initialising solver structure
164 % Setting solver structure fields (toolbox, name, param, solution,
165 % reconstructed and time) to zero values
166
167 SMALL.solver(2)=SMALL_init_solver;
168
169 % Defining the parameters needed for image denoising
170
171 SMALL.solver(2).toolbox='ompbox';
172 SMALL.solver(2).name='omp2';
173 SMALL.solver(2).param=struct(...
174 'epsilon',Edata,...
175 'maxatoms', maxatoms);
176
177 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
178 % but backward compatible with KSVD definition of denoising
179 % Pay attention that since implicit base dictionary is used, denoising
180 % can be much faster then using explicit dictionary in KSVD example.
181
182 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
183 %%
184
185 for i =1:1
186
187 X=SMALL.Problem.b1;
188 X_norm=sqrt(sum(X.^2, 1));
189 [X_norm_sort, p]=sort(X_norm);
190 p1=p(X_norm_sort>Edata);
191 if size(p1,2)>140000
192 p2 = randperm(size(p1,2));
193 p2=sort(p2(1:40000));
194 size(p2,2)
195 SMALL.Problem.b=X(:,p1(p2));
196 else
197 size(p1,2)
198 SMALL.Problem.b=X(:,p1);
199
200 end
201
202 lambda=0.9998
203
204 % Use Recursive Least Squares
205 % to Learn overcomplete dictionary
206
207 % Initialising Dictionary structure
208 % Setting Dictionary structure fields (toolbox, name, param, D and time)
209 % to zero values
210
211 SMALL.DL(3)=SMALL_init_DL();
212
213 % Defining fields needed for dictionary learning
214
215 SMALL.DL(3).toolbox = 'SMALL';
216 SMALL.DL(3).name = 'SMALL_rlsdla';
217 SMALL.DL(3).param=struct(...
218 'Edata', Edata,...
219 'initdict', SMALL.Problem.initdict,...
220 'dictsize', SMALL.Problem.p,...
221 'forgettingMode', 'FIX',...
222 'forgettingFactor', lambda);
223
224 % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
225 %
226 % SMALL.DL(3).param=struct(...
227 % 'D', SMALL.Problem.initdict,...
228 % 'K', SMALL.Problem.p,...
229 % 'lambda', 2,...
230 % 'iter', 200,...
231 % 'mode', 3, ...
232 % 'modeD', 0);
233
234 % Learn the dictionary
235
236 SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3));
237 %SMALL.DL(3).D(:,1)=SMALL.DL(1).D(:,1);
238 %
239 % % Set SMALL.Problem.A dictionary
240 % % (backward compatiblity with SPARCO: solver structure communicate
241 % % only with Problem structure, ie no direct communication between DL and
242 % % solver structures)
243 %
244 %
245 %
246 % %%
247 % % Initialising solver structure
248 % % Setting solver structure fields (toolbox, name, param, solution,
249 % % reconstructed and time) to zero values
250 % SMALL.Problem.A = SMALL.DL(1).D;
251 % SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
252 % maxatoms=5;
253 % SMALL.solver(3)=SMALL_init_solver;
254 %
255 % % Defining the parameters needed for denoising
256 %
257 % % SMALL.solver(3).toolbox='SPAMS';
258 % % SMALL.solver(3).name='mexLasso';
259 % % SMALL.solver(3).param=struct(...
260 % % 'mode', 1, ...
261 % % 'lambda',Edata*Edata,...
262 % % 'L', maxatoms);
263 % % % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
264 % % % but backward compatible with KSVD definition of denoising
265 % %
266 % % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3));
267 % SMALL.solver(3).toolbox='SMALL';
268 % SMALL.solver(3).name='SMALL_cgp';
269 % SMALL.solver(3).param=sprintf('%d, %.2f', maxatoms, sqrt(Edata));
270 % % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
271 % % but backward compatible with KSVD definition of denoising
272 %
273 % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3));
274
275 % %%
276 % % Use RLS-DLA
277 %
278 % % Initialising Dictionary structure
279 % % Setting Dictionary structure fields (toolbox, name, param, D and time)
280 % % to zero values
281 %
282 % SMALL.DL(3)=SMALL_init_DL();
283 %
284 % % Defining fields needed for dictionary learning
285 %
286 % SMALL.DL(3).toolbox = 'mpv2';
287 % SMALL.DL(3).name = 'rlsdla';
288 %
289 % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
290 %
291 % SMALL.DL(3).param=struct(...
292 % 'D', SMALL.Problem.initdict,...
293 % 'K', SMALL.Problem.p,...
294 % 'abs', Edata*Edata,...
295 % 'lambda', 0.995,...
296 % 'iternum',1);
297 %
298 % % Learn the dictionary
299 %
300 % SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3));
301 %
302 % % Set SMALL.Problem.A dictionary
303 % % (backward compatiblity with SPARCO: solver structure communicate
304 % % only with Problem structure, ie no direct communication between DL and
305 % % solver structures)
306 %
307 %
308
309 %%
310 % Initialising solver structure
311 % Setting solver structure fields (toolbox, name, param, solution,
312 % reconstructed and time) to zero values
313 %SMALL.DL(3).D(:,225:256)=0;
314 SMALL.Problem.A = SMALL.DL(3).D;
315 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
316 %maxatoms=32;
317 SMALL.solver(3)=SMALL_init_solver;
318
319 % Defining the parameters needed for denoising
320
321 % SMALL.solver(3).toolbox='SPAMS';
322 % SMALL.solver(3).name='mexLasso';
323 % SMALL.solver(3).param=struct(...
324 % 'mode', 1, ...
325 % 'lambda',Edata*Edata,...
326 % 'L', maxatoms);
327 % % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
328 % % but backward compatible with KSVD definition of denoising
329 %
330 % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3));
331
332 % Initialising solver structure
333 % Setting solver structure fields (toolbox, name, param, solution,
334 % reconstructed and time) to zero values
335
336 SMALL.solver(3)=SMALL_init_solver;
337
338 % Defining the parameters needed for image denoising
339
340 SMALL.solver(3).toolbox='ompbox';
341 SMALL.solver(3).name='omp2';
342 SMALL.solver(3).param=struct(...
343 'epsilon',Edata,...
344 'maxatoms', maxatoms);
345 % SMALL.solver(3).toolbox='SPAMS';
346 % SMALL.solver(3).name='mexLasso';
347 % SMALL.solver(3).param=struct(...
348 % 'mode', 2, ...
349 % 'lambda',40,...
350 % 'L', maxatoms);
351
352 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
353 % but backward compatible with KSVD definition of denoising
354
355 SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3));
356 % Plot results and save midi files
357 SMALL.solver(3).reconstructed.psnr
358 % show results %
359
360 SMALL_ImgDeNoiseResult(SMALL);
361 end
362 results(noise_ind,im_num).psnr.ksvd=SMALL.solver(1).reconstructed.psnr;
363 results(noise_ind,im_num).psnr.odct=SMALL.solver(2).reconstructed.psnr;
364 results(noise_ind,im_num).psnr.rlsdla=SMALL.solver(3).reconstructed.psnr;
365 results(noise_ind,im_num).vmrse.ksvd=SMALL.solver(1).reconstructed.vmrse;
366 results(noise_ind,im_num).vmrse.odct=SMALL.solver(2).reconstructed.vmrse;
367 results(noise_ind,im_num).vmrse.rlsdla=SMALL.solver(3).reconstructed.vmrse;
368 results(noise_ind,im_num).ssim.ksvd=SMALL.solver(1).reconstructed.ssim;
369 results(noise_ind,im_num).ssim.odct=SMALL.solver(2).reconstructed.ssim;
370 results(noise_ind,im_num).ssim.rlsdla=SMALL.solver(3).reconstructed.ssim;
371
372 results(noise_ind,im_num).time.ksvd=SMALL.solver(1).time+SMALL.DL(1).time;
373 results(noise_ind,im_num).time.rlsdla.time=SMALL.solver(3).time+SMALL.DL(3).time;
374 %clear SMALL;
375 end
376 end
377 save results.mat results