testSemanticSegmentation.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. 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. SemanticSegmentation *semseg = NULL;
  64. semseg = new SemSegContextTree( &conf, &md );
  65. const LabeledSet *testFiles = md["test"];
  66. set<int> forbidden_classes;
  67. std::string forbidden_classes_s = conf.gS( "analysis", "forbidden_classes", "" );
  68. classNames.getSelection( forbidden_classes_s, forbidden_classes );
  69. ProgressBar pb( "Semantic Segmentation Analysis" );
  70. pb.show();
  71. int fileno = 0, imageno = 0;
  72. vector< int > zsizeVec;
  73. semseg->getDepthVector( testFiles, zsizeVec );
  74. int depthCount = 0, idx = 0;
  75. vector< string > filelist;
  76. NICE::MultiChannelImageT<double> segresult;
  77. NICE::MultiChannelImageT<double> gt;
  78. std::vector< NICE::Matrix > M_vec;
  79. LOOP_ALL_S( *testFiles )
  80. {
  81. EACH_INFO( classno, info );
  82. std::string file = info.img();
  83. filelist.push_back( file );
  84. depthCount++;
  85. NICE::Image lm;
  86. NICE::Image lm_gt;
  87. if ( info.hasLocalizationInfo() )
  88. {
  89. const LocalizationResult *l_gt = info.localization();
  90. lm.resize( l_gt->xsize, l_gt->ysize );
  91. lm.set( 0 );
  92. lm_gt.resize( l_gt->xsize, l_gt->ysize );
  93. lm_gt.set( 0 );
  94. l_gt->calcLabeledImage( lm, classNames.getBackgroundClass() );
  95. fprintf( stderr, "testSemanticSegmentation: Generating Labeled NICE::Image (Ground-Truth)\n" );
  96. l_gt->calcLabeledImage( lm_gt, classNames.getBackgroundClass() );
  97. }
  98. segresult.addChannel( lm );
  99. gt.addChannel( lm_gt );
  100. if ( depthCount < zsizeVec[idx] ) continue;
  101. NICE::MultiChannelImage3DT<double> probabilities;
  102. NICE::MultiChannelImage3DT<double> imgData;
  103. semseg->make3DImage( filelist, imgData );
  104. semseg->semanticseg( imgData, segresult, probabilities, filelist );
  105. fprintf( stderr, "testSemanticSegmentation: Segmentation finished !\n" );
  106. // save to file
  107. for (int z = 0; z < zsizeVec[idx]; z++)
  108. {
  109. std::string fname = StringTools::baseName( filelist[z], false );
  110. if ( write_results_pascal )
  111. {
  112. NICE::Image pascal_lm( segresult.width(), segresult.height() );
  113. int backgroundClass = classNames.getBackgroundClass();
  114. for ( int y = 0 ; y < segresult.height(); y++ )
  115. {
  116. for ( int x = 0 ; x < segresult.width(); x++ )
  117. {
  118. int v = segresult.get( x, y, (uint)z );
  119. if ( v == backgroundClass )
  120. pascal_lm.setPixel( x, y, 255 );
  121. else
  122. pascal_lm.setPixel( x, y, 255 - v - 1 );
  123. }
  124. }
  125. char filename[1024];
  126. char *format = ( char * )"pgm";
  127. sprintf( filename, "%s/%s.%s", resultdir.c_str(), fname.c_str(), format );
  128. pascal_lm.write( filename );
  129. }
  130. if ( show_result || write_results )
  131. {
  132. NICE::ColorImage orig( filelist[z] );
  133. NICE::ColorImage rgb;
  134. NICE::ColorImage rgb_gt;
  135. NICE::Image lm( segresult.width(), segresult.height() );
  136. NICE::Image lm_gt( segresult.width(), segresult.height() );
  137. for ( int y = 0 ; y < segresult.height(); y++ )
  138. {
  139. for ( int x = 0 ; x < segresult.width(); x++ )
  140. {
  141. lm.setPixel( x, y, segresult.get( x, y, (uint)z ) );
  142. lm_gt.setPixel( x, y, gt.get( x, y, (uint)z ) );
  143. }
  144. }
  145. classNames.labelToRGB( lm, rgb );
  146. classNames.labelToRGB( lm_gt, rgb_gt );
  147. if ( write_results )
  148. {
  149. char filename[1024];
  150. char *format = ( char * )"ppm";
  151. sprintf( filename, "%03d_%03d.%s", imageno, fileno, format );
  152. std::string origfilename = resultdir + "/orig_" + string( filename );
  153. cerr << "Writing to file " << origfilename << endl;
  154. orig.write( origfilename );
  155. rgb.write( resultdir + "/result_" + string( filename ) );
  156. rgb_gt.write( resultdir + "/groundtruth_" + string( filename ) );
  157. fileno++;
  158. }
  159. if ( show_result )
  160. {
  161. #ifndef NOVISUAL
  162. showImage( rgb, "Result" );
  163. showImage( rgb_gt, "Groundtruth" );
  164. showImage( orig, "Input" );
  165. #endif
  166. }
  167. }
  168. }
  169. //#pragma omp critical
  170. for (int z = 0; z < zsizeVec[idx]; z++)
  171. {
  172. NICE::Image lm( segresult.width(), segresult.height() );
  173. NICE::Image lm_gt( segresult.width(), segresult.height() );
  174. for ( int y = 0 ; y < segresult.height(); y++ )
  175. {
  176. for ( int x = 0 ; x < segresult.width(); x++ )
  177. {
  178. lm.setPixel( x, y, segresult.get( x, y, (uint)z ) );
  179. lm_gt.setPixel( x, y, gt.get( x, y, (uint)z ) );
  180. }
  181. }
  182. NICE::Matrix M( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  183. M.set( 0 );
  184. updateMatrix( lm, lm_gt, M, forbidden_classes );
  185. M_vec.push_back( M );
  186. cerr << M << endl;
  187. }
  188. // prepare for new 3d image
  189. filelist.clear();
  190. NICE::MultiChannelImageT<double> segresult;
  191. NICE::MultiChannelImageT<double> gt;
  192. depthCount = 0;
  193. idx++;
  194. imageno++;
  195. pb.update( testFiles->count() );
  196. }
  197. segresult.freeData();
  198. pb.hide();
  199. long maxMemory;
  200. rs.getMaximumMemory(maxMemory);
  201. cerr << "Maximum memory used: " << maxMemory << " KB" << endl;
  202. double overall = 0.0;
  203. double sumall = 0.0;
  204. NICE::Matrix M( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  205. M.set( 0 );
  206. for (int s = 0; s < ( int )M_vec.size(); s++ )
  207. {
  208. NICE::Matrix M_tmp = M_vec[s];
  209. for ( int r = 0; r < ( int )M_tmp.rows(); r++ )
  210. {
  211. for ( int c = 0; c < ( int )M_tmp.cols(); c++ )
  212. {
  213. if ( r == c )
  214. overall += M_tmp( r, c );
  215. sumall += M_tmp( r, c );
  216. M( r, c ) += M_tmp( r, c );
  217. }
  218. }
  219. }
  220. overall /= sumall;
  221. // normalizing M using rows
  222. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  223. {
  224. double sum = 0.0;
  225. for ( int c = 0 ; c < ( int )M.cols() ; c++ )
  226. sum += M( r, c );
  227. if ( fabs( sum ) > 1e-4 )
  228. for ( int c = 0 ; c < ( int )M.cols() ; c++ )
  229. M( r, c ) /= sum;
  230. }
  231. cerr << M << endl;
  232. double avg_perf = 0.0;
  233. int classes_trained = 0;
  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. avg_perf += M( r, r );
  239. double lsum = 0.0;
  240. for(int r2 = 0; r2 < ( int )M.rows(); r2++)
  241. {
  242. lsum += M(r,r2);
  243. }
  244. if(lsum != 0.0)
  245. {
  246. classes_trained++;
  247. }
  248. }
  249. }
  250. if ( write_results )
  251. {
  252. ofstream fout(( resultdir + "/res.txt" ).c_str(), ios::out );
  253. fout << "overall: " << overall << endl;
  254. fout << "Average Performance " << avg_perf / ( classes_trained ) << endl;
  255. fout << "Lower Bound " << 1.0 / classes_trained << endl;
  256. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  257. {
  258. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  259. {
  260. std::string classname = classNames.text( r );
  261. fout << classname.c_str() << ": " << M( r, r ) << endl;
  262. }
  263. }
  264. fout.close();
  265. }
  266. fprintf( stderr, "overall: %f\n", overall );
  267. fprintf( stderr, "Average Performance %f\n", avg_perf / ( classes_trained ) );
  268. //fprintf(stderr, "Lower Bound %f\n", 1.0 / classes_trained);
  269. for ( int r = 0 ; r < ( int )M.rows() ; r++ )
  270. {
  271. if (( classNames.existsClassno( r ) ) && ( forbidden_classes.find( r ) == forbidden_classes.end() ) )
  272. {
  273. std::string classname = classNames.text( r );
  274. fprintf( stderr, "%s: %f\n", classname.c_str(), M( r, r ) );
  275. }
  276. }
  277. delete semseg;
  278. return 0;
  279. }