testSemanticSegmentation.cpp 9.1 KB

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