Mercurial > hg > smallbox
comparison examples/MajorizationMinimization tests/SMALL_AMT_DL_test_KSVD_MM.m @ 164:4205744092e6 release_1.9
Merge from branch "ivand_dev"
author | Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk> |
---|---|
date | Wed, 07 Sep 2011 14:17:30 +0100 |
parents | 855025f4c779 |
children | 9c418bea7f6a |
comparison
equal
deleted
inserted
replaced
151:af5abc34a5e1 | 164:4205744092e6 |
---|---|
1 %% Dictionary Learning for Automatic Music Transcription - KSVD vs SPAMS | |
2 % | |
3 % | |
4 % This file contains an example of how SMALLbox can be used to test diferent | |
5 % dictionary learning techniques in Automatic Music Transcription problem. | |
6 % It calls generateAMT_Learning_Problem that will let you to choose midi, | |
7 % wave or mat file to be transcribe. If file is midi it will be first | |
8 % converted to wave and original midi file will be used for comparison with | |
9 % results of dictionary learning and reconstruction. | |
10 % The function will generarte the Problem structure that is used to learn | |
11 % Problem.p notes spectrograms from training set Problem.b using | |
12 % dictionary learning technique defined in DL structure. | |
13 % Two dictionary learning techniques were compared: | |
14 % | |
15 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient | |
16 % Implementation of the K-SVD Algorithm using Batch Orthogonal | |
17 % Matching Pursuit", Technical Report - CS, Technion, April 2008. | |
18 % | |
19 % - MMDL - M. Yaghoobi, T. Blumensath and M. Davies, "Dictionary Learning | |
20 % for Sparse Approximations with the Majorization Method", IEEE | |
21 % Trans. on Signal Processing, Vol. 57, No. 6, pp 2178-2191, | |
22 % 2009. | |
23 | |
24 % | |
25 % Centre for Digital Music, Queen Mary, University of London. | |
26 % This file copyright 2011 Ivan Damnjanovic. | |
27 % | |
28 % This program is free software; you can redistribute it and/or | |
29 % modify it under the terms of the GNU General Public License as | |
30 % published by the Free Software Foundation; either version 2 of the | |
31 % License, or (at your option) any later version. See the file | |
32 % COPYING included with this distribution for more information. | |
33 %% | |
34 | |
35 clear; | |
36 | |
37 | |
38 % Defining Automatic Transcription of Piano tune as Dictionary Learning | |
39 % Problem | |
40 | |
41 SMALL.Problem = generateAMTProblem('',2048,0.75); | |
42 | |
43 %% | |
44 % Use KSVD Dictionary Learning Algorithm to Learn 88 notes (defined in | |
45 % SMALL.Problem.p) using sparsity constrain only | |
46 | |
47 % Initialising Dictionary structure | |
48 % Setting Dictionary structure fields (toolbox, name, param, D and time) | |
49 % to zero values | |
50 | |
51 SMALL.DL(1)=SMALL_init_DL(); | |
52 | |
53 % Defining fields needed for dictionary learning | |
54 | |
55 SMALL.DL(1).toolbox = 'KSVD'; | |
56 SMALL.DL(1).name = 'ksvd'; | |
57 % Defining the parameters for KSVD | |
58 % In this example we are learning 88 atoms in 100 iterations, so that | |
59 % every frame in the training set can be represented with maximum Tdata | |
60 % dictionary elements. Type help ksvd in MATLAB prompt for more options. | |
61 | |
62 SMALL.DL(1).param=struct(... | |
63 'Tdata', 5,... | |
64 'dictsize', SMALL.Problem.p,... | |
65 'iternum', 50); | |
66 | |
67 % Learn the dictionary | |
68 | |
69 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); | |
70 | |
71 % Set SMALL.Problem.A dictionary and reconstruction function | |
72 % (backward compatiblity with SPARCO: solver structure communicate | |
73 % only with Problem structure, ie no direct communication between DL and | |
74 % solver structures) | |
75 | |
76 SMALL.Problem.A = SMALL.DL(1).D; | |
77 SMALL.Problem.reconstruct = @(x) AMT_reconstruct(x, SMALL.Problem); | |
78 | |
79 %% | |
80 % Initialising solver structure | |
81 % Setting solver structure fields (toolbox, name, param, solution, | |
82 % reconstructed and time) to zero values | |
83 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS | |
84 % algorithm is used for representation of training set in the learned | |
85 % dictionary. | |
86 | |
87 SMALL.solver(1)=SMALL_init_solver; | |
88 | |
89 % Defining the parameters needed for sparse representation | |
90 | |
91 SMALL.solver(1).toolbox='SMALL'; | |
92 SMALL.solver(1).name='SMALL_pcgp'; | |
93 | |
94 % Here we use mexLasso mode=2, with lambda=2, lambda2=0 and positivity | |
95 % constrain (type 'help mexLasso' for more information about modes): | |
96 % | |
97 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2 | |
98 | |
99 SMALL.solver(1).param='20, 1e-2'; | |
100 % struct(... | |
101 % 'lambda', 2,... | |
102 % 'pos', 1,... | |
103 % 'mode', 2); | |
104 | |
105 % Call SMALL_soolve to represent the signal in the given dictionary. | |
106 % As a final command SMALL_solve will call above defined reconstruction | |
107 % function to reconstruct the training set (Problem.b) in the learned | |
108 % dictionary (Problem.A) | |
109 | |
110 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); | |
111 | |
112 %% | |
113 % Analysis of the result of automatic music transcription. If groundtruth | |
114 % exists, we can compare transcribed notes and original and get usual | |
115 % True Positives, False Positives and False Negatives measures. | |
116 | |
117 if ~isempty(SMALL.Problem.notesOriginal) | |
118 AMT_res(1) = AMT_analysis(SMALL.Problem, SMALL.solver(1)); | |
119 end | |
120 | |
121 | |
122 | |
123 %% | |
124 % % Here we solve the same problem using non-negative sparse coding with | |
125 % % SPAMS online dictionary learning (Julien Mairal 2009) | |
126 % % | |
127 % Initialising solver structure | |
128 % Setting solver structure fields (toolbox, name, param, solution, | |
129 % reconstructed and time) to zero values | |
130 % As an example, SPAMS (Julien Mairal 2009) implementation of LARS | |
131 % algorithm is used for representation of training set in the learned | |
132 % dictionary. | |
133 | |
134 SMALL.solver(2)=SMALL_init_solver; | |
135 | |
136 % Defining the parameters needed for sparse representation | |
137 | |
138 SMALL.solver(2).toolbox='SPAMS'; | |
139 SMALL.solver(2).name='mexLasso'; | |
140 | |
141 % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity | |
142 % constrain (type 'help mexLasso' for more information about modes): | |
143 % | |
144 % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2 | |
145 | |
146 SMALL.solver(2).param=struct('lambda', 3, 'pos', 1, 'mode', 2); | |
147 | |
148 | |
149 % You can also test ALPS, IST from MMbox or any other solver, but results | |
150 % are not as good as SPAMS | |
151 % | |
152 % % Initialising solver structure | |
153 % % Setting solver structure fields (toolbox, name, param, solution, | |
154 % % reconstructed and time) to zero values | |
155 % | |
156 % SMALL.solver(2)=SMALL_init_solver; | |
157 % | |
158 % % Defining the parameters needed for image denoising | |
159 % | |
160 % SMALL.solver(2).toolbox='ALPS'; | |
161 % SMALL.solver(2).name='AlebraicPursuit'; | |
162 % | |
163 % SMALL.solver(2).param=struct(... | |
164 % 'sparsity', 10,... | |
165 % 'memory', 1,... | |
166 % 'mode', 6,... | |
167 % 'iternum', 100,... | |
168 % 'tau',-1,... | |
169 % 'tolerance', 1e-14',... | |
170 % 'verbose',1); | |
171 | |
172 % % Initialising Dictionary structure | |
173 % % Setting Dictionary structure fields (toolbox, name, param, D and time) | |
174 % % to zero values | |
175 % % Initialising solver structure | |
176 % % Setting solver structure fields (toolbox, name, param, solution, | |
177 % % reconstructed and time) to zero values | |
178 % | |
179 % SMALL.solver(2)=SMALL_init_solver; | |
180 % | |
181 % % Defining the parameters needed for image denoising | |
182 % | |
183 % SMALL.solver(2).toolbox='MMbox'; | |
184 % SMALL.solver(2).name='mm1'; | |
185 % SMALL.solver(2).param=struct(... | |
186 % 'lambda',50,... | |
187 % 'iternum',1000,... | |
188 % 'map',0); | |
189 | |
190 SMALL.DL(2)=SMALL_init_DL('MMbox', 'MM_cn', '', 1); | |
191 | |
192 | |
193 % Defining the parameters for Majorization Minimization dictionary update | |
194 % | |
195 % In this example we are learning 88 atoms in 200 iterations, so that | |
196 | |
197 | |
198 SMALL.DL(2).param=struct(... | |
199 'solver', SMALL.solver(2),... | |
200 'initdict', SMALL.Problem.A,... | |
201 'dictsize', SMALL.Problem.p,... | |
202 'iternum', 200,... | |
203 'iterDictUpdate', 1000,... | |
204 'epsDictUpdate', 1e-7,... | |
205 'cvset',0,... | |
206 'show_dict', 0); | |
207 | |
208 | |
209 % Learn the dictionary | |
210 | |
211 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); | |
212 | |
213 % Set SMALL.Problem.A dictionary and reconstruction function | |
214 % (backward compatiblity with SPARCO: solver structure communicate | |
215 % only with Problem structure, ie no direct communication between DL and | |
216 % solver structures) | |
217 | |
218 SMALL.Problem.A = SMALL.DL(2).D; | |
219 SMALL.Problem.reconstruct=@(x) AMT_reconstruct(x, SMALL.Problem); | |
220 | |
221 | |
222 % Call SMALL_soolve to represent the signal in the given dictionary. | |
223 % As a final command SMALL_solve will call above defined reconstruction | |
224 % function to reconstruct the training set (Problem.b) in the learned | |
225 % dictionary (Problem.A) | |
226 | |
227 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); | |
228 | |
229 | |
230 % Analysis of the result of automatic music transcription. If groundtruth | |
231 % exists, we can compare transcribed notes and original and get usual | |
232 % True Positives, False Positives and False Negatives measures. | |
233 | |
234 if ~isempty(SMALL.Problem.notesOriginal) | |
235 AMT_res(2) = AMT_analysis(SMALL.Problem, SMALL.solver(2)); | |
236 end | |
237 | |
238 | |
239 % Plot results and save midi files | |
240 | |
241 if ~isempty(SMALL.Problem.notesOriginal) | |
242 figAMT = SMALL_AMT_plot(SMALL, AMT_res); | |
243 else | |
244 figAMT = figure('Name', 'Automatic Music Transcription KSVD vs SPAMS'); | |
245 subplot(2,1,1); plot(SMALL.solver(1).reconstructed.notes(:,5), SMALL.solver(1).reconstructed.notes(:,3), 'kd '); | |
246 title (sprintf('%s dictionary in %.2f s', SMALL.DL(1).name, SMALL.DL(1).time)); | |
247 xlabel('Time'); | |
248 ylabel('Note Number'); | |
249 subplot(2,1,2); plot(SMALL.solver(2).reconstructed.notes(:,5), SMALL.solver(2).reconstructed.notes(:,3), 'b* '); | |
250 title (sprintf('%s dictionary in %.2f s', SMALL.DL(2).name, SMALL.DL(2).time)); | |
251 xlabel('Time'); | |
252 ylabel('Note Number'); | |
253 end | |
254 | |
255 FS=filesep; | |
256 | |
257 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); | |
258 cd([pathstr1,FS,'results']); | |
259 | |
260 [filename,pathname] = uiputfile({' *.mid;' },'Save KSVD result midi'); | |
261 if filename~=0 writemidi(SMALL.solver(1).reconstructed.midi, [pathname,FS,filename]);end | |
262 | |
263 [filename,pathname] = uiputfile({' *.mid;' },'Save SPAMS result midi'); | |
264 if filename~=0 writemidi(SMALL.solver(2).reconstructed.midi, [pathname,FS,filename]);end | |
265 | |
266 [filename,pathname] = uiputfile({' *.fig;' },'Save KSVD vs SPAMS AMT figure'); | |
267 if filename~=0 saveas(figAMT, [pathname,FS,filename]);end | |
268 | |
269 | |
270 |