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