testSemanticSegmentation.cpp 7.8 KB

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