testSemanticSegmentation.cpp 9.7 KB

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