annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_KSVDvsSPAMS.m @ 50:d5155eaa3f68

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