annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_Training_size.m @ 217:8b3c71bb44eb luisf_dev

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