annotate examples/Automatic Music Transcription/SMALL_AMT_SPAMS_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
ivan@107 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
idamnjanovic@6 25 clear;
idamnjanovic@6 26
idamnjanovic@6 27
idamnjanovic@6 28 % Defining Automatic Transcription of Piano tune as Dictionary Learning
idamnjanovic@6 29 % Problem
idamnjanovic@6 30
idamnjanovic@6 31 SMALL.Problem = generateAMT_Learning_Problem();
idamnjanovic@6 32 TPmax=0;
idamnjanovic@6 33 %%
idamnjanovic@6 34 for i=1:10
idamnjanovic@6 35 %%
idamnjanovic@6 36 % Solving AMT problem using non-negative sparse coding with
idamnjanovic@6 37 % SPAMS online dictionary learning (Julien Mairal 2009)
idamnjanovic@6 38 %
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(i)=SMALL_init_DL();
idamnjanovic@6 45
idamnjanovic@6 46 % Defining fields needed for dictionary learning
idamnjanovic@6 47
idamnjanovic@6 48 SMALL.DL(i).toolbox = 'SPAMS';
idamnjanovic@6 49 SMALL.DL(i).name = 'mexTrainDL';
idamnjanovic@6 50
idamnjanovic@6 51 % We test SPAMS for ten different values of parameter lambda
idamnjanovic@6 52 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
idamnjanovic@6 53
idamnjanovic@6 54 lambda(i)=1.4+0.2*i;
idamnjanovic@6 55
idamnjanovic@6 56 SMALL.DL(i).param=struct(...
idamnjanovic@6 57 'K', SMALL.Problem.p,...
idamnjanovic@6 58 'lambda', lambda(i),...
idamnjanovic@6 59 'iter', 300,...
idamnjanovic@6 60 'posAlpha', 1,...
idamnjanovic@6 61 'posD', 1,...
idamnjanovic@6 62 'whiten', 0,...
idamnjanovic@6 63 'mode', 2);
idamnjanovic@6 64
idamnjanovic@6 65 % Learn the dictionary
idamnjanovic@6 66
idamnjanovic@6 67 SMALL.DL(i) = SMALL_learn(SMALL.Problem, SMALL.DL(i));
idamnjanovic@6 68
idamnjanovic@6 69 % Set SMALL.Problem.A dictionary and reconstruction function
idamnjanovic@6 70 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 71 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 72 % solver structures)
idamnjanovic@6 73
idamnjanovic@6 74 SMALL.Problem.A = SMALL.DL(i).D;
idamnjanovic@6 75 SMALL.Problem.reconstruct=@(x) SMALL_midiGenerate(x, SMALL.Problem);
idamnjanovic@6 76
idamnjanovic@6 77
idamnjanovic@6 78 %%
idamnjanovic@6 79 % Initialising solver structure
idamnjanovic@6 80 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 81 % reconstructed and time) to zero values
idamnjanovic@6 82 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
idamnjanovic@6 83 % algorithm is used for representation of training set in the learned
idamnjanovic@6 84 % dictionary.
idamnjanovic@6 85
idamnjanovic@6 86 SMALL.solver(1)=SMALL_init_solver;
idamnjanovic@6 87
idamnjanovic@6 88 % Defining the parameters needed for sparse representation
idamnjanovic@6 89
idamnjanovic@6 90 SMALL.solver(1).toolbox='SPAMS';
idamnjanovic@6 91 SMALL.solver(1).name='mexLasso';
idamnjanovic@6 92
idamnjanovic@6 93 % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity
idamnjanovic@6 94 % constrain (type 'help mexLasso' for more information about modes):
idamnjanovic@6 95 %
idamnjanovic@6 96 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
idamnjanovic@6 97
idamnjanovic@6 98 SMALL.solver(1).param=struct(...
idamnjanovic@6 99 'lambda', 3,...
idamnjanovic@6 100 'pos', 1,...
idamnjanovic@6 101 'mode', 2);
idamnjanovic@6 102
idamnjanovic@6 103 % Call SMALL_soolve to represent the signal in the given dictionary.
idamnjanovic@6 104 % As a final command SMALL_solve will call above defined reconstruction
idamnjanovic@6 105 % function to reconstruct the training set (Problem.b) in the learned
idamnjanovic@6 106 % dictionary (Problem.A)
idamnjanovic@6 107
idamnjanovic@6 108 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 109
idamnjanovic@6 110 %%
idamnjanovic@6 111 % Analysis of the result of automatic music transcription. If groundtruth
idamnjanovic@6 112 % exists, we can compare transcribed notes and original and get usual
idamnjanovic@6 113 % True Positives, False Positives and False Negatives measures.
idamnjanovic@6 114
idamnjanovic@6 115 AMT_res(i) = AMT_analysis(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 116 if AMT_res(i).TP>TPmax
idamnjanovic@6 117 TPmax=AMT_res(i).TP;
idamnjanovic@6 118 BLmidi=SMALL.solver(1).reconstructed.midi;
idamnjanovic@6 119 writemidi(SMALL.solver(1).reconstructed.midi, ['testL',i,'.mid']);
idamnjanovic@6 120 max=i;
idamnjanovic@6 121 end
idamnjanovic@6 122 end %end of for loop
idamnjanovic@6 123 %%
idamnjanovic@6 124 % Plot results and save midi files
idamnjanovic@6 125
idamnjanovic@6 126 figAMTbest=SMALL_AMT_plot(SMALL, AMT_res(max));
idamnjanovic@6 127
idamnjanovic@6 128 resFig=figure('Name', 'Automatic Music Transcription SPAMS lambda TEST');
idamnjanovic@6 129
idamnjanovic@6 130 subplot (3,1,1); plot(lambda(:), [AMT_res(:).TP], 'ro-');
idamnjanovic@6 131 title('True Positives vs lambda');
idamnjanovic@6 132
idamnjanovic@6 133 subplot (3,1,2); plot(lambda(:), [AMT_res(:).FN], 'ro-');
idamnjanovic@6 134 title('False Negatives vs lambda');
idamnjanovic@6 135
idamnjanovic@6 136 subplot (3,1,3); plot(lambda(:), [AMT_res(:).FP], 'ro-');
idamnjanovic@6 137 title('False Positives vs lambda');
idamnjanovic@6 138
idamnjanovic@6 139 FS=filesep;
idamnjanovic@6 140 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@6 141 cd([pathstr1,FS,'results']);
idamnjanovic@6 142 [filename,pathname] = uiputfile({' *.mid;' },'Save midi');
idamnjanovic@6 143 if filename~=0 writemidi(BLmidi, [pathname,FS,filename]);end
idamnjanovic@6 144 [filename,pathname] = uiputfile({' *.fig;' },'Save figure TP/FN/FP vs lambda');
idamnjanovic@6 145 if filename~=0 saveas(resFig, [pathname,FS,filename]);end
idamnjanovic@6 146
idamnjanovic@6 147 [filename,pathname] = uiputfile({' *.fig;' },'Save BEST AMT figure');
idamnjanovic@6 148 if filename~=0 saveas(figAMTbest, [pathname,FS,filename]);end
idamnjanovic@6 149
idamnjanovic@6 150