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