annotate examples/Automatic Music Transcription/SMALL_AMT_DL_test.m @ 128:8e660fd14774 ivand_dev

Feature 186
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Mon, 13 Jun 2011 14:55:45 +0100
parents dab78a3598b6
children f42aa8bcb82f
rev   line source
ivan@128 1 %% Dictionary Learning for Automatic Music Transcription - KSVD vs SPAMS
idamnjanovic@25 2 %
ivan@128 3 % *WARNING!* You should have SPAMS in your search path in order for this
ivan@128 4 % script to work.Due to licensing issues SPAMS can not be automatically
ivan@128 5 % provided in SMALLbox (http://www.di.ens.fr/willow/SPAMS/downloads.html).
ivan@128 6 %
idamnjanovic@6 7 % This file contains an example of how SMALLbox can be used to test diferent
idamnjanovic@6 8 % dictionary learning techniques in Automatic Music Transcription problem.
idamnjanovic@6 9 % It calls generateAMT_Learning_Problem that will let you to choose midi,
idamnjanovic@6 10 % wave or mat file to be transcribe. If file is midi it will be first
idamnjanovic@6 11 % converted to wave and original midi file will be used for comparison with
idamnjanovic@6 12 % results of dictionary learning and reconstruction.
idamnjanovic@6 13 % The function will generarte the Problem structure that is used to learn
idamnjanovic@6 14 % Problem.p notes spectrograms from training set Problem.b using
idamnjanovic@6 15 % dictionary learning technique defined in DL structure.
ivan@107 16
idamnjanovic@6 17 %
ivan@107 18 % Centre for Digital Music, Queen Mary, University of London.
ivan@107 19 % This file copyright 2010 Ivan Damnjanovic.
ivan@107 20 %
ivan@107 21 % This program is free software; you can redistribute it and/or
ivan@107 22 % modify it under the terms of the GNU General Public License as
ivan@107 23 % published by the Free Software Foundation; either version 2 of the
ivan@107 24 % License, or (at your option) any later version. See the file
ivan@107 25 % COPYING included with this distribution for more information.
idamnjanovic@6 26 %%
idamnjanovic@6 27
idamnjanovic@6 28 clear;
idamnjanovic@6 29
idamnjanovic@6 30
idamnjanovic@6 31 % Defining Automatic Transcription of Piano tune as Dictionary Learning
idamnjanovic@6 32 % Problem
idamnjanovic@6 33
idamnjanovic@6 34 SMALL.Problem = generateAMT_Learning_Problem();
idamnjanovic@6 35
idamnjanovic@6 36 %%
idamnjanovic@6 37 % Use KSVD Dictionary Learning Algorithm to Learn 88 notes (defined in
idamnjanovic@6 38 % SMALL.Problem.p) using sparsity constrain only
idamnjanovic@6 39
idamnjanovic@6 40 % Initialising Dictionary structure
idamnjanovic@6 41 % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@6 42 % to zero values
idamnjanovic@6 43
idamnjanovic@6 44 SMALL.DL(1)=SMALL_init_DL();
idamnjanovic@6 45
idamnjanovic@6 46 % Defining fields needed for dictionary learning
idamnjanovic@6 47
idamnjanovic@6 48 SMALL.DL(1).toolbox = 'KSVD';
idamnjanovic@6 49 SMALL.DL(1).name = 'ksvd';
idamnjanovic@6 50 % Defining the parameters for KSVD
idamnjanovic@6 51 % In this example we are learning 88 atoms in 100 iterations, so that
ivan@107 52 % every frame in the training set can be represented with maximum Tdata
idamnjanovic@6 53 % dictionary elements. Type help ksvd in MATLAB prompt for more options.
idamnjanovic@6 54
idamnjanovic@6 55 SMALL.DL(1).param=struct(...
ivan@107 56 'Tdata', 3,...
idamnjanovic@6 57 'dictsize', SMALL.Problem.p,...
ivan@107 58 'iternum', 50);
idamnjanovic@6 59
idamnjanovic@6 60 % Learn the dictionary
idamnjanovic@6 61
idamnjanovic@6 62 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
idamnjanovic@6 63
idamnjanovic@6 64 % Set SMALL.Problem.A dictionary and reconstruction function
idamnjanovic@6 65 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 66 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 67 % solver structures)
idamnjanovic@6 68
idamnjanovic@6 69 SMALL.Problem.A = SMALL.DL(1).D;
idamnjanovic@6 70 SMALL.Problem.reconstruct = @(x) SMALL_midiGenerate(x, SMALL.Problem);
idamnjanovic@6 71
idamnjanovic@6 72 %%
idamnjanovic@6 73 % Initialising solver structure
idamnjanovic@6 74 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 75 % reconstructed and time) to zero values
idamnjanovic@6 76 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
idamnjanovic@6 77 % algorithm is used for representation of training set in the learned
idamnjanovic@6 78 % dictionary.
idamnjanovic@6 79
idamnjanovic@6 80 SMALL.solver(1)=SMALL_init_solver;
idamnjanovic@6 81
idamnjanovic@6 82 % Defining the parameters needed for sparse representation
idamnjanovic@6 83
idamnjanovic@25 84 SMALL.solver(1).toolbox='SMALL';
idamnjanovic@25 85 SMALL.solver(1).name='SMALL_cgp';
idamnjanovic@6 86
idamnjanovic@6 87 % Here we use mexLasso mode=2, with lambda=2, lambda2=0 and positivity
idamnjanovic@6 88 % constrain (type 'help mexLasso' for more information about modes):
idamnjanovic@6 89 %
idamnjanovic@6 90 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
idamnjanovic@6 91
idamnjanovic@25 92 SMALL.solver(1).param='20, 1e-2';
idamnjanovic@25 93 % struct(...
idamnjanovic@25 94 % 'lambda', 2,...
idamnjanovic@25 95 % 'pos', 1,...
idamnjanovic@25 96 % 'mode', 2);
idamnjanovic@6 97
idamnjanovic@6 98 % Call SMALL_soolve to represent the signal in the given dictionary.
idamnjanovic@6 99 % As a final command SMALL_solve will call above defined reconstruction
idamnjanovic@6 100 % function to reconstruct the training set (Problem.b) in the learned
idamnjanovic@6 101 % dictionary (Problem.A)
idamnjanovic@6 102
idamnjanovic@6 103 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 104
idamnjanovic@6 105 %%
idamnjanovic@6 106 % Analysis of the result of automatic music transcription. If groundtruth
idamnjanovic@6 107 % exists, we can compare transcribed notes and original and get usual
idamnjanovic@6 108 % True Positives, False Positives and False Negatives measures.
idamnjanovic@6 109
idamnjanovic@6 110 if ~isempty(SMALL.Problem.notesOriginal)
idamnjanovic@6 111 AMT_res(1) = AMT_analysis(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 112 end
idamnjanovic@6 113
idamnjanovic@6 114
idamnjanovic@6 115 %%
idamnjanovic@6 116
ivan@107 117 % Here we solve the same problem using non-negative sparse coding with
ivan@107 118 % SPAMS online dictionary learning (Julien Mairal 2009)
ivan@107 119 %
ivan@107 120
ivan@107 121 % Initialising Dictionary structure
ivan@107 122 % Setting Dictionary structure fields (toolbox, name, param, D and time)
ivan@107 123 % to zero values
ivan@107 124
ivan@107 125 SMALL.DL(2)=SMALL_init_DL();
ivan@107 126
ivan@107 127
ivan@107 128 % Defining fields needed for dictionary learning
ivan@107 129
ivan@107 130 SMALL.DL(2).toolbox = 'SPAMS';
ivan@107 131 SMALL.DL(2).name = 'mexTrainDL';
ivan@107 132
ivan@107 133 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
ivan@107 134
ivan@107 135 SMALL.DL(2).param=struct(...
ivan@107 136 'K', SMALL.Problem.p,...
ivan@107 137 'lambda', 3,...
ivan@107 138 'iter', 300,...
ivan@107 139 'posAlpha', 1,...
ivan@107 140 'posD', 1,...
ivan@107 141 'whiten', 0,...
ivan@107 142 'mode', 2);
ivan@107 143
ivan@107 144 % Learn the dictionary
ivan@107 145
ivan@107 146 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
ivan@107 147
ivan@107 148 % Set SMALL.Problem.A dictionary and reconstruction function
ivan@107 149 % (backward compatiblity with SPARCO: solver structure communicate
ivan@107 150 % only with Problem structure, ie no direct communication between DL and
ivan@107 151 % solver structures)
ivan@107 152
ivan@107 153 SMALL.Problem.A = SMALL.DL(2).D;
ivan@107 154 SMALL.Problem.reconstruct=@(x) SMALL_midiGenerate(x, SMALL.Problem);
ivan@107 155
ivan@107 156 %%
ivan@107 157 % Initialising solver structure
ivan@107 158 % Setting solver structure fields (toolbox, name, param, solution,
ivan@107 159 % reconstructed and time) to zero values
ivan@107 160 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
ivan@107 161 % algorithm is used for representation of training set in the learned
ivan@107 162 % dictionary.
ivan@107 163
ivan@107 164 SMALL.solver(2)=SMALL_init_solver;
ivan@107 165
ivan@107 166 % Defining the parameters needed for sparse representation
ivan@107 167
ivan@107 168 SMALL.solver(2).toolbox='SPAMS';
ivan@107 169 SMALL.solver(2).name='mexLasso';
ivan@107 170
ivan@107 171 % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity
ivan@107 172 % constrain (type 'help mexLasso' for more information about modes):
ivan@107 173 %
ivan@107 174 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
ivan@107 175
ivan@107 176 SMALL.solver(2).param=struct('lambda', 3, 'pos', 1, 'mode', 2);
ivan@107 177
ivan@107 178 % Call SMALL_soolve to represent the signal in the given dictionary.
ivan@107 179 % As a final command SMALL_solve will call above defined reconstruction
ivan@107 180 % function to reconstruct the training set (Problem.b) in the learned
ivan@107 181 % dictionary (Problem.A)
ivan@107 182
ivan@107 183 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
ivan@107 184
ivan@107 185 %%
ivan@107 186 % Analysis of the result of automatic music transcription. If groundtruth
ivan@107 187 % exists, we can compare transcribed notes and original and get usual
ivan@107 188 % True Positives, False Positives and False Negatives measures.
ivan@107 189
ivan@107 190 if ~isempty(SMALL.Problem.notesOriginal)
ivan@107 191 AMT_res(2) = AMT_analysis(SMALL.Problem, SMALL.solver(2));
ivan@107 192 end
idamnjanovic@6 193
idamnjanovic@6 194 %%
idamnjanovic@6 195 % Plot results and save midi files
idamnjanovic@6 196
idamnjanovic@6 197 if ~isempty(SMALL.Problem.notesOriginal)
idamnjanovic@6 198 figAMT = SMALL_AMT_plot(SMALL, AMT_res);
idamnjanovic@6 199 else
idamnjanovic@6 200 figAMT = figure('Name', 'Automatic Music Transcription KSVD vs SPAMS');
idamnjanovic@6 201 subplot(2,1,1); plot(SMALL.solver(1).reconstructed.notes(:,5), SMALL.solver(1).reconstructed.notes(:,3), 'kd ');
idamnjanovic@6 202 title (sprintf('%s dictionary in %.2f s', SMALL.DL(1).name, SMALL.DL(1).time));
idamnjanovic@6 203 xlabel('Time');
idamnjanovic@6 204 ylabel('Note Number');
idamnjanovic@6 205 subplot(2,1,2); plot(SMALL.solver(2).reconstructed.notes(:,5), SMALL.solver(2).reconstructed.notes(:,3), 'b* ');
idamnjanovic@6 206 title (sprintf('%s dictionary in %.2f s', SMALL.DL(2).name, SMALL.DL(2).time));
idamnjanovic@6 207 xlabel('Time');
idamnjanovic@6 208 ylabel('Note Number');
idamnjanovic@6 209 end
idamnjanovic@6 210
idamnjanovic@6 211 FS=filesep;
idamnjanovic@6 212
idamnjanovic@6 213 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@6 214 cd([pathstr1,FS,'results']);
idamnjanovic@6 215
idamnjanovic@6 216 [filename,pathname] = uiputfile({' *.mid;' },'Save KSVD result midi');
idamnjanovic@6 217 if filename~=0 writemidi(SMALL.solver(1).reconstructed.midi, [pathname,FS,filename]);end
idamnjanovic@6 218
idamnjanovic@6 219 [filename,pathname] = uiputfile({' *.mid;' },'Save SPAMS result midi');
idamnjanovic@6 220 if filename~=0 writemidi(SMALL.solver(2).reconstructed.midi, [pathname,FS,filename]);end
idamnjanovic@6 221
idamnjanovic@6 222 [filename,pathname] = uiputfile({' *.fig;' },'Save KSVD vs SPAMS AMT figure');
idamnjanovic@6 223 if filename~=0 saveas(figAMT, [pathname,FS,filename]);end
idamnjanovic@6 224
idamnjanovic@6 225
idamnjanovic@6 226