testActiveSemanticSegmentationBinary.cpp 8.9 KB

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