comparison examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsSKSVD.m @ 236:5f4e47b78f2b ver_2.0_alpha1

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