annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsRLSDLA.m @ 121:5ded5e2e7d07 ivand_dev

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