comparison examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsTwoStepKSVD.m @ 153:af307f247ac7 ivand_dev

Example scripts for Two Step Dictionary Learning - Image Denoising experiments.
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Fri, 29 Jul 2011 12:35:52 +0100
parents
children f42aa8bcb82f
comparison
equal deleted inserted replaced
152:485747bf39e0 153:af307f247ac7
1 %% Dictionary Learning for Image Denoising - KSVD vs Recursive Least Squares
2 %
3 % This file contains an example of how SMALLbox can be used to test different
4 % dictionary learning techniques in Image Denoising problem.
5 % It calls generateImageDenoiseProblem that will let you to choose image,
6 % add noise and use noisy image to generate training set for dictionary
7 % learning.
8 % Two dictionary learning techniques were compared:
9 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
10 % Implementation of the K-SVD Algorithm using Batch Orthogonal
11 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
12 % - RLS-DLA - Skretting, K.; Engan, K.; , "Recursive Least Squares
13 % Dictionary Learning Algorithm," Signal Processing, IEEE Transactions on,
14 % vol.58, no.4, pp.2121-2130, April 2010
15 %
16
17
18 % Centre for Digital Music, Queen Mary, University of London.
19 % This file copyright 2011 Ivan Damnjanovic.
20 %
21 % This program is free software; you can redistribute it and/or
22 % modify it under the terms of the GNU General Public License as
23 % published by the Free Software Foundation; either version 2 of the
24 % License, or (at your option) any later version. See the file
25 % COPYING included with this distribution for more information.
26 %
27 %%
28
29
30
31 % If you want to load the image outside of generateImageDenoiseProblem
32 % function uncomment following lines. This can be useful if you want to
33 % denoise more then one image for example.
34 % Here we are loading test_image.mat that contains structure with 5 images : lena,
35 % barbara,boat, house and peppers.
36 clear;
37 TMPpath=pwd;
38 FS=filesep;
39 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
40 cd([pathstr1,FS,'data',FS,'images']);
41 load('test_image.mat');
42 cd(TMPpath);
43
44 % Deffining the noise levels that we want to test
45
46 noise_level=[10 20 25 50 100];
47
48 % Here we loop through different noise levels and images
49
50 for noise_ind=1:1
51 for im_num=1:1
52
53 % Defining Image Denoising Problem as Dictionary Learning
54 % Problem. As an input we set the number of training patches.
55
56 SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind));
57 SMALL.Problem.name=int2str(im_num);
58
59 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
60 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
61
62 % results structure is to store all results
63
64 results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr;
65
66 %%
67 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
68 % Ron Rubinstein implementation
69
70 % Initialising Dictionary structure
71 % Setting Dictionary structure fields (toolbox, name, param, D and time)
72 % to zero values
73
74 SMALL.DL(1)=SMALL_init_DL();
75
76 % Defining the parameters needed for dictionary learning
77
78 SMALL.DL(1).toolbox = 'KSVD';
79 SMALL.DL(1).name = 'ksvd';
80
81 % Defining the parameters for KSVD
82 % In this example we are learning 256 atoms in 20 iterations, so that
83 % every patch in the training set can be represented with target error in
84 % L2-norm (Edata)
85 % Type help ksvd in MATLAB prompt for more options.
86
87
88 SMALL.DL(1).param=struct(...
89 'Edata', Edata,...
90 'initdict', SMALL.Problem.initdict,...
91 'dictsize', SMALL.Problem.p,...
92 'exact', 1, ...
93 'iternum', 20,...
94 'memusage', 'high');
95
96 % Learn the dictionary
97
98 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
99
100 % Set SMALL.Problem.A dictionary
101 % (backward compatiblity with SPARCO: solver structure communicate
102 % only with Problem structure, ie no direct communication between DL and
103 % solver structures)
104
105 SMALL.Problem.A = SMALL.DL(1).D;
106 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
107
108 %%
109 % Initialising solver structure
110 % Setting solver structure fields (toolbox, name, param, solution,
111 % reconstructed and time) to zero values
112
113 SMALL.solver(1)=SMALL_init_solver;
114
115 % Defining the parameters needed for image denoising
116
117 SMALL.solver(1).toolbox='ompbox';
118 SMALL.solver(1).name='omp2';
119 SMALL.solver(1).param=struct(...
120 'epsilon',Edata,...
121 'maxatoms', maxatoms);
122
123 % Denoising the image - find the sparse solution in the learned
124 % dictionary for all patches in the image and the end it uses
125 % reconstruction function to reconstruct the patches and put them into a
126 % denoised image
127
128 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
129
130 % Show PSNR after reconstruction
131
132 SMALL.solver(1).reconstructed.psnr
133
134 %%
135 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
136 % Boris Mailhe ksvd update implentation omp is the same as with Rubinstein
137 % implementation
138
139
140 % Initialising solver structure
141 % Setting solver structure fields (toolbox, name, param, solution,
142 % reconstructed and time) to zero values
143
144 SMALL.solver(2)=SMALL_init_solver;
145
146 % Defining the parameters needed for image denoising
147
148 SMALL.solver(2).toolbox='ompbox';
149 SMALL.solver(2).name='omp2';
150 SMALL.solver(2).param=struct(...
151 'epsilon',Edata,...
152 'maxatoms', maxatoms);
153
154 % Initialising Dictionary structure
155 % Setting Dictionary structure fields (toolbox, name, param, D and time)
156 % to zero values
157
158 SMALL.DL(2)=SMALL_init_DL('TwoStepDL', 'KSVD', '', 1);
159
160
161 % Defining the parameters for KSVD
162 % In this example we are learning 256 atoms in 20 iterations, so that
163 % every patch in the training set can be represented with target error in
164 % L2-norm (EData)
165 % Type help ksvd in MATLAB prompt for more options.
166
167
168 SMALL.DL(2).param=struct(...
169 'solver', SMALL.solver(2),...
170 'initdict', SMALL.Problem.initdict,...
171 'dictsize', SMALL.Problem.p,...
172 'iternum', 20,...
173 'show_dict', 1);
174
175 % Learn the dictionary
176
177 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
178
179 % Set SMALL.Problem.A dictionary
180 % (backward compatiblity with SPARCO: solver structure communicate
181 % only with Problem structure, ie no direct communication between DL and
182 % solver structures)
183
184 SMALL.Problem.A = SMALL.DL(2).D;
185 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
186
187 % Denoising the image - find the sparse solution in the learned
188 % dictionary for all patches in the image and the end it uses
189 % reconstruction function to reconstruct the patches and put them into a
190 % denoised image
191
192 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
193
194
195 %% show results %%
196
197 SMALL_ImgDeNoiseResult(SMALL);
198
199 clear SMALL;
200 end
201 end
202