testSemanticSegmentation3D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Beispielhafter Aufruf: BUILD_x86_64/progs/testSemanticSegmentation -config <CONFIGFILE>
  2. /**
  3. * @file testSemanticSegmentation.cpp
  4. * @brief test semantic segmentation routines for 3d images and 2d images
  5. * @author Erik Rodner, Björn Fröhlich, Sven Sickert
  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 <core/image/MultiChannelImage3DT.h>
  15. #include <semseg3d/semseg/SemSegContextTree3D.h>
  16. #include <core/basics/ResourceStatistics.h>
  17. #include <core/image/Morph.h>
  18. #include <fstream>
  19. #include <vector>
  20. #undef DEBUG
  21. using namespace OBJREC;
  22. using namespace NICE;
  23. using namespace std;
  24. void segmentToOverlay ( const NICE::Image *orig, const NICE::ColorImage & segment,
  25. NICE::ColorImage & result )
  26. {
  27. int xsize = orig->width();
  28. int ysize = orig->height();
  29. result.resize( xsize, ysize );
  30. vector< NICE::MatrixT<double> > channelMat;
  31. double alpha = .5;
  32. for (int c = 0; c < 3; c++)
  33. {
  34. NICE::MatrixT<double> chan ( xsize, ysize );
  35. channelMat.push_back( chan );
  36. }
  37. for (int y = 0; y < ysize; y++)
  38. {
  39. for (int x = 0; x < xsize; x++)
  40. {
  41. uchar val = orig->getPixelQuick(x,y);
  42. for (int c = 0; c < 3; c++)
  43. channelMat[c](x,y) = (double)val + alpha*(double)segment.getPixel( x, y, c );
  44. }
  45. }
  46. for (int c = 0; c < 3; c++)
  47. {
  48. channelMat[c] /= channelMat[c].Max();
  49. channelMat[c] *= 255;
  50. }
  51. for (int y = 0; y < ysize; y++)
  52. {
  53. for (int x = 0; x < xsize; x++)
  54. {
  55. for (int c = 0; c < 3; c++)
  56. {
  57. int val = channelMat[c](x,y);
  58. result.setPixel( x, y, c, (uchar)val);
  59. }
  60. }
  61. }
  62. }
  63. void updateMatrix ( const NICE::Image & img, const NICE::Image & gt,
  64. NICE::Matrix & M, const set<int> & forbidden_classes )
  65. {
  66. double subsamplex = gt.width() / ( double ) img.width();
  67. double subsampley = gt.height() / ( double ) img.height();
  68. for ( int y = 0 ; y < gt.height() ; y++ )
  69. for ( int x = 0 ; x < gt.width() ; x++ )
  70. {
  71. int xx = ( int ) ( x / subsamplex );
  72. int yy = ( int ) ( y / subsampley );
  73. if ( xx < 0 ) xx = 0;
  74. if ( yy < 0 ) yy = 0;
  75. if ( xx > img.width() - 1 ) xx = img.width() - 1;
  76. if ( yy > img.height() - 1 ) yy = img.height() - 1;
  77. int cimg = img.getPixel ( xx, yy );
  78. int gimg = gt.getPixel ( x, y );
  79. if ( forbidden_classes.find ( gimg ) == forbidden_classes.end() )
  80. {
  81. M ( gimg, cimg ) ++;
  82. }
  83. }
  84. }
  85. /**
  86. test semantic segmentation routines
  87. */
  88. int main ( int argc, char **argv )
  89. {
  90. std::set_terminate ( __gnu_cxx::__verbose_terminate_handler );
  91. Config conf ( argc, argv );
  92. ResourceStatistics rs;
  93. /*-------------I/O CONFIGURATION-------------*/
  94. bool postProcessing = conf.gB( "main", "post_process", false);
  95. bool run_3Dseg = conf.gB( "SSContextTree", "run_3dseg", false);
  96. bool show_result = conf.gB ( "debug", "show_results", false );
  97. bool write_results = conf.gB ( "debug", "write_results", false );
  98. string output_type = conf.gS ( "debug", "output_type", "ppm" );
  99. string output_postfix = conf.gS ( "debug", "output_postfix", "" );
  100. string resultdir = conf.gS ( "debug", "resultdir", "." );
  101. /*-------------------------------------------*/
  102. #ifdef DEBUG
  103. cerr << "Writing Results to " << resultdir << endl;
  104. #endif
  105. MultiDataset md ( &conf );
  106. const ClassNames & classNames = md.getClassNames ( "train" );
  107. // initialize semantic segmentation method
  108. SemanticSegmentation *semseg = NULL;
  109. semseg = new SemSegContextTree3D ( &conf, &md );
  110. // train semantic segmentation method
  111. cout << "\nTRAINING" << endl;
  112. cout << "########\n" << endl;
  113. semseg->train( &md );
  114. const LabeledSet *testFiles = md["test"];
  115. set<int> forbidden_classes;
  116. std::string forbidden_classes_s = conf.gS ( "analysis", "forbidden_classes", "" );
  117. classNames.getSelection ( forbidden_classes_s, forbidden_classes );
  118. // ProgressBar pb ( "Semantic Segmentation Analysis" );
  119. // pb.show();
  120. vector< int > zsizeVec;
  121. semseg->getDepthVector ( testFiles, zsizeVec, run_3Dseg );
  122. int depthCount = 0, idx = 0;
  123. vector< string > filelist;
  124. NICE::MultiChannelImageT<double> segresult;
  125. NICE::MultiChannelImageT<double> gt;
  126. std::vector< NICE::Matrix > M_vec;
  127. cout << "\nCLASSIFICATION" << endl;
  128. cout << "##############\n" << endl;
  129. for (LabeledSet::const_iterator it = testFiles->begin(); it != testFiles->end(); it++)
  130. {
  131. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  132. jt != it->second.end(); jt++)
  133. {
  134. ImageInfo & info = *(*jt);
  135. std::string file = info.img();
  136. filelist.push_back ( file );
  137. depthCount++;
  138. NICE::Image lm;
  139. NICE::Image lm_gt;
  140. if ( info.hasLocalizationInfo() )
  141. {
  142. const LocalizationResult *l_gt = info.localization();
  143. lm.resize ( l_gt->xsize, l_gt->ysize );
  144. lm.set ( 0 );
  145. lm_gt.resize ( l_gt->xsize, l_gt->ysize );
  146. lm_gt.set ( 0 );
  147. l_gt->calcLabeledImage ( lm, classNames.getBackgroundClass() );
  148. #ifdef DEBUG
  149. cout << "testSemanticSegmentation3D: Generating Labeled NICE::Image (Ground-Truth)" << endl;
  150. #endif
  151. l_gt->calcLabeledImage ( lm_gt, classNames.getBackgroundClass() );
  152. }
  153. segresult.addChannel ( lm );
  154. gt.addChannel ( lm_gt );
  155. int depthBoundary = 0;
  156. if ( run_3Dseg )
  157. {
  158. depthBoundary = zsizeVec[idx];
  159. }
  160. if ( depthCount < depthBoundary ) continue;
  161. NICE::MultiChannelImage3DT<double> probabilities;
  162. NICE::MultiChannelImage3DT<double> imgData;
  163. semseg->make3DImage ( filelist, imgData );
  164. semseg->classify ( imgData, segresult, probabilities, filelist );
  165. // save to file
  166. for ( int z = 0; z < segresult.channels(); z++ )
  167. {
  168. std::string fname = StringTools::baseName ( filelist[z], false );
  169. if ( show_result || write_results )
  170. {
  171. NICE::ColorImage orig ( filelist[z] );
  172. NICE::ColorImage rgb;
  173. NICE::ColorImage rgb_gt;
  174. NICE::ColorImage ov_rgb;
  175. NICE::ColorImage ov_rgb_gt;
  176. for ( int y = 0 ; y < segresult.height(); y++ )
  177. {
  178. for ( int x = 0 ; x < segresult.width(); x++ )
  179. {
  180. lm.setPixel ( x, y, segresult.get ( x, y, ( uint ) z ) );
  181. if ( run_3Dseg )
  182. lm_gt.setPixel ( x, y, gt.get ( x, y, ( uint ) z ) );
  183. }
  184. }
  185. // confusion matrix
  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. classNames.labelToRGB ( lm, rgb );
  191. classNames.labelToRGB ( lm_gt, rgb_gt );
  192. if (postProcessing)
  193. {
  194. // median filter
  195. for (int r = 0; r < 3; r++)
  196. {
  197. NICE::Image postIm(rgb.width(), rgb.height());
  198. NICE::median(*(rgb.getChannel(r)), &postIm, 1);
  199. for (int y = 0; y < rgb.height(); y++)
  200. for (int x = 0; x < rgb.width(); x++)
  201. rgb.setPixel(x,y,r, postIm.getPixelQuick(x,y));
  202. }
  203. }
  204. segmentToOverlay ( orig.getChannel(1), rgb, ov_rgb );
  205. segmentToOverlay ( orig.getChannel(1), rgb_gt, ov_rgb_gt );
  206. if ( write_results )
  207. {
  208. std::stringstream out;
  209. if ( output_postfix.size() > 0 )
  210. out << resultdir << "/" << fname << output_postfix;
  211. else
  212. out << resultdir << "/" << fname;
  213. #ifdef DEBUG
  214. cout << "Writing to file " << out.str() << "_*." << output_type << endl;
  215. #endif
  216. orig.write ( out.str() + "_orig." + output_type );
  217. rgb.write ( out.str() + "_result." + output_type );
  218. rgb_gt.write ( out.str() + "_groundtruth." + output_type );
  219. ov_rgb.write ( out.str() + "_overlay_res." + output_type );
  220. ov_rgb_gt.write ( out.str() + "_overlay_gt." + output_type );
  221. }
  222. }
  223. }
  224. // prepare for new 3d image
  225. filelist.clear();
  226. segresult.reInit(0,0,0);
  227. gt.reInit(0,0,0);
  228. depthCount = 0;
  229. idx++;
  230. // pb.update ( testFiles->count() );
  231. }
  232. }
  233. segresult.freeData();
  234. // pb.hide();
  235. cout << "\nSTATISTICS" << endl;
  236. cout << "##########\n" << endl;
  237. long maxMemory;
  238. double userCPUTime, sysCPUTime;
  239. rs.getStatistics ( maxMemory, userCPUTime, sysCPUTime );
  240. cout << "Memory (max): " << maxMemory << " KB" << endl;
  241. cout << "CPU Time (user): " << userCPUTime << " seconds" << endl;
  242. cout << "CPU Time (sys): " << sysCPUTime << " seconds" << endl;
  243. double overall = 0.0;
  244. double sumall = 0.0;
  245. NICE::Matrix M ( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  246. M.set ( 0 );
  247. for ( int s = 0; s < ( int ) M_vec.size(); s++ )
  248. {
  249. NICE::Matrix M_tmp = M_vec[s];
  250. for ( int r = 0; r < ( int ) M_tmp.rows(); r++ )
  251. {
  252. for ( int c = 0; c < ( int ) M_tmp.cols(); c++ )
  253. {
  254. if ( r == c )
  255. overall += M_tmp ( r, c );
  256. sumall += M_tmp ( r, c );
  257. M ( r, c ) += M_tmp ( r, c );
  258. }
  259. }
  260. }
  261. overall /= sumall;
  262. // normalizing M using rows
  263. for ( int r = 0 ; r < ( int ) M.rows() ; r++ )
  264. {
  265. double sum = 0.0;
  266. for ( int c = 0 ; c < ( int ) M.cols() ; c++ )
  267. sum += M ( r, c );
  268. if ( fabs ( sum ) > 1e-4 )
  269. for ( int c = 0 ; c < ( int ) M.cols() ; c++ )
  270. M ( r, c ) /= sum;
  271. }
  272. double avg_perf = 0.0;
  273. int classes_trained = 0;
  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. avg_perf += M ( r, r );
  279. double lsum = 0.0;
  280. for ( int r2 = 0; r2 < ( int ) M.rows(); r2++ )
  281. {
  282. lsum += M ( r,r2 );
  283. }
  284. if ( lsum != 0.0 )
  285. {
  286. classes_trained++;
  287. }
  288. }
  289. }
  290. // print/save results of evaluation
  291. cout << "\nPERFORMANCE" << endl;
  292. cout << "###########\n" << endl;
  293. ofstream fout ( ( resultdir + "/res.txt" ).c_str(), ios::out );
  294. fout << "Overall Recognition Rate: " << overall << endl;
  295. fout << "Average Recognition Rate: " << avg_perf / ( classes_trained ) << endl;
  296. fout << "Lower Bound: " << 1.0 / classes_trained << endl;
  297. cout << "Overall Recogntion Rate: " << overall << endl;
  298. cout << "Average Recogntion Rate: " << avg_perf / ( classes_trained ) << endl;
  299. cout << "Lower Bound: " << 1.0 / classes_trained << endl;
  300. cout <<"\nClasses:" << endl;
  301. for ( int r = 0 ; r < ( int ) M.rows() ; r++ )
  302. {
  303. if ( ( classNames.existsClassno ( r ) ) && ( forbidden_classes.find ( r ) == forbidden_classes.end() ) )
  304. {
  305. std::string classname = classNames.text ( r );
  306. fout << classname.c_str() << ": " << M ( r, r ) << endl;
  307. cout << classname.c_str() << ": " << M ( r, r ) << endl;
  308. }
  309. }
  310. fout.close();
  311. delete semseg;
  312. return 0;
  313. }