ivan@155: %% Dictionary Learning for Automatic Music Transcription - KSVD vs SPAMS ivan@155: % ivan@155: % ivan@155: % This file contains an example of how SMALLbox can be used to test diferent ivan@155: % dictionary learning techniques in Automatic Music Transcription problem. ivan@155: % It calls generateAMT_Learning_Problem that will let you to choose midi, ivan@155: % wave or mat file to be transcribe. If file is midi it will be first ivan@155: % converted to wave and original midi file will be used for comparison with ivan@155: % results of dictionary learning and reconstruction. ivan@155: % The function will generarte the Problem structure that is used to learn ivan@155: % Problem.p notes spectrograms from training set Problem.b using ivan@155: % dictionary learning technique defined in DL structure. ivan@155: % Two dictionary learning techniques were compared: ivan@155: % ivan@155: % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient ivan@155: % Implementation of the K-SVD Algorithm using Batch Orthogonal ivan@155: % Matching Pursuit", Technical Report - CS, Technion, April 2008. ivan@155: % ivan@155: % - MMDL - M. Yaghoobi, T. Blumensath and M. Davies, "Dictionary Learning ivan@155: % for Sparse Approximations with the Majorization Method", IEEE ivan@155: % Trans. on Signal Processing, Vol. 57, No. 6, pp 2178-2191, ivan@155: % 2009. ivan@155: ivan@155: % ivan@155: % Centre for Digital Music, Queen Mary, University of London. ivan@155: % This file copyright 2011 Ivan Damnjanovic. ivan@155: % ivan@155: % This program is free software; you can redistribute it and/or ivan@155: % modify it under the terms of the GNU General Public License as ivan@155: % published by the Free Software Foundation; either version 2 of the ivan@155: % License, or (at your option) any later version. See the file ivan@155: % COPYING included with this distribution for more information. ivan@155: %% ivan@155: ivan@155: clear; ivan@155: ivan@155: ivan@155: % Defining Automatic Transcription of Piano tune as Dictionary Learning ivan@155: % Problem ivan@155: ivan@161: SMALL.Problem = generateAMTProblem('',2048,0.75); ivan@155: ivan@155: %% ivan@155: % Use KSVD Dictionary Learning Algorithm to Learn 88 notes (defined in ivan@155: % SMALL.Problem.p) using sparsity constrain only ivan@155: ivan@155: % Initialising Dictionary structure ivan@155: % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@155: % to zero values ivan@155: ivan@155: SMALL.DL(1)=SMALL_init_DL(); ivan@155: ivan@155: % Defining fields needed for dictionary learning ivan@155: ivan@155: SMALL.DL(1).toolbox = 'KSVD'; ivan@155: SMALL.DL(1).name = 'ksvd'; ivan@155: % Defining the parameters for KSVD ivan@155: % In this example we are learning 88 atoms in 100 iterations, so that ivan@155: % every frame in the training set can be represented with maximum Tdata ivan@155: % dictionary elements. Type help ksvd in MATLAB prompt for more options. ivan@155: ivan@155: SMALL.DL(1).param=struct(... ivan@155: 'Tdata', 5,... ivan@155: 'dictsize', SMALL.Problem.p,... ivan@155: 'iternum', 50); ivan@155: ivan@155: % Learn the dictionary ivan@155: ivan@155: SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); ivan@155: ivan@155: % Set SMALL.Problem.A dictionary and reconstruction function ivan@155: % (backward compatiblity with SPARCO: solver structure communicate ivan@155: % only with Problem structure, ie no direct communication between DL and ivan@155: % solver structures) ivan@155: ivan@155: SMALL.Problem.A = SMALL.DL(1).D; ivan@161: SMALL.Problem.reconstruct = @(x) AMT_reconstruct(x, SMALL.Problem); ivan@155: ivan@155: %% ivan@155: % Initialising solver structure ivan@155: % Setting solver structure fields (toolbox, name, param, solution, ivan@155: % reconstructed and time) to zero values ivan@155: % As an example, SPAMS (Julien Mairal 2009) implementation of LARS ivan@155: % algorithm is used for representation of training set in the learned ivan@155: % dictionary. ivan@155: ivan@155: SMALL.solver(1)=SMALL_init_solver; ivan@155: ivan@155: % Defining the parameters needed for sparse representation ivan@155: ivan@155: SMALL.solver(1).toolbox='SMALL'; ivan@163: SMALL.solver(1).name='SMALL_pcgp'; ivan@155: ivan@155: % Here we use mexLasso mode=2, with lambda=2, lambda2=0 and positivity ivan@155: % constrain (type 'help mexLasso' for more information about modes): ivan@155: % ivan@155: % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2 ivan@155: ivan@155: SMALL.solver(1).param='20, 1e-2'; ivan@155: % struct(... ivan@155: % 'lambda', 2,... ivan@155: % 'pos', 1,... ivan@155: % 'mode', 2); ivan@155: ivan@155: % Call SMALL_soolve to represent the signal in the given dictionary. ivan@155: % As a final command SMALL_solve will call above defined reconstruction ivan@155: % function to reconstruct the training set (Problem.b) in the learned ivan@155: % dictionary (Problem.A) ivan@155: ivan@155: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); ivan@155: ivan@155: %% ivan@155: % Analysis of the result of automatic music transcription. If groundtruth ivan@155: % exists, we can compare transcribed notes and original and get usual ivan@155: % True Positives, False Positives and False Negatives measures. ivan@155: ivan@155: if ~isempty(SMALL.Problem.notesOriginal) ivan@155: AMT_res(1) = AMT_analysis(SMALL.Problem, SMALL.solver(1)); ivan@155: end ivan@155: ivan@155: ivan@155: ivan@155: %% ivan@155: % % Here we solve the same problem using non-negative sparse coding with ivan@155: % % SPAMS online dictionary learning (Julien Mairal 2009) ivan@155: % % ivan@155: % Initialising solver structure ivan@155: % Setting solver structure fields (toolbox, name, param, solution, ivan@155: % reconstructed and time) to zero values ivan@155: % As an example, SPAMS (Julien Mairal 2009) implementation of LARS ivan@155: % algorithm is used for representation of training set in the learned ivan@155: % dictionary. ivan@155: ivan@155: SMALL.solver(2)=SMALL_init_solver; ivan@155: ivan@155: % Defining the parameters needed for sparse representation ivan@155: ivan@155: SMALL.solver(2).toolbox='SPAMS'; ivan@155: SMALL.solver(2).name='mexLasso'; ivan@155: ivan@155: % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity ivan@155: % constrain (type 'help mexLasso' for more information about modes): ivan@155: % ivan@155: % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2 ivan@155: ivan@155: SMALL.solver(2).param=struct('lambda', 3, 'pos', 1, 'mode', 2); ivan@155: ivan@155: ivan@155: % You can also test ALPS, IST from MMbox or any other solver, but results ivan@155: % are not as good as SPAMS ivan@155: % ivan@155: % % Initialising solver structure ivan@155: % % Setting solver structure fields (toolbox, name, param, solution, ivan@155: % % reconstructed and time) to zero values ivan@155: % ivan@155: % SMALL.solver(2)=SMALL_init_solver; ivan@155: % ivan@155: % % Defining the parameters needed for image denoising ivan@155: % ivan@155: % SMALL.solver(2).toolbox='ALPS'; ivan@155: % SMALL.solver(2).name='AlebraicPursuit'; ivan@155: % ivan@155: % SMALL.solver(2).param=struct(... ivan@155: % 'sparsity', 10,... ivan@155: % 'memory', 1,... ivan@155: % 'mode', 6,... ivan@155: % 'iternum', 100,... ivan@155: % 'tau',-1,... ivan@155: % 'tolerance', 1e-14',... ivan@155: % 'verbose',1); ivan@155: ivan@155: % % Initialising Dictionary structure ivan@155: % % Setting Dictionary structure fields (toolbox, name, param, D and time) ivan@155: % % to zero values ivan@155: % % Initialising solver structure ivan@155: % % Setting solver structure fields (toolbox, name, param, solution, ivan@155: % % reconstructed and time) to zero values ivan@155: % ivan@155: % SMALL.solver(2)=SMALL_init_solver; ivan@155: % ivan@155: % % Defining the parameters needed for image denoising ivan@155: % ivan@155: % SMALL.solver(2).toolbox='MMbox'; ivan@155: % SMALL.solver(2).name='mm1'; ivan@155: % SMALL.solver(2).param=struct(... ivan@155: % 'lambda',50,... ivan@155: % 'iternum',1000,... ivan@155: % 'map',0); ivan@155: ivan@155: SMALL.DL(2)=SMALL_init_DL('MMbox', 'MM_cn', '', 1); ivan@155: ivan@155: ivan@155: % Defining the parameters for Majorization Minimization dictionary update ivan@155: % ivan@155: % In this example we are learning 88 atoms in 200 iterations, so that ivan@155: ivan@155: ivan@155: SMALL.DL(2).param=struct(... ivan@155: 'solver', SMALL.solver(2),... ivan@155: 'initdict', SMALL.Problem.A,... ivan@155: 'dictsize', SMALL.Problem.p,... ivan@155: 'iternum', 200,... ivan@155: 'iterDictUpdate', 1000,... ivan@155: 'epsDictUpdate', 1e-7,... ivan@155: 'cvset',0,... ivan@155: 'show_dict', 0); ivan@155: ivan@155: ivan@155: % Learn the dictionary ivan@155: ivan@155: SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2)); ivan@155: ivan@155: % Set SMALL.Problem.A dictionary and reconstruction function ivan@155: % (backward compatiblity with SPARCO: solver structure communicate ivan@155: % only with Problem structure, ie no direct communication between DL and ivan@155: % solver structures) ivan@155: ivan@155: SMALL.Problem.A = SMALL.DL(2).D; ivan@161: SMALL.Problem.reconstruct=@(x) AMT_reconstruct(x, SMALL.Problem); ivan@155: ivan@155: ivan@155: % Call SMALL_soolve to represent the signal in the given dictionary. ivan@155: % As a final command SMALL_solve will call above defined reconstruction ivan@155: % function to reconstruct the training set (Problem.b) in the learned ivan@155: % dictionary (Problem.A) ivan@155: ivan@155: SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2)); ivan@155: ivan@155: ivan@155: % Analysis of the result of automatic music transcription. If groundtruth ivan@155: % exists, we can compare transcribed notes and original and get usual ivan@155: % True Positives, False Positives and False Negatives measures. ivan@155: ivan@155: if ~isempty(SMALL.Problem.notesOriginal) ivan@155: AMT_res(2) = AMT_analysis(SMALL.Problem, SMALL.solver(2)); ivan@155: end ivan@155: ivan@155: ivan@155: % Plot results and save midi files ivan@155: ivan@155: if ~isempty(SMALL.Problem.notesOriginal) ivan@155: figAMT = SMALL_AMT_plot(SMALL, AMT_res); ivan@155: else ivan@155: figAMT = figure('Name', 'Automatic Music Transcription KSVD vs SPAMS'); ivan@155: subplot(2,1,1); plot(SMALL.solver(1).reconstructed.notes(:,5), SMALL.solver(1).reconstructed.notes(:,3), 'kd '); ivan@155: title (sprintf('%s dictionary in %.2f s', SMALL.DL(1).name, SMALL.DL(1).time)); ivan@155: xlabel('Time'); ivan@155: ylabel('Note Number'); ivan@155: subplot(2,1,2); plot(SMALL.solver(2).reconstructed.notes(:,5), SMALL.solver(2).reconstructed.notes(:,3), 'b* '); ivan@155: title (sprintf('%s dictionary in %.2f s', SMALL.DL(2).name, SMALL.DL(2).time)); ivan@155: xlabel('Time'); ivan@155: ylabel('Note Number'); ivan@155: end ivan@155: ivan@155: FS=filesep; ivan@155: ivan@155: [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); ivan@155: cd([pathstr1,FS,'results']); ivan@155: ivan@155: [filename,pathname] = uiputfile({' *.mid;' },'Save KSVD result midi'); ivan@155: if filename~=0 writemidi(SMALL.solver(1).reconstructed.midi, [pathname,FS,filename]);end ivan@155: ivan@155: [filename,pathname] = uiputfile({' *.mid;' },'Save SPAMS result midi'); ivan@155: if filename~=0 writemidi(SMALL.solver(2).reconstructed.midi, [pathname,FS,filename]);end ivan@155: ivan@155: [filename,pathname] = uiputfile({' *.fig;' },'Save KSVD vs SPAMS AMT figure'); ivan@155: if filename~=0 saveas(figAMT, [pathname,FS,filename]);end ivan@155: ivan@155: ivan@155: