comparison examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsSPAMS.m @ 6:f72603404233

(none)
author idamnjanovic
date Mon, 22 Mar 2010 10:45:01 +0000
parents
children cd55209c69e1
comparison
equal deleted inserted replaced
5:f44689e95ea4 6:f72603404233
1 %% DICTIONARY LEARNING FOR IMAGE DENOISING
2 % This file contains an example of how SMALLbox can be used to test different
3 % dictionary learning techniques in Image Denoising problem.
4 % It calls generateImageDenoiseProblem that will let you to choose image,
5 % add noise and use noisy image to generate training set for dictionary
6 % learning.
7 % Three dictionary learning techniques were compared:
8 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
9 % Implementation of the K-SVD Algorithm using Batch Orthogonal
10 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
11 % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse
12 % Dictionaries for Sparse Signal Approximation", Technical
13 % Report - CS, Technion, June 2009.
14 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
15 % Dictionary Learning for Sparse Coding. International
16 % Conference on Machine Learning,Montreal, Canada, 2009
17 %
18 %
19 % Ivan Damnjanovic 2010
20 %%
21
22 clear;
23
24 % If you want to load the image outside of generateImageDenoiseProblem
25 % function uncomment following lines. This can be useful if you want to
26 % denoise more then one image for example.
27
28 % TMPpath=pwd;
29 % FS=filesep;
30 % [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
31 % cd([pathstr1,FS,'data',FS,'images']);
32 % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
33 % [pathstr, name, ext, versn] = fileparts(filename);
34 % test_image = imread(filename);
35 % test_image = double(test_image);
36 % cd(TMPpath);
37 % SMALL.Problem.name=name;
38
39
40 % Defining Image Denoising Problem as Dictionary Learning
41 % Problem. As an input we set the number of training patches.
42
43 SMALL.Problem = generateImageDenoiseProblem('', 40000);
44
45
46 %%
47 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
48
49 % Initialising Dictionary structure
50 % Setting Dictionary structure fields (toolbox, name, param, D and time)
51 % to zero values
52
53 SMALL.DL(1)=SMALL_init_DL();
54
55 % Defining the parameters needed for dictionary learning
56
57 SMALL.DL(1).toolbox = 'KSVD';
58 SMALL.DL(1).name = 'ksvd';
59
60 % Defining the parameters for KSVD
61 % In this example we are learning 256 atoms in 20 iterations, so that
62 % every patch in the training set can be represented with target error in
63 % L2-norm (EData)
64 % Type help ksvd in MATLAB prompt for more options.
65
66 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
67 SMALL.DL(1).param=struct(...
68 'Edata', Edata,...
69 'initdict', SMALL.Problem.initdict,...
70 'dictsize', SMALL.Problem.p,...
71 'iternum', 20,...
72 'memusage', 'high');
73
74 % Learn the dictionary
75
76 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
77
78 % Set SMALL.Problem.A dictionary
79 % (backward compatiblity with SPARCO: solver structure communicate
80 % only with Problem structure, ie no direct communication between DL and
81 % solver structures)
82
83 SMALL.Problem.A = SMALL.DL(1).D;
84
85
86 %%
87 % Initialising solver structure
88 % Setting solver structure fields (toolbox, name, param, solution,
89 % reconstructed and time) to zero values
90
91 SMALL.solver(1)=SMALL_init_solver;
92
93 % Defining the parameters needed for image denoising
94
95 SMALL.solver(1).toolbox='ompbox';
96 SMALL.solver(1).name='ompdenoise';
97
98 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
99 % but backward compatible with KSVD definition of denoising
100
101 SMALL.solver(1)=SMALL_denoise(SMALL.Problem, SMALL.solver(1));
102
103 %%
104 % Use KSVDS Dictionary Learning Algorithm to denoise image
105
106 % Initialising solver structure
107 % Setting solver structure fields (toolbox, name, param, solution,
108 % reconstructed and time) to zero values
109
110 SMALL.DL(2)=SMALL_init_DL();
111
112 % Defining the parameters needed for dictionary learning
113
114 SMALL.DL(2).toolbox = 'KSVDS';
115 SMALL.DL(2).name = 'ksvds';
116
117 % Defining the parameters for KSVDS
118 % In this example we are learning 256 atoms in 20 iterations, so that
119 % every patch in the training set can be represented with target error in
120 % L2-norm (EDataS). We also impose "double sparsity" - dictionary itself
121 % has to be sparse in the given base dictionary (Tdict - number of
122 % nonzero elements per atom).
123 % Type help ksvds in MATLAB prompt for more options.
124
125 EdataS=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
126 SMALL.DL(2).param=struct(...
127 'Edata', EdataS, ...
128 'Tdict', 6,...
129 'stepsize', 1,...
130 'dictsize', SMALL.Problem.p,...
131 'iternum', 20,...
132 'memusage', 'high');
133 SMALL.DL(2).param.initA = speye(SMALL.Problem.p);
134 SMALL.DL(2).param.basedict{1} = odctdict(8,16);
135 SMALL.DL(2).param.basedict{2} = odctdict(8,16);
136
137 % Learn the dictionary
138
139 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
140
141 % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary
142 % (backward compatiblity with SPARCO: solver structure communicate
143 % only with Problem structure, ie no direct communication between DL and
144 % solver structures)
145
146 SMALL.Problem.A = SMALL.DL(2).D;
147 SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1};
148 SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2};
149
150 %%
151 % Initialising solver structure
152 % Setting solver structure fields (toolbox, name, param, solution,
153 % reconstructed and time) to zero values
154
155 SMALL.solver(2)=SMALL_init_solver;
156
157 % Defining the parameters needed for image denoising
158
159 SMALL.solver(2).toolbox='ompsbox';
160 SMALL.solver(2).name='ompsdenoise';
161
162 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
163 % but backward compatible with KSVD definition of denoising
164 % Pay attention that since implicit base dictionary is used, denoising
165 % can be much faster then using explicit dictionary in KSVD example.
166
167 SMALL.solver(2)=SMALL_denoise(SMALL.Problem, SMALL.solver(2));
168
169 %%
170 % Use SPAMS Online Dictionary Learning Algorithm
171 % to Learn overcomplete dictionary (Julien Mairal 2009)
172 % (If you have not installed SPAMS please comment the following two cells)
173
174 % Initialising Dictionary structure
175 % Setting Dictionary structure fields (toolbox, name, param, D and time)
176 % to zero values
177
178 SMALL.DL(3)=SMALL_init_DL();
179
180 % Defining fields needed for dictionary learning
181
182 SMALL.DL(3).toolbox = 'SPAMS';
183 SMALL.DL(3).name = 'mexTrainDL';
184
185 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
186
187 SMALL.DL(3).param=struct(...
188 'D', SMALL.Problem.initdict,...
189 'K', SMALL.Problem.p,...
190 'lambda', 2,...
191 'iter', 200,...
192 'mode', 3, ...
193 'modeD', 0);
194
195 % Learn the dictionary
196
197 SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3));
198
199 % Set SMALL.Problem.A dictionary
200 % (backward compatiblity with SPARCO: solver structure communicate
201 % only with Problem structure, ie no direct communication between DL and
202 % solver structures)
203
204 SMALL.Problem.A = SMALL.DL(3).D;
205
206
207 %%
208 % Initialising solver structure
209 % Setting solver structure fields (toolbox, name, param, solution,
210 % reconstructed and time) to zero values
211
212 SMALL.solver(3)=SMALL_init_solver;
213
214 % Defining the parameters needed for denoising
215
216 SMALL.solver(3).toolbox='ompbox';
217 SMALL.solver(3).name='ompdenoise';
218
219 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
220 % but backward compatible with KSVD definition of denoising
221
222 SMALL.solver(3)=SMALL_denoise(SMALL.Problem, SMALL.solver(3));
223
224 %%
225 % Plot results and save midi files
226
227 % show results %
228
229 SMALL_ImgDeNoiseResult(SMALL);