comparison examples/Automatic Music Transcription/SMALL_AMT_SPAMS_test.m @ 6:f72603404233

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