comparison examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsRLSDLAvsTwoStepMOD.m @ 164:4205744092e6 release_1.9

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