/** * @file testImageNetBinary.cpp * @brief perform ImageNet tests with binary classification * @author Erik Rodner * @date 01/04/2012 */ #include #ifdef NICE_USELIB_MATIO #include //---------- #include #include #include //---------- #include #include #include #include #include using namespace std; using namespace NICE; using namespace OBJREC; /** test the basic functionality of fast-hik hyperparameter optimization */ int main (int argc, char **argv) { std::set_terminate(__gnu_cxx::__verbose_terminate_handler); Config conf ( argc, argv ); string resultsfile = conf.gS("main", "results", "results.txt" ); int positiveClass = conf.gI("main", "positive_class"); cerr << "Positive class is " << positiveClass << endl; conf.sD( "FMKGPHyperparameterOptimization", "parameter_upper_bound", 5.0 ); conf.sD( "FMKGPHyperparameterOptimization", "parameter_lower_bound", 1.0 ); std::string pf_s = conf.gS("main", "transform", "absexp"); if ( pf_s == "absexp" ) conf.sS( "FMKGPHyperparameterOptimization", "transform", "absexp" ); else if ( pf_s == "exp" ) conf.sS( "FMKGPHyperparameterOptimization", "transform", "exp" ); else if ( pf_s == "weighted" ) { conf.sS( "FMKGPHyperparameterOptimization", "transform", "weightedDim" ); conf.sI( "FMKGPHyperparameterOptimization", "pf_dim", conf.gI("main", "dimension") ); } else fthrow(Exception, "Parameterized function type " << pf_s << " not yet implemented"); std::cerr << "Transformation type: " << pf_s << std::endl; double noise = conf.gD("GPHIKClassifier", "noise", 0.1); FMKGPHyperparameterOptimization hypopt ( &conf ); sparse_t data; NICE::Vector y; cerr << "Reading ImageNet data ..." << endl; bool imageNetLocal = conf.gB("main", "imageNetLocal" , false); string imageNetPath; if (imageNetLocal) imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/"; else imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/"; ImageNetData imageNet ( imageNetPath + "demo/" ); imageNet.getBatchData ( data, y, "train", "training" ); uint n = y.size(); cerr << "Performing hyperparameter optimization ... " << endl; set positives; set negatives; map< int, set > mysets; for ( uint i = 0 ; i < n; i++ ) mysets[ y[i] ].insert ( i ); if ( mysets[ positiveClass ].size() == 0 ) fthrow(Exception, "Class " << positiveClass << " is not available."); // add our positive examples for ( set::const_iterator i = mysets[positiveClass].begin(); i != mysets[positiveClass].end(); i++ ) positives.insert ( *i ); int Nneg = conf.gI("main", "nneg", 1 ); for ( map >::const_iterator k = mysets.begin(); k != mysets.end(); k++ ) { int classno = k->first; if ( classno == positiveClass ) continue; const set & s = k->second; uint ind = 0; for ( set::const_iterator i = s.begin(); (i != s.end() && ind < Nneg); i++,ind++ ) negatives.insert ( *i ); } cerr << "Number of positive examples: " << positives.size() << endl; cerr << "Number of negative examples: " << negatives.size() << endl; std::cerr << "hypopt.optimize( data, y, positives, negatives ) " << std::endl; hypopt.optimizeBinary ( data, y, positives, negatives, noise ); // ------------------------------ TESTING ------------------------------ cerr << "Reading ImageNet test data files (takes some seconds)..." << endl; imageNet.preloadData ( "val", "testing" ); imageNet.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" ); ClassificationResults results; cerr << "Classification step ... with " << imageNet.getNumPreloadedExamples() << " examples" << endl; ProgressBar pb; for ( uint i = 0 ; i < (uint)imageNet.getNumPreloadedExamples(); i++ ) { pb.update ( imageNet.getNumPreloadedExamples() ); const SparseVector & svec = imageNet.getPreloadedExample ( i ); SparseVector scores; // classification step int classno = hypopt.classify ( svec, scores ); // building the result ClassificationResult r ( classno, scores ); // set ground truth label r.classno_groundtruth = (((int)imageNet.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0; results.push_back ( r ); } cerr << "Writing results to " << resultsfile << endl; results.writeWEKA ( resultsfile, 0 ); double perfvalue = results.getBinaryClassPerformance( ClassificationResults::PERF_AUC ); cerr << "Performance: " << perfvalue << endl; return 0; } #else int main (int argc, char **argv) { std::cerr << "MatIO library is missing in your system - this program will have no effect. " << std::endl; } #endif