annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_SPAMS_lambda.m @ 108:b14e1f6ee4be ver_1.1

Merge
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Wed, 18 May 2011 11:50:28 +0100
parents dab78a3598b6
children 8e660fd14774
rev   line source
idamnjanovic@6 1 %% DICTIONARY LEARNING FOR IMAGE DENOISING
idamnjanovic@25 2 %
ivan@107 3 %
ivan@107 4 % This file contains an example of how SMALLbox can be used to test different
ivan@107 5 % dictionary learning techniques in Image Denoising problem.
ivan@107 6 % This example can be used to test SPAMS for different values of
ivan@107 7 % parameter lambda. In no way it represents extensive testing of image
ivan@107 8 % denoising. It should only give an idea how SMALL structure can be used
ivan@107 9 % for testing.
ivan@107 10
ivan@107 11 %
idamnjanovic@25 12 % Centre for Digital Music, Queen Mary, University of London.
idamnjanovic@25 13 % This file copyright 2010 Ivan Damnjanovic.
idamnjanovic@25 14 %
idamnjanovic@25 15 % This program is free software; you can redistribute it and/or
idamnjanovic@25 16 % modify it under the terms of the GNU General Public License as
idamnjanovic@25 17 % published by the Free Software Foundation; either version 2 of the
idamnjanovic@25 18 % License, or (at your option) any later version. See the file
idamnjanovic@25 19 % COPYING included with this distribution for more information.
idamnjanovic@6 20 %%
idamnjanovic@6 21
idamnjanovic@6 22 clear all;
idamnjanovic@6 23
idamnjanovic@6 24 %% Load an image
idamnjanovic@6 25 TMPpath=pwd;
idamnjanovic@6 26 FS=filesep;
idamnjanovic@6 27 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@6 28 cd([pathstr1,FS,'data',FS,'images']);
idamnjanovic@6 29 [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
idamnjanovic@6 30 [pathstr, name, ext, versn] = fileparts(filename);
idamnjanovic@6 31 test_image = imread(filename);
idamnjanovic@6 32 test_image = double(test_image);
idamnjanovic@6 33 cd(TMPpath);
idamnjanovic@6 34 %%
idamnjanovic@6 35
idamnjanovic@6 36 % number of different values we want to test
idamnjanovic@6 37
idamnjanovic@6 38 n =4;
idamnjanovic@6 39
idamnjanovic@6 40 lambda=zeros(1,n);
idamnjanovic@6 41 time = zeros(2,n);
idamnjanovic@6 42 psnr = zeros(2,n);
idamnjanovic@6 43
idamnjanovic@6 44 for i=1:n
idamnjanovic@6 45
idamnjanovic@6 46 % Here we want to test time spent and quality of denoising for
idamnjanovic@6 47 % different lambda parameters.
idamnjanovic@6 48
idamnjanovic@6 49 lambda(i)=1+i*0.5;
idamnjanovic@6 50
idamnjanovic@6 51 % Defining Image Denoising Problem as Dictionary Learning Problem.
idamnjanovic@6 52
idamnjanovic@6 53 SMALL.Problem = generateImageDenoiseProblem(test_image);
idamnjanovic@6 54 SMALL.Problem.name=name;
idamnjanovic@6 55 %%
idamnjanovic@6 56 % Use SPAMS Online Dictionary Learning Algorithm
idamnjanovic@6 57 % to Learn overcomplete dictionary (Julien Mairal 2009)
idamnjanovic@6 58
idamnjanovic@6 59 % Initialising Dictionary structure
idamnjanovic@6 60 % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@6 61 % to zero values
idamnjanovic@6 62
idamnjanovic@6 63 SMALL.DL(1)=SMALL_init_DL();
idamnjanovic@6 64
idamnjanovic@6 65 % Defining fields needed for dictionary learning
idamnjanovic@6 66
idamnjanovic@6 67 SMALL.DL(1).toolbox = 'SPAMS';
idamnjanovic@6 68 SMALL.DL(1).name = 'mexTrainDL';
idamnjanovic@6 69
idamnjanovic@6 70 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
idamnjanovic@6 71
idamnjanovic@6 72 SMALL.DL(1).param=struct(...
idamnjanovic@6 73 'D', SMALL.Problem.initdict,...
idamnjanovic@6 74 'K', SMALL.Problem.p,...
idamnjanovic@6 75 'lambda', lambda(i),...
idamnjanovic@6 76 'iter', 200,...
idamnjanovic@6 77 'mode', 3,...
idamnjanovic@6 78 'modeD', 0);
idamnjanovic@6 79
idamnjanovic@6 80 % Learn the dictionary
idamnjanovic@6 81
idamnjanovic@6 82 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
idamnjanovic@6 83
idamnjanovic@6 84 % Set SMALL.Problem.A dictionary
idamnjanovic@6 85 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 86 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 87 % solver structures)
idamnjanovic@6 88
idamnjanovic@6 89 SMALL.Problem.A = SMALL.DL(1).D;
ivan@107 90 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem);
ivan@107 91
ivan@107 92 %%
ivan@107 93 % Initialising solver structure
ivan@107 94 % Setting solver structure fields (toolbox, name, param, solution,
ivan@107 95 % reconstructed and time) to zero values
ivan@107 96
ivan@107 97 SMALL.solver(1)=SMALL_init_solver;
ivan@107 98
ivan@107 99 % Defining the parameters needed for image denoising
ivan@107 100 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
ivan@107 101 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
ivan@107 102
ivan@107 103 SMALL.solver(1).toolbox='ompbox';
ivan@107 104 SMALL.solver(1).name='omp2';
ivan@107 105 SMALL.solver(1).param=struct(...
ivan@107 106 'epsilon',Edata,...
ivan@107 107 'maxatoms', maxatoms);
ivan@107 108
ivan@107 109 % Denoising the image - find the sparse solution in the learned
ivan@107 110 % dictionary for all patches in the image and the end it uses
ivan@107 111 % reconstruction function to reconstruct the patches and put them into a
ivan@107 112 % denoised image
ivan@107 113
ivan@107 114 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 115
idamnjanovic@6 116 %% show results %%
idamnjanovic@6 117 % This will show denoised image and dictionary for all lambdas. If you
idamnjanovic@6 118 % are not interested to see it and do not want clutter your screen
idamnjanovic@6 119 % comment following line
idamnjanovic@6 120
idamnjanovic@6 121 SMALL_ImgDeNoiseResult(SMALL);
idamnjanovic@6 122
idamnjanovic@6 123
idamnjanovic@6 124 time(1,i) = SMALL.DL(1).time;
idamnjanovic@6 125 psnr(1,i) = SMALL.solver(1).reconstructed.psnr;
idamnjanovic@6 126
idamnjanovic@6 127 clear SMALL
idamnjanovic@6 128 end
idamnjanovic@6 129
idamnjanovic@6 130 %% show time and psnr %%
idamnjanovic@6 131 figure('Name', 'SPAMS LAMBDA TEST');
idamnjanovic@6 132
idamnjanovic@6 133 subplot(1,2,1); plot(lambda, time(1,:), 'ro-');
idamnjanovic@6 134 title('time vs lambda');
idamnjanovic@6 135 subplot(1,2,2); plot(lambda, psnr(1,:), 'b*-');
idamnjanovic@6 136 title('PSNR vs lambda');
idamnjanovic@6 137