annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsRLSDLAvsTwoStepMOD.m @ 186:9c418bea7f6a bug_386

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