testSemanticSegmentation.cpp 8.1 KB

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