#include #include // for cout #include //#include // For auto_ptr #include // For greater and bind2nd #include "RutgersTest.h" #include "rimage.h" #include "rimage_ser.h" #include "mp_SVMWeights.h" #include "mpt_RutgersIterator.h" #include "mpt_SimpleStats.h" extern "C" { #include "perf_roc.h" } using namespace std; unsigned int indexForAU( int AU ) { switch( AU ) { case 1: return 0; break; case 2: return 1; break; case 4: return 2; break; case 5: return 3; break; case 9: return 4; break; case 10: return 5; break; case 12: return 6; break; case 14: return 7; break; case 15: return 8; break; case 17: return 9; break; case 20: return 10; break; } return 99; } CPPUNIT_TEST_SUITE_REGISTRATION( RutgersTest ); RutgersTest::RutgersTest() : cert( true ) { // std::string fdDir( "./featuredetector/frozenData/" ); // cert.setDoEmotions( true ); // cert.loadFeatureDetector( fdDir ); // may throw a std::string error // cert.setUseFeatures( true ); CERT::Config *config = NULL; try { config = new CERT::Config( "./RutgersTest.cfg" ); } catch( ... ) { CPPUNIT_FAIL( "Error loading config from ./RutgersTest.cfg" ); } // Now set up the plugin controller with the config object // pc = CERT::PluginController::getPluginController(); pc = &cert.pluginController(); pc->loadPluginsFromConfig( config ); std::vector svmPlugins = pc->pluginsForStep( CERT::PluginSteps::SVM ); CPPUNIT_ASSERT_MESSAGE( "RutgersTest.cfg should only have one enabled SVM plugin", (1 == svmPlugins.size()) ); plugin = dynamic_cast(svmPlugins[0]); CPPUNIT_ASSERT( plugin && plugin->isEnabled() ); // cert.setUseFeatures( true ); cert.loadPlugins(); initTestData(); #ifdef xDEBUG printTestData(); #endif } /** Parse the rutgersTest.txt file. * Build a map with AU as key and a vector of data objects as the value. * Each test function will get the vector for its AU, then would run each data object in the vector. */ void RutgersTest::initTestData() { mpt_RutgersData data; mpt_RutgersIterator iter( "rutgersTest.txt" ); while( iter.nextData( data ) ) { std::vector &vec = auTestData[ data.getAU() ]; vec.push_back(data); auTestData[ data.getAU() ] = vec; } } /** Walk through the test data map and print it out. */ void RutgersTest::printTestData() { std::map >::iterator pos; std::cout << "AUTestData:" << std::endl; for( pos = auTestData.begin(); pos != auTestData.end(); ++pos) { std::vector vec = pos->second; std::cout << "AU " << pos->first << ":" << std::endl; for( std::vector::iterator iter = vec.begin(); iter != vec.end(); ++iter ) { mpt_RutgersData data = *iter; std::cout << " Subject: " << data.getSubject() << std::endl; // print out the present/not-present ranges? } } std::cout << std::endl; } void RutgersTest::setUp() { // Make sure the SVMWeights are set up correctly mp_SVMWeights *svm = mp_SVMWeights::getSVMWeights(); svm->setDirectory( "SVMWeights_dir" ); } void RutgersTest::tearDown() { } void RutgersTest::testAU1() { testAU( 1 ); } void RutgersTest::testAU2() { testAU( 2 ); } void RutgersTest::testAU4() { testAU( 4 ); } void RutgersTest::testAU5() { testAU( 5 ); } void RutgersTest::testAU10() { testAU( 10 ); } void RutgersTest::testAU12() { testAU( 12 ); } void RutgersTest::testAU14() { testAU( 14 ); } void RutgersTest::testAU20() { testAU( 20 ); } void RutgersTest::testAU( unsigned int au ) { bool passedAllSubjects = true; std::string errorString; std::vector vec = auTestData[ au ]; CPPUNIT_ASSERT_MESSAGE( "There should be at least two different subjects for each AU", (vec.size() >= 2) ); cout << endl; for( std::vector::iterator iter = vec.begin(); iter != vec.end(); ++iter ) { double dPrime = -1.0; double AUR = -1.0; vector auPresent; vector auNotPresent; mpt_RutgersData data = *iter; calcAUsForData( data, auPresent, auNotPresent ); if( !dprimeForData( auPresent, auNotPresent, dPrime ) ) { CPPUNIT_FAIL( "Unable to calculate D-prime" ); } if( !areaUnderROC( auPresent, auNotPresent, AUR ) ) { CPPUNIT_FAIL( "Unable to calculate area under ROC curve" ); } double AURErr = AROCerr( AUR, auPresent.size(), auNotPresent.size() ); cout << " : Subject: " << data.getSubject() << " DPrime: " << dPrime; cout << " AUROC: " << AUR << " AROCerr: " << AURErr; cout << " Present: " << auPresent.size() << " Not Present: " << auNotPresent.size() << endl; std::stringstream errorStream; errorStream << "DPrime (" << dPrime << ") <= 1.0 for Subject " << data.getSubject(); #ifdef DEBUG cout << errorStream.str() << endl; #else if( dPrime <= 1.0 ) { passedAllSubjects = false; errorString += errorStream.str(); errorString += "\n "; } // CPPUNIT_ASSERT_MESSAGE( errorStream.str(), dPrime > 1.0 ); #endif errorStream.str( "" ); errorStream << "Area under the ROC (" << AUR << ") <= 0.5 for Subject " << data.getSubject(); #ifdef DEBUG cout << errorStream.str() << endl; #else if( AUR <= 0.5 ) { passedAllSubjects = false; errorString += errorStream.str(); errorString += "\n "; } // CPPUNIT_ASSERT_MESSAGE( errorStream.str(), AUR > 0.5 ); #endif } CPPUNIT_ASSERT_MESSAGE( errorString, passedAllSubjects ); } static const string imagedir( "RutgersImages/" ); bool RutgersTest::calcAUs( mpt_RutgersData &data, unsigned int frame, double &auValue, bool present ) { string filename( data.filenameForFrame( frame ) ); #ifdef WRITE_FACES string basename( filename ); #endif filename = imagedir + filename; RImage image; if( restoreRImageFromFile( image, filename ) ) { cert.calcAUs( image ); if( cert.didFindFace() ) { std::vector res = plugin->getResults(); // cert.resultsForCategory( plugin->getInternalName() ); auValue = res[ indexForAU( data.getAU() ) ]; #ifdef DEBUG cout << filename; if( present ) cout << " AU"; else cout << " No AU"; cout << data.getAU() << " " << auValue << endl; // cout << " "; // for( std::map::iterator iter=aus.begin(); iter != aus.end(); ++iter ) // cout << " " << iter->second; // cout << endl; #ifdef WRITE_FACES RImage tmpFace = cert.getPatch(); string facepath( "RutgersFaces/" ); facepath += basename; saveRImageToFile( tmpFace, facepath ); #endif WRITE_FACES #endif return true; } else { if( present ) cout << "Subject/AU/Frame "; else cout << "Subject/No AU/Frame "; cout << data.getSubject() << "/" << data.getAU() << "/" << frame << ". "; cout << "Failed to find a face." << endl; } } else { cout << "Unable to read file: " << filename << endl; } return false; } void RutgersTest::calcAUsForData( mpt_RutgersData &data, vector &auPresent, vector &auNotPresent ) { for( mpt_frames::iterator iter=data.beginPresent(); iter != data.endPresent(); ++iter ) { double auValue; unsigned int frame = *iter; if( calcAUs( data, frame, auValue, true ) ) auPresent.push_back( auValue ); } for( mpt_frames::iterator iter=data.beginNotPresent(); iter != data.endNotPresent(); ++iter ) { double auValue; unsigned int frame = *iter; if( calcAUs( data, frame, auValue, false ) ) auNotPresent.push_back( auValue ); } } bool RutgersTest::dprimeForData( vector &auPresent, vector &auNotPresent, double &dPrime ) { try { double meanPresent = computeMean( auPresent.begin(), auPresent.end() ); double stdevPresent = computeStdDev( auPresent.begin(), auPresent.end(), meanPresent ); double meanAbsent = computeMean( auNotPresent.begin(), auNotPresent.end() ); double stdevAbsent = computeStdDev( auNotPresent.begin(), auNotPresent.end(), meanAbsent ); // d-prime = (mean(AU-present) - mean(AU-absent)) / (0.5*(stdev(AU-present) + stdev(AU-absent))) dPrime = (meanPresent - meanAbsent) / ( 0.5 * (stdevPresent + stdevAbsent) ); #ifdef DEBUG cout << " Mean/Stdev Present: " << meanPresent << "/" << stdevPresent; cout << " Mean/Stdev Not Present: " << meanAbsent << "/" << stdevAbsent << endl << endl; #endif return true; } catch( ... ) { // cout << "Unable to calculate D-prime." << endl; } return false; } /* Oh I see. That's not a bad idea but the problem is that Dprime is based on a Gaussian model of sensitivity, which is unlikely to be correct in this case. This is what you need to do: 1) get the histograms of the outputs given positive and given negative examples. 2) Get the overall min and max output values. 3) For x = min; x <= max; x + dx HitRate(x) = proportion of positive examples above x; FalseAlarmRate(x) = proportion of negative examples above x; 4) Plot the (HitRate (x), False AlarmRate(x)) pairs. This gives you the ROC curve. 5) Use simple numerical integration to get the area under the ROC. It'd be interesting to compare the empirical ROC with the one predicted by the Gaussian model. */ bool RutgersTest::areaUnderROC( vector &auPresent, vector &auNotPresent, double &dPrime ) { unsigned int count = auPresent.size() + auNotPresent.size(); float actual[count]; float pred[count]; unsigned int index = 0; for( std::vector::iterator iter = auPresent.begin(); iter != auPresent.end(); ++iter ) { actual[index] = 1; pred[index] = *iter; ++index; } for( std::vector::iterator iter = auNotPresent.begin(); iter != auNotPresent.end(); ++iter ) { actual[index] = 0; pred[index] = *iter; ++index; } int err = loadDataFromArrays( actual, pred, count ); if( err ) return false; start_computations(); dPrime = calc_roca(); return true; } /** Calculating the standard error of an Area under the ROC, From Marni. function SE = AROCerr(A,nt,nn) %A = Area under the ROC %nt = number of targets %nn = number of nontargets Q1 = A/(2-A); Q2 = 2*A*A / (1+A); SE = sqrt( A*(1+A) + (nt-1)*(Q1-A*A) + (nn-1)*(Q2-A*A) / nt*nn); */ double RutgersTest::AROCerr( double AUR, size_t numPresent, size_t numNotPresent ) { double Q1 = AUR/(2-AUR); double Q2 = (2.0*AUR*AUR) / (1.0+AUR); double SE = sqrt( ( AUR*(1.0+AUR) + (static_cast(numPresent)-1.0)*(Q1-AUR*AUR) + (static_cast(numNotPresent)-1.0)*(Q2-AUR*AUR) ) / static_cast(numPresent*numNotPresent) ); return SE; } /* * Copyright (c) 2006 Machine Perception Laboratory * University of California San Diego. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */