testActiveSemanticSegmentation.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // Beispielhafter Aufruf: BUILD_x86_64/progs/testActiveSemanticSegmentation -config <CONFIGFILE>
  2. /**
  3. * @file testActiveSemanticSegmentation.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. void updateMatrix( const NICE::ImageT<int> & img, const NICE::ImageT<int> & gt,
  27. NICE::Matrix & M, const set<int> & forbidden_classes )
  28. {
  29. double subsamplex = gt.width() / ( double )img.width();
  30. double subsampley = gt.height() / ( double )img.height();
  31. for ( int y = 0 ; y < gt.height() ; y++ )
  32. for ( int x = 0 ; x < gt.width() ; x++ )
  33. {
  34. int xx = ( int )( x / subsamplex );
  35. int yy = ( int )( y / subsampley );
  36. if ( xx < 0 ) xx = 0;
  37. if ( yy < 0 ) yy = 0;
  38. if ( xx > img.width() - 1 ) xx = img.width() - 1;
  39. if ( yy > img.height() - 1 ) yy = img.height() - 1;
  40. int cimg = img.getPixel( xx, yy );
  41. int gimg = gt.getPixel( x, y );
  42. if ( forbidden_classes.find( gimg ) == forbidden_classes.end() )
  43. {
  44. M( gimg, cimg )++;
  45. }
  46. }
  47. }
  48. /**
  49. test semantic segmentation routines
  50. */
  51. int main( int argc, char **argv )
  52. {
  53. std::set_terminate( __gnu_cxx::__verbose_terminate_handler );
  54. Config conf( argc, argv );
  55. ResourceStatistics rs;
  56. bool show_result = conf.gB( "debug", "show_results", false );
  57. bool write_results = conf.gB( "debug", "write_results", false );
  58. bool write_results_pascal = conf.gB( "debug", "write_results_pascal", false );
  59. std::string resultdir = conf.gS( "debug", "resultdir", "." );
  60. //how often do we want to iterate between sem-seg and active query?
  61. int activeIterations = conf.gI("main", "activeIterations", 1 );
  62. if ( write_results )
  63. {
  64. cerr << "Writing Results to " << resultdir << endl;
  65. }
  66. MultiDataset md( &conf );
  67. const ClassNames & classNames = md.getClassNames( "train" );
  68. string method = conf.gS( "main", "method", "SSCsurka" );
  69. //currently, we only allow SemSegNovelty, because it implements addNovelExamples()
  70. SemanticSegmentation *semseg = NULL;
  71. Timer timer;
  72. timer.start();
  73. if ( method == "SSCsurka" )
  74. {
  75. semseg = new SemSegCsurka( &conf, &md );
  76. }
  77. else if ( method == "SSContext" )
  78. {
  79. semseg = new SemSegContextTree( &conf, &md );
  80. }
  81. else if( method == "SSNovelty" )
  82. {
  83. semseg = new SemSegNovelty( &conf, &md );
  84. }
  85. else if( method == "SSNoveltyBinary" )
  86. {
  87. semseg = new SemSegNoveltyBinary( &conf, &md );
  88. }
  89. timer.stop();
  90. std::cerr << "AL time for training: " << timer.getLast() << std::endl;
  91. const LabeledSet *testFiles = md["test"];
  92. NICE::Matrix M( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  93. M.set( 0 );
  94. std::set<int> forbidden_classes;
  95. std::string forbidden_classes_s = conf.gS( "analysis", "forbidden_classesTrain", "" );
  96. classNames.getSelection( forbidden_classes_s, forbidden_classes );
  97. std::set<int> forbidden_classesForActiveLearning;
  98. std::string forbidden_classesForActiveLearning_s = conf.gS( "analysis", "forbidden_classesForActiveLearning", "" );
  99. classNames.getSelection( forbidden_classesForActiveLearning_s, forbidden_classesForActiveLearning );
  100. for (int iterationCount = 0; iterationCount < activeIterations; iterationCount++)
  101. {
  102. //TODO shouldn't we clean the confusion matrix at the beginning of each iteration?
  103. std::cerr << "SemSeg AL Iteration: " << iterationCount << std::endl;
  104. semseg->setIterationCountSuffix(iterationCount);
  105. // ProgressBar pb( "Semantic Segmentation Analysis" );
  106. //
  107. // pb.show();
  108. int fileno = 0;
  109. std::cerr << "start looping over all files" << std::endl;
  110. LOOP_ALL_S( *testFiles )
  111. {
  112. EACH_INFO( classno, info );
  113. std::string file = info.img();
  114. NICE::ImageT<int> lm;
  115. NICE::MultiChannelImageT<double> probabilities;
  116. if ( info.hasLocalizationInfo() )
  117. {
  118. const LocalizationResult *l_gt = info.localization();
  119. lm.resize( l_gt->xsize, l_gt->ysize );
  120. //lm.set( 0 );
  121. l_gt->calcLabeledImage( lm, classNames.getBackgroundClass() );
  122. }
  123. semseg->semanticseg( file, lm, probabilities );
  124. fprintf( stderr, "testSemanticSegmentation: Segmentation finished !\n" );
  125. //ground truth image, needed for updating the confusion matrix
  126. //TODO check whether this is really needed, since we computed such a label image already within SemSegNovelty
  127. NICE::ImageT<int> lm_gt;
  128. if ( info.hasLocalizationInfo() )
  129. {
  130. const LocalizationResult *l_gt = info.localization();
  131. lm_gt.resize( l_gt->xsize, l_gt->ysize );
  132. lm_gt.set( 0 );
  133. fprintf( stderr, "testSemanticSegmentation: Generating Labeled NICE::Image (Ground-Truth)\n" );
  134. l_gt->calcLabeledImage( lm_gt, classNames.getBackgroundClass() );
  135. }
  136. // // //
  137. // // // std::string fname = StringTools::baseName( file, false );
  138. // // //
  139. // // // if ( write_results_pascal )
  140. // // // {
  141. // // //
  142. // // // NICE::Image pascal_lm( lm.width(), lm.height() );
  143. // // // int backgroundClass = classNames.getBackgroundClass();
  144. // // //
  145. // // // for ( int y = 0 ; y < lm.height(); y++ )
  146. // // // for ( int x = 0 ; x < lm.width(); x++ )
  147. // // // {
  148. // // // int v = lm.getPixel( x, y );
  149. // // //
  150. // // // if ( v == backgroundClass )
  151. // // // pascal_lm.setPixel( x, y, 255 );
  152. // // // else
  153. // // // pascal_lm.setPixel( x, y, 255 - v - 1 );
  154. // // // }
  155. // // //
  156. // // // char filename[1024];
  157. // // //
  158. // // // char *format = ( char * )"pgm";
  159. // // // sprintf( filename, "%s/%s.%s", resultdir.c_str(), fname.c_str(), format );
  160. // // //
  161. // // // pascal_lm.write( filename );
  162. // // // }
  163. // // //
  164. if ( show_result || write_results )
  165. {
  166. NICE::ColorImage orig( file );
  167. NICE::ColorImage rgb;
  168. NICE::ColorImage rgb_gt;
  169. classNames.labelToRGB( lm, rgb );
  170. classNames.labelToRGB( lm_gt, rgb_gt );
  171. if ( write_results )
  172. {
  173. // char filename[1024];
  174. // char *format = ( char * )"ppm";
  175. // sprintf( filename, "%06d.%s", fileno, format );
  176. // std::string origfilename = resultdir + "/orig_" + string( filename );
  177. // cerr << "Writing to file " << origfilename << endl;
  178. // orig.write( origfilename );
  179. // rgb.write( resultdir + "/result_" + string( filename ) );
  180. // rgb_gt.write( resultdir + "/groundtruth_" + string( filename ) );
  181. std::stringstream out;
  182. std::vector< std::string > myList;
  183. StringTools::split ( Globals::getCurrentImgFN (), '/', myList );
  184. out << resultdir << "/" << myList.back();
  185. cerr << "Writing to file " << resultdir << "/"<< myList.back() << endl;
  186. std::string noveltyMethodString = conf.gS( "SemSegNovelty", "noveltyMethod", "gp-variance");
  187. orig.write ( out.str() + "_orig.ppm" );
  188. rgb.write ( out.str() + "_" + noveltyMethodString + "_result_run_" + NICE::intToString(iterationCount) + ".ppm" );
  189. rgb_gt.write ( out.str() + "_groundtruth.ppm" );
  190. }
  191. if ( show_result )
  192. {
  193. #ifndef NOVISUAL
  194. showImage( rgb, "Result" );
  195. showImage( rgb_gt, "Groundtruth" );
  196. showImage( orig, "Input" );
  197. #endif
  198. }
  199. }
  200. //#pragma omp critical
  201. updateMatrix( lm, lm_gt, M, forbidden_classes );
  202. std::cerr << M << std::endl;
  203. fileno++;
  204. // pb.update( testFiles->count() );
  205. } //Loop over all test images
  206. // pb.hide();
  207. //**********************************************
  208. // EVALUATION
  209. // COMPUTE CONFUSION MAT AND FINAL SCORES
  210. //**********************************************
  211. timer.start();
  212. long maxMemory;
  213. rs.getMaximumMemory(maxMemory);
  214. cerr << "Maximum memory used: " << maxMemory << " KB" << endl;
  215. double overall = 0.0;
  216. double sumall = 0.0;
  217. for ( int r = 0; r < ( int )M.rows(); r++ )
  218. {
  219. for ( int c = 0; c < ( int )M.cols(); c++ )
  220. {
  221. if ( r == c )
  222. overall += M( r, c );
  223. sumall += M( r, c );
  224. }
  225. }
  226. overall /= sumall;
  227. // normalizing M using rows
  228. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  229. {
  230. double sum = 0.0;
  231. for ( int c = 0 ; c < ( int )M.cols() ; c++ )
  232. sum += M( r, c );
  233. if ( fabs( sum ) > 1e-4 )
  234. for ( int c = 0 ; c < ( int )M.cols() ; c++ )
  235. M( r, c ) /= sum;
  236. }
  237. std::cerr << M << std::endl;
  238. double avg_perf = 0.0;
  239. int classes_trained = 0;
  240. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  241. {
  242. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  243. {
  244. avg_perf += M( r, r );
  245. double lsum = 0.0;
  246. for(int r2 = 0; r2 < ( int )M.rows(); r2++)
  247. {
  248. lsum += M(r,r2);
  249. }
  250. if(lsum != 0.0)
  251. {
  252. classes_trained++;
  253. }
  254. }
  255. }
  256. if ( write_results )
  257. {
  258. ofstream fout(( resultdir + "/res.txt" ).c_str(), ios::out );
  259. fout << "overall: " << overall << endl;
  260. fout << "Average Performance " << avg_perf / ( classes_trained ) << endl;
  261. fout << "Lower Bound " << 1.0 / classes_trained << endl;
  262. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  263. {
  264. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  265. {
  266. std::string classname = classNames.text( r );
  267. fout << classname.c_str() << ": " << M( r, r ) << endl;
  268. }
  269. }
  270. fout.close();
  271. }
  272. fprintf( stderr, "overall: %f\n", overall );
  273. fprintf( stderr, "Average Performance %f\n", avg_perf / ( classes_trained ) );
  274. //fprintf(stderr, "Lower Bound %f\n", 1.0 / classes_trained);
  275. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  276. {
  277. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  278. {
  279. std::string classname = classNames.text( r );
  280. fprintf( stderr, "%s: %f\n", classname.c_str(), M( r, r ) );
  281. }
  282. }
  283. timer.stop();
  284. std::cout << "AL time for evaluation: " << timer.getLastAbsolute() << std::endl;
  285. //**********************************************
  286. // READ QUERY SCORE IMAGES
  287. // AND SELECT THE REGION TO BE LABELED
  288. //**********************************************
  289. //NOTE this is not needed anymore, since we store everything within SemSegNovelty
  290. //However, it is still needed if we use the NN-classifier for the feature learning approach
  291. // string alSection = "SemSegNovelty";
  292. // std::string noveltyMethodString = conf.gS( alSection, "noveltyMethod", "gp-variance");
  293. // std::string uncertdir = conf.gS("debug", "resultdir", "result");
  294. // int testWSize = conf.gI(alSection, "test_window_size", 10);
  295. //
  296. // float maxVal(0);
  297. // int maxValX(0);
  298. // int maxValY(0);
  299. // std::vector<ImageInfo *>::const_iterator maxValInfoIt = testFiles->begin()->second.begin();
  300. //
  301. //
  302. // for(LabeledSet::const_iterator outerIt = testFiles->begin() ; outerIt != testFiles->end() ; outerIt++)
  303. // {
  304. // for ( std::vector<ImageInfo *>::const_iterator imageIt = outerIt->second.begin(); imageIt != outerIt->second.end(); imageIt++ )
  305. // {
  306. // const ImageInfo & (info) = *(*imageIt);
  307. //
  308. // std::string file = info.img();
  309. //
  310. // std::stringstream dest;
  311. // std::vector< std::string > list2;
  312. // StringTools::split ( file, '/', list2 );
  313. // dest << uncertdir << "/" << list2.back();
  314. //
  315. // FloatImage noveltyImage;
  316. // noveltyImage.readRaw(dest.str() + "_run_" + NICE::intToString(iterationCount) + "_" + noveltyMethodString+".rawfloat");
  317. //
  318. // int xsize ( noveltyImage.width() );
  319. // int ysize ( noveltyImage.height() );
  320. //
  321. // //compute the GT-image to ensure that we only query "useful" new features, i.e., not query background or similar "forbidden" stuff
  322. // NICE::Image lm_gt;
  323. // if ( (*maxValInfoIt)->hasLocalizationInfo() )
  324. // {
  325. // const LocalizationResult *l_gt = (*maxValInfoIt)->localization();
  326. //
  327. // lm_gt.resize( l_gt->xsize, l_gt->ysize );
  328. // lm_gt.set( 0 );
  329. //
  330. // l_gt->calcLabeledImage( lm_gt, classNames.getBackgroundClass() );
  331. // }
  332. //
  333. // for ( int y = 0; y < ysize; y += testWSize )
  334. // {
  335. // for ( int x = 0; x < xsize; x += testWSize)
  336. // {
  337. // if ( (noveltyImage ( x, y ) > maxVal) && ( forbidden_classesForActiveLearning.find ( lm_gt(x, y) ) == forbidden_classesForActiveLearning.end() ) )
  338. // {
  339. // maxVal = noveltyImage ( x, y );
  340. // maxValX = x;
  341. // maxValY = y;
  342. // maxValInfoIt = imageIt;
  343. // }
  344. // }
  345. // }
  346. //
  347. // }//iterate over inner loop
  348. // }//iterate over testFiles
  349. //
  350. //
  351. // std::cerr << "maxVal: " << maxVal << " maxValX: " << maxValX << " maxValY: " << maxValY << " maxValInfo: " << (*maxValInfoIt)->img() << std::endl;
  352. //**********************************************
  353. // INCLUDE THE NEW INFORMATION
  354. // AND UPDATE THE CLASSIFIER
  355. //**********************************************
  356. timer.start();
  357. semseg->addNovelExamples();
  358. timer.stop();
  359. std::cout << "AL time for incremental update: " << timer.getLastAbsolute() << std::endl;
  360. //alternatively, we could call the destructor of semseg, and create it again, which does the same thing
  361. // (add new features, save the classifier, re-read it after initialization)
  362. //BUT this would not setup the forbidden and known classes properly!!! We should fix that!
  363. const Examples * novelExamples = semseg->getNovelExamples();
  364. // std::cerr << " ==================================== " << std::endl;
  365. // std::cerr << "new examples to be added: " << std::endl;
  366. // for ( uint i = 0 ; i < novelExamples->size() ; i++ )
  367. // {
  368. // std::cerr << (*novelExamples)[i].first << " "; (*novelExamples)[i].second.store(std::cerr);
  369. // }
  370. // std::cerr << " ==================================== " << std::endl;
  371. //check which classes will be added using the features from the novel region
  372. std::set<int> newClassNumbers;
  373. newClassNumbers.clear(); //just to be sure
  374. for ( uint i = 0 ; i < novelExamples->size() ; i++ )
  375. {
  376. if (newClassNumbers.find( (*novelExamples)[i].first /* classNumber*/) == newClassNumbers.end() )
  377. {
  378. newClassNumbers.insert( (*novelExamples)[i].first );
  379. }
  380. }
  381. //accept the new classes as valid information
  382. for (std::set<int>::const_iterator clNoIt = newClassNumbers.begin(); clNoIt != newClassNumbers.end(); clNoIt++)
  383. {
  384. if ( forbidden_classes.find ( *clNoIt ) != forbidden_classes.end() )
  385. {
  386. forbidden_classes.erase(*clNoIt);
  387. }
  388. }
  389. //NOTE Below comes the old version:
  390. // it is not needed anymore, since we store everything within SemSegNovelty
  391. //However, it is still needed if we use the NN-classifier for the feature learning approach
  392. // // ----------------------------------------------------
  393. // // therefore, we first recompute the features for the whole image and
  394. // //take the one which we desire
  395. //
  396. // //this is NOT efficient, but a nice and easy first step
  397. //
  398. // NICE::ColorImage img ( (*maxValInfoIt)->img() );
  399. //
  400. // MultiChannelImageT<double> feats;
  401. //
  402. // // extract features
  403. // LFColorWeijer * featExtract = new LFColorWeijer ( &conf );
  404. // featExtract->getFeats ( img, feats );
  405. // int featdim = feats.channels();
  406. // feats.addChannel(featdim);
  407. //
  408. // for (int c = 0; c < featdim; c++)
  409. // {
  410. // ImageT<double> tmp = feats[c];
  411. // ImageT<double> tmp2 = feats[c+featdim];
  412. //
  413. // NICE::FilterT<double, double, double>::gradientStrength (tmp, tmp2);
  414. // }
  415. // featdim += featdim;
  416. //
  417. // // compute integral images
  418. // for ( int c = 0; c < featdim; c++ )
  419. // {
  420. // feats.calcIntegral ( c );
  421. // }
  422. //
  423. // // ----------------------------------------------------
  424. // //now take the feature
  425. // NICE::Vector newFeature(featdim);
  426. // for ( int f = 0; f < featdim; f++ )
  427. // {
  428. // double val = feats.getIntegralValue ( maxValX - testWSize, maxValY - testWSize, maxValX + testWSize, maxValY + testWSize, f );
  429. // newFeature[f] = val;
  430. // }
  431. // newFeature.normalizeL1();
  432. //
  433. // NICE::Image lm_gt;
  434. // // take the gt class number as well
  435. // if ( (*maxValInfoIt)->hasLocalizationInfo() )
  436. // {
  437. // const LocalizationResult *l_gt = (*maxValInfoIt)->localization();
  438. //
  439. // lm_gt.resize( l_gt->xsize, l_gt->ysize );
  440. // lm_gt.set( 0 );
  441. //
  442. // l_gt->calcLabeledImage( lm_gt, classNames.getBackgroundClass() );
  443. // }
  444. // int classNoGT = lm_gt(maxValX, maxValY);
  445. // std::cerr << "class number GT: " << classNoGT << std::endl;
  446. //
  447. //
  448. // semseg->addNewExample(newFeature, classNoGT);
  449. //
  450. // //accept the new class as valid information
  451. // if ( forbidden_classes.find ( classNoGT ) != forbidden_classes.end() )
  452. // {
  453. // forbidden_classes.erase(classNoGT);
  454. // }
  455. std::cerr << "iteration finished - start the next round" << std::endl;
  456. } //iterationCount
  457. delete semseg;
  458. return 0;
  459. }