annotate examples/MajorizationMinimization tests/SMALL_AMT_DL_test_KSVD_MM.m @ 216:a986ee86651e luisf_dev

Calls SMALLboxInit in the beginning of both solve and learn, in order not to lose the SMALL_path variable.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Thu, 22 Mar 2012 11:41:04 +0000
parents 9c418bea7f6a
children
rev   line source
ivan@155 1 %% Dictionary Learning for Automatic Music Transcription - KSVD vs SPAMS
ivan@155 2 %
ivan@155 3 %
ivan@155 4 % This file contains an example of how SMALLbox can be used to test diferent
ivan@155 5 % dictionary learning techniques in Automatic Music Transcription problem.
ivan@155 6 % It calls generateAMT_Learning_Problem that will let you to choose midi,
ivan@155 7 % wave or mat file to be transcribe. If file is midi it will be first
ivan@155 8 % converted to wave and original midi file will be used for comparison with
ivan@155 9 % results of dictionary learning and reconstruction.
ivan@155 10 % The function will generarte the Problem structure that is used to learn
ivan@155 11 % Problem.p notes spectrograms from training set Problem.b using
ivan@155 12 % dictionary learning technique defined in DL structure.
ivan@155 13 % Two dictionary learning techniques were compared:
ivan@155 14 %
ivan@155 15 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
ivan@155 16 % Implementation of the K-SVD Algorithm using Batch Orthogonal
ivan@155 17 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
ivan@155 18 %
ivan@155 19 % - MMDL - M. Yaghoobi, T. Blumensath and M. Davies, "Dictionary Learning
ivan@155 20 % for Sparse Approximations with the Majorization Method", IEEE
ivan@155 21 % Trans. on Signal Processing, Vol. 57, No. 6, pp 2178-2191,
ivan@155 22 % 2009.
ivan@155 23
ivan@155 24 %
ivan@155 25 % Centre for Digital Music, Queen Mary, University of London.
ivan@155 26 % This file copyright 2011 Ivan Damnjanovic.
ivan@155 27 %
ivan@155 28 % This program is free software; you can redistribute it and/or
ivan@155 29 % modify it under the terms of the GNU General Public License as
ivan@155 30 % published by the Free Software Foundation; either version 2 of the
ivan@155 31 % License, or (at your option) any later version. See the file
ivan@155 32 % COPYING included with this distribution for more information.
ivan@155 33 %%
ivan@155 34
ivan@155 35 clear;
ivan@155 36
ivan@155 37
ivan@155 38 % Defining Automatic Transcription of Piano tune as Dictionary Learning
ivan@155 39 % Problem
ivan@155 40
ivan@161 41 SMALL.Problem = generateAMTProblem('',2048,0.75);
ivan@155 42
ivan@155 43 %%
ivan@155 44 % Use KSVD Dictionary Learning Algorithm to Learn 88 notes (defined in
ivan@155 45 % SMALL.Problem.p) using sparsity constrain only
ivan@155 46
ivan@155 47 % Initialising Dictionary structure
ivan@155 48 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@155 49 % to zero values
ivan@155 50
ivan@155 51 SMALL.DL(1)=SMALL_init_DL();
ivan@155 52
ivan@155 53 % Defining fields needed for dictionary learning
ivan@155 54
ivan@155 55 SMALL.DL(1).toolbox = 'KSVD';
ivan@155 56 SMALL.DL(1).name = 'ksvd';
ivan@155 57 % Defining the parameters for KSVD
ivan@155 58 % In this example we are learning 88 atoms in 100 iterations, so that
ivan@155 59 % every frame in the training set can be represented with maximum Tdata
ivan@155 60 % dictionary elements. Type help ksvd in MATLAB prompt for more options.
ivan@155 61
ivan@155 62 SMALL.DL(1).param=struct(...
ivan@155 63 'Tdata', 5,...
ivan@155 64 'dictsize', SMALL.Problem.p,...
ivan@155 65 'iternum', 50);
ivan@155 66
ivan@155 67 % Learn the dictionary
ivan@155 68
ivan@155 69 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
ivan@155 70
ivan@155 71 % Set SMALL.Problem.A dictionary and reconstruction function
ivan@155 72 % (backward compatiblity with SPARCO: solver structure communicate
ivan@155 73 % only with Problem structure, ie no direct communication between DL and
ivan@155 74 % solver structures)
ivan@155 75
ivan@155 76 SMALL.Problem.A = SMALL.DL(1).D;
ivan@161 77 SMALL.Problem.reconstruct = @(x) AMT_reconstruct(x, SMALL.Problem);
ivan@155 78
ivan@155 79 %%
ivan@155 80 % Initialising solver structure
ivan@155 81 % Setting solver structure fields (toolbox, name, param, solution,
ivan@155 82 % reconstructed and time) to zero values
ivan@155 83 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
ivan@155 84 % algorithm is used for representation of training set in the learned
ivan@155 85 % dictionary.
ivan@155 86
ivan@155 87 SMALL.solver(1)=SMALL_init_solver;
ivan@155 88
ivan@155 89 % Defining the parameters needed for sparse representation
ivan@155 90
ivan@155 91 SMALL.solver(1).toolbox='SMALL';
ivan@163 92 SMALL.solver(1).name='SMALL_pcgp';
ivan@155 93
ivan@155 94 % Here we use mexLasso mode=2, with lambda=2, lambda2=0 and positivity
ivan@155 95 % constrain (type 'help mexLasso' for more information about modes):
ivan@155 96 %
ivan@155 97 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
ivan@155 98
ivan@155 99 SMALL.solver(1).param='20, 1e-2';
ivan@155 100 % struct(...
ivan@155 101 % 'lambda', 2,...
ivan@155 102 % 'pos', 1,...
ivan@155 103 % 'mode', 2);
ivan@155 104
ivan@155 105 % Call SMALL_soolve to represent the signal in the given dictionary.
ivan@155 106 % As a final command SMALL_solve will call above defined reconstruction
ivan@155 107 % function to reconstruct the training set (Problem.b) in the learned
ivan@155 108 % dictionary (Problem.A)
ivan@155 109
ivan@155 110 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
ivan@155 111
ivan@155 112 %%
ivan@155 113 % Analysis of the result of automatic music transcription. If groundtruth
ivan@155 114 % exists, we can compare transcribed notes and original and get usual
ivan@155 115 % True Positives, False Positives and False Negatives measures.
ivan@155 116
ivan@155 117 if ~isempty(SMALL.Problem.notesOriginal)
ivan@155 118 AMT_res(1) = AMT_analysis(SMALL.Problem, SMALL.solver(1));
ivan@155 119 end
ivan@155 120
ivan@155 121
ivan@155 122
ivan@155 123 %%
ivan@155 124 % % Here we solve the same problem using non-negative sparse coding with
ivan@155 125 % % SPAMS online dictionary learning (Julien Mairal 2009)
ivan@155 126 % %
ivan@155 127 % Initialising solver structure
ivan@155 128 % Setting solver structure fields (toolbox, name, param, solution,
ivan@155 129 % reconstructed and time) to zero values
ivan@155 130 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
ivan@155 131 % algorithm is used for representation of training set in the learned
ivan@155 132 % dictionary.
ivan@155 133
ivan@155 134 SMALL.solver(2)=SMALL_init_solver;
ivan@155 135
ivan@155 136 % Defining the parameters needed for sparse representation
ivan@155 137
ivan@155 138 SMALL.solver(2).toolbox='SPAMS';
ivan@155 139 SMALL.solver(2).name='mexLasso';
ivan@155 140
ivan@155 141 % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity
ivan@155 142 % constrain (type 'help mexLasso' for more information about modes):
ivan@155 143 %
ivan@155 144 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
ivan@155 145
ivan@155 146 SMALL.solver(2).param=struct('lambda', 3, 'pos', 1, 'mode', 2);
ivan@155 147
ivan@155 148
ivan@155 149 % You can also test ALPS, IST from MMbox or any other solver, but results
ivan@155 150 % are not as good as SPAMS
ivan@155 151 %
ivan@155 152 % % Initialising solver structure
ivan@155 153 % % Setting solver structure fields (toolbox, name, param, solution,
ivan@155 154 % % reconstructed and time) to zero values
ivan@155 155 %
ivan@155 156 % SMALL.solver(2)=SMALL_init_solver;
ivan@155 157 %
ivan@155 158 % % Defining the parameters needed for image denoising
ivan@155 159 %
ivan@155 160 % SMALL.solver(2).toolbox='ALPS';
ivan@155 161 % SMALL.solver(2).name='AlebraicPursuit';
ivan@155 162 %
ivan@155 163 % SMALL.solver(2).param=struct(...
ivan@155 164 % 'sparsity', 10,...
ivan@155 165 % 'memory', 1,...
ivan@155 166 % 'mode', 6,...
ivan@155 167 % 'iternum', 100,...
ivan@155 168 % 'tau',-1,...
ivan@155 169 % 'tolerance', 1e-14',...
ivan@155 170 % 'verbose',1);
ivan@155 171
ivan@155 172 % % Initialising Dictionary structure
ivan@155 173 % % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@155 174 % % to zero values
ivan@155 175 % % Initialising solver structure
ivan@155 176 % % Setting solver structure fields (toolbox, name, param, solution,
ivan@155 177 % % reconstructed and time) to zero values
ivan@155 178 %
ivan@155 179 % SMALL.solver(2)=SMALL_init_solver;
ivan@155 180 %
ivan@155 181 % % Defining the parameters needed for image denoising
ivan@155 182 %
ivan@155 183 % SMALL.solver(2).toolbox='MMbox';
ivan@155 184 % SMALL.solver(2).name='mm1';
ivan@155 185 % SMALL.solver(2).param=struct(...
ivan@155 186 % 'lambda',50,...
ivan@155 187 % 'iternum',1000,...
ivan@155 188 % 'map',0);
ivan@155 189
ivan@155 190 SMALL.DL(2)=SMALL_init_DL('MMbox', 'MM_cn', '', 1);
ivan@155 191
ivan@155 192
ivan@155 193 % Defining the parameters for Majorization Minimization dictionary update
ivan@155 194 %
ivan@155 195 % In this example we are learning 88 atoms in 200 iterations, so that
ivan@155 196
ivan@155 197
ivan@155 198 SMALL.DL(2).param=struct(...
ivan@155 199 'solver', SMALL.solver(2),...
ivan@155 200 'initdict', SMALL.Problem.A,...
ivan@155 201 'dictsize', SMALL.Problem.p,...
ivan@155 202 'iternum', 200,...
ivan@155 203 'iterDictUpdate', 1000,...
ivan@155 204 'epsDictUpdate', 1e-7,...
ivan@155 205 'cvset',0,...
ivan@155 206 'show_dict', 0);
ivan@155 207
ivan@155 208
ivan@155 209 % Learn the dictionary
ivan@155 210
ivan@155 211 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
ivan@155 212
ivan@155 213 % Set SMALL.Problem.A dictionary and reconstruction function
ivan@155 214 % (backward compatiblity with SPARCO: solver structure communicate
ivan@155 215 % only with Problem structure, ie no direct communication between DL and
ivan@155 216 % solver structures)
ivan@155 217
ivan@155 218 SMALL.Problem.A = SMALL.DL(2).D;
ivan@161 219 SMALL.Problem.reconstruct=@(x) AMT_reconstruct(x, SMALL.Problem);
ivan@155 220
ivan@155 221
ivan@155 222 % Call SMALL_soolve to represent the signal in the given dictionary.
ivan@155 223 % As a final command SMALL_solve will call above defined reconstruction
ivan@155 224 % function to reconstruct the training set (Problem.b) in the learned
ivan@155 225 % dictionary (Problem.A)
ivan@155 226
ivan@155 227 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
ivan@155 228
ivan@155 229
ivan@155 230 % Analysis of the result of automatic music transcription. If groundtruth
ivan@155 231 % exists, we can compare transcribed notes and original and get usual
ivan@155 232 % True Positives, False Positives and False Negatives measures.
ivan@155 233
ivan@155 234 if ~isempty(SMALL.Problem.notesOriginal)
ivan@155 235 AMT_res(2) = AMT_analysis(SMALL.Problem, SMALL.solver(2));
ivan@155 236 end
ivan@155 237
ivan@155 238
ivan@155 239 % Plot results and save midi files
ivan@155 240
ivan@155 241 if ~isempty(SMALL.Problem.notesOriginal)
ivan@155 242 figAMT = SMALL_AMT_plot(SMALL, AMT_res);
ivan@155 243 else
ivan@155 244 figAMT = figure('Name', 'Automatic Music Transcription KSVD vs SPAMS');
ivan@155 245 subplot(2,1,1); plot(SMALL.solver(1).reconstructed.notes(:,5), SMALL.solver(1).reconstructed.notes(:,3), 'kd ');
ivan@155 246 title (sprintf('%s dictionary in %.2f s', SMALL.DL(1).name, SMALL.DL(1).time));
ivan@155 247 xlabel('Time');
ivan@155 248 ylabel('Note Number');
ivan@155 249 subplot(2,1,2); plot(SMALL.solver(2).reconstructed.notes(:,5), SMALL.solver(2).reconstructed.notes(:,3), 'b* ');
ivan@155 250 title (sprintf('%s dictionary in %.2f s', SMALL.DL(2).name, SMALL.DL(2).time));
ivan@155 251 xlabel('Time');
ivan@155 252 ylabel('Note Number');
ivan@155 253 end
ivan@155 254
ivan@155 255 FS=filesep;
ivan@155 256
luis@186 257 [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m'));
ivan@155 258 cd([pathstr1,FS,'results']);
ivan@155 259
ivan@155 260 [filename,pathname] = uiputfile({' *.mid;' },'Save KSVD result midi');
ivan@155 261 if filename~=0 writemidi(SMALL.solver(1).reconstructed.midi, [pathname,FS,filename]);end
ivan@155 262
ivan@155 263 [filename,pathname] = uiputfile({' *.mid;' },'Save SPAMS result midi');
ivan@155 264 if filename~=0 writemidi(SMALL.solver(2).reconstructed.midi, [pathname,FS,filename]);end
ivan@155 265
ivan@155 266 [filename,pathname] = uiputfile({' *.fig;' },'Save KSVD vs SPAMS AMT figure');
ivan@155 267 if filename~=0 saveas(figAMT, [pathname,FS,filename]);end
ivan@155 268
ivan@155 269
ivan@155 270