123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /**
- * @file testImageNetBinary.cpp
- * @brief perform ImageNet tests with binary classification
- * @author Erik Rodner
- * @date 01/04/2012
- */
- #include <core/basics/Config.h>
- #ifdef NICE_USELIB_MATIO
- #include <core/matlabAccess/MatFileIO.h>
- //----------
- #include <vislearning/cbaselib/ClassificationResults.h>
- #include <vislearning/baselib/ProgressBar.h>
- #include <vislearning/matlabAccessHighLevel/ImageNetData.h>
- //----------
- #include <gp-hik-core/FMKGPHyperparameterOptimization.h>
- #include <gp-hik-core/parameterizedFunctions/PFAbsExp.h>
- #include <gp-hik-core/parameterizedFunctions/PFExp.h>
- #include <gp-hik-core/parameterizedFunctions/PFWeightedDim.h>
- #include <gp-hik-core/tools.h>
- 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/datasets/ilsvrc2010/devkit-1.0/"; // /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<int> positives;
- set<int> negatives;
- map< int, set<int> > 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<int>::const_iterator i = mysets[positiveClass].begin(); i != mysets[positiveClass].end(); i++ )
- positives.insert ( *i );
- int Nneg = conf.gI("main", "nneg", 1 );
- for ( map<int, set<int> >::const_iterator k = mysets.begin(); k != mysets.end(); k++ )
- {
- int classno = k->first;
- if ( classno == positiveClass )
- continue;
- const set<int> & s = k->second;
- uint ind = 0;
- for ( set<int>::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
|