ivan@155: %% Dictionary Learning for Image Denoising - KSVD vs Recursive Least Squares ivan@155: % ivan@155: % This file contains an example of how SMALLbox can be used to test different ivan@155: % dictionary learning techniques in Image Denoising problem. ivan@155: % It calls generateImageDenoiseProblem that will let you to choose image, ivan@155: % add noise and use noisy image to generate training set for dictionary ivan@155: % learning. ivan@155: % Two dictionary learning techniques were compared: ivan@155: % ivan@155: % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient ivan@155: % Implementation of the K-SVD Algorithm using Batch Orthogonal ivan@155: % Matching Pursuit", Technical Report - CS, Technion, April 2008. ivan@155: % ivan@155: % - MMDL - M. Yaghoobi, T. Blumensath and M. Davies, "Dictionary Learning ivan@155: % for Sparse Approximations with the Majorization Method", IEEE ivan@155: % Trans. on Signal Processing, Vol. 57, No. 6, pp 2178-2191, 2009. ivan@155: ivan@155: ivan@155: % Centre for Digital Music, Queen Mary, University of London. ivan@155: % This file copyright 2011 Ivan Damnjanovic. ivan@155: % ivan@155: % This program is free software; you can redistribute it and/or ivan@155: % modify it under the terms of the GNU General Public License as ivan@155: % published by the Free Software Foundation; either version 2 of the ivan@155: % License, or (at your option) any later version. See the file ivan@155: % COPYING included with this distribution for more information. ivan@155: % ivan@155: %% ivan@155: ivan@155: ivan@155: ivan@155: % If you want to load the image outside of generateImageDenoiseProblem ivan@155: % function uncomment following lines. This can be useful if you want to ivan@155: % denoise more then one image for example. ivan@155: % Here we are loading test_image.mat that contains structure with 5 images : lena, ivan@155: % barbara,boat, house and peppers. ivan@155: clear; ivan@155: TMPpath=pwd; ivan@155: FS=filesep; luis@186: [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m')); ivan@155: cd([pathstr1,FS,'data',FS,'images']); ivan@155: load('test_image.mat'); ivan@155: cd(TMPpath); ivan@155: ivan@155: % Deffining the noise levels that we want to test ivan@155: ivan@155: noise_level=[10 20 25 50 100]; ivan@155: ivan@155: % Here we loop through different noise levels and images ivan@155: ivan@155: for noise_ind=2:2 ivan@155: for im_num=1:1 ivan@155: ivan@155: % Defining Image Denoising Problem as Dictionary Learning ivan@155: % Problem. As an input we set the number of training patches. ivan@155: ivan@155: SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind)); ivan@155: SMALL.Problem.name=int2str(im_num); ivan@155: ivan@155: Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain; ivan@155: maxatoms = floor(prod(SMALL.Problem.blocksize)/2); ivan@155: ivan@155: % results structure is to store all results ivan@155: ivan@155: results(noise_ind,im_num).noisy_psnr=SMALL.Problem.noisy_psnr; ivan@155: ivan@155: %% ivan@155: % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary ivan@155: ivan@155: % Initialising Dictionary structure ivan@155: % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@155: % to zero values ivan@155: ivan@155: SMALL.DL(1)=SMALL_init_DL(); ivan@155: ivan@155: % Defining the parameters needed for dictionary learning ivan@155: ivan@155: SMALL.DL(1).toolbox = 'KSVD'; ivan@155: SMALL.DL(1).name = 'ksvd'; ivan@155: ivan@155: % Defining the parameters for KSVD ivan@155: % In this example we are learning 256 atoms in 20 iterations, so that ivan@155: % every patch in the training set can be represented with target error in ivan@155: % L2-norm (Edata) ivan@155: % Type help ksvd in MATLAB prompt for more options. ivan@155: ivan@155: ivan@155: SMALL.DL(1).param=struct(... ivan@155: 'Edata', Edata,... ivan@155: 'initdict', SMALL.Problem.initdict,... ivan@155: 'dictsize', SMALL.Problem.p,... ivan@155: 'exact', 1, ... ivan@155: 'iternum', 20,... ivan@155: 'memusage', 'high'); ivan@155: ivan@155: % Learn the dictionary ivan@155: ivan@155: SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); ivan@155: ivan@155: % Set SMALL.Problem.A dictionary ivan@155: % (backward compatiblity with SPARCO: solver structure communicate ivan@155: % only with Problem structure, ie no direct communication between DL and ivan@155: % solver structures) ivan@155: ivan@155: SMALL.Problem.A = SMALL.DL(1).D; ivan@161: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem); ivan@155: ivan@155: %% ivan@155: % Initialising solver structure ivan@155: % Setting solver structure fields (toolbox, name, param, solution, ivan@155: % reconstructed and time) to zero values ivan@155: ivan@155: SMALL.solver(1)=SMALL_init_solver; ivan@155: ivan@155: % Defining the parameters needed for image denoising ivan@155: ivan@155: SMALL.solver(1).toolbox='ompbox'; ivan@155: SMALL.solver(1).name='omp2'; ivan@155: SMALL.solver(1).param=struct(... ivan@155: 'epsilon',Edata,... ivan@155: 'maxatoms', maxatoms); ivan@155: ivan@155: % Denoising the image - find the sparse solution in the learned ivan@155: % dictionary for all patches in the image and the end it uses ivan@155: % reconstruction function to reconstruct the patches and put them into a ivan@155: % denoised image ivan@155: ivan@155: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); ivan@155: ivan@155: % Show PSNR after reconstruction ivan@155: ivan@155: SMALL.solver(1).reconstructed.psnr ivan@155: ivan@155: %% ivan@155: % For comparison purposes we will denoise image with Majorization ivan@155: % Minimization method ivan@155: % ivan@155: ivan@155: % Initialising solver structure ivan@155: % Setting solver structure fields (toolbox, name, param, solution, ivan@155: % reconstructed and time) to zero values ivan@155: ivan@155: SMALL.solver(2)=SMALL_init_solver; ivan@155: ivan@155: % Defining the parameters needed for image denoising ivan@155: ivan@155: SMALL.solver(2).toolbox='ompbox'; ivan@155: SMALL.solver(2).name='omp2'; ivan@155: SMALL.solver(2).param=struct(... ivan@155: 'epsilon',Edata,... ivan@155: 'maxatoms', maxatoms); ivan@155: ivan@155: % Initialising Dictionary structure ivan@155: % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@155: % to zero values ivan@155: ivan@155: SMALL.DL(2)=SMALL_init_DL('MMbox', 'MM_cn', '', 1); ivan@155: ivan@155: ivan@155: % Defining the parameters for MOD ivan@155: % In this example we are learning 256 atoms in 20 iterations, so that ivan@155: % every patch in the training set can be represented with target error in ivan@155: % L2-norm (EData) ivan@155: % Type help ksvd in MATLAB prompt for more options. ivan@155: ivan@155: ivan@155: SMALL.DL(2).param=struct(... ivan@155: 'solver', SMALL.solver(2),... ivan@155: 'initdict', SMALL.Problem.initdict,... ivan@155: 'dictsize', SMALL.Problem.p,... ivan@155: 'iternum', 20,... ivan@155: 'iterDictUpdate', 1000,... ivan@155: 'epsDictUpdate', 1e-7,... ivan@155: 'cvset',0,... ivan@155: 'show_dict', 0); ivan@155: ivan@155: % Learn the dictionary ivan@155: ivan@155: SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); ivan@155: ivan@155: % Set SMALL.Problem.A dictionary ivan@155: % (backward compatiblity with SPARCO: solver structure communicate ivan@155: % only with Problem structure, ie no direct communication between DL and ivan@155: % solver structures) ivan@155: ivan@155: SMALL.Problem.A = SMALL.DL(2).D; ivan@161: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem); ivan@155: ivan@155: % Denoising the image - find the sparse solution in the learned ivan@155: % dictionary for all patches in the image and the end it uses ivan@155: % reconstruction function to reconstruct the patches and put them into a ivan@155: % denoised image ivan@155: ivan@155: SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); ivan@155: ivan@155: ivan@155: ivan@155: % show results % ivan@155: ivan@155: SMALL_ImgDeNoiseResult(SMALL); ivan@155: ivan@155: results(noise_ind,im_num).psnr.ksvd=SMALL.solver(1).reconstructed.psnr; ivan@155: results(noise_ind,im_num).psnr.odct=SMALL.solver(2).reconstructed.psnr; ivan@155: results(noise_ind,im_num).vmrse.ksvd=SMALL.solver(1).reconstructed.vmrse; ivan@155: results(noise_ind,im_num).vmrse.odct=SMALL.solver(2).reconstructed.vmrse; ivan@155: results(noise_ind,im_num).ssim.ksvd=SMALL.solver(1).reconstructed.ssim; ivan@155: results(noise_ind,im_num).ssim.odct=SMALL.solver(2).reconstructed.ssim; ivan@155: ivan@155: ivan@155: results(noise_ind,im_num).time.ksvd=SMALL.solver(1).time+SMALL.DL(1).time; ivan@155: ivan@155: %clear SMALL; ivan@155: end ivan@155: end ivan@155: % save results.mat results