annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsTwoStepKSVD.m @ 247:ecce33192fcc tip

Added tag ver_2.1 for changeset cef4500b936f
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Wed, 31 Oct 2012 12:24:44 +0000
parents 9c418bea7f6a
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=1:1
ivan@153 51 for im_num=1:1
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 % results structure is to store all results
ivan@153 63
ivan@153 64 results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr;
ivan@153 65
ivan@153 66 %%
ivan@153 67 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
ivan@153 68 % Ron Rubinstein implementation
ivan@153 69
ivan@153 70 % Initialising Dictionary structure
ivan@153 71 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@153 72 % to zero values
ivan@153 73
ivan@153 74 SMALL.DL(1)=SMALL_init_DL();
ivan@153 75
ivan@153 76 % Defining the parameters needed for dictionary learning
ivan@153 77
ivan@153 78 SMALL.DL(1).toolbox = 'KSVD';
ivan@153 79 SMALL.DL(1).name = 'ksvd';
ivan@153 80
ivan@153 81 % Defining the parameters for KSVD
ivan@153 82 % In this example we are learning 256 atoms in 20 iterations, so that
ivan@153 83 % every patch in the training set can be represented with target error in
ivan@153 84 % L2-norm (Edata)
ivan@153 85 % Type help ksvd in MATLAB prompt for more options.
ivan@153 86
ivan@153 87
ivan@153 88 SMALL.DL(1).param=struct(...
ivan@153 89 'Edata', Edata,...
ivan@153 90 'initdict', SMALL.Problem.initdict,...
ivan@153 91 'dictsize', SMALL.Problem.p,...
ivan@153 92 'exact', 1, ...
ivan@153 93 'iternum', 20,...
ivan@153 94 'memusage', 'high');
ivan@153 95
ivan@153 96 % Learn the dictionary
ivan@153 97
ivan@153 98 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
ivan@153 99
ivan@153 100 % Set SMALL.Problem.A dictionary
ivan@153 101 % (backward compatiblity with SPARCO: solver structure communicate
ivan@153 102 % only with Problem structure, ie no direct communication between DL and
ivan@153 103 % solver structures)
ivan@153 104
ivan@153 105 SMALL.Problem.A = SMALL.DL(1).D;
ivan@161 106 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
ivan@153 107
ivan@153 108 %%
ivan@153 109 % Initialising solver structure
ivan@153 110 % Setting solver structure fields (toolbox, name, param, solution,
ivan@153 111 % reconstructed and time) to zero values
ivan@153 112
ivan@153 113 SMALL.solver(1)=SMALL_init_solver;
ivan@153 114
ivan@153 115 % Defining the parameters needed for image denoising
ivan@153 116
ivan@153 117 SMALL.solver(1).toolbox='ompbox';
ivan@153 118 SMALL.solver(1).name='omp2';
ivan@153 119 SMALL.solver(1).param=struct(...
ivan@153 120 'epsilon',Edata,...
ivan@153 121 'maxatoms', maxatoms);
ivan@153 122
ivan@153 123 % Denoising the image - find the sparse solution in the learned
ivan@153 124 % dictionary for all patches in the image and the end it uses
ivan@153 125 % reconstruction function to reconstruct the patches and put them into a
ivan@153 126 % denoised image
ivan@153 127
ivan@153 128 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
ivan@153 129
ivan@153 130 % Show PSNR after reconstruction
ivan@153 131
ivan@153 132 SMALL.solver(1).reconstructed.psnr
ivan@153 133
ivan@153 134 %%
ivan@153 135 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
ivan@153 136 % Boris Mailhe ksvd update implentation omp is the same as with Rubinstein
ivan@153 137 % implementation
ivan@153 138
ivan@153 139
ivan@153 140 % Initialising solver structure
ivan@153 141 % Setting solver structure fields (toolbox, name, param, solution,
ivan@153 142 % reconstructed and time) to zero values
ivan@153 143
ivan@153 144 SMALL.solver(2)=SMALL_init_solver;
ivan@153 145
ivan@153 146 % Defining the parameters needed for image denoising
ivan@153 147
ivan@153 148 SMALL.solver(2).toolbox='ompbox';
ivan@153 149 SMALL.solver(2).name='omp2';
ivan@153 150 SMALL.solver(2).param=struct(...
ivan@153 151 'epsilon',Edata,...
ivan@153 152 'maxatoms', maxatoms);
ivan@153 153
ivan@153 154 % Initialising Dictionary structure
ivan@153 155 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@153 156 % to zero values
ivan@153 157
ivan@153 158 SMALL.DL(2)=SMALL_init_DL('TwoStepDL', 'KSVD', '', 1);
ivan@153 159
ivan@153 160
ivan@153 161 % Defining the parameters for KSVD
ivan@153 162 % In this example we are learning 256 atoms in 20 iterations, so that
ivan@153 163 % every patch in the training set can be represented with target error in
ivan@153 164 % L2-norm (EData)
ivan@153 165 % Type help ksvd in MATLAB prompt for more options.
ivan@153 166
ivan@153 167
ivan@153 168 SMALL.DL(2).param=struct(...
ivan@153 169 'solver', SMALL.solver(2),...
ivan@153 170 'initdict', SMALL.Problem.initdict,...
ivan@153 171 'dictsize', SMALL.Problem.p,...
ivan@153 172 'iternum', 20,...
ivan@153 173 'show_dict', 1);
ivan@153 174
ivan@153 175 % Learn the dictionary
ivan@153 176
ivan@153 177 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
ivan@153 178
ivan@153 179 % Set SMALL.Problem.A dictionary
ivan@153 180 % (backward compatiblity with SPARCO: solver structure communicate
ivan@153 181 % only with Problem structure, ie no direct communication between DL and
ivan@153 182 % solver structures)
ivan@153 183
ivan@153 184 SMALL.Problem.A = SMALL.DL(2).D;
ivan@161 185 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
ivan@153 186
ivan@153 187 % Denoising the image - find the sparse solution in the learned
ivan@153 188 % dictionary for all patches in the image and the end it uses
ivan@153 189 % reconstruction function to reconstruct the patches and put them into a
ivan@153 190 % denoised image
ivan@153 191
ivan@153 192 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
ivan@153 193
ivan@153 194
ivan@153 195 %% show results %%
ivan@153 196
ivan@153 197 SMALL_ImgDeNoiseResult(SMALL);
ivan@153 198
ivan@153 199 clear SMALL;
ivan@153 200 end
ivan@153 201 end
ivan@153 202