annotate examples/Automatic Music Transcription/SMALL_AMT_KSVD_Sparsity_test.m @ 6:f72603404233

(none)
author idamnjanovic
date Mon, 22 Mar 2010 10:45:01 +0000
parents
children cbf3521c25eb
rev   line source
idamnjanovic@6 1 %% DICTIONARY LEARNING FOR AUTOMATIC MUSIC TRANSCRIPTION EXAMPLE 1
idamnjanovic@6 2 % This file contains an example of how SMALLbox can be used to test diferent
idamnjanovic@6 3 % dictionary learning techniques in Automatic Music Transcription problem.
idamnjanovic@6 4 % It calls generateAMT_Learning_Problem that will let you to choose midi,
idamnjanovic@6 5 % wave or mat file to be transcribe. If file is midi it will be first
idamnjanovic@6 6 % converted to wave and original midi file will be used for comparison with
idamnjanovic@6 7 % results of dictionary learning and reconstruction.
idamnjanovic@6 8 % The function will generarte the Problem structure that is used to learn
idamnjanovic@6 9 % Problem.p notes spectrograms from training set Problem.b using
idamnjanovic@6 10 % dictionary learning technique defined in DL structure.
idamnjanovic@6 11 %
idamnjanovic@6 12 % Ivan Damnjanovic 2010
idamnjanovic@6 13 %%
idamnjanovic@6 14
idamnjanovic@6 15 clear;
idamnjanovic@6 16
idamnjanovic@6 17
idamnjanovic@6 18 % Defining Automatic Transcription of Piano tune as Dictionary Learning
idamnjanovic@6 19 % Problem
idamnjanovic@6 20
idamnjanovic@6 21 SMALL.Problem = generateAMT_Learning_Problem();
idamnjanovic@6 22
idamnjanovic@6 23 TPmax=0;
idamnjanovic@6 24
idamnjanovic@6 25 for i=1:10
idamnjanovic@6 26
idamnjanovic@6 27 %%
idamnjanovic@6 28 % Use KSVD Dictionary Learning Algorithm to Learn 88 notes (defined in
idamnjanovic@6 29 % SMALL.Problem.p) using sparsity constrain only
idamnjanovic@6 30
idamnjanovic@6 31 % Initialising Dictionary structure
idamnjanovic@6 32 % Setting Dictionary structure fields (toolbox, name, param, D and time)
idamnjanovic@6 33 % to zero values
idamnjanovic@6 34
idamnjanovic@6 35 SMALL.DL(i)=SMALL_init_DL(i);
idamnjanovic@6 36
idamnjanovic@6 37 % Defining fields needed for dictionary learning
idamnjanovic@6 38
idamnjanovic@6 39 SMALL.DL(i).toolbox = 'KSVD';
idamnjanovic@6 40 SMALL.DL(i).name = 'ksvd';
idamnjanovic@6 41
idamnjanovic@6 42 % Defining the parameters for KSVD
idamnjanovic@6 43 % In this example we are learning 88 atoms in 100 iterations.
idamnjanovic@6 44 % our aim here is to show how individual parameters can be tested in
idamnjanovic@6 45 % the AMT problem. We test ten different values for sparity (Tdata)
idamnjanovic@6 46 % in KSVD algorithm.
idamnjanovic@6 47 % Type help ksvd in MATLAB prompt for more options.
idamnjanovic@6 48 Tdata(i)=i;
idamnjanovic@6 49 SMALL.DL(i).param=struct('Tdata', Tdata(i), 'dictsize', SMALL.Problem.p, 'iternum', 100);
idamnjanovic@6 50
idamnjanovic@6 51 % Learn the dictionary
idamnjanovic@6 52
idamnjanovic@6 53 SMALL.DL(i) = SMALL_learn(SMALL.Problem, SMALL.DL(i));
idamnjanovic@6 54
idamnjanovic@6 55 % Set SMALL.Problem.A dictionary and reconstruction function
idamnjanovic@6 56 % (backward compatiblity with SPARCO: solver structure communicate
idamnjanovic@6 57 % only with Problem structure, ie no direct communication between DL and
idamnjanovic@6 58 % solver structures)
idamnjanovic@6 59
idamnjanovic@6 60 SMALL.Problem.A = SMALL.DL(i).D;
idamnjanovic@6 61 SMALL.Problem.reconstruct = @(x) SMALL_midiGenerate(x, SMALL.Problem);
idamnjanovic@6 62
idamnjanovic@6 63 %%
idamnjanovic@6 64 % Initialising solver structure
idamnjanovic@6 65 % Setting solver structure fields (toolbox, name, param, solution,
idamnjanovic@6 66 % reconstructed and time) to zero values
idamnjanovic@6 67 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
idamnjanovic@6 68 % algorithm is used for representation of training set in the learned
idamnjanovic@6 69 % dictionary.
idamnjanovic@6 70
idamnjanovic@6 71 SMALL.solver(1)=SMALL_init_solver;
idamnjanovic@6 72
idamnjanovic@6 73 % Defining the parameters needed for sparse representation
idamnjanovic@6 74
idamnjanovic@6 75 SMALL.solver(1).toolbox='SPAMS';
idamnjanovic@6 76 SMALL.solver(1).name='mexLasso';
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).param=struct(...
idamnjanovic@6 87 'lambda', 2,...
idamnjanovic@6 88 'pos', 1,...
idamnjanovic@6 89 'mode', 2);
idamnjanovic@6 90
idamnjanovic@6 91 % Call SMALL_soolve to represent the signal in the given dictionary.
idamnjanovic@6 92 % As a final command SMALL_solve will call above defined reconstruction
idamnjanovic@6 93 % function to reconstruct the training set (Problem.b) in the learned
idamnjanovic@6 94 % dictionary (Problem.A)
idamnjanovic@6 95
idamnjanovic@6 96
idamnjanovic@6 97 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 98
idamnjanovic@6 99 %%
idamnjanovic@6 100 % Analysis of the result of automatic music transcription. If groundtruth
idamnjanovic@6 101 % exists, we can compare transcribed notes and original and get usual
idamnjanovic@6 102 % True Positives, False Positives and False Negatives measures.
idamnjanovic@6 103
idamnjanovic@6 104 AMT_res(i) = AMT_analysis(SMALL.Problem, SMALL.solver(1));
idamnjanovic@6 105 if AMT_res(i).TP>TPmax
idamnjanovic@6 106 TPmax=AMT_res(i).TP;
idamnjanovic@6 107 BLmidi=SMALL.solver(1).reconstructed.midi;
idamnjanovic@6 108 max=i;
idamnjanovic@6 109 end
idamnjanovic@6 110 end % end of for loop
idamnjanovic@6 111
idamnjanovic@6 112 %%
idamnjanovic@6 113 % Plot results and save midi files
idamnjanovic@6 114
idamnjanovic@6 115 figAMTbest=SMALL_AMT_plot(SMALL, AMT_res(max));
idamnjanovic@6 116
idamnjanovic@6 117 resFig=figure('Name', 'Automatic Music Transcription KSVD Sparsity TEST');
idamnjanovic@6 118
idamnjanovic@6 119 subplot (3,1,1); plot(Tdata(:), [AMT_res(:).TP], 'ro-');
idamnjanovic@6 120 title('True Positives vs Tdata');
idamnjanovic@6 121
idamnjanovic@6 122 subplot (3,1,2); plot(Tdata(:), [AMT_res(:).FN], 'ro-');
idamnjanovic@6 123 title('False Negatives vs Tdata');
idamnjanovic@6 124
idamnjanovic@6 125 subplot (3,1,3); plot(Tdata(:), [AMT_res(:).FP], 'ro-');
idamnjanovic@6 126 title('False Positives vs Tdata');
idamnjanovic@6 127
idamnjanovic@6 128 FS=filesep;
idamnjanovic@6 129 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@6 130 cd([pathstr1,FS,'results']);
idamnjanovic@6 131 [filename,pathname] = uiputfile({' *.mid;' },'Save midi');
idamnjanovic@6 132 if filename~=0 writemidi(BLmidi, [pathname,FS,filename]);end
idamnjanovic@6 133 [filename,pathname] = uiputfile({' *.fig;' },'Save figure TP/FN/FP vs Tdata');
idamnjanovic@6 134 if filename~=0 saveas(resFig, [pathname,FS,filename]);end
idamnjanovic@6 135
idamnjanovic@6 136 [filename,pathname] = uiputfile({' *.fig;' },'Save BEST AMT figure');
idamnjanovic@6 137 if filename~=0 saveas(figAMTbest, [pathname,FS,filename]);end
idamnjanovic@6 138