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