testSemanticSegmentation.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Beispielhafter Aufruf: BUILD_x86_64/progs/testSemanticSegmentation -config <CONFIGFILE>
  2. /**
  3. * @file testSemanticSegmentation.cpp
  4. * @brief test semantic segmentation routines
  5. * @author Erik Rodner
  6. * @date 03/20/2008
  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 <objrec-froehlichexp/semseg/SemanticSegmentation.h>
  15. #include <objrec-froehlichexp/semseg/SemSegLocal.h>
  16. #include <objrec-froehlichexp/semseg/SemSegCsurka.h>
  17. #include <objrec-froehlichexp/semseg/SemSegNovelty.h>
  18. #include <objrec-froehlichexp/semseg/SemSegRegionBased.h>
  19. #include <objrec-froehlichexp/semseg/SemSegContextTree.h>
  20. #include <core/basics/ResourceStatistics.h>
  21. #include <fstream>
  22. using namespace OBJREC;
  23. using namespace NICE;
  24. using namespace std;
  25. void updateMatrix( const NICE::Image & img, const NICE::Image & gt,
  26. NICE::Matrix & M, const set<int> & forbidden_classes )
  27. {
  28. double subsamplex = gt.width() / ( double )img.width();
  29. double subsampley = gt.height() / ( double )img.height();
  30. for ( int y = 0 ; y < gt.height() ; y++ )
  31. for ( int x = 0 ; x < gt.width() ; x++ )
  32. {
  33. int xx = ( int )( x / subsamplex );
  34. int yy = ( int )( y / subsampley );
  35. if ( xx < 0 ) xx = 0;
  36. if ( yy < 0 ) yy = 0;
  37. if ( xx > img.width() - 1 ) xx = img.width() - 1;
  38. if ( yy > img.height() - 1 ) yy = img.height() - 1;
  39. int cimg = img.getPixel( xx, yy );
  40. int gimg = gt.getPixel( x, y );
  41. if ( forbidden_classes.find( gimg ) == forbidden_classes.end() )
  42. {
  43. M( gimg, cimg )++;
  44. }
  45. }
  46. }
  47. /**
  48. test semantic segmentation routines
  49. */
  50. int main( int argc, char **argv )
  51. {
  52. std::set_terminate( __gnu_cxx::__verbose_terminate_handler );
  53. Config conf( argc, argv );
  54. ResourceStatistics rs;
  55. bool show_result = conf.gB( "debug", "show_results", false );
  56. bool write_results = conf.gB( "debug", "write_results", false );
  57. bool write_results_pascal = conf.gB( "debug", "write_results_pascal", false );
  58. std::string resultdir = conf.gS( "debug", "resultdir", "." );
  59. if ( write_results )
  60. {
  61. cerr << "Writing Results to " << resultdir << endl;
  62. }
  63. MultiDataset md( &conf );
  64. const ClassNames & classNames = md.getClassNames( "train" );
  65. string method = conf.gS( "main", "method", "SSCsurka" );
  66. SemanticSegmentation *semseg = NULL;
  67. if ( method == "SSCsurka" )
  68. {
  69. semseg = new SemSegCsurka( &conf, &md );
  70. }
  71. else if ( method == "SSContext" )
  72. {
  73. semseg = new SemSegContextTree( &conf, &md );
  74. }
  75. else if( method == "SSNovelty" )
  76. {
  77. semseg = new SemSegNovelty( &conf, &md );
  78. }
  79. //SemanticSegmentation *semseg = new SemSegLocal ( &conf, &md );
  80. //SemanticSegmentation *semseg = new SemSegSTF ( &conf, &md );
  81. //SemanticSegmentation *semseg = new SemSegRegionBased(&conf, &md);
  82. const LabeledSet *testFiles = md["test"];
  83. NICE::Matrix M( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  84. M.set( 0 );
  85. set<int> forbidden_classes;
  86. std::string forbidden_classes_s = conf.gS( "analysis", "forbidden_classes", "" );
  87. classNames.getSelection( forbidden_classes_s, forbidden_classes );
  88. ProgressBar pb( "Semantic Segmentation Analysis" );
  89. pb.show();
  90. int fileno = 0;
  91. LOOP_ALL_S( *testFiles )
  92. {
  93. EACH_INFO( classno, info );
  94. std::string file = info.img();
  95. NICE::Image lm;
  96. NICE::MultiChannelImageT<double> probabilities;
  97. if ( info.hasLocalizationInfo() )
  98. {
  99. const LocalizationResult *l_gt = info.localization();
  100. lm.resize( l_gt->xsize, l_gt->ysize );
  101. lm.set( 0 );
  102. l_gt->calcLabeledImage( lm, classNames.getBackgroundClass() );
  103. }
  104. semseg->semanticseg( file, lm, probabilities );
  105. fprintf( stderr, "testSemanticSegmentation: Segmentation finished !\n" );
  106. NICE::Image lm_gt;
  107. if ( info.hasLocalizationInfo() )
  108. {
  109. const LocalizationResult *l_gt = info.localization();
  110. lm_gt.resize( l_gt->xsize, l_gt->ysize );
  111. lm_gt.set( 0 );
  112. fprintf( stderr, "testSemanticSegmentation: Generating Labeled NICE::Image (Ground-Truth)\n" );
  113. l_gt->calcLabeledImage( lm_gt, classNames.getBackgroundClass() );
  114. }
  115. std::string fname = StringTools::baseName( file, false );
  116. if ( write_results_pascal )
  117. {
  118. NICE::Image pascal_lm( lm.width(), lm.height() );
  119. int backgroundClass = classNames.getBackgroundClass();
  120. for ( int y = 0 ; y < lm.height(); y++ )
  121. for ( int x = 0 ; x < lm.width(); x++ )
  122. {
  123. int v = lm.getPixel( x, y );
  124. if ( v == backgroundClass )
  125. pascal_lm.setPixel( x, y, 255 );
  126. else
  127. pascal_lm.setPixel( x, y, 255 - v - 1 );
  128. }
  129. char filename[1024];
  130. char *format = ( char * )"pgm";
  131. sprintf( filename, "%s/%s.%s", resultdir.c_str(), fname.c_str(), format );
  132. pascal_lm.write( filename );
  133. }
  134. if ( show_result || write_results )
  135. {
  136. NICE::ColorImage orig( file );
  137. NICE::ColorImage rgb;
  138. NICE::ColorImage rgb_gt;
  139. classNames.labelToRGB( lm, rgb );
  140. classNames.labelToRGB( lm_gt, rgb_gt );
  141. if ( write_results )
  142. {
  143. char filename[1024];
  144. char *format = ( char * )"ppm";
  145. sprintf( filename, "%06d.%s", fileno, format );
  146. std::string origfilename = resultdir + "/orig_" + string( filename );
  147. cerr << "Writing to file " << origfilename << endl;
  148. orig.write( origfilename );
  149. rgb.write( resultdir + "/result_" + string( filename ) );
  150. rgb_gt.write( resultdir + "/groundtruth_" + string( filename ) );
  151. }
  152. if ( show_result )
  153. {
  154. #ifndef NOVISUAL
  155. showImage( rgb, "Result" );
  156. showImage( rgb_gt, "Groundtruth" );
  157. showImage( orig, "Input" );
  158. #endif
  159. }
  160. }
  161. //#pragma omp critical
  162. updateMatrix( lm, lm_gt, M, forbidden_classes );
  163. cerr << M << endl;
  164. fileno++;
  165. pb.update( testFiles->count() );
  166. }
  167. pb.hide();
  168. long maxMemory;
  169. rs.getMaximumMemory(maxMemory);
  170. cerr << "Maximum memory used: " << maxMemory << " KB" << endl;
  171. double overall = 0.0;
  172. double sumall = 0.0;
  173. for ( int r = 0; r < ( int )M.rows(); r++ )
  174. {
  175. for ( int c = 0; c < ( int )M.cols(); c++ )
  176. {
  177. if ( r == c )
  178. overall += M( r, c );
  179. sumall += M( r, c );
  180. }
  181. }
  182. overall /= sumall;
  183. // normalizing M using rows
  184. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  185. {
  186. double sum = 0.0;
  187. for ( int c = 0 ; c < ( int )M.cols() ; c++ )
  188. sum += M( r, c );
  189. if ( fabs( sum ) > 1e-4 )
  190. for ( int c = 0 ; c < ( int )M.cols() ; c++ )
  191. M( r, c ) /= sum;
  192. }
  193. cerr << M << endl;
  194. double avg_perf = 0.0;
  195. int classes_trained = 0;
  196. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  197. {
  198. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  199. {
  200. avg_perf += M( r, r );
  201. double lsum = 0.0;
  202. for(int r2 = 0; r2 < ( int )M.rows(); r2++)
  203. {
  204. lsum += M(r,r2);
  205. }
  206. if(lsum != 0.0)
  207. {
  208. classes_trained++;
  209. }
  210. }
  211. }
  212. if ( write_results )
  213. {
  214. ofstream fout(( resultdir + "/res.txt" ).c_str(), ios::out );
  215. fout << "overall: " << overall << endl;
  216. fout << "Average Performance " << avg_perf / ( classes_trained ) << endl;
  217. fout << "Lower Bound " << 1.0 / classes_trained << endl;
  218. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  219. {
  220. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  221. {
  222. std::string classname = classNames.text( r );
  223. fout << classname.c_str() << ": " << M( r, r ) << endl;
  224. }
  225. }
  226. fout.close();
  227. }
  228. fprintf( stderr, "overall: %f\n", overall );
  229. fprintf( stderr, "Average Performance %f\n", avg_perf / ( classes_trained ) );
  230. //fprintf(stderr, "Lower Bound %f\n", 1.0 / classes_trained);
  231. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  232. {
  233. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  234. {
  235. std::string classname = classNames.text( r );
  236. fprintf( stderr, "%s: %f\n", classname.c_str(), M( r, r ) );
  237. }
  238. }
  239. delete semseg;
  240. return 0;
  241. }