annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsSPAMS.m @ 13:cd55209c69e1

(none)
author idamnjanovic
date Thu, 25 Mar 2010 14:02:09 +0000
parents f72603404233
children cbf3521c25eb
rev   line source
idamnjanovic@6 1 %% DICTIONARY LEARNING FOR IMAGE DENOISING
idamnjanovic@6 2 % This file contains an example of how SMALLbox can be used to test different
idamnjanovic@6 3 % dictionary learning techniques in Image Denoising problem.
idamnjanovic@6 4 % It calls generateImageDenoiseProblem that will let you to choose image,
idamnjanovic@6 5 % add noise and use noisy image to generate training set for dictionary
idamnjanovic@6 6 % learning.
idamnjanovic@6 7 % Three dictionary learning techniques were compared:
idamnjanovic@6 8 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
idamnjanovic@6 9 % Implementation of the K-SVD Algorithm using Batch Orthogonal
idamnjanovic@6 10 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
idamnjanovic@6 11 % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse
idamnjanovic@6 12 % Dictionaries for Sparse Signal Approximation", Technical
idamnjanovic@6 13 % Report - CS, Technion, June 2009.
idamnjanovic@6 14 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
idamnjanovic@6 15 % Dictionary Learning for Sparse Coding. International
idamnjanovic@6 16 % Conference on Machine Learning,Montreal, Canada, 2009
idamnjanovic@6 17 %
idamnjanovic@6 18 %
idamnjanovic@6 19 % Ivan Damnjanovic 2010
idamnjanovic@6 20 %%
idamnjanovic@6 21
idamnjanovic@6 22 clear;
idamnjanovic@6 23
idamnjanovic@6 24 % If you want to load the image outside of generateImageDenoiseProblem
idamnjanovic@6 25 % function uncomment following lines. This can be useful if you want to
idamnjanovic@6 26 % denoise more then one image for example.
idamnjanovic@6 27
idamnjanovic@6 28 % TMPpath=pwd;
idamnjanovic@6 29 % FS=filesep;
idamnjanovic@6 30 % [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@6 31 % cd([pathstr1,FS,'data',FS,'images']);
idamnjanovic@6 32 % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
idamnjanovic@6 33 % [pathstr, name, ext, versn] = fileparts(filename);
idamnjanovic@6 34 % test_image = imread(filename);
idamnjanovic@6 35 % test_image = double(test_image);
idamnjanovic@6 36 % cd(TMPpath);
idamnjanovic@6 37 % SMALL.Problem.name=name;
idamnjanovic@6 38
idamnjanovic@6 39
idamnjanovic@6 40 % Defining Image Denoising Problem as Dictionary Learning
idamnjanovic@6 41 % Problem. As an input we set the number of training patches.
idamnjanovic@6 42
idamnjanovic@6 43 SMALL.Problem = generateImageDenoiseProblem('', 40000);
idamnjanovic@6 44
idamnjanovic@6 45
idamnjanovic@6 46 %%
idamnjanovic@6 47 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
idamnjanovic@6 48
idamnjanovic@6 49 % Initialising Dictionary structure
idamnjanovic@6 50 % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@6 51 % to zero values
idamnjanovic@6 52
idamnjanovic@6 53 SMALL.DL(1)=SMALL_init_DL();
idamnjanovic@6 54
idamnjanovic@6 55 % Defining the parameters needed for dictionary learning
idamnjanovic@6 56
idamnjanovic@6 57 SMALL.DL(1).toolbox = 'KSVD';
idamnjanovic@6 58 SMALL.DL(1).name = 'ksvd';
idamnjanovic@6 59
idamnjanovic@6 60 % Defining the parameters for KSVD
idamnjanovic@6 61 % In this example we are learning 256 atoms in 20 iterations, so that
idamnjanovic@6 62 % every patch in the training set can be represented with target error in
idamnjanovic@6 63 % L2-norm (EData)
idamnjanovic@6 64 % Type help ksvd in MATLAB prompt for more options.
idamnjanovic@6 65
idamnjanovic@6 66 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
idamnjanovic@6 67 SMALL.DL(1).param=struct(...
idamnjanovic@6 68 'Edata', Edata,...
idamnjanovic@6 69 'initdict', SMALL.Problem.initdict,...
idamnjanovic@6 70 'dictsize', SMALL.Problem.p,...
idamnjanovic@6 71 'iternum', 20,...
idamnjanovic@6 72 'memusage', 'high');
idamnjanovic@6 73
idamnjanovic@6 74 % Learn the dictionary
idamnjanovic@6 75
idamnjanovic@6 76 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
idamnjanovic@6 77
idamnjanovic@6 78 % Set SMALL.Problem.A dictionary
idamnjanovic@6 79 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 80 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 81 % solver structures)
idamnjanovic@6 82
idamnjanovic@6 83 SMALL.Problem.A = SMALL.DL(1).D;
idamnjanovic@6 84
idamnjanovic@6 85
idamnjanovic@6 86 %%
idamnjanovic@6 87 % Initialising solver structure
idamnjanovic@6 88 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 89 % reconstructed and time) to zero values
idamnjanovic@6 90
idamnjanovic@6 91 SMALL.solver(1)=SMALL_init_solver;
idamnjanovic@6 92
idamnjanovic@6 93 % Defining the parameters needed for image denoising
idamnjanovic@6 94
idamnjanovic@6 95 SMALL.solver(1).toolbox='ompbox';
idamnjanovic@6 96 SMALL.solver(1).name='ompdenoise';
idamnjanovic@6 97
idamnjanovic@6 98 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
idamnjanovic@6 99 % but backward compatible with KSVD definition of denoising
idamnjanovic@6 100
idamnjanovic@6 101 SMALL.solver(1)=SMALL_denoise(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 102
idamnjanovic@6 103 %%
idamnjanovic@6 104 % Use KSVDS Dictionary Learning Algorithm to denoise image
idamnjanovic@6 105
idamnjanovic@6 106 % Initialising solver structure
idamnjanovic@6 107 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 108 % reconstructed and time) to zero values
idamnjanovic@6 109
idamnjanovic@6 110 SMALL.DL(2)=SMALL_init_DL();
idamnjanovic@6 111
idamnjanovic@6 112 % Defining the parameters needed for dictionary learning
idamnjanovic@6 113
idamnjanovic@6 114 SMALL.DL(2).toolbox = 'KSVDS';
idamnjanovic@6 115 SMALL.DL(2).name = 'ksvds';
idamnjanovic@6 116
idamnjanovic@6 117 % Defining the parameters for KSVDS
idamnjanovic@6 118 % In this example we are learning 256 atoms in 20 iterations, so that
idamnjanovic@6 119 % every patch in the training set can be represented with target error in
idamnjanovic@6 120 % L2-norm (EDataS). We also impose "double sparsity" - dictionary itself
idamnjanovic@6 121 % has to be sparse in the given base dictionary (Tdict - number of
idamnjanovic@6 122 % nonzero elements per atom).
idamnjanovic@6 123 % Type help ksvds in MATLAB prompt for more options.
idamnjanovic@6 124
idamnjanovic@6 125 EdataS=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
idamnjanovic@6 126 SMALL.DL(2).param=struct(...
idamnjanovic@6 127 'Edata', EdataS, ...
idamnjanovic@6 128 'Tdict', 6,...
idamnjanovic@6 129 'stepsize', 1,...
idamnjanovic@6 130 'dictsize', SMALL.Problem.p,...
idamnjanovic@6 131 'iternum', 20,...
idamnjanovic@6 132 'memusage', 'high');
idamnjanovic@6 133 SMALL.DL(2).param.initA = speye(SMALL.Problem.p);
idamnjanovic@6 134 SMALL.DL(2).param.basedict{1} = odctdict(8,16);
idamnjanovic@6 135 SMALL.DL(2).param.basedict{2} = odctdict(8,16);
idamnjanovic@6 136
idamnjanovic@6 137 % Learn the dictionary
idamnjanovic@6 138
idamnjanovic@6 139 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
idamnjanovic@6 140
idamnjanovic@6 141 % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary
idamnjanovic@6 142 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 143 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 144 % solver structures)
idamnjanovic@6 145
idamnjanovic@6 146 SMALL.Problem.A = SMALL.DL(2).D;
idamnjanovic@6 147 SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1};
idamnjanovic@6 148 SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2};
idamnjanovic@6 149
idamnjanovic@6 150 %%
idamnjanovic@6 151 % Initialising solver structure
idamnjanovic@6 152 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 153 % reconstructed and time) to zero values
idamnjanovic@6 154
idamnjanovic@6 155 SMALL.solver(2)=SMALL_init_solver;
idamnjanovic@6 156
idamnjanovic@6 157 % Defining the parameters needed for image denoising
idamnjanovic@6 158
idamnjanovic@6 159 SMALL.solver(2).toolbox='ompsbox';
idamnjanovic@6 160 SMALL.solver(2).name='ompsdenoise';
idamnjanovic@6 161
idamnjanovic@6 162 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
idamnjanovic@6 163 % but backward compatible with KSVD definition of denoising
idamnjanovic@6 164 % Pay attention that since implicit base dictionary is used, denoising
idamnjanovic@6 165 % can be much faster then using explicit dictionary in KSVD example.
idamnjanovic@6 166
idamnjanovic@6 167 SMALL.solver(2)=SMALL_denoise(SMALL.Problem, SMALL.solver(2));
idamnjanovic@6 168
idamnjanovic@13 169 % %%
idamnjanovic@13 170 % % Use SPAMS Online Dictionary Learning Algorithm
idamnjanovic@13 171 % % to Learn overcomplete dictionary (Julien Mairal 2009)
idamnjanovic@13 172 % % (If you have not installed SPAMS please comment the following two cells)
idamnjanovic@13 173 %
idamnjanovic@13 174 % % Initialising Dictionary structure
idamnjanovic@13 175 % % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@13 176 % % to zero values
idamnjanovic@13 177 %
idamnjanovic@13 178 % SMALL.DL(3)=SMALL_init_DL();
idamnjanovic@13 179 %
idamnjanovic@13 180 % % Defining fields needed for dictionary learning
idamnjanovic@13 181 %
idamnjanovic@13 182 % SMALL.DL(3).toolbox = 'SPAMS';
idamnjanovic@13 183 % SMALL.DL(3).name = 'mexTrainDL';
idamnjanovic@13 184 %
idamnjanovic@13 185 % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
idamnjanovic@13 186 %
idamnjanovic@13 187 % SMALL.DL(3).param=struct(...
idamnjanovic@13 188 % 'D', SMALL.Problem.initdict,...
idamnjanovic@13 189 % 'K', SMALL.Problem.p,...
idamnjanovic@13 190 % 'lambda', 2,...
idamnjanovic@13 191 % 'iter', 200,...
idamnjanovic@13 192 % 'mode', 3, ...
idamnjanovic@13 193 % 'modeD', 0);
idamnjanovic@13 194 %
idamnjanovic@13 195 % % Learn the dictionary
idamnjanovic@13 196 %
idamnjanovic@13 197 % SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3));
idamnjanovic@13 198 %
idamnjanovic@13 199 % % Set SMALL.Problem.A dictionary
idamnjanovic@13 200 % % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@13 201 % % only with Problem structure, ie no direct communication between DL and
idamnjanovic@13 202 % % solver structures)
idamnjanovic@13 203 %
idamnjanovic@13 204 % SMALL.Problem.A = SMALL.DL(3).D;
idamnjanovic@13 205 %
idamnjanovic@13 206 %
idamnjanovic@13 207 % %%
idamnjanovic@13 208 % % Initialising solver structure
idamnjanovic@13 209 % % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@13 210 % % reconstructed and time) to zero values
idamnjanovic@13 211 %
idamnjanovic@13 212 % SMALL.solver(3)=SMALL_init_solver;
idamnjanovic@13 213 %
idamnjanovic@13 214 % % Defining the parameters needed for denoising
idamnjanovic@13 215 %
idamnjanovic@13 216 % SMALL.solver(3).toolbox='ompbox';
idamnjanovic@13 217 % SMALL.solver(3).name='ompdenoise';
idamnjanovic@13 218 %
idamnjanovic@13 219 % % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
idamnjanovic@13 220 % % but backward compatible with KSVD definition of denoising
idamnjanovic@13 221 %
idamnjanovic@13 222 % SMALL.solver(3)=SMALL_denoise(SMALL.Problem, SMALL.solver(3));
idamnjanovic@6 223
idamnjanovic@6 224 %%
idamnjanovic@6 225 % Plot results and save midi files
idamnjanovic@6 226
idamnjanovic@6 227 % show results %
idamnjanovic@6 228
idamnjanovic@6 229 SMALL_ImgDeNoiseResult(SMALL);