idamnjanovic@42: %% DICTIONARY LEARNING FOR IMAGE DENOISING idamnjanovic@42: % This file contains an example of how SMALLbox can be used to test different idamnjanovic@42: % dictionary learning techniques in Image Denoising problem. idamnjanovic@42: % It calls generateImageDenoiseProblem that will let you to choose image, idamnjanovic@42: % add noise and use noisy image to generate training set for dictionary idamnjanovic@42: % learning. maria@83: % Two dictionary learning techniques were compared: idamnjanovic@42: % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient idamnjanovic@42: % Implementation of the K-SVD Algorithm using Batch Orthogonal idamnjanovic@42: % Matching Pursuit", Technical Report - CS, Technion, April 2008. maria@83: % - RLS-DLA - Skretting, K.; Engan, K.; , "Recursive Least Squares maria@83: % Dictionary Learning Algorithm," Signal Processing, IEEE Transactions on, maria@83: % vol.58, no.4, pp.2121-2130, April 2010 idamnjanovic@42: % maria@83: maria@83: maria@83: % Centre for Digital Music, Queen Mary, University of London. maria@83: % This file copyright 2011 Ivan Damnjanovic. idamnjanovic@42: % maria@83: % This program is free software; you can redistribute it and/or maria@83: % modify it under the terms of the GNU General Public License as maria@83: % published by the Free Software Foundation; either version 2 of the maria@83: % License, or (at your option) any later version. See the file maria@83: % COPYING included with this distribution for more information. maria@83: % idamnjanovic@42: %% idamnjanovic@42: idamnjanovic@42: idamnjanovic@42: idamnjanovic@42: % If you want to load the image outside of generateImageDenoiseProblem idamnjanovic@42: % function uncomment following lines. This can be useful if you want to idamnjanovic@42: % denoise more then one image for example. maria@83: % Here we are loading test_image.mat that contains structure with 5 images : lena, maria@83: % barbara,boat, house and peppers. idamnjanovic@42: clear; idamnjanovic@42: TMPpath=pwd; idamnjanovic@42: FS=filesep; idamnjanovic@42: [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); idamnjanovic@42: cd([pathstr1,FS,'data',FS,'images']); idamnjanovic@42: load('test_image.mat'); ivan@77: cd(TMPpath); maria@83: maria@83: % Deffining the noise levels that we want to test idamnjanovic@42: idamnjanovic@42: noise_level=[10 20 25 50 100]; maria@83: maria@83: % Here we loop through different noise levels and images maria@83: maria@83: for noise_ind=2:2 maria@83: for im_num=1:1 maria@83: idamnjanovic@42: % Defining Image Denoising Problem as Dictionary Learning idamnjanovic@42: % Problem. As an input we set the number of training patches. maria@83: idamnjanovic@65: SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind)); ivan@77: SMALL.Problem.name=int2str(im_num); ivan@84: % results structure is to store all results idamnjanovic@42: idamnjanovic@42: results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr; idamnjanovic@42: idamnjanovic@42: %% idamnjanovic@42: % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary idamnjanovic@42: idamnjanovic@42: % Initialising Dictionary structure idamnjanovic@42: % Setting Dictionary structure fields (toolbox, name, param, D and time) idamnjanovic@42: % to zero values idamnjanovic@42: idamnjanovic@42: SMALL.DL(1)=SMALL_init_DL(); idamnjanovic@42: idamnjanovic@42: % Defining the parameters needed for dictionary learning idamnjanovic@42: idamnjanovic@42: SMALL.DL(1).toolbox = 'KSVD'; idamnjanovic@42: SMALL.DL(1).name = 'ksvd'; idamnjanovic@42: idamnjanovic@42: % Defining the parameters for KSVD idamnjanovic@42: % In this example we are learning 256 atoms in 20 iterations, so that idamnjanovic@42: % every patch in the training set can be represented with target error in idamnjanovic@42: % L2-norm (EData) idamnjanovic@42: % Type help ksvd in MATLAB prompt for more options. idamnjanovic@42: idamnjanovic@42: Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain; idamnjanovic@42: maxatoms = floor(prod(SMALL.Problem.blocksize)/2); idamnjanovic@42: SMALL.DL(1).param=struct(... idamnjanovic@42: 'Edata', Edata,... idamnjanovic@42: 'initdict', SMALL.Problem.initdict,... idamnjanovic@42: 'dictsize', SMALL.Problem.p,... idamnjanovic@42: 'exact', 1, ... idamnjanovic@42: 'iternum', 20,... idamnjanovic@42: 'memusage', 'high'); idamnjanovic@42: idamnjanovic@42: % Learn the dictionary idamnjanovic@42: idamnjanovic@42: SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); idamnjanovic@42: idamnjanovic@42: % Set SMALL.Problem.A dictionary idamnjanovic@42: % (backward compatiblity with SPARCO: solver structure communicate idamnjanovic@42: % only with Problem structure, ie no direct communication between DL and idamnjanovic@42: % solver structures) idamnjanovic@42: idamnjanovic@42: SMALL.Problem.A = SMALL.DL(1).D; idamnjanovic@42: SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); idamnjanovic@42: idamnjanovic@42: %% idamnjanovic@42: % Initialising solver structure idamnjanovic@42: % Setting solver structure fields (toolbox, name, param, solution, idamnjanovic@42: % reconstructed and time) to zero values idamnjanovic@42: idamnjanovic@42: SMALL.solver(1)=SMALL_init_solver; idamnjanovic@42: idamnjanovic@42: % Defining the parameters needed for image denoising idamnjanovic@42: idamnjanovic@42: SMALL.solver(1).toolbox='ompbox'; idamnjanovic@42: SMALL.solver(1).name='omp2'; idamnjanovic@42: SMALL.solver(1).param=struct(... idamnjanovic@42: 'epsilon',Edata,... idamnjanovic@42: 'maxatoms', maxatoms); idamnjanovic@42: ivan@84: % Denoising the image - find the sparse solution in the learned ivan@84: % dictionary for all patches in the image and the end it uses ivan@84: % reconstruction function to reconstruct the patches and put them into a ivan@84: % denoised image idamnjanovic@42: idamnjanovic@42: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); ivan@84: ivan@84: % Show PSNR after reconstruction ivan@84: idamnjanovic@42: SMALL.solver(1).reconstructed.psnr ivan@84: idamnjanovic@42: %% ivan@84: % For comparison purposes we will denoise image with overcomplete DCT ivan@84: % here ivan@84: % Set SMALL.Problem.A dictionary to be oDCT (i.e. Problem.initdict - ivan@84: % since initial dictionaruy is already set to be oDCT when generating the ivan@84: % denoising problem idamnjanovic@42: idamnjanovic@42: SMALL.Problem.A = SMALL.Problem.initdict; idamnjanovic@42: SMALL.DL(2).D=SMALL.Problem.initdict; ivan@84: ivan@84: % Setting up reconstruction function ivan@84: idamnjanovic@42: SparseDict=0; idamnjanovic@42: SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem, SparseDict); idamnjanovic@42: idamnjanovic@42: % Initialising solver structure idamnjanovic@42: % Setting solver structure fields (toolbox, name, param, solution, idamnjanovic@42: % reconstructed and time) to zero values idamnjanovic@42: idamnjanovic@42: SMALL.solver(2)=SMALL_init_solver; idamnjanovic@42: idamnjanovic@42: % Defining the parameters needed for image denoising idamnjanovic@42: idamnjanovic@42: SMALL.solver(2).toolbox='ompbox'; idamnjanovic@42: SMALL.solver(2).name='omp2'; idamnjanovic@42: SMALL.solver(2).param=struct(... idamnjanovic@42: 'epsilon',Edata,... idamnjanovic@42: 'maxatoms', maxatoms); idamnjanovic@42: ivan@84: % Denoising the image - find the sparse solution in the learned ivan@84: % dictionary for all patches in the image and the end it uses ivan@84: % reconstruction function to reconstruct the patches and put them into a ivan@84: % denoised image idamnjanovic@42: idamnjanovic@42: SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); idamnjanovic@42: %% ivan@84: % In the b1 field all patches from the image are stored. For RLS-DLA we ivan@84: % will first exclude all the patches that have l2 norm smaller then ivan@84: % threshold and then take min(40000, number_of_remaining_patches) in ivan@84: % ascending order as our training set (SMALL.Problem.b) idamnjanovic@42: idamnjanovic@42: X=SMALL.Problem.b1; idamnjanovic@42: X_norm=sqrt(sum(X.^2, 1)); idamnjanovic@42: [X_norm_sort, p]=sort(X_norm); idamnjanovic@42: p1=p(X_norm_sort>Edata); maria@83: if size(p1,2)>40000 idamnjanovic@42: p2 = randperm(size(p1,2)); idamnjanovic@42: p2=sort(p2(1:40000)); idamnjanovic@42: size(p2,2) idamnjanovic@42: SMALL.Problem.b=X(:,p1(p2)); idamnjanovic@42: else idamnjanovic@42: size(p1,2) idamnjanovic@42: SMALL.Problem.b=X(:,p1); idamnjanovic@42: idamnjanovic@42: end idamnjanovic@42: ivan@84: % Forgetting factor for RLS-DLA algorithm, in this case we are using ivan@84: % fixed value ivan@84: idamnjanovic@42: lambda=0.9998 idamnjanovic@42: idamnjanovic@42: % Use Recursive Least Squares idamnjanovic@42: % to Learn overcomplete dictionary idamnjanovic@42: idamnjanovic@42: % Initialising Dictionary structure idamnjanovic@42: % Setting Dictionary structure fields (toolbox, name, param, D and time) idamnjanovic@42: % to zero values idamnjanovic@42: idamnjanovic@42: SMALL.DL(3)=SMALL_init_DL(); idamnjanovic@42: idamnjanovic@42: % Defining fields needed for dictionary learning idamnjanovic@42: idamnjanovic@42: SMALL.DL(3).toolbox = 'SMALL'; idamnjanovic@42: SMALL.DL(3).name = 'SMALL_rlsdla'; idamnjanovic@42: SMALL.DL(3).param=struct(... idamnjanovic@42: 'Edata', Edata,... idamnjanovic@42: 'initdict', SMALL.Problem.initdict,... idamnjanovic@42: 'dictsize', SMALL.Problem.p,... idamnjanovic@42: 'forgettingMode', 'FIX',... idamnjanovic@42: 'forgettingFactor', lambda); idamnjanovic@42: idamnjanovic@42: idamnjanovic@42: SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3)); idamnjanovic@42: idamnjanovic@42: % Initialising solver structure idamnjanovic@42: % Setting solver structure fields (toolbox, name, param, solution, idamnjanovic@42: % reconstructed and time) to zero values idamnjanovic@42: ivan@84: SMALL.Problem.A = SMALL.DL(3).D; ivan@84: SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); ivan@84: idamnjanovic@42: SMALL.solver(3)=SMALL_init_solver; idamnjanovic@42: idamnjanovic@42: % Defining the parameters needed for image denoising idamnjanovic@42: idamnjanovic@42: SMALL.solver(3).toolbox='ompbox'; idamnjanovic@42: SMALL.solver(3).name='omp2'; idamnjanovic@42: SMALL.solver(3).param=struct(... idamnjanovic@42: 'epsilon',Edata,... idamnjanovic@42: 'maxatoms', maxatoms); idamnjanovic@42: idamnjanovic@42: idamnjanovic@42: SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); ivan@84: idamnjanovic@42: SMALL.solver(3).reconstructed.psnr ivan@84: ivan@84: idamnjanovic@42: % show results % idamnjanovic@42: idamnjanovic@42: SMALL_ImgDeNoiseResult(SMALL); ivan@84: idamnjanovic@42: results(noise_ind,im_num).psnr.ksvd=SMALL.solver(1).reconstructed.psnr; idamnjanovic@42: results(noise_ind,im_num).psnr.odct=SMALL.solver(2).reconstructed.psnr; idamnjanovic@42: results(noise_ind,im_num).psnr.rlsdla=SMALL.solver(3).reconstructed.psnr; idamnjanovic@42: results(noise_ind,im_num).vmrse.ksvd=SMALL.solver(1).reconstructed.vmrse; idamnjanovic@42: results(noise_ind,im_num).vmrse.odct=SMALL.solver(2).reconstructed.vmrse; idamnjanovic@42: results(noise_ind,im_num).vmrse.rlsdla=SMALL.solver(3).reconstructed.vmrse; idamnjanovic@42: results(noise_ind,im_num).ssim.ksvd=SMALL.solver(1).reconstructed.ssim; idamnjanovic@42: results(noise_ind,im_num).ssim.odct=SMALL.solver(2).reconstructed.ssim; idamnjanovic@42: results(noise_ind,im_num).ssim.rlsdla=SMALL.solver(3).reconstructed.ssim; idamnjanovic@42: idamnjanovic@42: results(noise_ind,im_num).time.ksvd=SMALL.solver(1).time+SMALL.DL(1).time; idamnjanovic@42: results(noise_ind,im_num).time.rlsdla.time=SMALL.solver(3).time+SMALL.DL(3).time; idamnjanovic@42: %clear SMALL; idamnjanovic@42: end idamnjanovic@42: end idamnjanovic@42: save results.mat results