idamnjanovic@8: function DL = SMALL_learn(Problem,DL) idamnjanovic@8: %%% SMALL Dictionary Learning idamnjanovic@24: % idamnjanovic@24: % Centre for Digital Music, Queen Mary, University of London. idamnjanovic@24: % This file copyright 2009 Ivan Damnjanovic. idamnjanovic@24: % idamnjanovic@24: % This program is free software; you can redistribute it and/or idamnjanovic@24: % modify it under the terms of the GNU General Public License as idamnjanovic@24: % published by the Free Software Foundation; either version 2 of the idamnjanovic@24: % License, or (at your option) any later version. See the file idamnjanovic@24: % COPYING included with this distribution for more information. idamnjanovic@24: % idamnjanovic@8: % Function gets as input Problem and Dictionary Learning (DL) structures idamnjanovic@8: % In Problem structure field b with the training set needs to be defined idamnjanovic@8: % In DL fields with name of the toolbox and solver, and parameters file idamnjanovic@8: % for particular dictionary learning technique needs to be present. idamnjanovic@8: % idamnjanovic@8: % Outputs are Learned dictionary and time spent as a part of DL structure idamnjanovic@8: %% idamnjanovic@8: idamnjanovic@8: fprintf('\nStarting Dictionary Learning %s... \n', DL.name); idamnjanovic@8: start=cputime; idamnjanovic@36: tStart=tic; idamnjanovic@8: if strcmpi(DL.toolbox,'KSVD') idamnjanovic@8: param=DL.param; idamnjanovic@8: param.data=Problem.b; idamnjanovic@8: idamnjanovic@36: D = eval([DL.name,'(param)']);%, ''t'', 5);']); idamnjanovic@8: elseif strcmpi(DL.toolbox,'KSVDS') idamnjanovic@8: param=DL.param; idamnjanovic@8: param.data=Problem.b; idamnjanovic@8: idamnjanovic@8: D = eval([DL.name,'(param, ''t'', 5);']); idamnjanovic@8: elseif strcmpi(DL.toolbox,'SPAMS') idamnjanovic@8: idamnjanovic@8: X = Problem.b; idamnjanovic@8: param=DL.param; idamnjanovic@8: idamnjanovic@8: D = eval([DL.name,'(X, param);']); idamnjanovic@8: % As some versions of SPAMS does not produce unit norm column idamnjanovic@8: % dictionaries, we need to make sure that columns are normalised to idamnjanovic@8: % unit lenght. idamnjanovic@8: idamnjanovic@8: for i = 1: size(D,2) idamnjanovic@8: D(:,i)=D(:,i)/norm(D(:,i)); idamnjanovic@8: end idamnjanovic@36: elseif strcmpi(DL.toolbox,'SMALL') idamnjanovic@8: idamnjanovic@36: X = Problem.b; idamnjanovic@36: param=DL.param; idamnjanovic@36: idamnjanovic@36: D = eval([DL.name,'(X, param);']); idamnjanovic@36: % we need to make sure that columns are normalised to idamnjanovic@36: % unit lenght. idamnjanovic@36: idamnjanovic@36: for i = 1: size(D,2) idamnjanovic@36: D(:,i)=D(:,i)/norm(D(:,i)); idamnjanovic@36: end idamnjanovic@36: elseif strcmpi(DL.toolbox,'mpv2') idamnjanovic@36: X = Problem.b(:,1:1:40000); idamnjanovic@36: jD0 = mpv2.SimpleMatrix(DL.param.D); idamnjanovic@36: jDicLea = mpv2.DictionaryLearning(jD0, 1,1); idamnjanovic@36: % !!!!! MAYBE lambda is not needed for ILS - NEED TO CHECK!!!! idamnjanovic@36: jDicLea.setLambda('L', DL.param.lambda, DL.param.lambda, 1000); idamnjanovic@36: jDicLea.setVerbose(2); idamnjanovic@36: jDicLea.setORMP(16, 1e-6, DL.param.abs); idamnjanovic@36: eval(['jDicLea.',DL.name,'( X(:), DL.param.iternum );']); idamnjanovic@36: jD = jDicLea.getDictionary(); idamnjanovic@36: idamnjanovic@36: D = reshape(jD.getAll(), size(X,1), DL.param.K); idamnjanovic@36: idamnjanovic@8: % To introduce new dictionary learning technique put the files in idamnjanovic@8: % your Matlab path. Next, unique name for your toolbox needs idamnjanovic@8: % to be defined and also prefferd API for toolbox functions idamnjanovic@8: % idamnjanovic@8: % elseif strcmpi(DL.toolbox,'') idamnjanovic@8: % % This is an example of API that can be used: idamnjanovic@8: % % - get training set from Problem part of structure idamnjanovic@8: % % - assign parameters defined in the main program idamnjanovic@8: % idamnjanovic@8: % X = Problem.b; idamnjanovic@8: % param=DL.param; idamnjanovic@8: % idamnjanovic@8: % % - Evaluate the function (DL.name - defined in the main) with idamnjanovic@8: % % parameters given above idamnjanovic@8: % idamnjanovic@8: % D = eval([DL.name,'();']); idamnjanovic@8: idamnjanovic@8: else idamnjanovic@8: printf('\nToolbox has not been registered. Please change SMALL_learn file.\n'); idamnjanovic@8: return idamnjanovic@8: end idamnjanovic@8: idamnjanovic@8: %% idamnjanovic@8: % Dictionary Learning time idamnjanovic@36: tElapsed=toc(tStart); idamnjanovic@8: DL.time = cputime - start; maria@86: fprintf('\n%s finished task in %2f seconds (cpu time). \n', DL.name, DL.time); maria@86: fprintf('\n%s finished task in %2f seconds (tic-toc time). \n', DL.name, tElapsed); idamnjanovic@36: DL.time=tElapsed; idamnjanovic@8: % If dictionary is given as a sparse matrix change it to full idamnjanovic@8: idamnjanovic@8: DL.D = full(D); idamnjanovic@8: idamnjanovic@8: end idamnjanovic@8: