annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_Training_size.m @ 107:dab78a3598b6

changes to comments for couple of scripts
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Wed, 18 May 2011 11:50:12 +0100
parents cbf3521c25eb
children 8e660fd14774
rev   line source
idamnjanovic@6 1 %% DICTIONARY LEARNING FOR IMAGE DENOISING
idamnjanovic@25 2 %
ivan@107 3
idamnjanovic@6 4 % This file contains an example of how SMALLbox can be used to test different
idamnjanovic@6 5 % dictionary learning techniques in Image Denoising problem.
idamnjanovic@6 6 % It calls generateImageDenoiseProblem that will let you to choose image,
idamnjanovic@6 7 % add noise and use noisy image to generate training set for dictionary
idamnjanovic@6 8 % learning.
idamnjanovic@6 9 % We tested time and psnr for two dictionary learning techniques. This
idamnjanovic@6 10 % example does not represnt any extensive testing. The aim of this
idamnjanovic@6 11 % example is just to show how SMALL structure can be used for testing.
idamnjanovic@6 12 %
idamnjanovic@6 13 % Two dictionary learning techniques were compared:
idamnjanovic@6 14 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
idamnjanovic@6 15 % Implementation of the K-SVD Algorithm using Batch Orthogonal
idamnjanovic@6 16 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
idamnjanovic@6 17 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
idamnjanovic@6 18 % Dictionary Learning for Sparse Coding. International
idamnjanovic@6 19 % Conference on Machine Learning,Montreal, Canada, 2009
ivan@107 20
idamnjanovic@6 21 %
ivan@107 22 % Centre for Digital Music, Queen Mary, University of London.
ivan@107 23 % This file copyright 2010 Ivan Damnjanovic.
idamnjanovic@6 24 %
ivan@107 25 % This program is free software; you can redistribute it and/or
ivan@107 26 % modify it under the terms of the GNU General Public License as
ivan@107 27 % published by the Free Software Foundation; either version 2 of the
ivan@107 28 % License, or (at your option) any later version. See the file
ivan@107 29 % COPYING included with this distribution for more information.%%
idamnjanovic@6 30 %%
idamnjanovic@6 31
idamnjanovic@6 32 clear all;
idamnjanovic@6 33
idamnjanovic@6 34 %% Load an image
idamnjanovic@6 35 TMPpath=pwd;
idamnjanovic@6 36 FS=filesep;
idamnjanovic@6 37 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@6 38 cd([pathstr1,FS,'data',FS,'images']);
idamnjanovic@6 39 [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
idamnjanovic@6 40 [pathstr, name, ext, versn] = fileparts(filename);
idamnjanovic@6 41 test_image = imread(filename);
idamnjanovic@6 42 test_image = double(test_image);
idamnjanovic@6 43 cd(TMPpath);
idamnjanovic@6 44
idamnjanovic@6 45 % number of different values we want to test
idamnjanovic@6 46 n =5;
idamnjanovic@6 47 step = floor((size(test_image,1)-8+1)*(size(test_image,2)-8+1)/n);
idamnjanovic@6 48 Training_size=zeros(1,n);
idamnjanovic@6 49 time = zeros(2,n);
idamnjanovic@6 50 psnr = zeros(2,n);
idamnjanovic@6 51 for i=1:n
idamnjanovic@6 52
idamnjanovic@6 53 % Here we want to test time spent and quality of denoising for
idamnjanovic@6 54 % different sizes of training sample.
idamnjanovic@6 55 Training_size(i)=i*step;
idamnjanovic@6 56
idamnjanovic@6 57 SMALL.Problem = generateImageDenoiseProblem(test_image,Training_size(i));
idamnjanovic@6 58 SMALL.Problem.name=name;
idamnjanovic@6 59 %%
idamnjanovic@6 60 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
idamnjanovic@6 61
idamnjanovic@6 62 % Initialising Dictionary structure
idamnjanovic@6 63 % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@6 64 % to zero values
idamnjanovic@6 65
idamnjanovic@6 66 SMALL.DL(1)=SMALL_init_DL();
idamnjanovic@6 67
idamnjanovic@6 68 % Defining the parameters needed for dictionary learning
idamnjanovic@6 69
idamnjanovic@6 70 SMALL.DL(1).toolbox = 'KSVD';
idamnjanovic@6 71 SMALL.DL(1).name = 'ksvd';
idamnjanovic@6 72
idamnjanovic@6 73 % Defining the parameters for KSVD
idamnjanovic@6 74 % In this example we are learning 256 atoms in 20 iterations, so that
idamnjanovic@6 75 % every patch in the training set can be represented with target error in
idamnjanovic@6 76 % L2-norm (EData)
idamnjanovic@6 77 % Type help ksvd in MATLAB prompt for more options.
idamnjanovic@6 78
idamnjanovic@6 79 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
ivan@107 80 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
ivan@107 81
idamnjanovic@6 82 SMALL.DL(1).param=struct(...
idamnjanovic@6 83 'Edata', Edata,...
idamnjanovic@6 84 'initdict', SMALL.Problem.initdict,...
idamnjanovic@6 85 'dictsize', SMALL.Problem.p,...
idamnjanovic@19 86 'iternum', 20);
idamnjanovic@19 87 %'memusage', 'high');
idamnjanovic@6 88
idamnjanovic@6 89 % Learn the dictionary
idamnjanovic@6 90
idamnjanovic@6 91 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
idamnjanovic@6 92
idamnjanovic@6 93 % Set SMALL.Problem.A dictionary
idamnjanovic@6 94 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 95 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 96 % solver structures)
idamnjanovic@6 97
idamnjanovic@6 98 SMALL.Problem.A = SMALL.DL(1).D;
ivan@107 99 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
idamnjanovic@6 100
idamnjanovic@6 101 %%
idamnjanovic@6 102 % Initialising solver structure
idamnjanovic@6 103 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 104 % reconstructed and time) to zero values
idamnjanovic@6 105
idamnjanovic@6 106
idamnjanovic@6 107 SMALL.solver(1)=SMALL_init_solver;
idamnjanovic@6 108
idamnjanovic@6 109 % Defining the parameters needed for denoising
idamnjanovic@6 110
idamnjanovic@6 111 SMALL.solver(1).toolbox='ompbox';
ivan@107 112 SMALL.solver(1).name='omp2';
idamnjanovic@6 113
ivan@107 114 SMALL.solver(1).param=struct(...
ivan@107 115 'epsilon',Edata,...
ivan@107 116 'maxatoms', maxatoms);
ivan@107 117
ivan@107 118 % Denoising the image - find the sparse solution in the learned
ivan@107 119 % dictionary for all patches in the image and the end it uses
ivan@107 120 % reconstruction function to reconstruct the patches and put them into a
ivan@107 121 % denoised image
ivan@107 122
ivan@107 123 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
ivan@107 124
ivan@107 125 % Show PSNR after reconstruction
ivan@107 126
ivan@107 127 SMALL.solver(1).reconstructed.psnr
ivan@107 128
idamnjanovic@6 129
idamnjanovic@6 130 %%
idamnjanovic@6 131 % Use SPAMS Online Dictionary Learning Algorithm
idamnjanovic@6 132 % to Learn overcomplete dictionary (Julien Mairal 2009)
idamnjanovic@6 133 % (If you have not installed SPAMS please comment the following two cells)
idamnjanovic@6 134
idamnjanovic@6 135 % Initialising Dictionary structure
idamnjanovic@6 136 % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@6 137 % to zero values
idamnjanovic@6 138
idamnjanovic@6 139 SMALL.DL(2)=SMALL_init_DL();
idamnjanovic@6 140
idamnjanovic@6 141 % Defining fields needed for dictionary learning
idamnjanovic@6 142
idamnjanovic@6 143 SMALL.DL(2).toolbox = 'SPAMS';
idamnjanovic@6 144 SMALL.DL(2).name = 'mexTrainDL';
idamnjanovic@6 145
idamnjanovic@6 146 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
idamnjanovic@6 147
idamnjanovic@6 148 SMALL.DL(2).param=struct(...
idamnjanovic@6 149 'D', SMALL.Problem.initdict,...
idamnjanovic@6 150 'K', SMALL.Problem.p,...
idamnjanovic@6 151 'lambda', 2,...
idamnjanovic@6 152 'iter', 300,...
idamnjanovic@6 153 'mode', 3,...
idamnjanovic@6 154 'modeD', 0 );
idamnjanovic@6 155
idamnjanovic@6 156 % Learn the dictionary
idamnjanovic@6 157
idamnjanovic@6 158 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
idamnjanovic@6 159
idamnjanovic@6 160 % Set SMALL.Problem.A dictionary
idamnjanovic@6 161 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 162 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 163 % solver structures)
idamnjanovic@6 164
idamnjanovic@6 165 SMALL.Problem.A = SMALL.DL(2).D;
ivan@107 166 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
idamnjanovic@6 167
idamnjanovic@6 168 %%
idamnjanovic@6 169 % Initialising solver structure
idamnjanovic@6 170 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 171 % reconstructed and time) to zero values
idamnjanovic@6 172
idamnjanovic@6 173 SMALL.solver(2)=SMALL_init_solver;
idamnjanovic@6 174
idamnjanovic@6 175 % Defining the parameters needed for denoising
idamnjanovic@6 176
idamnjanovic@6 177 SMALL.solver(2).toolbox='ompbox';
ivan@107 178 SMALL.solver(2).name='omp2';
ivan@107 179 SMALL.solver(2).param=struct(...
ivan@107 180 'epsilon',Edata,...
ivan@107 181 'maxatoms', maxatoms);
ivan@107 182
ivan@107 183 % Denoising the image - find the sparse solution in the learned
ivan@107 184 % dictionary for all patches in the image and the end it uses
ivan@107 185 % reconstruction function to reconstruct the patches and put them into a
ivan@107 186 % denoised image
ivan@107 187
ivan@107 188 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
ivan@107 189
idamnjanovic@6 190
idamnjanovic@6 191
idamnjanovic@6 192
idamnjanovic@6 193 %% show results %%
idamnjanovic@6 194 % This will show denoised images and dictionaries for all training sets.
idamnjanovic@6 195 % If you are not interested to see them and do not want clutter your
idamnjanovic@6 196 % screen comment following line
idamnjanovic@6 197
idamnjanovic@6 198 SMALL_ImgDeNoiseResult(SMALL);
idamnjanovic@6 199
idamnjanovic@6 200 time(1,i) = SMALL.DL(1).time;
idamnjanovic@6 201 psnr(1,i) = SMALL.solver(1).reconstructed.psnr;
idamnjanovic@6 202
idamnjanovic@6 203 time(2,i) = SMALL.DL(2).time;
idamnjanovic@6 204 psnr(2,i) = SMALL.solver(2).reconstructed.psnr;
idamnjanovic@6 205
idamnjanovic@6 206 clear SMALL
idamnjanovic@6 207 end
idamnjanovic@6 208
idamnjanovic@6 209 %% show time and psnr %%
idamnjanovic@6 210 figure('Name', 'KSVD vs SPAMS');
idamnjanovic@6 211
idamnjanovic@6 212 subplot(1,2,1); plot(Training_size, time(1,:), 'ro-', Training_size, time(2,:), 'b*-');
idamnjanovic@6 213 legend('KSVD','SPAMS',0);
idamnjanovic@6 214 title('Time vs Training size');
idamnjanovic@19 215 xlabel('Training Size (Num. of patches)');
idamnjanovic@19 216 ylabel('Time(s)');
idamnjanovic@6 217 subplot(1,2,2); plot(Training_size, psnr(1,:), 'ro-', Training_size, psnr(2,:), 'b*-');
idamnjanovic@6 218 legend('KSVD','SPAMS',0);
idamnjanovic@19 219 title('PSNR vs Training size');
idamnjanovic@19 220 xlabel('Training Size (Num. of patches)');
idamnjanovic@19 221 ylabel('PSNR(dB)');