idamnjanovic@6
|
1 %% DICTIONARY LEARNING FOR AUTOMATIC MUSIC TRANSCRIPTION
|
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@25
|
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 for i=1:5
|
idamnjanovic@6
|
34 %%
|
idamnjanovic@6
|
35 % Use KSVD Dictionary Learning Algorithm to Learn 88 notes (defined in
|
idamnjanovic@6
|
36 % SMALL.Problem.p) using sparsity constrain only
|
idamnjanovic@6
|
37
|
idamnjanovic@6
|
38 % Initialising Dictionary structure
|
idamnjanovic@6
|
39 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
idamnjanovic@6
|
40 % to zero values
|
idamnjanovic@6
|
41
|
idamnjanovic@6
|
42 SMALL.DL(i)=SMALL_init_DL(i);
|
idamnjanovic@6
|
43
|
idamnjanovic@6
|
44 % Defining the parameters needed for dictionary learning
|
idamnjanovic@6
|
45
|
idamnjanovic@6
|
46 SMALL.DL(i).toolbox = 'KSVD';
|
idamnjanovic@6
|
47 SMALL.DL(i).name = 'ksvd';
|
idamnjanovic@6
|
48
|
idamnjanovic@6
|
49 % Defining the parameters for KSVD
|
idamnjanovic@6
|
50 % In this example we are learning 88 atoms in 100 iterations, so that
|
idamnjanovic@6
|
51 % every frame in the training set can be represented with maximum 10
|
idamnjanovic@6
|
52 % dictionary elements. However, our aim here is to show how individual
|
idamnjanovic@6
|
53 % parameters can be ested in the AMT problem. We test five different
|
idamnjanovic@6
|
54 % values for residual error (Edata) in KSVD algorithm.
|
idamnjanovic@6
|
55 % Type help ksvd in MATLAB prompt for more options.
|
idamnjanovic@6
|
56
|
idamnjanovic@6
|
57 Edata(i)=8+i*2;
|
idamnjanovic@6
|
58 SMALL.DL(i).param=struct(...
|
idamnjanovic@6
|
59 'Edata', Edata(i),...
|
idamnjanovic@6
|
60 'dictsize', SMALL.Problem.p,...
|
idamnjanovic@6
|
61 'iternum', 100,...
|
idamnjanovic@6
|
62 'maxatoms', 10);
|
idamnjanovic@6
|
63
|
idamnjanovic@6
|
64 % Learn the dictionary
|
idamnjanovic@6
|
65
|
idamnjanovic@6
|
66 SMALL.DL(i) = SMALL_learn(SMALL.Problem, SMALL.DL(i));
|
idamnjanovic@6
|
67
|
idamnjanovic@6
|
68 % Set SMALL.Problem.A dictionary and reconstruction function
|
idamnjanovic@6
|
69 % (backward compatiblity with SPARCO: solver structure communicate
|
idamnjanovic@6
|
70 % only with Problem structure, ie no direct communication between DL and
|
idamnjanovic@6
|
71 % solver structures)
|
idamnjanovic@6
|
72
|
idamnjanovic@6
|
73 SMALL.Problem.A = SMALL.DL(i).D;
|
idamnjanovic@6
|
74 SMALL.Problem.reconstruct = @(x) SMALL_midiGenerate(x, SMALL.Problem);
|
idamnjanovic@6
|
75
|
idamnjanovic@6
|
76 %%
|
idamnjanovic@6
|
77 % Initialising solver structure
|
idamnjanovic@6
|
78 % Setting solver structure fields (toolbox, name, param, solution,
|
idamnjanovic@6
|
79 % reconstructed and time) to zero values
|
idamnjanovic@6
|
80 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
|
idamnjanovic@6
|
81 % algorithm is used for representation of training set in the learned
|
idamnjanovic@6
|
82 % dictionary.
|
idamnjanovic@6
|
83
|
idamnjanovic@6
|
84 SMALL.solver(1)=SMALL_init_solver;
|
idamnjanovic@6
|
85
|
idamnjanovic@6
|
86 % Defining the parameters needed for sparse representation
|
idamnjanovic@6
|
87
|
idamnjanovic@6
|
88 SMALL.solver(1).toolbox='SPAMS';
|
idamnjanovic@6
|
89 SMALL.solver(1).name='mexLasso';
|
idamnjanovic@6
|
90 SMALL.solver(1).param=struct('lambda', 2, 'pos', 1, 'mode', 2);
|
idamnjanovic@6
|
91
|
idamnjanovic@6
|
92 %Represent Training set in the learned dictionary
|
idamnjanovic@6
|
93
|
idamnjanovic@6
|
94 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
|
idamnjanovic@6
|
95
|
idamnjanovic@6
|
96 %%
|
idamnjanovic@6
|
97 % Analysis of the result of automatic music transcription. If groundtruth
|
idamnjanovic@6
|
98 % exists, we can compare transcribed notes and original and get usual
|
idamnjanovic@6
|
99 % True Positives, False Positives and False Negatives measures.
|
idamnjanovic@6
|
100
|
idamnjanovic@6
|
101 AMT_res(i) = AMT_analysis(SMALL.Problem, SMALL.solver(1));
|
idamnjanovic@6
|
102
|
idamnjanovic@6
|
103 if AMT_res(i).TP>TPmax
|
idamnjanovic@6
|
104 TPmax=AMT_res(i).TP;
|
idamnjanovic@6
|
105 BLmidi=SMALL.solver(1).reconstructed.midi;
|
idamnjanovic@6
|
106 max=i;
|
idamnjanovic@6
|
107 end
|
idamnjanovic@6
|
108
|
idamnjanovic@6
|
109 end %end of for loop
|
idamnjanovic@6
|
110
|
idamnjanovic@6
|
111 %%
|
idamnjanovic@6
|
112 % Plot results and save midi files
|
idamnjanovic@6
|
113
|
idamnjanovic@6
|
114 figAMTbest=SMALL_AMT_plot(SMALL, AMT_res(max));
|
idamnjanovic@6
|
115
|
idamnjanovic@6
|
116 resFig=figure('Name', 'Automatic Music Transcription KSVD Error TEST');
|
idamnjanovic@6
|
117
|
idamnjanovic@6
|
118 subplot (3,1,1); plot(Edata(:), [AMT_res(:).TP], 'ro-');
|
idamnjanovic@6
|
119 title('True Positives vs Edata');
|
idamnjanovic@6
|
120
|
idamnjanovic@6
|
121 subplot (3,1,2); plot(Edata(:), [AMT_res(:).FN], 'ro-');
|
idamnjanovic@6
|
122 title('False Negatives vs Edata');
|
idamnjanovic@6
|
123
|
idamnjanovic@6
|
124 subplot (3,1,3); plot(Edata(:), [AMT_res(:).FP], 'ro-');
|
idamnjanovic@6
|
125 title('False Positives vs Edata');
|
idamnjanovic@6
|
126
|
idamnjanovic@6
|
127 FS=filesep;
|
idamnjanovic@6
|
128 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
|
idamnjanovic@6
|
129 cd([pathstr1,FS,'results']);
|
idamnjanovic@6
|
130 [filename,pathname] = uiputfile({' *.mid;' },'Save midi');
|
idamnjanovic@6
|
131 if filename~=0 writemidi(BLmidi, [pathname,FS,filename]);end
|
idamnjanovic@6
|
132 [filename,pathname] = uiputfile({' *.fig;' },'Save figure TP/FN/FP vs lambda');
|
idamnjanovic@6
|
133 if filename~=0 saveas(resFig, [pathname,FS,filename]);end
|
idamnjanovic@6
|
134
|
idamnjanovic@6
|
135 [filename,pathname] = uiputfile({' *.fig;' },'Save BEST AMT figure');
|
idamnjanovic@6
|
136 if filename~=0 saveas(figAMTbest, [pathname,FS,filename]);end
|
idamnjanovic@6
|
137
|