testSemanticSegmentation.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. if ( run_3dseg )
  103. {
  104. depthBoundary = zsizeVec[idx];
  105. }
  106. if ( depthCount < depthBoundary ) continue;
  107. NICE::MultiChannelImage3DT<double> probabilities;
  108. NICE::MultiChannelImage3DT<double> imgData;
  109. semseg->make3DImage ( filelist, imgData );
  110. semseg->semanticseg ( imgData, segresult, probabilities, filelist );
  111. fprintf ( stderr, "testSemanticSegmentation: Segmentation finished !\n" );
  112. // save to file
  113. for ( int z = 0; z < segresult.channels(); z++ )
  114. {
  115. std::string fname = StringTools::baseName ( filelist[z], false );
  116. if ( write_results_pascal )
  117. {
  118. NICE::Image pascal_lm ( segresult.width(), segresult.height() );
  119. int backgroundClass = classNames.getBackgroundClass();
  120. for ( int y = 0 ; y < segresult.height(); y++ )
  121. {
  122. for ( int x = 0 ; x < segresult.width(); x++ )
  123. {
  124. int v = segresult.get ( x, y, ( uint ) z );
  125. if ( v == backgroundClass )
  126. pascal_lm.setPixel ( x, y, 255 );
  127. else
  128. pascal_lm.setPixel ( x, y, 255 - v - 1 );
  129. }
  130. }
  131. char filename[1024];
  132. char *format = ( char * ) "pgm";
  133. sprintf ( filename, "%s/%s.%s", resultdir.c_str(), fname.c_str(), format );
  134. pascal_lm.write ( filename );
  135. }
  136. if ( show_result || write_results )
  137. {
  138. NICE::ColorImage orig ( filelist[z] );
  139. NICE::ColorImage rgb;
  140. NICE::ColorImage rgb_gt;
  141. for ( int y = 0 ; y < segresult.height(); y++ )
  142. {
  143. for ( int x = 0 ; x < segresult.width(); x++ )
  144. {
  145. lm.setPixel ( x, y, segresult.get ( x, y, ( uint ) z ) );
  146. if ( run_3dseg )
  147. lm_gt.setPixel ( x, y, gt.get ( x, y, ( uint ) z ) );
  148. }
  149. }
  150. classNames.labelToRGB ( lm, rgb );
  151. classNames.labelToRGB ( lm_gt, rgb_gt );
  152. if ( write_results )
  153. {
  154. char filename[1024];
  155. char *format = ( char * ) "ppm";
  156. sprintf ( filename, "%03d_%03d.%s", imageno, fileno, format );
  157. std::string origfilename = resultdir + "/orig_" + string ( filename );
  158. cerr << "Writing to file " << origfilename << endl;
  159. orig.write ( origfilename );
  160. rgb.write ( resultdir + "/result_" + string ( filename ) );
  161. rgb_gt.write ( resultdir + "/groundtruth_" + string ( filename ) );
  162. fileno++;
  163. }
  164. if ( show_result )
  165. {
  166. #ifndef NOVISUAL
  167. showImage ( rgb, "Result" );
  168. showImage ( rgb_gt, "Groundtruth" );
  169. showImage ( orig, "Input" );
  170. #endif
  171. }
  172. }
  173. }
  174. //#pragma omp critical
  175. for ( int z = 0; z < segresult.channels(); z++ )
  176. {
  177. for ( int y = 0 ; y < segresult.height(); y++ )
  178. {
  179. for ( int x = 0 ; x < segresult.width(); x++ )
  180. {
  181. lm.setPixel ( x, y, segresult.get ( x, y, ( uint ) z ) );
  182. if ( run_3dseg )
  183. lm_gt.setPixel ( x, y, gt.get ( x, y, ( uint ) z ) );
  184. }
  185. }
  186. NICE::Matrix M ( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  187. M.set ( 0 );
  188. updateMatrix ( lm, lm_gt, M, forbidden_classes );
  189. M_vec.push_back ( M );
  190. cerr << M << endl;
  191. }
  192. // prepare for new 3d image
  193. filelist.clear();
  194. segresult.reInit(0,0,0);
  195. gt.reInit(0,0,0);
  196. depthCount = 0;
  197. idx++;
  198. imageno++;
  199. pb.update ( testFiles->count() );
  200. }
  201. segresult.freeData();
  202. pb.hide();
  203. long maxMemory;
  204. rs.getMaximumMemory ( maxMemory );
  205. cerr << "Maximum memory used: " << maxMemory << " KB" << endl;
  206. double overall = 0.0;
  207. double sumall = 0.0;
  208. NICE::Matrix M ( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  209. M.set ( 0 );
  210. for ( int s = 0; s < ( int ) M_vec.size(); s++ )
  211. {
  212. NICE::Matrix M_tmp = M_vec[s];
  213. for ( int r = 0; r < ( int ) M_tmp.rows(); r++ )
  214. {
  215. for ( int c = 0; c < ( int ) M_tmp.cols(); c++ )
  216. {
  217. if ( r == c )
  218. overall += M_tmp ( r, c );
  219. sumall += M_tmp ( r, c );
  220. M ( r, c ) += M_tmp ( r, c );
  221. }
  222. }
  223. }
  224. overall /= sumall;
  225. // normalizing M using rows
  226. for ( int r = 0 ; r < ( int ) M.rows() ; r++ )
  227. {
  228. double sum = 0.0;
  229. for ( int c = 0 ; c < ( int ) M.cols() ; c++ )
  230. sum += M ( r, c );
  231. if ( fabs ( sum ) > 1e-4 )
  232. for ( int c = 0 ; c < ( int ) M.cols() ; c++ )
  233. M ( r, c ) /= sum;
  234. }
  235. cerr << M << endl;
  236. double avg_perf = 0.0;
  237. int classes_trained = 0;
  238. for ( int r = 0 ; r < ( int ) M.rows() ; r++ )
  239. {
  240. if ( ( classNames.existsClassno ( r ) ) && ( forbidden_classes.find ( r ) == forbidden_classes.end() ) )
  241. {
  242. avg_perf += M ( r, r );
  243. double lsum = 0.0;
  244. for ( int r2 = 0; r2 < ( int ) M.rows(); r2++ )
  245. {
  246. lsum += M ( r,r2 );
  247. }
  248. if ( lsum != 0.0 )
  249. {
  250. classes_trained++;
  251. }
  252. }
  253. }
  254. if ( write_results )
  255. {
  256. ofstream fout ( ( resultdir + "/res.txt" ).c_str(), ios::out );
  257. fout << "overall: " << overall << endl;
  258. fout << "Average Performance " << avg_perf / ( classes_trained ) << endl;
  259. fout << "Lower Bound " << 1.0 / classes_trained << endl;
  260. for ( int r = 0 ; r < ( int ) M.rows() ; r++ )
  261. {
  262. if ( ( classNames.existsClassno ( r ) ) && ( forbidden_classes.find ( r ) == forbidden_classes.end() ) )
  263. {
  264. std::string classname = classNames.text ( r );
  265. fout << classname.c_str() << ": " << M ( r, r ) << endl;
  266. }
  267. }
  268. fout.close();
  269. }
  270. fprintf ( stderr, "overall: %f\n", overall );
  271. fprintf ( stderr, "Average Performance %f\n", avg_perf / ( classes_trained ) );
  272. //fprintf(stderr, "Lower Bound %f\n", 1.0 / classes_trained);
  273. for ( int r = 0 ; r < ( int ) M.rows() ; r++ )
  274. {
  275. if ( ( classNames.existsClassno ( r ) ) && ( forbidden_classes.find ( r ) == forbidden_classes.end() ) )
  276. {
  277. std::string classname = classNames.text ( r );
  278. fprintf ( stderr, "%s: %f\n", classname.c_str(), M ( r, r ) );
  279. }
  280. }
  281. delete semseg;
  282. return 0;
  283. }