idamnjanovic@6: %% DICTIONARY LEARNING FOR IMAGE DENOISING idamnjanovic@6: % This file contains an example of how SMALLbox can be used to test different idamnjanovic@6: % dictionary learning techniques in Image Denoising problem. idamnjanovic@6: % It calls generateImageDenoiseProblem that will let you to choose image, idamnjanovic@6: % add noise and use noisy image to generate training set for dictionary idamnjanovic@6: % learning. idamnjanovic@6: % We tested time and psnr for two dictionary learning techniques. This idamnjanovic@6: % example does not represnt any extensive testing. The aim of this idamnjanovic@6: % example is just to show how SMALL structure can be used for testing. idamnjanovic@6: % idamnjanovic@6: % Two dictionary learning techniques were compared: idamnjanovic@6: % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient idamnjanovic@6: % Implementation of the K-SVD Algorithm using Batch Orthogonal idamnjanovic@6: % Matching Pursuit", Technical Report - CS, Technion, April 2008. idamnjanovic@6: % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online idamnjanovic@6: % Dictionary Learning for Sparse Coding. International idamnjanovic@6: % Conference on Machine Learning,Montreal, Canada, 2009 idamnjanovic@6: % idamnjanovic@6: % idamnjanovic@6: % Ivan Damnjanovic 2010 idamnjanovic@6: %% idamnjanovic@6: idamnjanovic@6: clear all; idamnjanovic@6: idamnjanovic@6: %% Load an image idamnjanovic@6: TMPpath=pwd; idamnjanovic@6: FS=filesep; idamnjanovic@6: [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); idamnjanovic@6: cd([pathstr1,FS,'data',FS,'images']); idamnjanovic@6: [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes'); idamnjanovic@6: [pathstr, name, ext, versn] = fileparts(filename); idamnjanovic@6: test_image = imread(filename); idamnjanovic@6: test_image = double(test_image); idamnjanovic@6: cd(TMPpath); idamnjanovic@6: idamnjanovic@6: % number of different values we want to test idamnjanovic@6: n =5; idamnjanovic@6: step = floor((size(test_image,1)-8+1)*(size(test_image,2)-8+1)/n); idamnjanovic@6: Training_size=zeros(1,n); idamnjanovic@6: time = zeros(2,n); idamnjanovic@6: psnr = zeros(2,n); idamnjanovic@6: for i=1:n idamnjanovic@6: idamnjanovic@6: % Here we want to test time spent and quality of denoising for idamnjanovic@6: % different sizes of training sample. idamnjanovic@6: Training_size(i)=i*step; idamnjanovic@6: idamnjanovic@6: SMALL.Problem = generateImageDenoiseProblem(test_image,Training_size(i)); idamnjanovic@6: SMALL.Problem.name=name; idamnjanovic@6: %% idamnjanovic@6: % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary idamnjanovic@6: idamnjanovic@6: % Initialising Dictionary structure idamnjanovic@6: % Setting Dictionary structure fields (toolbox, name, param, D and time) idamnjanovic@6: % to zero values idamnjanovic@6: idamnjanovic@6: SMALL.DL(1)=SMALL_init_DL(); idamnjanovic@6: idamnjanovic@6: % Defining the parameters needed for dictionary learning idamnjanovic@6: idamnjanovic@6: SMALL.DL(1).toolbox = 'KSVD'; idamnjanovic@6: SMALL.DL(1).name = 'ksvd'; idamnjanovic@6: idamnjanovic@6: % Defining the parameters for KSVD idamnjanovic@6: % In this example we are learning 256 atoms in 20 iterations, so that idamnjanovic@6: % every patch in the training set can be represented with target error in idamnjanovic@6: % L2-norm (EData) idamnjanovic@6: % Type help ksvd in MATLAB prompt for more options. idamnjanovic@6: idamnjanovic@6: Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain; idamnjanovic@6: SMALL.DL(1).param=struct(... idamnjanovic@6: 'Edata', Edata,... idamnjanovic@6: 'initdict', SMALL.Problem.initdict,... idamnjanovic@6: 'dictsize', SMALL.Problem.p,... idamnjanovic@6: 'iternum', 20,... idamnjanovic@6: 'memusage', 'high'); idamnjanovic@6: idamnjanovic@6: % Learn the dictionary idamnjanovic@6: idamnjanovic@6: SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); idamnjanovic@6: idamnjanovic@6: % Set SMALL.Problem.A dictionary idamnjanovic@6: % (backward compatiblity with SPARCO: solver structure communicate idamnjanovic@6: % only with Problem structure, ie no direct communication between DL and idamnjanovic@6: % solver structures) idamnjanovic@6: idamnjanovic@6: SMALL.Problem.A = SMALL.DL(1).D; idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: %% idamnjanovic@6: % Initialising solver structure idamnjanovic@6: % Setting solver structure fields (toolbox, name, param, solution, idamnjanovic@6: % reconstructed and time) to zero values idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: SMALL.solver(1)=SMALL_init_solver; idamnjanovic@6: idamnjanovic@6: % Defining the parameters needed for denoising idamnjanovic@6: idamnjanovic@6: SMALL.solver(1).toolbox='ompbox'; idamnjanovic@6: SMALL.solver(1).name='ompdenoise'; idamnjanovic@6: idamnjanovic@6: % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@6: % but backward compatible with KSVD definition of denoising idamnjanovic@6: idamnjanovic@6: SMALL.solver(1)=SMALL_denoise(SMALL.Problem, SMALL.solver(1)); idamnjanovic@6: idamnjanovic@6: %% idamnjanovic@6: % Use SPAMS Online Dictionary Learning Algorithm idamnjanovic@6: % to Learn overcomplete dictionary (Julien Mairal 2009) idamnjanovic@6: % (If you have not installed SPAMS please comment the following two cells) idamnjanovic@6: idamnjanovic@6: % Initialising Dictionary structure idamnjanovic@6: % Setting Dictionary structure fields (toolbox, name, param, D and time) idamnjanovic@6: % to zero values idamnjanovic@6: idamnjanovic@6: SMALL.DL(2)=SMALL_init_DL(); idamnjanovic@6: idamnjanovic@6: % Defining fields needed for dictionary learning idamnjanovic@6: idamnjanovic@6: SMALL.DL(2).toolbox = 'SPAMS'; idamnjanovic@6: SMALL.DL(2).name = 'mexTrainDL'; idamnjanovic@6: idamnjanovic@6: % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters. idamnjanovic@6: idamnjanovic@6: SMALL.DL(2).param=struct(... idamnjanovic@6: 'D', SMALL.Problem.initdict,... idamnjanovic@6: 'K', SMALL.Problem.p,... idamnjanovic@6: 'lambda', 2,... idamnjanovic@6: 'iter', 300,... idamnjanovic@6: 'mode', 3,... idamnjanovic@6: 'modeD', 0 ); idamnjanovic@6: idamnjanovic@6: % Learn the dictionary idamnjanovic@6: idamnjanovic@6: SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); idamnjanovic@6: idamnjanovic@6: % Set SMALL.Problem.A dictionary idamnjanovic@6: % (backward compatiblity with SPARCO: solver structure communicate idamnjanovic@6: % only with Problem structure, ie no direct communication between DL and idamnjanovic@6: % solver structures) idamnjanovic@6: idamnjanovic@6: SMALL.Problem.A = SMALL.DL(2).D; idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: %% idamnjanovic@6: % Initialising solver structure idamnjanovic@6: % Setting solver structure fields (toolbox, name, param, solution, idamnjanovic@6: % reconstructed and time) to zero values idamnjanovic@6: idamnjanovic@6: SMALL.solver(2)=SMALL_init_solver; idamnjanovic@6: idamnjanovic@6: % Defining the parameters needed for denoising idamnjanovic@6: idamnjanovic@6: SMALL.solver(2).toolbox='ompbox'; idamnjanovic@6: SMALL.solver(2).name='ompdenoise'; idamnjanovic@6: idamnjanovic@6: % Denoising the image - SMALL_denoise function is similar to SMALL_solve, idamnjanovic@6: % but backward compatible with KSVD definition of denoising idamnjanovic@6: idamnjanovic@6: SMALL.solver(2)=SMALL_denoise(SMALL.Problem, SMALL.solver(2)); idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: %% show results %% idamnjanovic@6: % This will show denoised images and dictionaries for all training sets. idamnjanovic@6: % If you are not interested to see them and do not want clutter your idamnjanovic@6: % screen comment following line idamnjanovic@6: idamnjanovic@6: SMALL_ImgDeNoiseResult(SMALL); idamnjanovic@6: idamnjanovic@6: time(1,i) = SMALL.DL(1).time; idamnjanovic@6: psnr(1,i) = SMALL.solver(1).reconstructed.psnr; idamnjanovic@6: idamnjanovic@6: time(2,i) = SMALL.DL(2).time; idamnjanovic@6: psnr(2,i) = SMALL.solver(2).reconstructed.psnr; idamnjanovic@6: idamnjanovic@6: clear SMALL idamnjanovic@6: end idamnjanovic@6: idamnjanovic@6: %% show time and psnr %% idamnjanovic@6: figure('Name', 'KSVD vs SPAMS'); idamnjanovic@6: idamnjanovic@6: subplot(1,2,1); plot(Training_size, time(1,:), 'ro-', Training_size, time(2,:), 'b*-'); idamnjanovic@6: legend('KSVD','SPAMS',0); idamnjanovic@6: title('Time vs Training size'); idamnjanovic@6: subplot(1,2,2); plot(Training_size, psnr(1,:), 'ro-', Training_size, psnr(2,:), 'b*-'); idamnjanovic@6: legend('KSVD','SPAMS',0); idamnjanovic@6: title('PSNR vs Training size');