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. idamnjanovic@42: % Three 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. idamnjanovic@42: % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse idamnjanovic@42: % Dictionaries for Sparse Signal Approximation", Technical idamnjanovic@42: % Report - CS, Technion, June 2009. idamnjanovic@42: % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online idamnjanovic@42: % Dictionary Learning for Sparse Coding. International idamnjanovic@42: % Conference on Machine Learning,Montreal, Canada, 2009 idamnjanovic@42: % idamnjanovic@42: % idamnjanovic@42: % Ivan Damnjanovic 2010 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. 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@78: cd(TMPpath); idamnjanovic@42: % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes'); idamnjanovic@42: % [pathstr, name, ext, versn] = fileparts(filename); idamnjanovic@42: % test_image = imread(filename); idamnjanovic@42: % test_image = double(test_image); idamnjanovic@42: % cd(TMPpath); idamnjanovic@42: % SMALL.Problem.name=name; idamnjanovic@42: idamnjanovic@42: noise_level=[10 20 25 50 100]; idamnjanovic@42: % Defining Image Denoising Problem as Dictionary Learning idamnjanovic@42: % Problem. As an input we set the number of training patches. idamnjanovic@42: for noise_ind=1:1 ivan@78: for im_num=2:2 idamnjanovic@65: SMALL.Problem = generateImageDenoiseProblem(test_image(im_num).i, 40000, '',256, noise_level(noise_ind)); ivan@78: SMALL.Problem.name=int2str(im_num); 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: idamnjanovic@42: % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@42: % but backward compatible with KSVD definition of denoising idamnjanovic@42: idamnjanovic@42: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); idamnjanovic@42: SMALL.solver(1).reconstructed.psnr idamnjanovic@42: %% idamnjanovic@42: % Use KSVDS Dictionary Learning Algorithm to denoise image 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.DL(2)=SMALL_init_DL(); idamnjanovic@42: % idamnjanovic@42: % % Defining the parameters needed for dictionary learning idamnjanovic@42: % idamnjanovic@42: % SMALL.DL(2).toolbox = 'KSVDS'; idamnjanovic@42: % SMALL.DL(2).name = 'ksvds'; idamnjanovic@42: % idamnjanovic@42: % % Defining the parameters for KSVDS 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 (EDataS). We also impose "double sparsity" - dictionary itself idamnjanovic@42: % % has to be sparse in the given base dictionary (Tdict - number of idamnjanovic@42: % % nonzero elements per atom). idamnjanovic@42: % % Type help ksvds in MATLAB prompt for more options. idamnjanovic@42: % idamnjanovic@42: % idamnjanovic@42: % SMALL.DL(2).param=struct(... idamnjanovic@42: % 'Edata', Edata, ... idamnjanovic@42: % 'Tdict', 6,... idamnjanovic@42: % 'stepsize', 1,... idamnjanovic@42: % 'dictsize', SMALL.Problem.p,... idamnjanovic@42: % 'iternum', 20,... idamnjanovic@42: % 'memusage', 'high'); idamnjanovic@42: % SMALL.DL(2).param.initA = speye(SMALL.Problem.p); idamnjanovic@42: % SMALL.DL(2).param.basedict{1} = odctdict(8,16); idamnjanovic@42: % SMALL.DL(2).param.basedict{2} = odctdict(8,16); idamnjanovic@42: % idamnjanovic@42: % % Learn the dictionary idamnjanovic@42: % idamnjanovic@42: % SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); idamnjanovic@42: idamnjanovic@42: % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary 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.Problem.initdict; idamnjanovic@42: % SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1}; idamnjanovic@42: % SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2}; idamnjanovic@42: SMALL.DL(2).D=SMALL.Problem.initdict; idamnjanovic@42: SparseDict=0; idamnjanovic@42: SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem, SparseDict); 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(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: idamnjanovic@42: % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@42: % but backward compatible with KSVD definition of denoising idamnjanovic@42: % Pay attention that since implicit base dictionary is used, denoising idamnjanovic@42: % can be much faster then using explicit dictionary in KSVD example. idamnjanovic@42: idamnjanovic@42: SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); idamnjanovic@42: %% idamnjanovic@42: idamnjanovic@42: for i =1:1 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); idamnjanovic@42: if size(p1,2)>140000 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: 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: % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters. idamnjanovic@42: % idamnjanovic@42: % SMALL.DL(3).param=struct(... idamnjanovic@42: % 'D', SMALL.Problem.initdict,... idamnjanovic@42: % 'K', SMALL.Problem.p,... idamnjanovic@42: % 'lambda', 2,... idamnjanovic@42: % 'iter', 200,... idamnjanovic@42: % 'mode', 3, ... idamnjanovic@42: % 'modeD', 0); idamnjanovic@42: idamnjanovic@42: % Learn the dictionary idamnjanovic@42: idamnjanovic@42: SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3)); idamnjanovic@42: %SMALL.DL(3).D(:,1)=SMALL.DL(1).D(:,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: % 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: % SMALL.Problem.A = SMALL.DL(1).D; idamnjanovic@42: % SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); idamnjanovic@42: % maxatoms=5; idamnjanovic@42: % SMALL.solver(3)=SMALL_init_solver; idamnjanovic@42: % idamnjanovic@42: % % Defining the parameters needed for denoising idamnjanovic@42: % idamnjanovic@42: % % SMALL.solver(3).toolbox='SPAMS'; idamnjanovic@42: % % SMALL.solver(3).name='mexLasso'; idamnjanovic@42: % % SMALL.solver(3).param=struct(... idamnjanovic@42: % % 'mode', 1, ... idamnjanovic@42: % % 'lambda',Edata*Edata,... idamnjanovic@42: % % 'L', maxatoms); idamnjanovic@42: % % % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@42: % % % but backward compatible with KSVD definition of denoising idamnjanovic@42: % % idamnjanovic@42: % % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); idamnjanovic@42: % SMALL.solver(3).toolbox='SMALL'; idamnjanovic@42: % SMALL.solver(3).name='SMALL_cgp'; idamnjanovic@42: % SMALL.solver(3).param=sprintf('%d, %.2f', maxatoms, sqrt(Edata)); idamnjanovic@42: % % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@42: % % but backward compatible with KSVD definition of denoising idamnjanovic@42: % idamnjanovic@42: % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); idamnjanovic@42: idamnjanovic@42: % %% idamnjanovic@42: % % Use RLS-DLA 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 = 'mpv2'; idamnjanovic@42: % SMALL.DL(3).name = 'rlsdla'; idamnjanovic@42: % idamnjanovic@42: % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters. idamnjanovic@42: % idamnjanovic@42: % SMALL.DL(3).param=struct(... idamnjanovic@42: % 'D', SMALL.Problem.initdict,... idamnjanovic@42: % 'K', SMALL.Problem.p,... idamnjanovic@42: % 'abs', Edata*Edata,... idamnjanovic@42: % 'lambda', 0.995,... idamnjanovic@42: % 'iternum',1); idamnjanovic@42: % idamnjanovic@42: % % Learn the dictionary idamnjanovic@42: % idamnjanovic@42: % SMALL.DL(3) = SMALL_learn(SMALL.Problem, SMALL.DL(3)); 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: % 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: %SMALL.DL(3).D(:,225:256)=0; idamnjanovic@42: SMALL.Problem.A = SMALL.DL(3).D; idamnjanovic@42: SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); idamnjanovic@42: %maxatoms=32; idamnjanovic@42: SMALL.solver(3)=SMALL_init_solver; idamnjanovic@42: idamnjanovic@42: % Defining the parameters needed for denoising idamnjanovic@42: idamnjanovic@42: % SMALL.solver(3).toolbox='SPAMS'; idamnjanovic@42: % SMALL.solver(3).name='mexLasso'; idamnjanovic@42: % SMALL.solver(3).param=struct(... idamnjanovic@42: % 'mode', 1, ... idamnjanovic@42: % 'lambda',Edata*Edata,... idamnjanovic@42: % 'L', maxatoms); idamnjanovic@42: % % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@42: % % but backward compatible with KSVD definition of denoising idamnjanovic@42: % idamnjanovic@42: % SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(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: 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: % SMALL.solver(3).toolbox='SPAMS'; idamnjanovic@42: % SMALL.solver(3).name='mexLasso'; idamnjanovic@42: % SMALL.solver(3).param=struct(... idamnjanovic@42: % 'mode', 2, ... idamnjanovic@42: % 'lambda',40,... idamnjanovic@42: % 'L', maxatoms); idamnjanovic@42: idamnjanovic@42: % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@42: % but backward compatible with KSVD definition of denoising idamnjanovic@42: idamnjanovic@42: SMALL.solver(3)=SMALL_solve(SMALL.Problem, SMALL.solver(3)); idamnjanovic@42: % Plot results and save midi files idamnjanovic@42: SMALL.solver(3).reconstructed.psnr idamnjanovic@42: % show results % idamnjanovic@42: idamnjanovic@42: SMALL_ImgDeNoiseResult(SMALL); idamnjanovic@42: end 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