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