annotate examples/MajorizationMinimization tests/SMALL_ImgDenoise_DL_test_KSVDvsMajorizationMinimization.m @ 200:69ce11724b1f luisf_dev

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