ivan@128
|
1 %% Dictionary Learning for Image Denoising - KSVD vs KSVDS vs SPAMS
|
ivan@128
|
2 %
|
ivan@128
|
3 % *WARNING!* You should have SPAMS in your search path in order for this
|
ivan@128
|
4 % script to work.Due to licensing issues SPAMS can not be automatically
|
ivan@128
|
5 % provided in SMALLbox (http://www.di.ens.fr/willow/SPAMS/downloads.html).
|
ivan@128
|
6 %
|
idamnjanovic@6
|
7 % This file contains an example of how SMALLbox can be used to test different
|
idamnjanovic@6
|
8 % dictionary learning techniques in Image Denoising problem.
|
idamnjanovic@6
|
9 % It calls generateImageDenoiseProblem that will let you to choose image,
|
idamnjanovic@6
|
10 % add noise and use noisy image to generate training set for dictionary
|
idamnjanovic@6
|
11 % learning.
|
idamnjanovic@6
|
12 % Three dictionary learning techniques were compared:
|
idamnjanovic@6
|
13 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
|
idamnjanovic@6
|
14 % Implementation of the K-SVD Algorithm using Batch Orthogonal
|
idamnjanovic@6
|
15 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
|
idamnjanovic@6
|
16 % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse
|
idamnjanovic@6
|
17 % Dictionaries for Sparse Signal Approximation", Technical
|
idamnjanovic@6
|
18 % Report - CS, Technion, June 2009.
|
idamnjanovic@6
|
19 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
|
idamnjanovic@6
|
20 % Dictionary Learning for Sparse Coding. International
|
idamnjanovic@6
|
21 % Conference on Machine Learning,Montreal, Canada, 2009
|
idamnjanovic@6
|
22 %
|
ivan@107
|
23
|
ivan@107
|
24 %
|
ivan@107
|
25 % Centre for Digital Music, Queen Mary, University of London.
|
ivan@107
|
26 % This file copyright 2009 Ivan Damnjanovic.
|
ivan@107
|
27 %
|
ivan@107
|
28 % This program is free software; you can redistribute it and/or
|
ivan@107
|
29 % modify it under the terms of the GNU General Public License as
|
ivan@107
|
30 % published by the Free Software Foundation; either version 2 of the
|
ivan@107
|
31 % License, or (at your option) any later version. See the file
|
ivan@107
|
32 % COPYING included with this distribution for more information.
|
idamnjanovic@6
|
33 %
|
idamnjanovic@6
|
34 %%
|
idamnjanovic@6
|
35
|
idamnjanovic@6
|
36 clear;
|
idamnjanovic@6
|
37
|
idamnjanovic@6
|
38 % If you want to load the image outside of generateImageDenoiseProblem
|
idamnjanovic@6
|
39 % function uncomment following lines. This can be useful if you want to
|
idamnjanovic@6
|
40 % denoise more then one image for example.
|
idamnjanovic@6
|
41
|
idamnjanovic@6
|
42 % TMPpath=pwd;
|
idamnjanovic@6
|
43 % FS=filesep;
|
idamnjanovic@6
|
44 % [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
|
idamnjanovic@6
|
45 % cd([pathstr1,FS,'data',FS,'images']);
|
idamnjanovic@6
|
46 % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
|
idamnjanovic@6
|
47 % [pathstr, name, ext, versn] = fileparts(filename);
|
idamnjanovic@6
|
48 % test_image = imread(filename);
|
idamnjanovic@6
|
49 % test_image = double(test_image);
|
idamnjanovic@6
|
50 % cd(TMPpath);
|
idamnjanovic@6
|
51 % SMALL.Problem.name=name;
|
idamnjanovic@6
|
52
|
idamnjanovic@6
|
53
|
idamnjanovic@6
|
54 % Defining Image Denoising Problem as Dictionary Learning
|
idamnjanovic@6
|
55 % Problem. As an input we set the number of training patches.
|
idamnjanovic@6
|
56
|
idamnjanovic@6
|
57 SMALL.Problem = generateImageDenoiseProblem('', 40000);
|
idamnjanovic@6
|
58
|
idamnjanovic@6
|
59
|
idamnjanovic@6
|
60 %%
|
idamnjanovic@6
|
61 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
|
idamnjanovic@6
|
62
|
idamnjanovic@6
|
63 % Initialising Dictionary structure
|
idamnjanovic@6
|
64 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
idamnjanovic@6
|
65 % to zero values
|
idamnjanovic@6
|
66
|
idamnjanovic@6
|
67 SMALL.DL(1)=SMALL_init_DL();
|
idamnjanovic@6
|
68
|
idamnjanovic@6
|
69 % Defining the parameters needed for dictionary learning
|
idamnjanovic@6
|
70
|
idamnjanovic@6
|
71 SMALL.DL(1).toolbox = 'KSVD';
|
idamnjanovic@6
|
72 SMALL.DL(1).name = 'ksvd';
|
idamnjanovic@6
|
73
|
idamnjanovic@6
|
74 % Defining the parameters for KSVD
|
idamnjanovic@6
|
75 % In this example we are learning 256 atoms in 20 iterations, so that
|
idamnjanovic@6
|
76 % every patch in the training set can be represented with target error in
|
idamnjanovic@6
|
77 % L2-norm (EData)
|
idamnjanovic@6
|
78 % Type help ksvd in MATLAB prompt for more options.
|
idamnjanovic@6
|
79
|
idamnjanovic@6
|
80 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
|
ivan@107
|
81 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
|
ivan@107
|
82
|
idamnjanovic@6
|
83 SMALL.DL(1).param=struct(...
|
idamnjanovic@6
|
84 'Edata', Edata,...
|
idamnjanovic@6
|
85 'initdict', SMALL.Problem.initdict,...
|
idamnjanovic@6
|
86 'dictsize', SMALL.Problem.p,...
|
idamnjanovic@6
|
87 'iternum', 20,...
|
idamnjanovic@6
|
88 'memusage', 'high');
|
idamnjanovic@6
|
89
|
idamnjanovic@6
|
90 % Learn the dictionary
|
idamnjanovic@6
|
91
|
idamnjanovic@6
|
92 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
|
idamnjanovic@6
|
93
|
idamnjanovic@6
|
94 % Set SMALL.Problem.A dictionary
|
idamnjanovic@6
|
95 % (backward compatiblity with SPARCO: solver structure communicate
|
idamnjanovic@6
|
96 % only with Problem structure, ie no direct communication between DL and
|
idamnjanovic@6
|
97 % solver structures)
|
idamnjanovic@6
|
98
|
idamnjanovic@6
|
99 SMALL.Problem.A = SMALL.DL(1).D;
|
ivan@107
|
100 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
|
idamnjanovic@6
|
101
|
idamnjanovic@6
|
102 %%
|
idamnjanovic@6
|
103 % Initialising solver structure
|
idamnjanovic@6
|
104 % Setting solver structure fields (toolbox, name, param, solution,
|
idamnjanovic@6
|
105 % reconstructed and time) to zero values
|
idamnjanovic@6
|
106
|
idamnjanovic@6
|
107 SMALL.solver(1)=SMALL_init_solver;
|
idamnjanovic@6
|
108
|
idamnjanovic@6
|
109 % Defining the parameters needed for image denoising
|
idamnjanovic@6
|
110
|
idamnjanovic@6
|
111 SMALL.solver(1).toolbox='ompbox';
|
ivan@107
|
112 SMALL.solver(1).name='omp2';
|
ivan@107
|
113 SMALL.solver(1).param=struct(...
|
ivan@107
|
114 'epsilon',Edata,...
|
ivan@107
|
115 'maxatoms', maxatoms);
|
idamnjanovic@6
|
116
|
ivan@107
|
117 % Denoising the image - find the sparse solution in the learned
|
ivan@107
|
118 % dictionary for all patches in the image and the end it uses
|
ivan@107
|
119 % reconstruction function to reconstruct the patches and put them into a
|
ivan@107
|
120 % denoised image
|
idamnjanovic@6
|
121
|
ivan@107
|
122 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
|
ivan@107
|
123
|
ivan@107
|
124 % Show PSNR after reconstruction
|
ivan@107
|
125
|
ivan@107
|
126 SMALL.solver(1).reconstructed.psnr
|
idamnjanovic@6
|
127
|
idamnjanovic@6
|
128 %%
|
idamnjanovic@6
|
129 % Use KSVDS Dictionary Learning Algorithm to denoise image
|
idamnjanovic@6
|
130
|
idamnjanovic@6
|
131 % Initialising solver structure
|
idamnjanovic@6
|
132 % Setting solver structure fields (toolbox, name, param, solution,
|
idamnjanovic@6
|
133 % reconstructed and time) to zero values
|
idamnjanovic@6
|
134
|
idamnjanovic@6
|
135 SMALL.DL(2)=SMALL_init_DL();
|
idamnjanovic@6
|
136
|
idamnjanovic@6
|
137 % Defining the parameters needed for dictionary learning
|
idamnjanovic@6
|
138
|
idamnjanovic@6
|
139 SMALL.DL(2).toolbox = 'KSVDS';
|
idamnjanovic@6
|
140 SMALL.DL(2).name = 'ksvds';
|
idamnjanovic@6
|
141
|
idamnjanovic@6
|
142 % Defining the parameters for KSVDS
|
idamnjanovic@6
|
143 % In this example we are learning 256 atoms in 20 iterations, so that
|
idamnjanovic@6
|
144 % every patch in the training set can be represented with target error in
|
idamnjanovic@6
|
145 % L2-norm (EDataS). We also impose "double sparsity" - dictionary itself
|
idamnjanovic@6
|
146 % has to be sparse in the given base dictionary (Tdict - number of
|
idamnjanovic@6
|
147 % nonzero elements per atom).
|
idamnjanovic@6
|
148 % Type help ksvds in MATLAB prompt for more options.
|
idamnjanovic@6
|
149
|
idamnjanovic@6
|
150 EdataS=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
|
idamnjanovic@6
|
151 SMALL.DL(2).param=struct(...
|
idamnjanovic@6
|
152 'Edata', EdataS, ...
|
idamnjanovic@6
|
153 'Tdict', 6,...
|
idamnjanovic@6
|
154 'stepsize', 1,...
|
idamnjanovic@6
|
155 'dictsize', SMALL.Problem.p,...
|
idamnjanovic@6
|
156 'iternum', 20,...
|
idamnjanovic@6
|
157 'memusage', 'high');
|
idamnjanovic@6
|
158 SMALL.DL(2).param.initA = speye(SMALL.Problem.p);
|
idamnjanovic@6
|
159 SMALL.DL(2).param.basedict{1} = odctdict(8,16);
|
idamnjanovic@6
|
160 SMALL.DL(2).param.basedict{2} = odctdict(8,16);
|
idamnjanovic@6
|
161
|
idamnjanovic@6
|
162 % Learn the dictionary
|
idamnjanovic@6
|
163
|
idamnjanovic@6
|
164 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
|
idamnjanovic@6
|
165
|
idamnjanovic@6
|
166 % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary
|
idamnjanovic@6
|
167 % (backward compatiblity with SPARCO: solver structure communicate
|
idamnjanovic@6
|
168 % only with Problem structure, ie no direct communication between DL and
|
idamnjanovic@6
|
169 % solver structures)
|
idamnjanovic@6
|
170
|
idamnjanovic@6
|
171 SMALL.Problem.A = SMALL.DL(2).D;
|
idamnjanovic@6
|
172 SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1};
|
idamnjanovic@6
|
173 SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2};
|
idamnjanovic@6
|
174
|
ivan@107
|
175 % Setting up reconstruction function
|
ivan@107
|
176
|
ivan@107
|
177 SparseDict=1;
|
ivan@107
|
178 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem, SparseDict);
|
ivan@107
|
179
|
idamnjanovic@6
|
180 % Initialising solver structure
|
idamnjanovic@6
|
181 % Setting solver structure fields (toolbox, name, param, solution,
|
idamnjanovic@6
|
182 % reconstructed and time) to zero values
|
idamnjanovic@6
|
183
|
idamnjanovic@6
|
184 SMALL.solver(2)=SMALL_init_solver;
|
idamnjanovic@6
|
185
|
idamnjanovic@6
|
186 % Defining the parameters needed for image denoising
|
idamnjanovic@6
|
187
|
idamnjanovic@6
|
188 SMALL.solver(2).toolbox='ompsbox';
|
ivan@107
|
189 SMALL.solver(2).name='omps2';
|
ivan@107
|
190 SMALL.solver(2).param=struct(...
|
ivan@107
|
191 'epsilon',Edata,...
|
ivan@107
|
192 'maxatoms', maxatoms);
|
idamnjanovic@6
|
193
|
ivan@107
|
194 % Denoising the image - find the sparse solution in the learned
|
ivan@107
|
195 % dictionary for all patches in the image and the end it uses
|
ivan@107
|
196 % reconstruction function to reconstruct the patches and put them into a
|
ivan@107
|
197 % denoised image
|
idamnjanovic@6
|
198
|
ivan@107
|
199 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
|
idamnjanovic@6
|
200
|
ivan@107
|
201 %%
|
ivan@107
|
202 % Use SPAMS Online Dictionary Learning Algorithm
|
ivan@107
|
203 % to Learn overcomplete dictionary (Julien Mairal 2009)
|
ivan@107
|
204 % (If you have not installed SPAMS please comment the following two cells)
|
ivan@107
|
205
|
ivan@107
|
206 % Initialising Dictionary structure
|
ivan@107
|
207 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
ivan@107
|
208 % to zero values
|
ivan@107
|
209
|
ivan@107
|
210 SMALL.DL(3)=SMALL_init_DL();
|
ivan@107
|
211
|
ivan@107
|
212 % Defining fields needed for dictionary learning
|
ivan@107
|
213
|
ivan@107
|
214 SMALL.DL(3).toolbox = 'SPAMS';
|
ivan@107
|
215 SMALL.DL(3).name = 'mexTrainDL';
|
ivan@107
|
216
|
ivan@107
|
217 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
|
ivan@107
|
218
|
ivan@107
|
219 SMALL.DL(3).param=struct(...
|
ivan@107
|
220 'D', SMALL.Problem.initdict,...
|
ivan@107
|
221 'K', SMALL.Problem.p,...
|
ivan@107
|
222 'lambda', 2,...
|
ivan@107
|
223 'iter', 200,...
|
ivan@107
|
224 'mode', 3, ...
|
ivan@107
|
225 'modeD', 0);
|
ivan@107
|
226
|
ivan@107
|
227 % Learn the dictionary
|
ivan@107
|
228
|
ivan@107
|
229 SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3));
|
ivan@107
|
230
|
ivan@107
|
231 % Set SMALL.Problem.A dictionary
|
ivan@107
|
232 % (backward compatiblity with SPARCO: solver structure communicate
|
ivan@107
|
233 % only with Problem structure, ie no direct communication between DL and
|
ivan@107
|
234 % solver structures)
|
ivan@107
|
235
|
ivan@107
|
236 SMALL.Problem.A = SMALL.DL(3).D;
|
ivan@107
|
237
|
ivan@107
|
238 % Setting up reconstruction function
|
ivan@107
|
239
|
ivan@107
|
240 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
|
ivan@107
|
241
|
ivan@107
|
242 % Initialising solver structure
|
ivan@107
|
243 % Setting solver structure fields (toolbox, name, param, solution,
|
ivan@107
|
244 % reconstructed and time) to zero values
|
ivan@107
|
245
|
ivan@107
|
246 SMALL.solver(3)=SMALL_init_solver;
|
ivan@107
|
247
|
ivan@107
|
248 % Defining the parameters needed for image denoising
|
ivan@107
|
249
|
ivan@107
|
250 SMALL.solver(3).toolbox='ompbox';
|
ivan@107
|
251 SMALL.solver(3).name='omp2';
|
ivan@107
|
252 SMALL.solver(3).param=struct(...
|
ivan@107
|
253 'epsilon',Edata,...
|
ivan@107
|
254 'maxatoms', maxatoms);
|
ivan@107
|
255
|
ivan@107
|
256 % Denoising the image - find the sparse solution in the learned
|
ivan@107
|
257 % dictionary for all patches in the image and the end it uses
|
ivan@107
|
258 % reconstruction function to reconstruct the patches and put them into a
|
ivan@107
|
259 % denoised image
|
ivan@107
|
260
|
ivan@107
|
261 SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3));
|
idamnjanovic@6
|
262
|
idamnjanovic@6
|
263 %%
|
idamnjanovic@6
|
264 % Plot results and save midi files
|
idamnjanovic@6
|
265
|
idamnjanovic@6
|
266 % show results %
|
idamnjanovic@6
|
267
|
idamnjanovic@6
|
268 SMALL_ImgDeNoiseResult(SMALL);
|