evaluateCompleteBoWPipeline.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**
  2. * @file evaluateCompleteBoWPipeline.cpp
  3. * @brief A complete BoW pipeline: feature extraction, codebook creation, vector quantization, classifier training, evaluation on separate test set
  4. * @author Alexander Freytag
  5. * @date 10-05-2013
  6. */
  7. //STL
  8. #include <iostream>
  9. #include <limits>
  10. //core -- basic stuff
  11. #include <core/basics/Config.h>
  12. #include <core/basics/ResourceStatistics.h>
  13. #include <core/basics/Timer.h>
  14. #include <core/image/Convert.h>
  15. #include <core/vector/VectorT.h>
  16. //vislearning -- basic stuff
  17. #include <vislearning/baselib/Globals.h>
  18. #include <vislearning/baselib/ICETools.h>
  19. #include <vislearning/cbaselib/MultiDataset.h>
  20. #include <vislearning/cbaselib/Example.h>
  21. #include <vislearning/cbaselib/ClassificationResult.h>
  22. #include <vislearning/cbaselib/ClassificationResults.h>
  23. //
  24. // vislearning -- classifier
  25. #include <vislearning/classifier/classifierbase/VecClassifier.h>
  26. #include <vislearning/classifier/genericClassifierSelection.h>
  27. //
  28. // vislearning -- BoW codebooks
  29. #include "vislearning/features/simplefeatures/CodebookPrototypes.h"
  30. #include "vislearning/features/simplefeatures/BoWFeatureConverter.h"
  31. //
  32. // vislearning -- local features
  33. #include <vislearning/features/localfeatures/LFonHSG.h>
  34. #include <vislearning/features/localfeatures/LFColorSande.h>
  35. #include <vislearning/features/localfeatures/LFColorWeijer.h>
  36. #include <vislearning/features/localfeatures/LFReadCache.h>
  37. #include <vislearning/features/localfeatures/LFWriteCache.h>
  38. #include <vislearning/features/localfeatures/GenericLocalFeatureSelection.h>
  39. //
  40. // vislearning -- clustering methods
  41. #include <vislearning/math/cluster/ClusterAlgorithm.h>
  42. #include "vislearning/math/cluster/RandomClustering.h"
  43. #include <vislearning/math/cluster/KMeans.h>
  44. #include <vislearning/math/cluster/KMedian.h>
  45. #include <vislearning/math/cluster/GMM.h>
  46. //
  47. //
  48. #include <vl/generic.h>
  49. #include <vl/dsift.h>
  50. using namespace std;
  51. using namespace NICE;
  52. using namespace OBJREC;
  53. LocalFeatureRepresentation * setFeatureExtractor( const Config * _conf )
  54. {
  55. LocalFeatureRepresentation * featureExtractor;
  56. //feature stuff
  57. // which OpponentSIFT implementation to use {NICE, VANDESANDE}
  58. std::string opSiftImpl;
  59. opSiftImpl = _conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
  60. // read features?
  61. bool readfeat;
  62. readfeat = _conf->gB ( "Descriptor", "read", true );
  63. // write features?
  64. bool writefeat;
  65. writefeat = _conf->gB ( "Descriptor", "write", true );
  66. // Welche Opponentsift Implementierung soll genutzt werden ?
  67. LocalFeatureRepresentation *cSIFT = NULL;
  68. LocalFeatureRepresentation *writeFeats = NULL;
  69. LocalFeatureRepresentation *readFeats = NULL;
  70. featureExtractor = NULL;
  71. if ( opSiftImpl == "NICE" )
  72. {
  73. cSIFT = new OBJREC::LFonHSG ( _conf, "HSG" );
  74. }
  75. else if ( opSiftImpl == "VANDESANDE" )
  76. {
  77. cSIFT = new OBJREC::LFColorSande ( _conf, "LFColorSande" );
  78. }
  79. else
  80. {
  81. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  82. }
  83. featureExtractor = cSIFT;
  84. if ( writefeat )
  85. {
  86. // write the features to a file, if there isn't any to read
  87. writeFeats = new LFWriteCache ( _conf, cSIFT );
  88. featureExtractor = writeFeats;
  89. }
  90. if ( readfeat )
  91. {
  92. // read the features from a file
  93. if ( writefeat )
  94. {
  95. readFeats = new LFReadCache ( _conf, writeFeats, -1 );
  96. }
  97. else
  98. {
  99. readFeats = new LFReadCache ( _conf, cSIFT, -1 );
  100. }
  101. featureExtractor = readFeats;
  102. }
  103. //only set feature stuff to NULL, deletion of the underlying object is done in the destructor
  104. if ( cSIFT != NULL )
  105. cSIFT = NULL;
  106. if ( writeFeats != NULL )
  107. writeFeats = NULL;
  108. if ( readFeats != NULL )
  109. readFeats = NULL ;
  110. return featureExtractor;
  111. }
  112. OBJREC::ClusterAlgorithm * setClusterAlgo( const Config * _conf )
  113. {
  114. std::string section ( "clusteringStuff" );
  115. // define the initial number of clusters our codebook shall contain
  116. int initialNumberOfClusters = _conf->gI(section, "initialNumberOfClusters", 10);
  117. // define the clustering algorithm to be used
  118. std::string clusterAlgoString = _conf->gS(section, "clusterAlgo", "kmeans");
  119. OBJREC::ClusterAlgorithm * clusterAlgo;
  120. if (clusterAlgoString.compare("kmeans") == 0)
  121. {
  122. clusterAlgo = new OBJREC::KMeans(initialNumberOfClusters);
  123. }
  124. else if (clusterAlgoString.compare("kmedian") == 0)
  125. {
  126. clusterAlgo = new OBJREC::KMedian(initialNumberOfClusters);
  127. }
  128. else if (clusterAlgoString.compare("GMM") == 0)
  129. {
  130. clusterAlgo = new OBJREC::GMM( _conf, initialNumberOfClusters );
  131. }
  132. else if ( clusterAlgoString.compare("RandomClustering") == 0 )
  133. {
  134. clusterAlgo = new OBJREC::RandomClustering( _conf, section );
  135. }
  136. else
  137. {
  138. std::cerr << "Unknown cluster algorithm selected, use random clustering instead" << std::endl;
  139. clusterAlgo = new OBJREC::RandomClustering( _conf, section );
  140. }
  141. return clusterAlgo;
  142. }
  143. /**
  144. a complete BoW pipeline
  145. possibly, we can make use of objrec/progs/testClassifier.cpp
  146. */
  147. int main( int argc, char **argv )
  148. {
  149. std::set_terminate( __gnu_cxx::__verbose_terminate_handler );
  150. Config * conf = new Config ( argc, argv );
  151. const bool writeClassificationResults = conf->gB( "main", "writeClassificationResults", true );
  152. const std::string resultsfile = conf->gS( "main", "resultsfile", "/tmp/results.txt" );
  153. ResourceStatistics rs;
  154. // ========================================================================
  155. // TRAINING STEP
  156. // ========================================================================
  157. MultiDataset md( conf );
  158. const LabeledSet *trainFiles = md["train"];
  159. //**********************************************
  160. //
  161. // FEATURE EXTRACTION FOR TRAINING IMAGES
  162. //
  163. //**********************************************
  164. LocalFeatureRepresentation * featureExtractor = setFeatureExtractor( conf );
  165. //collect features in a single data structure
  166. NICE::VVector featuresFromAllTrainingImages;
  167. featuresFromAllTrainingImages.clear();
  168. //okay, this is redundant - but I see no way to do it more easy right now...
  169. std::vector<NICE::VVector> featuresOfImages ( trainFiles->size() );
  170. //this again is somehow redundant, but we need the labels lateron for easy access - change this to a better solution :)
  171. NICE::VectorT<int> labelsTrain ( trainFiles->size(), 0 );
  172. //TODO replace the nasty makro by a suitable for-loop to make it omp-ready (parallelization)
  173. int imgCnt ( 0 );
  174. LOOP_ALL_S( *trainFiles )
  175. {
  176. EACH_INFO( classno, info );
  177. std::string filename = info.img();
  178. NICE::ColorImage img( filename );
  179. //compute features
  180. //variables to store feature information
  181. NICE::VVector features;
  182. NICE::VVector positions;
  183. Globals::setCurrentImgFN ( filename );
  184. featureExtractor->extractFeatures ( img, features, positions );
  185. //normalization :)
  186. for ( NICE::VVector::iterator i = features.begin();
  187. i != features.end();
  188. i++)
  189. {
  190. i->normalizeL1();
  191. }
  192. //collect them all in a larger data structure
  193. featuresFromAllTrainingImages.append( features );
  194. //and store it as well in the data struct that additionally keeps the information which features belong to which image
  195. //TODO this can be made more clever!
  196. // featuresOfImages.push_back( features );
  197. featuresOfImages[imgCnt] = features;
  198. labelsTrain[imgCnt] = classno;
  199. imgCnt++;
  200. }
  201. //**********************************************
  202. //
  203. // CODEBOOK CREATION
  204. //
  205. //**********************************************
  206. OBJREC::ClusterAlgorithm * clusterAlgo = setClusterAlgo( conf );
  207. NICE::VVector prototypes;
  208. std::vector<double> weights;
  209. std::vector<int> assignments;
  210. clusterAlgo->cluster( featuresFromAllTrainingImages, prototypes, weights, assignments );
  211. OBJREC::CodebookPrototypes * codebook = new OBJREC::CodebookPrototypes ( prototypes );
  212. //**********************************************
  213. //
  214. // VECTOR QUANTIZATION OF
  215. // FEATURES OF TRAINING IMAGES
  216. //
  217. //**********************************************
  218. OBJREC::BoWFeatureConverter * bowConverter = new OBJREC::BoWFeatureConverter ( conf, codebook );
  219. OBJREC::LabeledSetVector trainSet;
  220. NICE::VVector histograms ( featuresOfImages.size() /* number of vectors*/, 0 /* dimension of vectors*/ ); //the internal vectors will be resized within calcHistogram
  221. NICE::VVector::iterator histogramIt = histograms.begin();
  222. NICE::VectorT<int>::const_iterator labelsIt = labelsTrain.begin();
  223. for (std::vector<NICE::VVector>::const_iterator imgIt = featuresOfImages.begin(); imgIt != featuresOfImages.end(); imgIt++, histogramIt++, labelsIt++)
  224. {
  225. bowConverter->calcHistogram ( *imgIt, *histogramIt );
  226. bowConverter->normalizeHistogram ( *histogramIt );
  227. //NOTE perhaps we should use add_reference here
  228. trainSet.add( *labelsIt, *histogramIt );
  229. }
  230. //**********************************************
  231. //
  232. // CLASSIFIER TRAINING
  233. //
  234. //**********************************************
  235. std::string classifierType = conf->gS( "main", "classifierType", "GPHIK" );
  236. OBJREC::VecClassifier * classifier = OBJREC::GenericClassifierSelection::selectVecClassifier( conf, classifierType );
  237. //TODO integrate GP-HIK-NICE into vislearning and add it into genericClassifierSelection
  238. //this method adds the training data to the temporary knowledge of our classifier
  239. classifier->teach( trainSet );
  240. //now the actual training step starts (e.g., parameter estimation, ... )
  241. classifier->finishTeaching();
  242. // ========================================================================
  243. // TEST STEP
  244. // ========================================================================
  245. const LabeledSet *testFiles = md["test"];
  246. NICE::Matrix confusionMat;
  247. NICE::Timer t;
  248. ClassificationResults results;
  249. LOOP_ALL_S( *testFiles )
  250. {
  251. EACH_INFO( classno, info );
  252. std::string filename = info.img();
  253. //**********************************************
  254. //
  255. // FEATURE EXTRACTION FOR TEST IMAGES
  256. //
  257. //**********************************************
  258. NICE::ColorImage img( filename );
  259. //compute features
  260. //variables to store feature information
  261. NICE::VVector features;
  262. NICE::VVector positions;
  263. Globals::setCurrentImgFN ( filename );
  264. featureExtractor->extractFeatures ( img, features, positions );
  265. //normalization :)
  266. for ( NICE::VVector::iterator i = features.begin();
  267. i != features.end();
  268. i++)
  269. {
  270. i->normalizeL1();
  271. }
  272. //**********************************************
  273. //
  274. // VECTOR QUANTIZATION OF
  275. // FEATURES OF TEST IMAGES
  276. //
  277. //**********************************************
  278. NICE::Vector histogramOfCurrentImg;
  279. bowConverter->calcHistogram ( features, histogramOfCurrentImg );
  280. bowConverter->normalizeHistogram ( histogramOfCurrentImg );
  281. //**********************************************
  282. //
  283. // CLASSIFIER EVALUATION
  284. //
  285. //**********************************************
  286. uint classno_groundtruth = classno;
  287. t.start();
  288. ClassificationResult r = classifier->classify ( histogramOfCurrentImg );
  289. t.stop();
  290. uint classno_estimated = r.classno;
  291. //if we like to store the classification results for external post processing, uncomment this
  292. if ( writeClassificationResults )
  293. {
  294. results.push_back( r );
  295. }
  296. confusionMat( classno_estimated, classno_groundtruth ) += 1;
  297. }
  298. confusionMat.normalizeColumnsL1();
  299. std::cerr << confusionMat << std::endl;
  300. std::cerr << "average recognition rate: " << confusionMat.trace()/confusionMat.rows() << std::endl;
  301. if ( writeClassificationResults )
  302. {
  303. double avgRecogResults ( results.getAverageRecognitionRate () );
  304. std::cerr << "average recognition rate according to classificationResults: " << avgRecogResults << std::endl;
  305. results.writeWEKA ( resultsfile, 0 );
  306. }
  307. return 0;
  308. }