testActiveSemanticSegmentationBinary.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Beispielhafter Aufruf: BUILD_x86_64/progs/testActiveSemanticSegmentationBinary -config <CONFIGFILE>
  2. /**
  3. * @file testActiveSemanticSegmentationBinary.cpp
  4. * @brief test semantic segmentation routines with actively selecting regions for labeling
  5. * @author Alexander Freytag
  6. * @date 27-02-2013
  7. */
  8. #ifdef NICE_USELIB_OPENMP
  9. #include <omp.h>
  10. #endif
  11. #include "core/basics/Config.h"
  12. #include "core/basics/StringTools.h"
  13. #include <vislearning/baselib/ICETools.h>
  14. #include <semseg/semseg/SemanticSegmentation.h>
  15. #include <semseg/semseg/SemSegLocal.h>
  16. #include <semseg/semseg/SemSegCsurka.h>
  17. #include <semseg/semseg/SemSegNovelty.h>
  18. #include <semseg/semseg/SemSegNoveltyBinary.h>
  19. #include <semseg/semseg/SemSegContextTree.h>
  20. #include "core/image/FilterT.h"
  21. #include <core/basics/ResourceStatistics.h>
  22. #include <fstream>
  23. using namespace OBJREC;
  24. using namespace NICE;
  25. using namespace std;
  26. /**
  27. test semantic segmentation routines
  28. */
  29. int main( int argc, char **argv )
  30. {
  31. std::set_terminate( __gnu_cxx::__verbose_terminate_handler );
  32. Config conf( argc, argv );
  33. ResourceStatistics rs;
  34. NICE::MatrixT<double> matTemp;
  35. std::cerr << "foo " << std::endl;
  36. std::cerr << matTemp << std::endl;
  37. bool show_result = conf.gB( "debug", "show_results", false );
  38. bool write_results = conf.gB( "debug", "write_results", false );
  39. bool write_results_pascal = conf.gB( "debug", "write_results_pascal", false );
  40. std::string resultdir = conf.gS( "debug", "resultdir", "." );
  41. //how often do we want to iterate between sem-seg and active query?
  42. int activeIterations = conf.gI("main", "activeIterations", 1 );
  43. if ( write_results )
  44. {
  45. cerr << "Writing Results to " << resultdir << endl;
  46. }
  47. MultiDataset md( &conf );
  48. const ClassNames & classNames = md.getClassNames( "train" );
  49. string method = conf.gS( "main", "method", "SSCsurka" );
  50. //currently, we only allow SemSegNoveltyBinary, because it implements addNovelExamples()
  51. SemSegNoveltyBinary *semseg = NULL;
  52. Timer timer;
  53. timer.start();
  54. semseg = new SemSegNoveltyBinary( &conf, &md );
  55. timer.stop();
  56. std::cerr << "AL time for training: " << timer.getLast() << std::endl;
  57. const LabeledSet *testFiles = md["test"];
  58. std::set<int> forbidden_classes;
  59. std::string forbidden_classes_s = conf.gS( "analysis", "forbidden_classesTrain", "" );
  60. classNames.getSelection( forbidden_classes_s, forbidden_classes );
  61. std::set<int> forbidden_classesForActiveLearning;
  62. std::string forbidden_classesForActiveLearning_s = conf.gS( "analysis", "forbidden_classesForActiveLearning", "" );
  63. classNames.getSelection( forbidden_classesForActiveLearning_s, forbidden_classesForActiveLearning );
  64. int positiveClass;
  65. //check whether we have a single positive class
  66. std::string positiveClass_s = conf.gS ( "SemSegNoveltyBinary", "positiveClass", "" );
  67. std::set<int> positiveClassNumberTmp;
  68. classNames.getSelection ( positiveClass_s, positiveClassNumberTmp );
  69. switch ( positiveClassNumberTmp.size() )
  70. {
  71. case 0:
  72. {
  73. positiveClass = 0;
  74. // std::cerr << "no positive class given, assume 0 as positive class" << std::endl;
  75. break;
  76. }
  77. case 1:
  78. {
  79. positiveClass = *(positiveClassNumberTmp.begin());
  80. // std::cerr << "positive class will be number" << positiveClass << " with the name: " << positiveClass_s << std::endl;
  81. break;
  82. }
  83. default:
  84. {
  85. //we specified more than a single positive class. right now, this is not what we are interested in, but
  86. //in theory we could also accept this and convert positiveClass into a set of ints of possible positive classes
  87. positiveClass = 0;
  88. // std::cerr << "no positive class given, assume 0 as positive class" << std::endl;
  89. break;
  90. }
  91. }
  92. std::cerr << "number of AL iterations: " << activeIterations << std::endl;
  93. for (int iterationCount = 0; iterationCount < activeIterations; iterationCount++)
  94. {
  95. std::cerr << "SemSeg AL Iteration: " << iterationCount << std::endl;
  96. semseg->setIterationCountSuffix(iterationCount);
  97. int fileno = 0;
  98. std::cerr << "start looping over all files" << std::endl;
  99. LOOP_ALL_S( *testFiles )
  100. {
  101. EACH_INFO( classno, info );
  102. std::string file = info.img();
  103. NICE::ImageT<int> lm;
  104. NICE::MultiChannelImageT<double> probabilities;
  105. if ( info.hasLocalizationInfo() )
  106. {
  107. const LocalizationResult *l_gt = info.localization();
  108. lm.resize( l_gt->xsize, l_gt->ysize );
  109. //lm.set( 0 );
  110. l_gt->calcLabeledImage( lm, classNames.getBackgroundClass() );
  111. }
  112. ((SemanticSegmentation*)semseg)->semanticseg( file, lm, probabilities );
  113. fprintf( stderr, "testSemanticSegmentation: Segmentation finished !\n" );
  114. //ground truth image, needed for updating the confusion matrix
  115. //TODO check whether this is really needed, since we computed such a label image already within SemSegNovelty
  116. NICE::ImageT<int> lm_gt;
  117. if ( info.hasLocalizationInfo() )
  118. {
  119. const LocalizationResult *l_gt = info.localization();
  120. lm_gt.resize( l_gt->xsize, l_gt->ysize );
  121. lm_gt.set( 0 );
  122. fprintf( stderr, "testSemanticSegmentation: Generating Labeled NICE::Image (Ground-Truth)\n" );
  123. l_gt->calcLabeledImage( lm_gt, classNames.getBackgroundClass() );
  124. }
  125. if ( show_result || write_results )
  126. {
  127. NICE::ColorImage orig( file );
  128. NICE::ColorImage rgb;
  129. NICE::ColorImage rgb_gt;
  130. classNames.labelToRGB( lm, rgb );
  131. classNames.labelToRGB( lm_gt, rgb_gt );
  132. if ( write_results )
  133. {
  134. std::stringstream out;
  135. std::vector< std::string > myList;
  136. StringTools::split ( Globals::getCurrentImgFN (), '/', myList );
  137. out << resultdir << "/" << myList.back();
  138. cerr << "Writing to file " << resultdir << "/"<< myList.back() << endl;
  139. std::string noveltyMethodString = conf.gS( "SemSegNoveltyBinary", "noveltyMethod", "gp-variance");
  140. orig.write ( out.str() + "_orig.ppm" );
  141. rgb.write ( out.str() + "_" + noveltyMethodString + "_result_run_" + NICE::intToString(iterationCount) + ".ppm" );
  142. rgb_gt.write ( out.str() + "_groundtruth.ppm" );
  143. }
  144. if ( show_result )
  145. {
  146. #ifndef NOVISUAL
  147. showImage( rgb, "Result" );
  148. showImage( rgb_gt, "Groundtruth" );
  149. showImage( orig, "Input" );
  150. #endif
  151. }
  152. }
  153. fileno++;
  154. } //Loop over all test images
  155. //**********************************************
  156. // EVALUATION
  157. // COMPUTE CONFUSION MAT AND FINAL SCORES
  158. //**********************************************
  159. timer.start();
  160. double score = semseg->getAUCPerformance();
  161. std::cerr << "auc scores of run : " << iterationCount << " : " << score << std::endl;
  162. long maxMemory;
  163. rs.getMaximumMemory(maxMemory);
  164. cerr << "Maximum memory used: " << maxMemory << " KB" << endl;
  165. timer.stop();
  166. std::cout << "AL time for evaluation: " << timer.getLastAbsolute() << std::endl;
  167. //**********************************************
  168. // INCLUDE THE NEW INFORMATION
  169. // AND UPDATE THE CLASSIFIER
  170. //**********************************************
  171. timer.start();
  172. semseg->addNovelExamples();
  173. timer.stop();
  174. std::cout << "AL time for incremental update: " << timer.getLastAbsolute() << std::endl;
  175. //alternatively, we could call the destructor of semseg, and create it again, which does the same thing
  176. // (add new features, save the classifier, re-read it after initialization)
  177. //BUT this would not setup the forbidden and known classes properly!!! We should fix that!
  178. const Examples * novelExamples = semseg->getNovelExamples();
  179. // std::cerr << " ==================================== " << std::endl;
  180. // std::cerr << "new examples to be added: " << std::endl;
  181. // for ( uint i = 0 ; i < novelExamples->size() ; i++ )
  182. // {
  183. // std::cerr << (*novelExamples)[i].first << " "; (*novelExamples)[i].second.store(std::cerr);
  184. // }
  185. // std::cerr << " ==================================== " << std::endl;
  186. //check which classes will be added using the features from the novel region
  187. std::set<int> newClassNumbers;
  188. newClassNumbers.clear(); //just to be sure
  189. for ( uint i = 0 ; i < novelExamples->size() ; i++ )
  190. {
  191. if (newClassNumbers.find( (*novelExamples)[i].first /* classNumber*/) == newClassNumbers.end() )
  192. {
  193. newClassNumbers.insert( (*novelExamples)[i].first );
  194. }
  195. }
  196. //accept the new classes as valid information
  197. for (std::set<int>::const_iterator clNoIt = newClassNumbers.begin(); clNoIt != newClassNumbers.end(); clNoIt++)
  198. {
  199. if ( forbidden_classes.find ( *clNoIt ) != forbidden_classes.end() )
  200. {
  201. forbidden_classes.erase(*clNoIt);
  202. }
  203. }
  204. std::cerr << "iteration finished - start the next round" << std::endl;
  205. } //iterationCount
  206. delete semseg;
  207. return 0;
  208. }