ivan@128: %% Dictionary Learning for Automatic Music Transcription - SPAMS lambda ivan@128: %% test idamnjanovic@25: % ivan@128: % *WARNING!* You should have SPAMS in your search path in order for this ivan@128: % script to work.Due to licensing issues SPAMS can not be automatically ivan@128: % provided in SMALLbox (http://www.di.ens.fr/willow/SPAMS/downloads.html). ivan@128: % idamnjanovic@6: % This file contains an example of how SMALLbox can be used to test diferent idamnjanovic@6: % dictionary learning techniques in Automatic Music Transcription problem. idamnjanovic@6: % It calls generateAMT_Learning_Problem that will let you to choose midi, idamnjanovic@6: % wave or mat file to be transcribe. If file is midi it will be first idamnjanovic@6: % converted to wave and original midi file will be used for comparison with idamnjanovic@6: % results of dictionary learning and reconstruction. idamnjanovic@6: % The function will generarte the Problem structure that is used to learn idamnjanovic@6: % Problem.p notes spectrograms from training set Problem.b using idamnjanovic@6: % dictionary learning technique defined in DL structure. ivan@107: ivan@107: % ivan@107: % Centre for Digital Music, Queen Mary, University of London. ivan@107: % This file copyright 2010 Ivan Damnjanovic. ivan@107: % ivan@107: % This program is free software; you can redistribute it and/or ivan@107: % modify it under the terms of the GNU General Public License as ivan@107: % published by the Free Software Foundation; either version 2 of the ivan@107: % License, or (at your option) any later version. See the file ivan@107: % COPYING included with this distribution for more information. idamnjanovic@6: % idamnjanovic@6: %% idamnjanovic@6: idamnjanovic@6: clear; idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: % Defining Automatic Transcription of Piano tune as Dictionary Learning idamnjanovic@6: % Problem idamnjanovic@6: ivan@161: SMALL.Problem = generateAMTProblem(); idamnjanovic@6: TPmax=0; idamnjanovic@6: %% idamnjanovic@6: for i=1:10 idamnjanovic@6: %% idamnjanovic@6: % Solving AMT problem using non-negative sparse coding with idamnjanovic@6: % SPAMS online dictionary learning (Julien Mairal 2009) idamnjanovic@6: % idamnjanovic@6: idamnjanovic@6: % Initialising Dictionary structure idamnjanovic@6: % Setting Dictionary structure fields (toolbox, name, param, D and time) idamnjanovic@6: % to zero values idamnjanovic@6: idamnjanovic@6: SMALL.DL(i)=SMALL_init_DL(); idamnjanovic@6: idamnjanovic@6: % Defining fields needed for dictionary learning idamnjanovic@6: idamnjanovic@6: SMALL.DL(i).toolbox = 'SPAMS'; idamnjanovic@6: SMALL.DL(i).name = 'mexTrainDL'; idamnjanovic@6: idamnjanovic@6: % We test SPAMS for ten different values of parameter lambda idamnjanovic@6: % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters. idamnjanovic@6: idamnjanovic@6: lambda(i)=1.4+0.2*i; idamnjanovic@6: idamnjanovic@6: SMALL.DL(i).param=struct(... idamnjanovic@6: 'K', SMALL.Problem.p,... idamnjanovic@6: 'lambda', lambda(i),... idamnjanovic@6: 'iter', 300,... idamnjanovic@6: 'posAlpha', 1,... idamnjanovic@6: 'posD', 1,... idamnjanovic@6: 'whiten', 0,... idamnjanovic@6: 'mode', 2); idamnjanovic@6: idamnjanovic@6: % Learn the dictionary idamnjanovic@6: idamnjanovic@6: SMALL.DL(i) = SMALL_learn(SMALL.Problem, SMALL.DL(i)); idamnjanovic@6: idamnjanovic@6: % Set SMALL.Problem.A dictionary and reconstruction function idamnjanovic@6: % (backward compatiblity with SPARCO: solver structure communicate idamnjanovic@6: % only with Problem structure, ie no direct communication between DL and idamnjanovic@6: % solver structures) idamnjanovic@6: idamnjanovic@6: SMALL.Problem.A = SMALL.DL(i).D; ivan@161: SMALL.Problem.reconstruct=@(x) AMT_reconstruct(x, SMALL.Problem); idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: %% idamnjanovic@6: % Initialising solver structure idamnjanovic@6: % Setting solver structure fields (toolbox, name, param, solution, idamnjanovic@6: % reconstructed and time) to zero values idamnjanovic@6: % As an example, SPAMS (Julien Mairal 2009) implementation of LARS idamnjanovic@6: % algorithm is used for representation of training set in the learned idamnjanovic@6: % dictionary. idamnjanovic@6: idamnjanovic@6: SMALL.solver(1)=SMALL_init_solver; idamnjanovic@6: idamnjanovic@6: % Defining the parameters needed for sparse representation idamnjanovic@6: idamnjanovic@6: SMALL.solver(1).toolbox='SPAMS'; idamnjanovic@6: SMALL.solver(1).name='mexLasso'; idamnjanovic@6: idamnjanovic@6: % Here we use mexLasso mode=2, with lambda=3, lambda2=0 and positivity idamnjanovic@6: % constrain (type 'help mexLasso' for more information about modes): idamnjanovic@6: % idamnjanovic@6: % min_{alpha_i} (1/2)||x_i-Dalpha_i||_2^2 + lambda||alpha_i||_1 + (1/2)lambda2||alpha_i||_2^2 idamnjanovic@6: idamnjanovic@6: SMALL.solver(1).param=struct(... idamnjanovic@6: 'lambda', 3,... idamnjanovic@6: 'pos', 1,... idamnjanovic@6: 'mode', 2); idamnjanovic@6: idamnjanovic@6: % Call SMALL_soolve to represent the signal in the given dictionary. idamnjanovic@6: % As a final command SMALL_solve will call above defined reconstruction idamnjanovic@6: % function to reconstruct the training set (Problem.b) in the learned idamnjanovic@6: % dictionary (Problem.A) idamnjanovic@6: idamnjanovic@6: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); idamnjanovic@6: idamnjanovic@6: %% idamnjanovic@6: % Analysis of the result of automatic music transcription. If groundtruth idamnjanovic@6: % exists, we can compare transcribed notes and original and get usual idamnjanovic@6: % True Positives, False Positives and False Negatives measures. idamnjanovic@6: idamnjanovic@6: AMT_res(i) = AMT_analysis(SMALL.Problem, SMALL.solver(1)); idamnjanovic@6: if AMT_res(i).TP>TPmax idamnjanovic@6: TPmax=AMT_res(i).TP; idamnjanovic@6: BLmidi=SMALL.solver(1).reconstructed.midi; idamnjanovic@6: writemidi(SMALL.solver(1).reconstructed.midi, ['testL',i,'.mid']); idamnjanovic@6: max=i; idamnjanovic@6: end idamnjanovic@6: end %end of for loop idamnjanovic@6: %% idamnjanovic@6: % Plot results and save midi files idamnjanovic@6: idamnjanovic@6: figAMTbest=SMALL_AMT_plot(SMALL, AMT_res(max)); idamnjanovic@6: idamnjanovic@6: resFig=figure('Name', 'Automatic Music Transcription SPAMS lambda TEST'); idamnjanovic@6: idamnjanovic@6: subplot (3,1,1); plot(lambda(:), [AMT_res(:).TP], 'ro-'); idamnjanovic@6: title('True Positives vs lambda'); idamnjanovic@6: idamnjanovic@6: subplot (3,1,2); plot(lambda(:), [AMT_res(:).FN], 'ro-'); idamnjanovic@6: title('False Negatives vs lambda'); idamnjanovic@6: idamnjanovic@6: subplot (3,1,3); plot(lambda(:), [AMT_res(:).FP], 'ro-'); idamnjanovic@6: title('False Positives vs lambda'); idamnjanovic@6: idamnjanovic@6: FS=filesep; idamnjanovic@6: [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); idamnjanovic@6: cd([pathstr1,FS,'results']); idamnjanovic@6: [filename,pathname] = uiputfile({' *.mid;' },'Save midi'); idamnjanovic@6: if filename~=0 writemidi(BLmidi, [pathname,FS,filename]);end idamnjanovic@6: [filename,pathname] = uiputfile({' *.fig;' },'Save figure TP/FN/FP vs lambda'); idamnjanovic@6: if filename~=0 saveas(resFig, [pathname,FS,filename]);end idamnjanovic@6: idamnjanovic@6: [filename,pathname] = uiputfile({' *.fig;' },'Save BEST AMT figure'); idamnjanovic@6: if filename~=0 saveas(figAMTbest, [pathname,FS,filename]);end idamnjanovic@6: idamnjanovic@6: