annotate util/SMALL_learn.m @ 36:e6191f5bb21b

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:32:52 +0000
parents fc395272d53e
children f6cc633fd94b
rev   line source
idamnjanovic@8 1 function DL = SMALL_learn(Problem,DL)
idamnjanovic@8 2 %%% SMALL Dictionary Learning
idamnjanovic@24 3 %
idamnjanovic@24 4 % Centre for Digital Music, Queen Mary, University of London.
idamnjanovic@24 5 % This file copyright 2009 Ivan Damnjanovic.
idamnjanovic@24 6 %
idamnjanovic@24 7 % This program is free software; you can redistribute it and/or
idamnjanovic@24 8 % modify it under the terms of the GNU General Public License as
idamnjanovic@24 9 % published by the Free Software Foundation; either version 2 of the
idamnjanovic@24 10 % License, or (at your option) any later version. See the file
idamnjanovic@24 11 % COPYING included with this distribution for more information.
idamnjanovic@24 12 %
idamnjanovic@8 13 % Function gets as input Problem and Dictionary Learning (DL) structures
idamnjanovic@8 14 % In Problem structure field b with the training set needs to be defined
idamnjanovic@8 15 % In DL fields with name of the toolbox and solver, and parameters file
idamnjanovic@8 16 % for particular dictionary learning technique needs to be present.
idamnjanovic@8 17 %
idamnjanovic@8 18 % Outputs are Learned dictionary and time spent as a part of DL structure
idamnjanovic@8 19 %%
idamnjanovic@8 20
idamnjanovic@8 21 fprintf('\nStarting Dictionary Learning %s... \n', DL.name);
idamnjanovic@8 22 start=cputime;
idamnjanovic@36 23 tStart=tic;
idamnjanovic@8 24 if strcmpi(DL.toolbox,'KSVD')
idamnjanovic@8 25 param=DL.param;
idamnjanovic@8 26 param.data=Problem.b;
idamnjanovic@8 27
idamnjanovic@36 28 D = eval([DL.name,'(param)']);%, ''t'', 5);']);
idamnjanovic@8 29 elseif strcmpi(DL.toolbox,'KSVDS')
idamnjanovic@8 30 param=DL.param;
idamnjanovic@8 31 param.data=Problem.b;
idamnjanovic@8 32
idamnjanovic@8 33 D = eval([DL.name,'(param, ''t'', 5);']);
idamnjanovic@8 34 elseif strcmpi(DL.toolbox,'SPAMS')
idamnjanovic@8 35
idamnjanovic@8 36 X = Problem.b;
idamnjanovic@8 37 param=DL.param;
idamnjanovic@8 38
idamnjanovic@8 39 D = eval([DL.name,'(X, param);']);
idamnjanovic@8 40 % As some versions of SPAMS does not produce unit norm column
idamnjanovic@8 41 % dictionaries, we need to make sure that columns are normalised to
idamnjanovic@8 42 % unit lenght.
idamnjanovic@8 43
idamnjanovic@8 44 for i = 1: size(D,2)
idamnjanovic@8 45 D(:,i)=D(:,i)/norm(D(:,i));
idamnjanovic@8 46 end
idamnjanovic@36 47 elseif strcmpi(DL.toolbox,'SMALL')
idamnjanovic@8 48
idamnjanovic@36 49 X = Problem.b;
idamnjanovic@36 50 param=DL.param;
idamnjanovic@36 51
idamnjanovic@36 52 D = eval([DL.name,'(X, param);']);
idamnjanovic@36 53 % we need to make sure that columns are normalised to
idamnjanovic@36 54 % unit lenght.
idamnjanovic@36 55
idamnjanovic@36 56 for i = 1: size(D,2)
idamnjanovic@36 57 D(:,i)=D(:,i)/norm(D(:,i));
idamnjanovic@36 58 end
idamnjanovic@36 59 elseif strcmpi(DL.toolbox,'mpv2')
idamnjanovic@36 60 X = Problem.b(:,1:1:40000);
idamnjanovic@36 61 jD0 = mpv2.SimpleMatrix(DL.param.D);
idamnjanovic@36 62 jDicLea = mpv2.DictionaryLearning(jD0, 1,1);
idamnjanovic@36 63 % !!!!! MAYBE lambda is not needed for ILS - NEED TO CHECK!!!!
idamnjanovic@36 64 jDicLea.setLambda('L', DL.param.lambda, DL.param.lambda, 1000);
idamnjanovic@36 65 jDicLea.setVerbose(2);
idamnjanovic@36 66 jDicLea.setORMP(16, 1e-6, DL.param.abs);
idamnjanovic@36 67 eval(['jDicLea.',DL.name,'( X(:), DL.param.iternum );']);
idamnjanovic@36 68 jD = jDicLea.getDictionary();
idamnjanovic@36 69
idamnjanovic@36 70 D = reshape(jD.getAll(), size(X,1), DL.param.K);
idamnjanovic@36 71
idamnjanovic@8 72 % To introduce new dictionary learning technique put the files in
idamnjanovic@8 73 % your Matlab path. Next, unique name <TolboxID> for your toolbox needs
idamnjanovic@8 74 % to be defined and also prefferd API for toolbox functions <Preffered_API>
idamnjanovic@8 75 %
idamnjanovic@8 76 % elseif strcmpi(DL.toolbox,'<ToolboxID>')
idamnjanovic@8 77 % % This is an example of API that can be used:
idamnjanovic@8 78 % % - get training set from Problem part of structure
idamnjanovic@8 79 % % - assign parameters defined in the main program
idamnjanovic@8 80 %
idamnjanovic@8 81 % X = Problem.b;
idamnjanovic@8 82 % param=DL.param;
idamnjanovic@8 83 %
idamnjanovic@8 84 % % - Evaluate the function (DL.name - defined in the main) with
idamnjanovic@8 85 % % parameters given above
idamnjanovic@8 86 %
idamnjanovic@8 87 % D = eval([DL.name,'(<Preffered_API>);']);
idamnjanovic@8 88
idamnjanovic@8 89 else
idamnjanovic@8 90 printf('\nToolbox has not been registered. Please change SMALL_learn file.\n');
idamnjanovic@8 91 return
idamnjanovic@8 92 end
idamnjanovic@8 93
idamnjanovic@8 94 %%
idamnjanovic@8 95 % Dictionary Learning time
idamnjanovic@36 96 tElapsed=toc(tStart);
idamnjanovic@8 97 DL.time = cputime - start;
idamnjanovic@8 98 fprintf('\n%s finished task in %2f seconds. \n', DL.name, DL.time);
idamnjanovic@36 99 fprintf('\n%s finished task in %2f seconds. \n', DL.name, tElapsed);
idamnjanovic@36 100 DL.time=tElapsed;
idamnjanovic@8 101 % If dictionary is given as a sparse matrix change it to full
idamnjanovic@8 102
idamnjanovic@8 103 DL.D = full(D);
idamnjanovic@8 104
idamnjanovic@8 105 end
idamnjanovic@8 106