luis@236: %% Dictionary Learning for Image Denoising - KSVD vs KSVDS vs SPAMS luis@236: % luis@236: % *WARNING!* You should have SPAMS in your search path in order for this luis@236: % script to work.Due to licensing issues SPAMS can not be automatically luis@236: % provided in SMALLbox (http://www.di.ens.fr/willow/SPAMS/downloads.html). luis@236: % luis@236: % This file contains an example of how SMALLbox can be used to test different luis@236: % dictionary learning techniques in Image Denoising problem. luis@236: % It calls generateImageDenoiseProblem that will let you to choose image, luis@236: % add noise and use noisy image to generate training set for dictionary luis@236: % learning. luis@236: % Two dictionary learning techniques were compared: luis@236: % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient luis@236: % Implementation of the K-SVD Algorithm using Batch Orthogonal luis@236: % Matching Pursuit", Technical Report - CS, Technion, April 2008. luis@236: % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse luis@236: % Dictionaries for Sparse Signal Approximation", Technical luis@236: % Report - CS, Technion, June 2009. luis@236: % luis@236: luis@236: % luis@236: % Centre for Digital Music, Queen Mary, University of London. luis@236: % This file copyright 2009 Ivan Damnjanovic. luis@236: % luis@236: % This program is free software; you can redistribute it and/or luis@236: % modify it under the terms of the GNU General Public License as luis@236: % published by the Free Software Foundation; either version 2 of the luis@236: % License, or (at your option) any later version. See the file luis@236: % COPYING included with this distribution for more information. luis@236: % luis@236: %% luis@236: luis@236: clear; luis@236: luis@236: % If you want to load the image outside of generateImageDenoiseProblem luis@236: % function uncomment following lines. This can be useful if you want to luis@236: % denoise more then one image for example. luis@236: luis@236: % TMPpath=pwd; luis@236: % FS=filesep; luis@236: % [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m')); luis@236: % cd([pathstr1,FS,'data',FS,'images']); luis@236: % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes'); luis@236: % [pathstr, name, ext] = fileparts(filename); luis@236: % test_image = imread(filename); luis@236: % test_image = double(test_image); luis@236: % cd(TMPpath); luis@236: % SMALL.Problem.name=name; luis@236: luis@236: luis@236: % Defining Image Denoising Problem as Dictionary Learning luis@236: % Problem. As an input we set the number of training patches. luis@236: luis@236: SMALL.Problem = generateImageDenoiseProblem('', 40000); luis@236: luis@236: luis@236: %% luis@236: % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary luis@236: luis@236: % Initialising Dictionary structure luis@236: % Setting Dictionary structure fields (toolbox, name, param, D and time) luis@236: % to zero values luis@236: luis@236: SMALL.DL(1)=SMALL_init_DL(); luis@236: luis@236: % Defining the parameters needed for dictionary learning luis@236: luis@236: SMALL.DL(1).toolbox = 'KSVD'; luis@236: SMALL.DL(1).name = 'ksvd'; luis@236: luis@236: % Defining the parameters for KSVD luis@236: % In this example we are learning 256 atoms in 20 iterations, so that luis@236: % every patch in the training set can be represented with target error in luis@236: % L2-norm (EData) luis@236: % Type help ksvd in MATLAB prompt for more options. luis@236: luis@236: Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain; luis@236: maxatoms = floor(prod(SMALL.Problem.blocksize)/2); luis@236: luis@236: SMALL.DL(1).param=struct(... luis@236: 'Edata', Edata,... luis@236: 'initdict', SMALL.Problem.initdict,... luis@236: 'dictsize', SMALL.Problem.p,... luis@236: 'iternum', 20,... luis@236: 'memusage', 'high'); luis@236: luis@236: % Learn the dictionary luis@236: luis@236: SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); luis@236: luis@236: % Set SMALL.Problem.A dictionary luis@236: % (backward compatiblity with SPARCO: solver structure communicate luis@236: % only with Problem structure, ie no direct communication between DL and luis@236: % solver structures) luis@236: luis@236: SMALL.Problem.A = SMALL.DL(1).D; luis@236: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem); luis@236: luis@236: %% luis@236: % Initialising solver structure luis@236: % Setting solver structure fields (toolbox, name, param, solution, luis@236: % reconstructed and time) to zero values luis@236: luis@236: SMALL.solver(1)=SMALL_init_solver; luis@236: luis@236: % Defining the parameters needed for image denoising luis@236: luis@236: SMALL.solver(1).toolbox='ompbox'; luis@236: SMALL.solver(1).name='omp2'; luis@236: SMALL.solver(1).param=struct(... luis@236: 'epsilon',Edata,... luis@236: 'maxatoms', maxatoms); luis@236: luis@236: % Denoising the image - find the sparse solution in the learned luis@236: % dictionary for all patches in the image and the end it uses luis@236: % reconstruction function to reconstruct the patches and put them into a luis@236: % denoised image luis@236: luis@236: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); luis@236: luis@236: % Show PSNR after reconstruction luis@236: luis@236: SMALL.solver(1).reconstructed.psnr luis@236: luis@236: %% luis@236: % Use KSVDS Dictionary Learning Algorithm to denoise image luis@236: luis@236: % Initialising solver structure luis@236: % Setting solver structure fields (toolbox, name, param, solution, luis@236: % reconstructed and time) to zero values luis@236: luis@236: SMALL.DL(2)=SMALL_init_DL(); luis@236: luis@236: % Defining the parameters needed for dictionary learning luis@236: luis@236: SMALL.DL(2).toolbox = 'KSVDS'; luis@236: SMALL.DL(2).name = 'ksvds'; luis@236: luis@236: % Defining the parameters for KSVDS luis@236: % In this example we are learning 256 atoms in 20 iterations, so that luis@236: % every patch in the training set can be represented with target error in luis@236: % L2-norm (EDataS). We also impose "double sparsity" - dictionary itself luis@236: % has to be sparse in the given base dictionary (Tdict - number of luis@236: % nonzero elements per atom). luis@236: % Type help ksvds in MATLAB prompt for more options. luis@236: luis@236: EdataS=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain; luis@236: SMALL.DL(2).param=struct(... luis@236: 'Edata', EdataS, ... luis@236: 'Tdict', 6,... luis@236: 'stepsize', 1,... luis@236: 'dictsize', SMALL.Problem.p,... luis@236: 'iternum', 20,... luis@236: 'memusage', 'high'); luis@236: SMALL.DL(2).param.initA = speye(SMALL.Problem.p); luis@236: SMALL.DL(2).param.basedict{1} = odctdict(8,16); luis@236: SMALL.DL(2).param.basedict{2} = odctdict(8,16); luis@236: luis@236: % Learn the dictionary luis@236: luis@236: SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); luis@236: luis@236: % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary luis@236: % (backward compatiblity with SPARCO: solver structure communicate luis@236: % only with Problem structure, ie no direct communication between DL and luis@236: % solver structures) luis@236: luis@236: SMALL.Problem.A = SMALL.DL(2).D; luis@236: SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1}; luis@236: SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2}; luis@236: luis@236: % Setting up reconstruction function luis@236: luis@236: SparseDict=1; luis@236: SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem, SparseDict); luis@236: luis@236: % Initialising solver structure luis@236: % Setting solver structure fields (toolbox, name, param, solution, luis@236: % reconstructed and time) to zero values luis@236: luis@236: SMALL.solver(2)=SMALL_init_solver; luis@236: luis@236: % Defining the parameters needed for image denoising luis@236: luis@236: SMALL.solver(2).toolbox='ompsbox'; luis@236: SMALL.solver(2).name='omps2'; luis@236: SMALL.solver(2).param=struct(... luis@236: 'epsilon',Edata,... luis@236: 'maxatoms', maxatoms); luis@236: luis@236: % Denoising the image - find the sparse solution in the learned luis@236: % dictionary for all patches in the image and the end it uses luis@236: % reconstruction function to reconstruct the patches and put them into a luis@236: % denoised image luis@236: luis@236: SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); luis@236: luis@236: luis@236: %% luis@236: % Plot results and save midi files luis@236: luis@236: % show results % luis@236: luis@236: SMALL_ImgDeNoiseResult(SMALL);