Mercurial > hg > smallbox
comparison examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsRLSDLA.m @ 84:67aae1283973
comments to ksvd vs rls-dla
author | Ivan <ivan.damnjanovic@eecs.qmul.ac.uk> |
---|---|
date | Fri, 01 Apr 2011 14:27:44 +0100 |
parents | 4302a91e6033 |
children | fd1c32cda22c |
comparison
equal
deleted
inserted
replaced
83:4302a91e6033 | 84:67aae1283973 |
---|---|
52 % Defining Image Denoising Problem as Dictionary Learning | 52 % Defining Image Denoising Problem as Dictionary Learning |
53 % Problem. As an input we set the number of training patches. | 53 % Problem. As an input we set the number of training patches. |
54 | 54 |
55 SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind)); | 55 SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind)); |
56 SMALL.Problem.name=int2str(im_num); | 56 SMALL.Problem.name=int2str(im_num); |
57 % results structure is to store all results | |
57 | 58 |
58 results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr; | 59 results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr; |
59 | 60 |
60 %% | 61 %% |
61 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary | 62 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary |
112 SMALL.solver(1).name='omp2'; | 113 SMALL.solver(1).name='omp2'; |
113 SMALL.solver(1).param=struct(... | 114 SMALL.solver(1).param=struct(... |
114 'epsilon',Edata,... | 115 'epsilon',Edata,... |
115 'maxatoms', maxatoms); | 116 'maxatoms', maxatoms); |
116 | 117 |
117 % Denoising the image - SMALL_denoise function is similar to SMALL_solve, | 118 % Denoising the image - find the sparse solution in the learned |
118 % but backward compatible with KSVD definition of denoising | 119 % dictionary for all patches in the image and the end it uses |
120 % reconstruction function to reconstruct the patches and put them into a | |
121 % denoised image | |
119 | 122 |
120 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); | 123 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); |
124 | |
125 % Show PSNR after reconstruction | |
126 | |
121 SMALL.solver(1).reconstructed.psnr | 127 SMALL.solver(1).reconstructed.psnr |
122 %% | 128 |
123 % Use KSVDS Dictionary Learning Algorithm to denoise image | 129 %% |
124 | 130 % For comparison purposes we will denoise image with overcomplete DCT |
125 % Initialising solver structure | 131 % here |
126 % Setting solver structure fields (toolbox, name, param, solution, | 132 % Set SMALL.Problem.A dictionary to be oDCT (i.e. Problem.initdict - |
127 % reconstructed and time) to zero values | 133 % since initial dictionaruy is already set to be oDCT when generating the |
128 % | 134 % denoising problem |
129 % SMALL.DL(2)=SMALL_init_DL(); | |
130 % | |
131 % % Defining the parameters needed for dictionary learning | |
132 % | |
133 % SMALL.DL(2).toolbox = 'KSVDS'; | |
134 % SMALL.DL(2).name = 'ksvds'; | |
135 % | |
136 % % Defining the parameters for KSVDS | |
137 % % In this example we are learning 256 atoms in 20 iterations, so that | |
138 % % every patch in the training set can be represented with target error in | |
139 % % L2-norm (EDataS). We also impose "double sparsity" - dictionary itself | |
140 % % has to be sparse in the given base dictionary (Tdict - number of | |
141 % % nonzero elements per atom). | |
142 % % Type help ksvds in MATLAB prompt for more options. | |
143 % | |
144 % | |
145 % SMALL.DL(2).param=struct(... | |
146 % 'Edata', Edata, ... | |
147 % 'Tdict', 6,... | |
148 % 'stepsize', 1,... | |
149 % 'dictsize', SMALL.Problem.p,... | |
150 % 'iternum', 20,... | |
151 % 'memusage', 'high'); | |
152 % SMALL.DL(2).param.initA = speye(SMALL.Problem.p); | |
153 % SMALL.DL(2).param.basedict{1} = odctdict(8,16); | |
154 % SMALL.DL(2).param.basedict{2} = odctdict(8,16); | |
155 % | |
156 % % Learn the dictionary | |
157 % | |
158 % SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); | |
159 | |
160 % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary | |
161 % (backward compatiblity with SPARCO: solver structure communicate | |
162 % only with Problem structure, ie no direct communication between DL and | |
163 % solver structures) | |
164 | 135 |
165 SMALL.Problem.A = SMALL.Problem.initdict; | 136 SMALL.Problem.A = SMALL.Problem.initdict; |
166 % SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1}; | |
167 % SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2}; | |
168 SMALL.DL(2).D=SMALL.Problem.initdict; | 137 SMALL.DL(2).D=SMALL.Problem.initdict; |
138 | |
139 % Setting up reconstruction function | |
140 | |
169 SparseDict=0; | 141 SparseDict=0; |
170 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem, SparseDict); | 142 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem, SparseDict); |
171 | 143 |
172 %% | |
173 % Initialising solver structure | 144 % Initialising solver structure |
174 % Setting solver structure fields (toolbox, name, param, solution, | 145 % Setting solver structure fields (toolbox, name, param, solution, |
175 % reconstructed and time) to zero values | 146 % reconstructed and time) to zero values |
176 | 147 |
177 SMALL.solver(2)=SMALL_init_solver; | 148 SMALL.solver(2)=SMALL_init_solver; |
182 SMALL.solver(2).name='omp2'; | 153 SMALL.solver(2).name='omp2'; |
183 SMALL.solver(2).param=struct(... | 154 SMALL.solver(2).param=struct(... |
184 'epsilon',Edata,... | 155 'epsilon',Edata,... |
185 'maxatoms', maxatoms); | 156 'maxatoms', maxatoms); |
186 | 157 |
187 % Denoising the image - SMALL_denoise function is similar to SMALL_solve, | 158 % Denoising the image - find the sparse solution in the learned |
188 % but backward compatible with KSVD definition of denoising | 159 % dictionary for all patches in the image and the end it uses |
189 % Pay attention that since implicit base dictionary is used, denoising | 160 % reconstruction function to reconstruct the patches and put them into a |
190 % can be much faster then using explicit dictionary in KSVD example. | 161 % denoised image |
191 | 162 |
192 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); | 163 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); |
193 %% | 164 %% |
194 | 165 % In the b1 field all patches from the image are stored. For RLS-DLA we |
195 for i =1:1 | 166 % will first exclude all the patches that have l2 norm smaller then |
167 % threshold and then take min(40000, number_of_remaining_patches) in | |
168 % ascending order as our training set (SMALL.Problem.b) | |
196 | 169 |
197 X=SMALL.Problem.b1; | 170 X=SMALL.Problem.b1; |
198 X_norm=sqrt(sum(X.^2, 1)); | 171 X_norm=sqrt(sum(X.^2, 1)); |
199 [X_norm_sort, p]=sort(X_norm); | 172 [X_norm_sort, p]=sort(X_norm); |
200 p1=p(X_norm_sort>Edata); | 173 p1=p(X_norm_sort>Edata); |
207 size(p1,2) | 180 size(p1,2) |
208 SMALL.Problem.b=X(:,p1); | 181 SMALL.Problem.b=X(:,p1); |
209 | 182 |
210 end | 183 end |
211 | 184 |
185 % Forgetting factor for RLS-DLA algorithm, in this case we are using | |
186 % fixed value | |
187 | |
212 lambda=0.9998 | 188 lambda=0.9998 |
213 | 189 |
214 % Use Recursive Least Squares | 190 % Use Recursive Least Squares |
215 % to Learn overcomplete dictionary | 191 % to Learn overcomplete dictionary |
216 | 192 |
229 'initdict', SMALL.Problem.initdict,... | 205 'initdict', SMALL.Problem.initdict,... |
230 'dictsize', SMALL.Problem.p,... | 206 'dictsize', SMALL.Problem.p,... |
231 'forgettingMode', 'FIX',... | 207 'forgettingMode', 'FIX',... |
232 'forgettingFactor', lambda); | 208 'forgettingFactor', lambda); |
233 | 209 |
234 % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters. | |
235 % | |
236 % SMALL.DL(3).param=struct(... | |
237 % 'D', SMALL.Problem.initdict,... | |
238 % 'K', SMALL.Problem.p,... | |
239 % 'lambda', 2,... | |
240 % 'iter', 200,... | |
241 % 'mode', 3, ... | |
242 % 'modeD', 0); | |
243 | |
244 % Learn the dictionary | |
245 | 210 |
246 SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3)); | 211 SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3)); |
247 %SMALL.DL(3).D(:,1)=SMALL.DL(1).D(:,1); | 212 |
248 % | |
249 % % Set SMALL.Problem.A dictionary | |
250 % % (backward compatiblity with SPARCO: solver structure communicate | |
251 % % only with Problem structure, ie no direct communication between DL and | |
252 % % solver structures) | |
253 % | |
254 % | |
255 % | |
256 % %% | |
257 % % Initialising solver structure | |
258 % % Setting solver structure fields (toolbox, name, param, solution, | |
259 % % reconstructed and time) to zero values | |
260 % SMALL.Problem.A = SMALL.DL(1).D; | |
261 % SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); | |
262 % maxatoms=5; | |
263 % SMALL.solver(3)=SMALL_init_solver; | |
264 % | |
265 % % Defining the parameters needed for denoising | |
266 % | |
267 % % SMALL.solver(3).toolbox='SPAMS'; | |
268 % % SMALL.solver(3).name='mexLasso'; | |
269 % % SMALL.solver(3).param=struct(... | |
270 % % 'mode', 1, ... | |
271 % % 'lambda',Edata*Edata,... | |
272 % % 'L', maxatoms); | |
273 % % % Denoising the image - SMALL_denoise function is similar to SMALL_solve, | |
274 % % % but backward compatible with KSVD definition of denoising | |
275 % % | |
276 % % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); | |
277 % SMALL.solver(3).toolbox='SMALL'; | |
278 % SMALL.solver(3).name='SMALL_cgp'; | |
279 % SMALL.solver(3).param=sprintf('%d, %.2f', maxatoms, sqrt(Edata)); | |
280 % % Denoising the image - SMALL_denoise function is similar to SMALL_solve, | |
281 % % but backward compatible with KSVD definition of denoising | |
282 % | |
283 % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); | |
284 | |
285 % %% | |
286 % % Use RLS-DLA | |
287 % | |
288 % % Initialising Dictionary structure | |
289 % % Setting Dictionary structure fields (toolbox, name, param, D and time) | |
290 % % to zero values | |
291 % | |
292 % SMALL.DL(3)=SMALL_init_DL(); | |
293 % | |
294 % % Defining fields needed for dictionary learning | |
295 % | |
296 % SMALL.DL(3).toolbox = 'mpv2'; | |
297 % SMALL.DL(3).name = 'rlsdla'; | |
298 % | |
299 % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters. | |
300 % | |
301 % SMALL.DL(3).param=struct(... | |
302 % 'D', SMALL.Problem.initdict,... | |
303 % 'K', SMALL.Problem.p,... | |
304 % 'abs', Edata*Edata,... | |
305 % 'lambda', 0.995,... | |
306 % 'iternum',1); | |
307 % | |
308 % % Learn the dictionary | |
309 % | |
310 % SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3)); | |
311 % | |
312 % % Set SMALL.Problem.A dictionary | |
313 % % (backward compatiblity with SPARCO: solver structure communicate | |
314 % % only with Problem structure, ie no direct communication between DL and | |
315 % % solver structures) | |
316 % | |
317 % | |
318 | |
319 %% | |
320 % Initialising solver structure | 213 % Initialising solver structure |
321 % Setting solver structure fields (toolbox, name, param, solution, | 214 % Setting solver structure fields (toolbox, name, param, solution, |
322 % reconstructed and time) to zero values | 215 % reconstructed and time) to zero values |
323 %SMALL.DL(3).D(:,225:256)=0; | 216 |
324 SMALL.Problem.A = SMALL.DL(3).D; | 217 SMALL.Problem.A = SMALL.DL(3).D; |
325 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); | 218 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); |
326 %maxatoms=32; | |
327 SMALL.solver(3)=SMALL_init_solver; | |
328 | |
329 % Defining the parameters needed for denoising | |
330 | |
331 % SMALL.solver(3).toolbox='SPAMS'; | |
332 % SMALL.solver(3).name='mexLasso'; | |
333 % SMALL.solver(3).param=struct(... | |
334 % 'mode', 1, ... | |
335 % 'lambda',Edata*Edata,... | |
336 % 'L', maxatoms); | |
337 % % Denoising the image - SMALL_denoise function is similar to SMALL_solve, | |
338 % % but backward compatible with KSVD definition of denoising | |
339 % | |
340 % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); | |
341 | |
342 % Initialising solver structure | |
343 % Setting solver structure fields (toolbox, name, param, solution, | |
344 % reconstructed and time) to zero values | |
345 | 219 |
346 SMALL.solver(3)=SMALL_init_solver; | 220 SMALL.solver(3)=SMALL_init_solver; |
347 | 221 |
348 % Defining the parameters needed for image denoising | 222 % Defining the parameters needed for image denoising |
349 | 223 |
350 SMALL.solver(3).toolbox='ompbox'; | 224 SMALL.solver(3).toolbox='ompbox'; |
351 SMALL.solver(3).name='omp2'; | 225 SMALL.solver(3).name='omp2'; |
352 SMALL.solver(3).param=struct(... | 226 SMALL.solver(3).param=struct(... |
353 'epsilon',Edata,... | 227 'epsilon',Edata,... |
354 'maxatoms', maxatoms); | 228 'maxatoms', maxatoms); |
355 % SMALL.solver(3).toolbox='SPAMS'; | 229 |
356 % SMALL.solver(3).name='mexLasso'; | |
357 % SMALL.solver(3).param=struct(... | |
358 % 'mode', 2, ... | |
359 % 'lambda',40,... | |
360 % 'L', maxatoms); | |
361 | |
362 % Denoising the image - SMALL_denoise function is similar to SMALL_solve, | |
363 % but backward compatible with KSVD definition of denoising | |
364 | 230 |
365 SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); | 231 SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); |
366 % Plot results and save midi files | 232 |
367 SMALL.solver(3).reconstructed.psnr | 233 SMALL.solver(3).reconstructed.psnr |
234 | |
235 | |
368 % show results % | 236 % show results % |
369 | 237 |
370 SMALL_ImgDeNoiseResult(SMALL); | 238 SMALL_ImgDeNoiseResult(SMALL); |
371 end | 239 |
372 results(noise_ind,im_num).psnr.ksvd=SMALL.solver(1).reconstructed.psnr; | 240 results(noise_ind,im_num).psnr.ksvd=SMALL.solver(1).reconstructed.psnr; |
373 results(noise_ind,im_num).psnr.odct=SMALL.solver(2).reconstructed.psnr; | 241 results(noise_ind,im_num).psnr.odct=SMALL.solver(2).reconstructed.psnr; |
374 results(noise_ind,im_num).psnr.rlsdla=SMALL.solver(3).reconstructed.psnr; | 242 results(noise_ind,im_num).psnr.rlsdla=SMALL.solver(3).reconstructed.psnr; |
375 results(noise_ind,im_num).vmrse.ksvd=SMALL.solver(1).reconstructed.vmrse; | 243 results(noise_ind,im_num).vmrse.ksvd=SMALL.solver(1).reconstructed.vmrse; |
376 results(noise_ind,im_num).vmrse.odct=SMALL.solver(2).reconstructed.vmrse; | 244 results(noise_ind,im_num).vmrse.odct=SMALL.solver(2).reconstructed.vmrse; |