annotate examples/Automatic Music Transcription/SMALL_AMT_DL_test.m @ 116:5b2ae0af72f9 sup_158_IMG_Processing_toolbox_

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