ivan@153: %% Dictionary Learning for Image Denoising - KSVD vs Recursive Least Squares ivan@153: % ivan@153: % This file contains an example of how SMALLbox can be used to test different ivan@153: % dictionary learning techniques in Image Denoising problem. ivan@153: % It calls generateImageDenoiseProblem that will let you to choose image, ivan@153: % add noise and use noisy image to generate training set for dictionary ivan@153: % learning. ivan@153: % Two dictionary learning techniques were compared: ivan@153: % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient ivan@153: % Implementation of the K-SVD Algorithm using Batch Orthogonal ivan@153: % Matching Pursuit", Technical Report - CS, Technion, April 2008. ivan@153: % - RLS-DLA - Skretting, K.; Engan, K.; , "Recursive Least Squares ivan@153: % Dictionary Learning Algorithm," Signal Processing, IEEE Transactions on, ivan@153: % vol.58, no.4, pp.2121-2130, April 2010 ivan@153: % ivan@153: ivan@153: ivan@153: % Centre for Digital Music, Queen Mary, University of London. ivan@153: % This file copyright 2011 Ivan Damnjanovic. ivan@153: % ivan@153: % This program is free software; you can redistribute it and/or ivan@153: % modify it under the terms of the GNU General Public License as ivan@153: % published by the Free Software Foundation; either version 2 of the ivan@153: % License, or (at your option) any later version. See the file ivan@153: % COPYING included with this distribution for more information. ivan@153: % ivan@153: %% ivan@153: ivan@153: ivan@153: ivan@153: % If you want to load the image outside of generateImageDenoiseProblem ivan@153: % function uncomment following lines. This can be useful if you want to ivan@153: % denoise more then one image for example. ivan@153: % Here we are loading test_image.mat that contains structure with 5 images : lena, ivan@153: % barbara,boat, house and peppers. ivan@153: clear; ivan@153: TMPpath=pwd; ivan@153: FS=filesep; luis@186: [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m')); ivan@153: cd([pathstr1,FS,'data',FS,'images']); ivan@153: load('test_image.mat'); ivan@153: cd(TMPpath); ivan@153: ivan@153: % Deffining the noise levels that we want to test ivan@153: ivan@153: noise_level=[10 20 25 50 100]; ivan@153: ivan@153: % Here we loop through different noise levels and images ivan@153: ivan@153: for noise_ind=2:2 ivan@153: for im_num=2:2 ivan@153: ivan@153: % Defining Image Denoising Problem as Dictionary Learning ivan@153: % Problem. As an input we set the number of training patches. ivan@153: ivan@153: SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind)); ivan@153: SMALL.Problem.name=int2str(im_num); ivan@153: ivan@153: Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain; ivan@153: maxatoms = floor(prod(SMALL.Problem.blocksize)/2); ivan@153: ivan@153: ivan@153: %% ivan@153: % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary ivan@153: % Boris Mailhe ksvd update implentation omp is the same as with Rubinstein ivan@153: % implementation ivan@153: ivan@153: ivan@153: % Initialising solver structure ivan@153: % Setting solver structure fields (toolbox, name, param, solution, ivan@153: % reconstructed and time) to zero values ivan@153: ivan@153: SMALL.solver(1)=SMALL_init_solver; ivan@153: ivan@153: % Defining the parameters needed for image denoising ivan@153: ivan@153: SMALL.solver(1).toolbox='ompbox'; ivan@153: SMALL.solver(1).name='omp2'; ivan@153: SMALL.solver(1).param=struct(... ivan@153: 'epsilon',Edata,... ivan@153: 'maxatoms', maxatoms); ivan@153: ivan@153: % Initialising Dictionary structure ivan@153: % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@153: % to zero values ivan@153: ivan@153: SMALL.DL(1)=SMALL_init_DL('TwoStepDL', 'KSVD', '', 1); ivan@153: ivan@153: ivan@153: % Defining the parameters for KSVD ivan@153: % In this example we are learning 256 atoms in 20 iterations, so that ivan@153: % every patch in the training set can be represented with target error in ivan@153: % L2-norm (EData) ivan@153: % Type help ksvd in MATLAB prompt for more options. ivan@153: ivan@153: ivan@153: SMALL.DL(1).param=struct(... ivan@153: 'solver', SMALL.solver(1),... ivan@153: 'initdict', SMALL.Problem.initdict,... ivan@153: 'dictsize', SMALL.Problem.p,... ivan@153: 'iternum', 20,... ivan@153: 'show_dict', 1); ivan@153: ivan@153: % Learn the dictionary ivan@153: ivan@153: SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); ivan@153: ivan@153: % Set SMALL.Problem.A dictionary ivan@153: % (backward compatiblity with SPARCO: solver structure communicate ivan@153: % only with Problem structure, ie no direct communication between DL and ivan@153: % solver structures) ivan@153: ivan@153: SMALL.Problem.A = SMALL.DL(1).D; ivan@161: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem); ivan@153: ivan@153: % Denoising the image - find the sparse solution in the learned ivan@153: % dictionary for all patches in the image and the end it uses ivan@153: % reconstruction function to reconstruct the patches and put them into a ivan@153: % denoised image ivan@153: ivan@153: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); ivan@153: ivan@153: %% ivan@153: % Use MOD Dictionary Learning Algorithm to Learn overcomplete dictionary ivan@153: % Boris Mailhe MOD update implentation omp is the Ron Rubinstein ivan@153: % implementation ivan@153: ivan@153: ivan@153: % Initialising solver structure ivan@153: % Setting solver structure fields (toolbox, name, param, solution, ivan@153: % reconstructed and time) to zero values ivan@153: ivan@153: SMALL.solver(2)=SMALL_init_solver; ivan@153: ivan@153: % Defining the parameters needed for image denoising ivan@153: ivan@153: SMALL.solver(2).toolbox='ompbox'; ivan@153: SMALL.solver(2).name='omp2'; ivan@153: SMALL.solver(2).param=struct(... ivan@153: 'epsilon',Edata,... ivan@153: 'maxatoms', maxatoms); ivan@153: ivan@153: % Initialising Dictionary structure ivan@153: % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@153: % to zero values ivan@153: ivan@153: SMALL.DL(2)=SMALL_init_DL('TwoStepDL', 'MOD', '', 1); ivan@153: ivan@153: ivan@153: % Defining the parameters for MOD ivan@153: % In this example we are learning 256 atoms in 20 iterations, so that ivan@153: % every patch in the training set can be represented with target error in ivan@153: % L2-norm (EData) ivan@153: % Type help ksvd in MATLAB prompt for more options ivan@153: ivan@153: SMALL.DL(2).param=struct(... ivan@153: 'solver', SMALL.solver(2),... ivan@153: 'initdict', SMALL.Problem.initdict,... ivan@153: 'dictsize', SMALL.Problem.p,... ivan@153: 'iternum', 20,... ivan@153: 'show_dict', 1); ivan@153: ivan@153: % Learn the dictionary ivan@153: ivan@153: SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); ivan@153: ivan@153: % Set SMALL.Problem.A dictionary ivan@153: % (backward compatiblity with SPARCO: solver structure communicate ivan@153: % only with Problem structure, ie no direct communication between DL and ivan@153: % solver structures) ivan@153: ivan@153: SMALL.Problem.A = SMALL.DL(2).D; ivan@161: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem); ivan@153: ivan@153: % Denoising the image - find the sparse solution in the learned ivan@153: % dictionary for all patches in the image and the end it uses ivan@153: % reconstruction function to reconstruct the patches and put them into a ivan@153: % denoised image ivan@153: ivan@153: SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); ivan@153: %% ivan@153: % Use OLS Dictionary Learning Algorithm to Learn overcomplete dictionary ivan@153: % Boris Mailhe ksvd update implentation omp is the Ron Rubinstein ivan@153: % implementation ivan@153: ivan@153: ivan@153: % Initialising solver structure ivan@153: % Setting solver structure fields (toolbox, name, param, solution, ivan@153: % reconstructed and time) to zero values ivan@153: ivan@153: SMALL.solver(3)=SMALL_init_solver; ivan@153: ivan@153: % Defining the parameters needed for image denoising ivan@153: ivan@153: SMALL.solver(3).toolbox='ompbox'; ivan@153: SMALL.solver(3).name='omp2'; ivan@153: SMALL.solver(3).param=struct(... ivan@153: 'epsilon',Edata,... ivan@153: 'maxatoms', maxatoms); ivan@153: ivan@153: % Initialising Dictionary structure ivan@153: % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@153: % to zero values ivan@153: ivan@153: SMALL.DL(3)=SMALL_init_DL('TwoStepDL', 'ols', '', 1); ivan@153: ivan@153: ivan@153: % Defining the parameters for KSVD ivan@153: % In this example we are learning 256 atoms in 20 iterations, so that ivan@153: % every patch in the training set can be represented with target error in ivan@153: % L2-norm (EData) ivan@153: % Type help ksvd in MATLAB prompt for more options. ivan@153: ivan@153: ivan@153: SMALL.DL(3).param=struct(... ivan@153: 'solver', SMALL.solver(3),... ivan@153: 'initdict', SMALL.Problem.initdict,... ivan@153: 'dictsize', SMALL.Problem.p,... ivan@153: 'iternum', 20,... ivan@153: 'learningRate', 0.1,... ivan@153: 'show_dict', 1); ivan@153: ivan@153: % Learn the dictionary ivan@153: ivan@153: SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3)); ivan@153: ivan@153: % Set SMALL.Problem.A dictionary ivan@153: % (backward compatiblity with SPARCO: solver structure communicate ivan@153: % only with Problem structure, ie no direct communication between DL and ivan@153: % solver structures) ivan@153: ivan@153: SMALL.Problem.A = SMALL.DL(3).D; ivan@161: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem); ivan@153: ivan@153: % Denoising the image - find the sparse solution in the learned ivan@153: % dictionary for all patches in the image and the end it uses ivan@153: % reconstruction function to reconstruct the patches and put them into a ivan@153: % denoised image ivan@153: ivan@153: SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); ivan@153: %% ivan@153: % Use Mailhe Dictionary Learning Algorithm to Learn overcomplete dictionary ivan@153: % Boris Mailhe ksvd update implentation omp is the Ron Rubinstein ivan@153: % implementation ivan@153: ivan@153: ivan@153: % Initialising solver structure ivan@153: % Setting solver structure fields (toolbox, name, param, solution, ivan@153: % reconstructed and time) to zero values ivan@153: ivan@153: SMALL.solver(4)=SMALL_init_solver; ivan@153: ivan@153: % Defining the parameters needed for image denoising ivan@153: ivan@153: SMALL.solver(4).toolbox='ompbox'; ivan@153: SMALL.solver(4).name='omp2'; ivan@153: SMALL.solver(4).param=struct(... ivan@153: 'epsilon',Edata,... ivan@153: 'maxatoms', maxatoms); ivan@153: ivan@153: % Initialising Dictionary structure ivan@153: % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@153: % to zero values ivan@153: bmailhe@212: SMALL.DL(4)=SMALL_init_DL('TwoStepDL', 'opt', '', 1); ivan@153: ivan@153: ivan@153: % Defining the parameters for KSVD ivan@153: % In this example we are learning 256 atoms in 20 iterations, so that ivan@153: % every patch in the training set can be represented with target error in ivan@153: % L2-norm (EData) ivan@153: % Type help ksvd in MATLAB prompt for more options. ivan@153: ivan@153: ivan@153: SMALL.DL(4).param=struct(... ivan@153: 'solver', SMALL.solver(4),... ivan@153: 'initdict', SMALL.Problem.initdict,... ivan@153: 'dictsize', SMALL.Problem.p,... ivan@153: 'iternum', 20,... ivan@153: 'learningRate', 2,... ivan@153: 'show_dict', 1); ivan@153: ivan@153: % Learn the dictionary ivan@153: ivan@153: SMALL.DL(4) = SMALL_learn(SMALL.Problem, SMALL.DL(4)); ivan@153: ivan@153: % Set SMALL.Problem.A dictionary ivan@153: % (backward compatiblity with SPARCO: solver structure communicate ivan@153: % only with Problem structure, ie no direct communication between DL and ivan@153: % solver structures) ivan@153: ivan@153: SMALL.Problem.A = SMALL.DL(4).D; ivan@161: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem); ivan@153: ivan@153: % Denoising the image - find the sparse solution in the learned ivan@153: % dictionary for all patches in the image and the end it uses ivan@153: % reconstruction function to reconstruct the patches and put them into a ivan@153: % denoised image ivan@153: ivan@153: SMALL.solver(4)=SMALL_solve(SMALL.Problem, SMALL.solver(4)); ivan@153: ivan@153: %% show results %% ivan@153: ivan@153: SMALL_ImgDeNoiseResult(SMALL); ivan@153: ivan@153: %clear SMALL; ivan@153: end ivan@153: end ivan@153: