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