annotate examples/Automatic Music Transcription/SMALL_AMT_DL_test.m @ 38:84b7c44e54c4

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:33:18 +0000
parents cbf3521c25eb
children dab78a3598b6
rev   line source
idamnjanovic@6 1 %% DICTIONARY LEARNING FOR AUTOMATIC MUSIC TRANSCRIPTION EXAMPLE 1
idamnjanovic@25 2 %
idamnjanovic@25 3 % Centre for Digital Music, Queen Mary, University of London.
idamnjanovic@25 4 % This file copyright 2010 Ivan Damnjanovic.
idamnjanovic@25 5 %
idamnjanovic@25 6 % This program is free software; you can redistribute it and/or
idamnjanovic@25 7 % modify it under the terms of the GNU General Public License as
idamnjanovic@25 8 % published by the Free Software Foundation; either version 2 of the
idamnjanovic@25 9 % License, or (at your option) any later version. See the file
idamnjanovic@25 10 % COPYING included with this distribution for more information.
idamnjanovic@25 11 %
idamnjanovic@6 12 % This file contains an example of how SMALLbox can be used to test diferent
idamnjanovic@6 13 % dictionary learning techniques in Automatic Music Transcription problem.
idamnjanovic@6 14 % It calls generateAMT_Learning_Problem that will let you to choose midi,
idamnjanovic@6 15 % wave or mat file to be transcribe. If file is midi it will be first
idamnjanovic@6 16 % converted to wave and original midi file will be used for comparison with
idamnjanovic@6 17 % results of dictionary learning and reconstruction.
idamnjanovic@6 18 % The function will generarte the Problem structure that is used to learn
idamnjanovic@6 19 % Problem.p notes spectrograms from training set Problem.b using
idamnjanovic@6 20 % dictionary learning technique defined in DL structure.
idamnjanovic@6 21 %
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
idamnjanovic@6 48 % every frame in the training set can be represented with maximum 3
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(...
idamnjanovic@25 52 'Tdata', 10,...
idamnjanovic@6 53 'dictsize', SMALL.Problem.p,...
idamnjanovic@6 54 'iternum', 100);
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
idamnjanovic@25 113 % % Here we solve the same problem using non-negative sparse coding with
idamnjanovic@25 114 % % SPAMS online dictionary learning (Julien Mairal 2009)
idamnjanovic@25 115 % %
idamnjanovic@25 116 %
idamnjanovic@25 117 % % Initialising Dictionary structure
idamnjanovic@25 118 % % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@25 119 % % to zero values
idamnjanovic@25 120 %
idamnjanovic@25 121 % SMALL.DL(2)=SMALL_init_DL();
idamnjanovic@25 122 %
idamnjanovic@25 123 %
idamnjanovic@25 124 % % Defining fields needed for dictionary learning
idamnjanovic@25 125 %
idamnjanovic@25 126 % SMALL.DL(2).toolbox = 'SPAMS';
idamnjanovic@25 127 % SMALL.DL(2).name = 'mexTrainDL';
idamnjanovic@25 128 %
idamnjanovic@25 129 % % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
idamnjanovic@25 130 %
idamnjanovic@25 131 % SMALL.DL(2).param=struct(...
idamnjanovic@25 132 % 'K', SMALL.Problem.p,...
idamnjanovic@25 133 % 'lambda', 3,...
idamnjanovic@25 134 % 'iter', 300,...
idamnjanovic@25 135 % 'posAlpha', 1,...
idamnjanovic@25 136 % 'posD', 1,...
idamnjanovic@25 137 % 'whiten', 0,...
idamnjanovic@25 138 % 'mode', 2);
idamnjanovic@25 139 %
idamnjanovic@25 140 % % Learn the dictionary
idamnjanovic@25 141 %
idamnjanovic@25 142 % SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
idamnjanovic@25 143 %
idamnjanovic@25 144 % % Set SMALL.Problem.A dictionary and reconstruction function
idamnjanovic@25 145 % % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@25 146 % % only with Problem structure, ie no direct communication between DL and
idamnjanovic@25 147 % % solver structures)
idamnjanovic@25 148 %
idamnjanovic@25 149 % SMALL.Problem.A = SMALL.DL(2).D;
idamnjanovic@25 150 % SMALL.Problem.reconstruct=@(x) SMALL_midiGenerate(x, SMALL.Problem);
idamnjanovic@25 151 %
idamnjanovic@25 152 % %%
idamnjanovic@25 153 % % Initialising solver structure
idamnjanovic@25 154 % % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@25 155 % % reconstructed and time) to zero values
idamnjanovic@25 156 % % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
idamnjanovic@25 157 % % algorithm is used for representation of training set in the learned
idamnjanovic@25 158 % % dictionary.
idamnjanovic@25 159 %
idamnjanovic@25 160 % SMALL.solver(2)=SMALL_init_solver;
idamnjanovic@25 161 %
idamnjanovic@25 162 % % Defining the parameters needed for sparse representation
idamnjanovic@25 163 %
idamnjanovic@25 164 % SMALL.solver(2).toolbox='SPAMS';
idamnjanovic@25 165 % SMALL.solver(2).name='mexLasso';
idamnjanovic@25 166 %
idamnjanovic@25 167 % % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity
idamnjanovic@25 168 % % constrain (type 'help mexLasso' for more information about modes):
idamnjanovic@25 169 % %
idamnjanovic@25 170 % % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
idamnjanovic@25 171 %
idamnjanovic@25 172 % SMALL.solver(2).param=struct('lambda', 3, 'pos', 1, 'mode', 2);
idamnjanovic@25 173 %
idamnjanovic@25 174 % % Call SMALL_soolve to represent the signal in the given dictionary.
idamnjanovic@25 175 % % As a final command SMALL_solve will call above defined reconstruction
idamnjanovic@25 176 % % function to reconstruct the training set (Problem.b) in the learned
idamnjanovic@25 177 % % dictionary (Problem.A)
idamnjanovic@25 178 %
idamnjanovic@25 179 % SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
idamnjanovic@25 180 %
idamnjanovic@25 181 % %%
idamnjanovic@25 182 % % Analysis of the result of automatic music transcription. If groundtruth
idamnjanovic@25 183 % % exists, we can compare transcribed notes and original and get usual
idamnjanovic@25 184 % % True Positives, False Positives and False Negatives measures.
idamnjanovic@25 185 %
idamnjanovic@25 186 % if ~isempty(SMALL.Problem.notesOriginal)
idamnjanovic@25 187 % AMT_res(2) = AMT_analysis(SMALL.Problem, SMALL.solver(2));
idamnjanovic@25 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