comparison examples/Automatic Music Transcription/SMALL_AMT_DL_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
23 %%
24 % Use KSVD Dictionary Learning Algorithm to Learn 88 notes (defined in
25 % SMALL.Problem.p) using sparsity constrain only
26
27 % Initialising Dictionary structure
28 % Setting Dictionary structure fields (toolbox, name, param, D and time)
29 % to zero values
30
31 SMALL.DL(1)=SMALL_init_DL();
32
33 % Defining fields needed for dictionary learning
34
35 SMALL.DL(1).toolbox = 'KSVD';
36 SMALL.DL(1).name = 'ksvd';
37 % Defining the parameters for KSVD
38 % In this example we are learning 88 atoms in 100 iterations, so that
39 % every frame in the training set can be represented with maximum 3
40 % dictionary elements. Type help ksvd in MATLAB prompt for more options.
41
42 SMALL.DL(1).param=struct(...
43 'Tdata', 3,...
44 'dictsize', SMALL.Problem.p,...
45 'iternum', 100);
46
47 % Learn the dictionary
48
49 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
50
51 % Set SMALL.Problem.A dictionary and reconstruction function
52 % (backward compatiblity with SPARCO: solver structure communicate
53 % only with Problem structure, ie no direct communication between DL and
54 % solver structures)
55
56 SMALL.Problem.A = SMALL.DL(1).D;
57 SMALL.Problem.reconstruct = @(x) SMALL_midiGenerate(x, SMALL.Problem);
58
59 %%
60 % Initialising solver structure
61 % Setting solver structure fields (toolbox, name, param, solution,
62 % reconstructed and time) to zero values
63 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
64 % algorithm is used for representation of training set in the learned
65 % dictionary.
66
67 SMALL.solver(1)=SMALL_init_solver;
68
69 % Defining the parameters needed for sparse representation
70
71 SMALL.solver(1).toolbox='SPAMS';
72 SMALL.solver(1).name='mexLasso';
73
74 % Here we use mexLasso mode=2, with lambda=2, lambda2=0 and positivity
75 % constrain (type 'help mexLasso' for more information about modes):
76 %
77 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
78
79 SMALL.solver(1).param=struct(...
80 'lambda', 2,...
81 'pos', 1,...
82 'mode', 2);
83
84 % Call SMALL_soolve to represent the signal in the given dictionary.
85 % As a final command SMALL_solve will call above defined reconstruction
86 % function to reconstruct the training set (Problem.b) in the learned
87 % dictionary (Problem.A)
88
89 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
90
91 %%
92 % Analysis of the result of automatic music transcription. If groundtruth
93 % exists, we can compare transcribed notes and original and get usual
94 % True Positives, False Positives and False Negatives measures.
95
96 if ~isempty(SMALL.Problem.notesOriginal)
97 AMT_res(1) = AMT_analysis(SMALL.Problem, SMALL.solver(1));
98 end
99
100
101 %%
102 % Here we solve the same problem using non-negative sparse coding with
103 % SPAMS online dictionary learning (Julien Mairal 2009)
104 %
105
106 % Initialising Dictionary structure
107 % Setting Dictionary structure fields (toolbox, name, param, D and time)
108 % to zero values
109
110 SMALL.DL(2)=SMALL_init_DL();
111
112
113 % Defining fields needed for dictionary learning
114
115 SMALL.DL(2).toolbox = 'SPAMS';
116 SMALL.DL(2).name = 'mexTrainDL';
117
118 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
119
120 SMALL.DL(2).param=struct(...
121 'K', SMALL.Problem.p,...
122 'lambda', 3,...
123 'iter', 300,...
124 'posAlpha', 1,...
125 'posD', 1,...
126 'whiten', 0,...
127 'mode', 2);
128
129 % Learn the dictionary
130
131 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
132
133 % Set SMALL.Problem.A dictionary and reconstruction function
134 % (backward compatiblity with SPARCO: solver structure communicate
135 % only with Problem structure, ie no direct communication between DL and
136 % solver structures)
137
138 SMALL.Problem.A = SMALL.DL(2).D;
139 SMALL.Problem.reconstruct=@(x) SMALL_midiGenerate(x, SMALL.Problem);
140
141 %%
142 % Initialising solver structure
143 % Setting solver structure fields (toolbox, name, param, solution,
144 % reconstructed and time) to zero values
145 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS
146 % algorithm is used for representation of training set in the learned
147 % dictionary.
148
149 SMALL.solver(2)=SMALL_init_solver;
150
151 % Defining the parameters needed for sparse representation
152
153 SMALL.solver(2).toolbox='SPAMS';
154 SMALL.solver(2).name='mexLasso';
155
156 % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity
157 % constrain (type 'help mexLasso' for more information about modes):
158 %
159 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2
160
161 SMALL.solver(2).param=struct('lambda', 3, 'pos', 1, 'mode', 2);
162
163 % Call SMALL_soolve to represent the signal in the given dictionary.
164 % As a final command SMALL_solve will call above defined reconstruction
165 % function to reconstruct the training set (Problem.b) in the learned
166 % dictionary (Problem.A)
167
168 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
169
170 %%
171 % Analysis of the result of automatic music transcription. If groundtruth
172 % exists, we can compare transcribed notes and original and get usual
173 % True Positives, False Positives and False Negatives measures.
174
175 if ~isempty(SMALL.Problem.notesOriginal)
176 AMT_res(2) = AMT_analysis(SMALL.Problem, SMALL.solver(2));
177 end
178
179 %%
180 % Plot results and save midi files
181
182 if ~isempty(SMALL.Problem.notesOriginal)
183 figAMT = SMALL_AMT_plot(SMALL, AMT_res);
184 else
185 figAMT = figure('Name', 'Automatic Music Transcription KSVD vs SPAMS');
186 subplot(2,1,1); plot(SMALL.solver(1).reconstructed.notes(:,5), SMALL.solver(1).reconstructed.notes(:,3), 'kd ');
187 title (sprintf('%s dictionary in %.2f s', SMALL.DL(1).name, SMALL.DL(1).time));
188 xlabel('Time');
189 ylabel('Note Number');
190 subplot(2,1,2); plot(SMALL.solver(2).reconstructed.notes(:,5), SMALL.solver(2).reconstructed.notes(:,3), 'b* ');
191 title (sprintf('%s dictionary in %.2f s', SMALL.DL(2).name, SMALL.DL(2).time));
192 xlabel('Time');
193 ylabel('Note Number');
194 end
195
196 FS=filesep;
197
198 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
199 cd([pathstr1,FS,'results']);
200
201 [filename,pathname] = uiputfile({' *.mid;' },'Save KSVD result midi');
202 if filename~=0 writemidi(SMALL.solver(1).reconstructed.midi, [pathname,FS,filename]);end
203
204 [filename,pathname] = uiputfile({' *.mid;' },'Save SPAMS result midi');
205 if filename~=0 writemidi(SMALL.solver(2).reconstructed.midi, [pathname,FS,filename]);end
206
207 [filename,pathname] = uiputfile({' *.fig;' },'Save KSVD vs SPAMS AMT figure');
208 if filename~=0 saveas(figAMT, [pathname,FS,filename]);end
209
210
211