annotate Code/Classifiers/SVM_Singing.m @ 4:92ca03a8fa99 tip

Update to ICASSP 2013 benchmark
author Dawn Black
date Wed, 13 Feb 2013 11:02:39 +0000
parents
children
rev   line source
Dawn@4 1 function [] = SVM_Singing( inputFileName )
Dawn@4 2
Dawn@4 3 cd 'C:\Users\dawn\Dropbox\TestResults'
Dawn@4 4
Dawn@4 5 DEBUG = 1;
Dawn@4 6 % output results file name
Dawn@4 7 % masterFileOutputID = fopen( 'SVM_Singing_HNR.txt', 'a' );
Dawn@4 8 outputFileName = ['SVM_' inputFileName];
Dawn@4 9 masterFileOutputID = fopen( outputFileName, 'a' );
Dawn@4 10
Dawn@4 11 % input results file name
Dawn@4 12 % inputFileName = 'singingBasicHNRStats.txt';
Dawn@4 13
Dawn@4 14 fprintf( masterFileOutputID, '\n RESULTS FILE NAME: %s\n', inputFileName);
Dawn@4 15 inputFileID = fopen( inputFileName );
Dawn@4 16
Dawn@4 17 % noOfArguments = length(varargin);
Dawn@4 18
Dawn@4 19 % outputFileName = 'individualResults/SVM_Results_';
Dawn@4 20 % resultsFileName = 'SVM_Results_';
Dawn@4 21 titleName = '';
Dawn@4 22 % for i=1 : noOfArguments
Dawn@4 23 % titleName = [ titleName varargin{i} '_'];
Dawn@4 24 % fprintf( masterFileOutputID, '%s_', varargin{i} );
Dawn@4 25 % end
Dawn@4 26
Dawn@4 27 % outputFileName = [ outputFileName titleName ];
Dawn@4 28 % resultsFileName = [ resultsFileName titleName ];
Dawn@4 29
Dawn@4 30 fprintf( masterFileOutputID, '\t' );
Dawn@4 31
Dawn@4 32 % outputFileName = [ outputFileName '.txt'];
Dawn@4 33 % resultsFileName = [ resultsFileName '.txt'];
Dawn@4 34 %
Dawn@4 35 % fileOutputID = fopen( outputFileName, 'w' );
Dawn@4 36 % fileSVMOutputID = fopen( resultsFileName, 'w' );
Dawn@4 37
Dawn@4 38 % -------------------- get the data from the results file ---------------
Dawn@4 39 lineCount = 0;
Dawn@4 40 fileCount = 0;
Dawn@4 41 data = [];
Dawn@4 42 groups = [];
Dawn@4 43
Dawn@4 44 while( ~(feof(inputFileID)) )
Dawn@4 45
Dawn@4 46 outputValues = [];
Dawn@4 47 thestr = fgetl(inputFileID);
Dawn@4 48 fileCount = fileCount + 1;
Dawn@4 49
Dawn@4 50 % determine whether we have a positive or negative sample
Dawn@4 51 sampleEmotion( fileCount ) = 'U';
Dawn@4 52 if( ~(isempty(strfind(thestr,'pos'))))
Dawn@4 53 % sample is positive
Dawn@4 54 sampleEmotion( fileCount ) = 'P';
Dawn@4 55 groups( fileCount ) = 1;
Dawn@4 56 elseif( ~(isempty(strfind(thestr,'neg'))))
Dawn@4 57 % sample is negative
Dawn@4 58 sampleEmotion( fileCount ) = 'N';
Dawn@4 59 groups( fileCount ) = 0;
Dawn@4 60 else
Dawn@4 61 disp('EEEK!');
Dawn@4 62 fileCount = fileCount - 1;
Dawn@4 63 % pause;
Dawn@4 64 end
Dawn@4 65
Dawn@4 66 % determine whether we have a male, female or trans sample
Dawn@4 67 % gender( fileCount ) = '?';
Dawn@4 68 % if( ~(isempty(strfind(thestr,'fem'))))
Dawn@4 69 % % gender is female
Dawn@4 70 % gender( fileCount ) = 'F';
Dawn@4 71 % elseif( ~(isempty(strfind(thestr,'male'))))
Dawn@4 72 % % gender is male
Dawn@4 73 % gender( fileCount ) = 'M';
Dawn@4 74 % elseif( ~(isempty(strfind(thestr,'trans'))))
Dawn@4 75 % % gender is trans
Dawn@4 76 % gender( fileCount ) = 'T';
Dawn@4 77 % else
Dawn@4 78 % disp('EEEK!');
Dawn@4 79 % pause;
Dawn@4 80 % end
Dawn@4 81
Dawn@4 82 if(( ~(isempty(strfind(thestr,'pos')))) || ( ~(isempty(strfind(thestr,'neg')))) )
Dawn@4 83 %how many values are in the string?
Dawn@4 84 % spaces = strfind( thestr, ' ' );
Dawn@4 85 spaces = [ strfind( thestr, sprintf('\t')) strfind( thestr, ' ' )];
Dawn@4 86 numberstr = thestr( spaces(1) : end ); % chop off the file name
Dawn@4 87 vars = sscanf( numberstr, '%f', inf );
Dawn@4 88 data( fileCount, : ) = vars;
Dawn@4 89 end
Dawn@4 90
Dawn@4 91 lineCount = lineCount + 1;
Dawn@4 92
Dawn@4 93 end
Dawn@4 94 fclose(inputFileID);
Dawn@4 95
Dawn@4 96 % ------------ apply the SVM classifier ------------------------
Dawn@4 97
Dawn@4 98 resultMatrix = [];
Dawn@4 99
Dawn@4 100 noOfIterations = 100;
Dawn@4 101
Dawn@4 102 for n = 1:noOfIterations
Dawn@4 103 % Randomly select training and test sets, perhaps we should try all and
Dawn@4 104 % choose the best?
Dawn@4 105 [train, test] = crossvalind('holdOut',groups);
Dawn@4 106 cp = classperf(groups);
Dawn@4 107
Dawn@4 108 % Use a linear support vector machine classifier
Dawn@4 109 svmStruct = svmtrain(data(train,:),groups(train));
Dawn@4 110 classes = svmclassify(svmStruct,data(test,:));
Dawn@4 111 % See how well the classifier performed
Dawn@4 112 classperf(cp,classes,test);
Dawn@4 113 numbers = cp.CountingMatrix;
Dawn@4 114
Dawn@4 115 resultMatrix (n,:,:) = cp.DiagnosticTable;
Dawn@4 116 %
Dawn@4 117 end
Dawn@4 118
Dawn@4 119
Dawn@4 120
Dawn@4 121
Dawn@4 122 % for emotion detection give the confusion matrix as
Dawn@4 123 % -----------------------------------------------------------------
Dawn@4 124 % positive correctly identified | positive incorrectly identified (1,2)
Dawn@4 125 % negative incorrectly identified (2,1) | negative correctly identified
Dawn@4 126 % ------------------------------------------------------------------
Dawn@4 127
Dawn@4 128 % takes the average of 100 iterations - do we want to take the best?
Dawn@4 129
Dawn@4 130 meanResults(1,1) = mean( resultMatrix(:,1,1) );
Dawn@4 131 meanResults(1,2) = mean( resultMatrix(:,2,1) );
Dawn@4 132 meanResults(2,1) = mean( resultMatrix(:,1,2) );
Dawn@4 133 meanResults(2,2) = mean( resultMatrix(:,2,2) );
Dawn@4 134
Dawn@4 135 meanResults(3,:)=0;
Dawn@4 136 meanResults(:,3)=0;
Dawn@4 137
Dawn@4 138 meanResults(3,3) = (meanResults(1,1) + meanResults(2,2));% / sum(sum(meanResults));
Dawn@4 139
Dawn@4 140 % convert to percentages
Dawn@4 141 % how many of each sample do we have?
Dawn@4 142 groupNumbers = unique( groups( test ));
Dawn@4 143 groupNames = unique( sampleEmotion( test ));
Dawn@4 144 sampleEmotionTest = sampleEmotion( test );
Dawn@4 145 % which group is which emotion?
Dawn@4 146 thisGroupNumber = groupNumbers(1);
Dawn@4 147 thisGroup = find( groups( test ) == thisGroupNumber );
Dawn@4 148 thisGroupName = unique( sampleEmotionTest( thisGroup ));
Dawn@4 149
Dawn@4 150 thatGroupNumber = groupNumbers(2);
Dawn@4 151 thatGroup = find( groups( test ) == thatGroupNumber );
Dawn@4 152 thatGroupName = unique( sampleEmotionTest( thatGroup ));
Dawn@4 153
Dawn@4 154 if(length( thisGroupName ) ~= 1 )
Dawn@4 155 disp('ARGH!');
Dawn@4 156 pause;
Dawn@4 157 end
Dawn@4 158
Dawn@4 159 thisGroupNumberOfSamples = length( thisGroup );
Dawn@4 160 thatGroupNumberOfSamples = length( thatGroup );
Dawn@4 161
Dawn@4 162 if( thisGroupName == 'P' )
Dawn@4 163 %swap all the variables ready for checking
Dawn@4 164 temp = thisGroupNumberOfSamples;
Dawn@4 165 thisGroupNumberOfSamples = thatGroupNumberOfSamples;
Dawn@4 166 thatGroupNumberOfSamples = temp;
Dawn@4 167
Dawn@4 168 temp = thisGroupName;
Dawn@4 169 thisGroupName = thatGroupName
Dawn@4 170 thatGroupName = temp;
Dawn@4 171 disp('CHECK ME!');
Dawn@4 172 end
Dawn@4 173
Dawn@4 174 if( thisGroupName == 'N' )
Dawn@4 175 % group 0 is negative
Dawn@4 176 if( sum( meanResults(1,:) ) == thisGroupNumberOfSamples )
Dawn@4 177 %if the elements in the first row add up to the number of negative
Dawn@4 178 %samples, then swap the rows because we want the top row to be the
Dawn@4 179 %results for the positive samples
Dawn@4 180 temp(:,1) = meanResults(1:2,2);
Dawn@4 181 temp(:,2) = meanResults(1:2,1);
Dawn@4 182 temp2(1,:) = temp(2,:);
Dawn@4 183 temp2(2,:) = temp(1,:);
Dawn@4 184
Dawn@4 185 meanResults(1:2,1) = temp2(:,1);
Dawn@4 186 meanResults(1:2,2) = temp2(:,2);
Dawn@4 187
Dawn@4 188 % check the number of positive samples
Dawn@4 189 if(( sum( meanResults(1,:) ) == thatGroupNumberOfSamples ) ...
Dawn@4 190 && ( thatGroupName == 'P' ) )
Dawn@4 191 % row 1 is positive
Dawn@4 192 disp('matrix correct');
Dawn@4 193 else
Dawn@4 194 disp('ARGH!');
Dawn@4 195 pause;
Dawn@4 196 end
Dawn@4 197
Dawn@4 198 elseif( sum( meanResults(2,:) ) == thisGroupNumberOfSamples )
Dawn@4 199
Dawn@4 200 % the elements in the second row add up to the number of negative
Dawn@4 201 % samples, so the matrix is the correct way around
Dawn@4 202
Dawn@4 203 % check the number of positive samples
Dawn@4 204 if(( sum( meanResults(1,:) ) == thatGroupNumberOfSamples ) ...
Dawn@4 205 && ( thatGroupName == 'P' ) )
Dawn@4 206 % row 0 is positive
Dawn@4 207 disp('matrix correct');
Dawn@4 208 else
Dawn@4 209 disp('ARGH!');
Dawn@4 210 pause;
Dawn@4 211 end
Dawn@4 212 end
Dawn@4 213 end
Dawn@4 214
Dawn@4 215 % calculate the percentages
Dawn@4 216 numberOfSamples = sum(sum( meanResults(1:2,1:2)));
Dawn@4 217 percentageResults = meanResults;
Dawn@4 218 percentageResults(1,1) = meanResults(1,1) / numberOfSamples;
Dawn@4 219 percentageResults(1,2) = meanResults(1,2) / numberOfSamples;
Dawn@4 220 percentageResults(2,1) = meanResults(2,1) / numberOfSamples;
Dawn@4 221 percentageResults(2,2) = meanResults(2,2) / numberOfSamples;
Dawn@4 222 percentageResults(3,3) = meanResults(3,3) / numberOfSamples;
Dawn@4 223
Dawn@4 224 percentageResults = percentageResults * 100
Dawn@4 225
Dawn@4 226 confusionMatrix = percentageResults;
Dawn@4 227 fprintf( masterFileOutputID, '\n %f \t %f \n %f \t %f \n %f \t %f \t %f \n', confusionMatrix(1,1), confusionMatrix(1,2), confusionMatrix(2,1), confusionMatrix(2,2), 0, 0, confusionMatrix(3,3));
Dawn@4 228
Dawn@4 229 % print latex results to the screen
Dawn@4 230 str1 = sprintf(' & %2.2f & %2.2f & \\\\', percentageResults(1,1), percentageResults(1,2) );
Dawn@4 231 disp(str1);
Dawn@4 232 str1 = sprintf(' & %2.2f & %2.2f & \\\\', percentageResults(2,1), percentageResults(2,2) );
Dawn@4 233 disp(str1);
Dawn@4 234 str1 = sprintf(' & & & %2.2f \\\\',percentageResults(3,3) );
Dawn@4 235 disp(str1);
Dawn@4 236
Dawn@4 237 fprintf( masterFileOutputID, '\n' );
Dawn@4 238 fclose( masterFileOutputID );
Dawn@4 239
Dawn@4 240 end
Dawn@4 241
Dawn@4 242