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