annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_TwoStep_KSVD_MOD_OLS_OPT.m @ 234:c96880c0c47c

renamed file.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Thu, 19 Apr 2012 17:21:05 +0100
parents examples/Image Denoising/SMALL_ImgDenoise_DL_test_TwoStep_KSVD_MOD_OLS_Mailhe.m@1d134a1b6f95
children
rev   line source
ivan@153 1 %% Dictionary Learning for Image Denoising - KSVD vs Recursive Least Squares
ivan@153 2 %
ivan@153 3 % This file contains an example of how SMALLbox can be used to test different
ivan@153 4 % dictionary learning techniques in Image Denoising problem.
ivan@153 5 % It calls generateImageDenoiseProblem that will let you to choose image,
ivan@153 6 % add noise and use noisy image to generate training set for dictionary
ivan@153 7 % learning.
ivan@153 8 % Two dictionary learning techniques were compared:
ivan@153 9 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
ivan@153 10 % Implementation of the K-SVD Algorithm using Batch Orthogonal
ivan@153 11 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
ivan@153 12 % - RLS-DLA - Skretting, K.; Engan, K.; , "Recursive Least Squares
ivan@153 13 % Dictionary Learning Algorithm," Signal Processing, IEEE Transactions on,
ivan@153 14 % vol.58, no.4, pp.2121-2130, April 2010
ivan@153 15 %
ivan@153 16
ivan@153 17
ivan@153 18 % Centre for Digital Music, Queen Mary, University of London.
ivan@153 19 % This file copyright 2011 Ivan Damnjanovic.
ivan@153 20 %
ivan@153 21 % This program is free software; you can redistribute it and/or
ivan@153 22 % modify it under the terms of the GNU General Public License as
ivan@153 23 % published by the Free Software Foundation; either version 2 of the
ivan@153 24 % License, or (at your option) any later version. See the file
ivan@153 25 % COPYING included with this distribution for more information.
ivan@153 26 %
ivan@153 27 %%
ivan@153 28
ivan@153 29
ivan@153 30
ivan@153 31 % If you want to load the image outside of generateImageDenoiseProblem
ivan@153 32 % function uncomment following lines. This can be useful if you want to
ivan@153 33 % denoise more then one image for example.
ivan@153 34 % Here we are loading test_image.mat that contains structure with 5 images : lena,
ivan@153 35 % barbara,boat, house and peppers.
ivan@153 36 clear;
ivan@153 37 TMPpath=pwd;
ivan@153 38 FS=filesep;
luis@186 39 [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m'));
ivan@153 40 cd([pathstr1,FS,'data',FS,'images']);
ivan@153 41 load('test_image.mat');
ivan@153 42 cd(TMPpath);
ivan@153 43
ivan@153 44 % Deffining the noise levels that we want to test
ivan@153 45
ivan@153 46 noise_level=[10 20 25 50 100];
ivan@153 47
ivan@153 48 % Here we loop through different noise levels and images
ivan@153 49
ivan@153 50 for noise_ind=2:2
ivan@153 51 for im_num=2:2
ivan@153 52
ivan@153 53 % Defining Image Denoising Problem as Dictionary Learning
ivan@153 54 % Problem. As an input we set the number of training patches.
ivan@153 55
ivan@153 56 SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind));
ivan@153 57 SMALL.Problem.name=int2str(im_num);
ivan@153 58
ivan@153 59 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
ivan@153 60 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
ivan@153 61
ivan@153 62
ivan@153 63 %%
ivan@153 64 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
ivan@153 65 % Boris Mailhe ksvd update implentation omp is the same as with Rubinstein
ivan@153 66 % implementation
ivan@153 67
ivan@153 68
ivan@153 69 % Initialising solver structure
ivan@153 70 % Setting solver structure fields (toolbox, name, param, solution,
ivan@153 71 % reconstructed and time) to zero values
ivan@153 72
ivan@153 73 SMALL.solver(1)=SMALL_init_solver;
ivan@153 74
ivan@153 75 % Defining the parameters needed for image denoising
ivan@153 76
ivan@153 77 SMALL.solver(1).toolbox='ompbox';
ivan@153 78 SMALL.solver(1).name='omp2';
ivan@153 79 SMALL.solver(1).param=struct(...
ivan@153 80 'epsilon',Edata,...
ivan@153 81 'maxatoms', maxatoms);
ivan@153 82
ivan@153 83 % Initialising Dictionary structure
ivan@153 84 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@153 85 % to zero values
ivan@153 86
ivan@153 87 SMALL.DL(1)=SMALL_init_DL('TwoStepDL', 'KSVD', '', 1);
ivan@153 88
ivan@153 89
ivan@153 90 % Defining the parameters for KSVD
ivan@153 91 % In this example we are learning 256 atoms in 20 iterations, so that
ivan@153 92 % every patch in the training set can be represented with target error in
ivan@153 93 % L2-norm (EData)
ivan@153 94 % Type help ksvd in MATLAB prompt for more options.
ivan@153 95
ivan@153 96
ivan@153 97 SMALL.DL(1).param=struct(...
ivan@153 98 'solver', SMALL.solver(1),...
ivan@153 99 'initdict', SMALL.Problem.initdict,...
ivan@153 100 'dictsize', SMALL.Problem.p,...
ivan@153 101 'iternum', 20,...
ivan@153 102 'show_dict', 1);
ivan@153 103
ivan@153 104 % Learn the dictionary
ivan@153 105
ivan@153 106 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
ivan@153 107
ivan@153 108 % Set SMALL.Problem.A dictionary
ivan@153 109 % (backward compatiblity with SPARCO: solver structure communicate
ivan@153 110 % only with Problem structure, ie no direct communication between DL and
ivan@153 111 % solver structures)
ivan@153 112
ivan@153 113 SMALL.Problem.A = SMALL.DL(1).D;
ivan@161 114 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
ivan@153 115
ivan@153 116 % Denoising the image - find the sparse solution in the learned
ivan@153 117 % dictionary for all patches in the image and the end it uses
ivan@153 118 % reconstruction function to reconstruct the patches and put them into a
ivan@153 119 % denoised image
ivan@153 120
ivan@153 121 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
ivan@153 122
ivan@153 123 %%
ivan@153 124 % Use MOD Dictionary Learning Algorithm to Learn overcomplete dictionary
ivan@153 125 % Boris Mailhe MOD update implentation omp is the Ron Rubinstein
ivan@153 126 % implementation
ivan@153 127
ivan@153 128
ivan@153 129 % Initialising solver structure
ivan@153 130 % Setting solver structure fields (toolbox, name, param, solution,
ivan@153 131 % reconstructed and time) to zero values
ivan@153 132
ivan@153 133 SMALL.solver(2)=SMALL_init_solver;
ivan@153 134
ivan@153 135 % Defining the parameters needed for image denoising
ivan@153 136
ivan@153 137 SMALL.solver(2).toolbox='ompbox';
ivan@153 138 SMALL.solver(2).name='omp2';
ivan@153 139 SMALL.solver(2).param=struct(...
ivan@153 140 'epsilon',Edata,...
ivan@153 141 'maxatoms', maxatoms);
ivan@153 142
ivan@153 143 % Initialising Dictionary structure
ivan@153 144 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@153 145 % to zero values
ivan@153 146
ivan@153 147 SMALL.DL(2)=SMALL_init_DL('TwoStepDL', 'MOD', '', 1);
ivan@153 148
ivan@153 149
ivan@153 150 % Defining the parameters for MOD
ivan@153 151 % In this example we are learning 256 atoms in 20 iterations, so that
ivan@153 152 % every patch in the training set can be represented with target error in
ivan@153 153 % L2-norm (EData)
ivan@153 154 % Type help ksvd in MATLAB prompt for more options
ivan@153 155
ivan@153 156 SMALL.DL(2).param=struct(...
ivan@153 157 'solver', SMALL.solver(2),...
ivan@153 158 'initdict', SMALL.Problem.initdict,...
ivan@153 159 'dictsize', SMALL.Problem.p,...
ivan@153 160 'iternum', 20,...
ivan@153 161 'show_dict', 1);
ivan@153 162
ivan@153 163 % Learn the dictionary
ivan@153 164
ivan@153 165 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
ivan@153 166
ivan@153 167 % Set SMALL.Problem.A dictionary
ivan@153 168 % (backward compatiblity with SPARCO: solver structure communicate
ivan@153 169 % only with Problem structure, ie no direct communication between DL and
ivan@153 170 % solver structures)
ivan@153 171
ivan@153 172 SMALL.Problem.A = SMALL.DL(2).D;
ivan@161 173 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
ivan@153 174
ivan@153 175 % Denoising the image - find the sparse solution in the learned
ivan@153 176 % dictionary for all patches in the image and the end it uses
ivan@153 177 % reconstruction function to reconstruct the patches and put them into a
ivan@153 178 % denoised image
ivan@153 179
ivan@153 180 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
ivan@153 181 %%
ivan@153 182 % Use OLS Dictionary Learning Algorithm to Learn overcomplete dictionary
ivan@153 183 % Boris Mailhe ksvd update implentation omp is the Ron Rubinstein
ivan@153 184 % implementation
ivan@153 185
ivan@153 186
ivan@153 187 % Initialising solver structure
ivan@153 188 % Setting solver structure fields (toolbox, name, param, solution,
ivan@153 189 % reconstructed and time) to zero values
ivan@153 190
ivan@153 191 SMALL.solver(3)=SMALL_init_solver;
ivan@153 192
ivan@153 193 % Defining the parameters needed for image denoising
ivan@153 194
ivan@153 195 SMALL.solver(3).toolbox='ompbox';
ivan@153 196 SMALL.solver(3).name='omp2';
ivan@153 197 SMALL.solver(3).param=struct(...
ivan@153 198 'epsilon',Edata,...
ivan@153 199 'maxatoms', maxatoms);
ivan@153 200
ivan@153 201 % Initialising Dictionary structure
ivan@153 202 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@153 203 % to zero values
ivan@153 204
ivan@153 205 SMALL.DL(3)=SMALL_init_DL('TwoStepDL', 'ols', '', 1);
ivan@153 206
ivan@153 207
ivan@153 208 % Defining the parameters for KSVD
ivan@153 209 % In this example we are learning 256 atoms in 20 iterations, so that
ivan@153 210 % every patch in the training set can be represented with target error in
ivan@153 211 % L2-norm (EData)
ivan@153 212 % Type help ksvd in MATLAB prompt for more options.
ivan@153 213
ivan@153 214
ivan@153 215 SMALL.DL(3).param=struct(...
ivan@153 216 'solver', SMALL.solver(3),...
ivan@153 217 'initdict', SMALL.Problem.initdict,...
ivan@153 218 'dictsize', SMALL.Problem.p,...
ivan@153 219 'iternum', 20,...
ivan@153 220 'learningRate', 0.1,...
ivan@153 221 'show_dict', 1);
ivan@153 222
ivan@153 223 % Learn the dictionary
ivan@153 224
ivan@153 225 SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3));
ivan@153 226
ivan@153 227 % Set SMALL.Problem.A dictionary
ivan@153 228 % (backward compatiblity with SPARCO: solver structure communicate
ivan@153 229 % only with Problem structure, ie no direct communication between DL and
ivan@153 230 % solver structures)
ivan@153 231
ivan@153 232 SMALL.Problem.A = SMALL.DL(3).D;
ivan@161 233 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
ivan@153 234
ivan@153 235 % Denoising the image - find the sparse solution in the learned
ivan@153 236 % dictionary for all patches in the image and the end it uses
ivan@153 237 % reconstruction function to reconstruct the patches and put them into a
ivan@153 238 % denoised image
ivan@153 239
ivan@153 240 SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3));
ivan@153 241 %%
ivan@153 242 % Use Mailhe Dictionary Learning Algorithm to Learn overcomplete dictionary
ivan@153 243 % Boris Mailhe ksvd update implentation omp is the Ron Rubinstein
ivan@153 244 % implementation
ivan@153 245
ivan@153 246
ivan@153 247 % Initialising solver structure
ivan@153 248 % Setting solver structure fields (toolbox, name, param, solution,
ivan@153 249 % reconstructed and time) to zero values
ivan@153 250
ivan@153 251 SMALL.solver(4)=SMALL_init_solver;
ivan@153 252
ivan@153 253 % Defining the parameters needed for image denoising
ivan@153 254
ivan@153 255 SMALL.solver(4).toolbox='ompbox';
ivan@153 256 SMALL.solver(4).name='omp2';
ivan@153 257 SMALL.solver(4).param=struct(...
ivan@153 258 'epsilon',Edata,...
ivan@153 259 'maxatoms', maxatoms);
ivan@153 260
ivan@153 261 % Initialising Dictionary structure
ivan@153 262 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@153 263 % to zero values
ivan@153 264
bmailhe@212 265 SMALL.DL(4)=SMALL_init_DL('TwoStepDL', 'opt', '', 1);
ivan@153 266
ivan@153 267
ivan@153 268 % Defining the parameters for KSVD
ivan@153 269 % In this example we are learning 256 atoms in 20 iterations, so that
ivan@153 270 % every patch in the training set can be represented with target error in
ivan@153 271 % L2-norm (EData)
ivan@153 272 % Type help ksvd in MATLAB prompt for more options.
ivan@153 273
ivan@153 274
ivan@153 275 SMALL.DL(4).param=struct(...
ivan@153 276 'solver', SMALL.solver(4),...
ivan@153 277 'initdict', SMALL.Problem.initdict,...
ivan@153 278 'dictsize', SMALL.Problem.p,...
ivan@153 279 'iternum', 20,...
ivan@153 280 'learningRate', 2,...
ivan@153 281 'show_dict', 1);
ivan@153 282
ivan@153 283 % Learn the dictionary
ivan@153 284
ivan@153 285 SMALL.DL(4) = SMALL_learn(SMALL.Problem, SMALL.DL(4));
ivan@153 286
ivan@153 287 % Set SMALL.Problem.A dictionary
ivan@153 288 % (backward compatiblity with SPARCO: solver structure communicate
ivan@153 289 % only with Problem structure, ie no direct communication between DL and
ivan@153 290 % solver structures)
ivan@153 291
ivan@153 292 SMALL.Problem.A = SMALL.DL(4).D;
ivan@161 293 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
ivan@153 294
ivan@153 295 % Denoising the image - find the sparse solution in the learned
ivan@153 296 % dictionary for all patches in the image and the end it uses
ivan@153 297 % reconstruction function to reconstruct the patches and put them into a
ivan@153 298 % denoised image
ivan@153 299
ivan@153 300 SMALL.solver(4)=SMALL_solve(SMALL.Problem, SMALL.solver(4));
ivan@153 301
ivan@153 302 %% show results %%
ivan@153 303
ivan@153 304 SMALL_ImgDeNoiseResult(SMALL);
ivan@153 305
ivan@153 306 %clear SMALL;
ivan@153 307 end
ivan@153 308 end
ivan@153 309