annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_Training_size.m @ 99:e22f8494c5ff

Merge
author Mark Plumbley <mark.plumbley@eecs.qmul.ac.uk>
date Tue, 12 Apr 2011 14:33:24 +0100
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 20100 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 % We tested time and psnr for two dictionary learning techniques. This
idamnjanovic@6 18 % example does not represnt any extensive testing. The aim of this
idamnjanovic@6 19 % example is just to show how SMALL structure can be used for testing.
idamnjanovic@6 20 %
idamnjanovic@6 21 % Two dictionary learning techniques were compared:
idamnjanovic@6 22 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
idamnjanovic@6 23 % Implementation of the K-SVD Algorithm using Batch Orthogonal
idamnjanovic@6 24 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
idamnjanovic@6 25 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
idamnjanovic@6 26 % Dictionary Learning for Sparse Coding. International
idamnjanovic@6 27 % Conference on Machine Learning,Montreal, Canada, 2009
idamnjanovic@6 28 %
idamnjanovic@6 29 %
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;
idamnjanovic@6 80 SMALL.DL(1).param=struct(...
idamnjanovic@6 81 'Edata', Edata,...
idamnjanovic@6 82 'initdict', SMALL.Problem.initdict,...
idamnjanovic@6 83 'dictsize', SMALL.Problem.p,...
idamnjanovic@19 84 'iternum', 20);
idamnjanovic@19 85 %'memusage', 'high');
idamnjanovic@6 86
idamnjanovic@6 87 % Learn the dictionary
idamnjanovic@6 88
idamnjanovic@6 89 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
idamnjanovic@6 90
idamnjanovic@6 91 % Set SMALL.Problem.A dictionary
idamnjanovic@6 92 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 93 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 94 % solver structures)
idamnjanovic@6 95
idamnjanovic@6 96 SMALL.Problem.A = SMALL.DL(1).D;
idamnjanovic@6 97
idamnjanovic@6 98
idamnjanovic@6 99 %%
idamnjanovic@6 100 % Initialising solver structure
idamnjanovic@6 101 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 102 % reconstructed and time) to zero values
idamnjanovic@6 103
idamnjanovic@6 104
idamnjanovic@6 105 SMALL.solver(1)=SMALL_init_solver;
idamnjanovic@6 106
idamnjanovic@6 107 % Defining the parameters needed for denoising
idamnjanovic@6 108
idamnjanovic@6 109 SMALL.solver(1).toolbox='ompbox';
idamnjanovic@6 110 SMALL.solver(1).name='ompdenoise';
idamnjanovic@6 111
idamnjanovic@6 112 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
idamnjanovic@6 113 % but backward compatible with KSVD definition of denoising
idamnjanovic@6 114
idamnjanovic@6 115 SMALL.solver(1)=SMALL_denoise(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 116
idamnjanovic@6 117 %%
idamnjanovic@6 118 % Use SPAMS Online Dictionary Learning Algorithm
idamnjanovic@6 119 % to Learn overcomplete dictionary (Julien Mairal 2009)
idamnjanovic@6 120 % (If you have not installed SPAMS please comment the following two cells)
idamnjanovic@6 121
idamnjanovic@6 122 % Initialising Dictionary structure
idamnjanovic@6 123 % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@6 124 % to zero values
idamnjanovic@6 125
idamnjanovic@6 126 SMALL.DL(2)=SMALL_init_DL();
idamnjanovic@6 127
idamnjanovic@6 128 % Defining fields needed for dictionary learning
idamnjanovic@6 129
idamnjanovic@6 130 SMALL.DL(2).toolbox = 'SPAMS';
idamnjanovic@6 131 SMALL.DL(2).name = 'mexTrainDL';
idamnjanovic@6 132
idamnjanovic@6 133 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
idamnjanovic@6 134
idamnjanovic@6 135 SMALL.DL(2).param=struct(...
idamnjanovic@6 136 'D', SMALL.Problem.initdict,...
idamnjanovic@6 137 'K', SMALL.Problem.p,...
idamnjanovic@6 138 'lambda', 2,...
idamnjanovic@6 139 'iter', 300,...
idamnjanovic@6 140 'mode', 3,...
idamnjanovic@6 141 'modeD', 0 );
idamnjanovic@6 142
idamnjanovic@6 143 % Learn the dictionary
idamnjanovic@6 144
idamnjanovic@6 145 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
idamnjanovic@6 146
idamnjanovic@6 147 % Set SMALL.Problem.A dictionary
idamnjanovic@6 148 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 149 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 150 % solver structures)
idamnjanovic@6 151
idamnjanovic@6 152 SMALL.Problem.A = SMALL.DL(2).D;
idamnjanovic@6 153
idamnjanovic@6 154
idamnjanovic@6 155 %%
idamnjanovic@6 156 % Initialising solver structure
idamnjanovic@6 157 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 158 % reconstructed and time) to zero values
idamnjanovic@6 159
idamnjanovic@6 160 SMALL.solver(2)=SMALL_init_solver;
idamnjanovic@6 161
idamnjanovic@6 162 % Defining the parameters needed for denoising
idamnjanovic@6 163
idamnjanovic@6 164 SMALL.solver(2).toolbox='ompbox';
idamnjanovic@6 165 SMALL.solver(2).name='ompdenoise';
idamnjanovic@6 166
idamnjanovic@6 167 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
idamnjanovic@6 168 % but backward compatible with KSVD definition of denoising
idamnjanovic@6 169
idamnjanovic@6 170 SMALL.solver(2)=SMALL_denoise(SMALL.Problem, SMALL.solver(2));
idamnjanovic@6 171
idamnjanovic@6 172
idamnjanovic@6 173
idamnjanovic@6 174 %% show results %%
idamnjanovic@6 175 % This will show denoised images and dictionaries for all training sets.
idamnjanovic@6 176 % If you are not interested to see them and do not want clutter your
idamnjanovic@6 177 % screen comment following line
idamnjanovic@6 178
idamnjanovic@6 179 SMALL_ImgDeNoiseResult(SMALL);
idamnjanovic@6 180
idamnjanovic@6 181 time(1,i) = SMALL.DL(1).time;
idamnjanovic@6 182 psnr(1,i) = SMALL.solver(1).reconstructed.psnr;
idamnjanovic@6 183
idamnjanovic@6 184 time(2,i) = SMALL.DL(2).time;
idamnjanovic@6 185 psnr(2,i) = SMALL.solver(2).reconstructed.psnr;
idamnjanovic@6 186
idamnjanovic@6 187 clear SMALL
idamnjanovic@6 188 end
idamnjanovic@6 189
idamnjanovic@6 190 %% show time and psnr %%
idamnjanovic@6 191 figure('Name', 'KSVD vs SPAMS');
idamnjanovic@6 192
idamnjanovic@6 193 subplot(1,2,1); plot(Training_size, time(1,:), 'ro-', Training_size, time(2,:), 'b*-');
idamnjanovic@6 194 legend('KSVD','SPAMS',0);
idamnjanovic@6 195 title('Time vs Training size');
idamnjanovic@19 196 xlabel('Training Size (Num. of patches)');
idamnjanovic@19 197 ylabel('Time(s)');
idamnjanovic@6 198 subplot(1,2,2); plot(Training_size, psnr(1,:), 'ro-', Training_size, psnr(2,:), 'b*-');
idamnjanovic@6 199 legend('KSVD','SPAMS',0);
idamnjanovic@19 200 title('PSNR vs Training size');
idamnjanovic@19 201 xlabel('Training Size (Num. of patches)');
idamnjanovic@19 202 ylabel('PSNR(dB)');