testSemanticSegmentation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 and Björn Fröhlich and 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/SemanticSegmentation.h>
  16. #include <semseg3d/semseg/SemSegContextTree.h>
  17. #include <core/basics/ResourceStatistics.h>
  18. #include <core/image/Morph.h>
  19. #include <fstream>
  20. #include <vector>
  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 show_result = conf.gB ( "debug", "show_results", false );
  95. bool write_results = conf.gB ( "debug", "write_results", false );
  96. bool run_3dseg = conf.gB ( "debug", "run_3dseg", true );
  97. bool postProcessing = conf.gB( "debug", "post_process", 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. if ( write_results )
  103. {
  104. cerr << "Writing Results to " << resultdir << endl;
  105. }
  106. MultiDataset md ( &conf );
  107. const ClassNames & classNames = md.getClassNames ( "train" );
  108. // initialize semantic segmentation method
  109. SemanticSegmentation *semseg = NULL;
  110. semseg = new SemSegContextTree ( &conf, &md );
  111. // train semantic segmentation method
  112. semseg->train( &md );
  113. const LabeledSet *testFiles = md["test"];
  114. set<int> forbidden_classes;
  115. std::string forbidden_classes_s = conf.gS ( "analysis", "forbidden_classes", "" );
  116. classNames.getSelection ( forbidden_classes_s, forbidden_classes );
  117. // ProgressBar pb ( "Semantic Segmentation Analysis" );
  118. // pb.show();
  119. vector< int > zsizeVec;
  120. semseg->getDepthVector ( testFiles, zsizeVec, run_3dseg );
  121. int depthCount = 0, idx = 0;
  122. vector< string > filelist;
  123. NICE::MultiChannelImageT<double> segresult;
  124. NICE::MultiChannelImageT<double> gt;
  125. std::vector< NICE::Matrix > M_vec;
  126. for (LabeledSet::const_iterator it = testFiles->begin(); it != testFiles->end(); it++)
  127. {
  128. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  129. jt != it->second.end(); jt++)
  130. {
  131. ImageInfo & info = *(*jt);
  132. std::string file = info.img();
  133. filelist.push_back ( file );
  134. depthCount++;
  135. NICE::Image lm;
  136. NICE::Image lm_gt;
  137. if ( info.hasLocalizationInfo() )
  138. {
  139. const LocalizationResult *l_gt = info.localization();
  140. lm.resize ( l_gt->xsize, l_gt->ysize );
  141. lm.set ( 0 );
  142. lm_gt.resize ( l_gt->xsize, l_gt->ysize );
  143. lm_gt.set ( 0 );
  144. l_gt->calcLabeledImage ( lm, classNames.getBackgroundClass() );
  145. fprintf ( stderr, "testSemanticSegmentation: Generating Labeled NICE::Image (Ground-Truth)\n" );
  146. l_gt->calcLabeledImage ( lm_gt, classNames.getBackgroundClass() );
  147. }
  148. segresult.addChannel ( lm );
  149. gt.addChannel ( lm_gt );
  150. int depthBoundary = 0;
  151. if ( run_3dseg )
  152. {
  153. depthBoundary = zsizeVec[idx];
  154. }
  155. if ( depthCount < depthBoundary ) continue;
  156. NICE::MultiChannelImage3DT<double> probabilities;
  157. NICE::MultiChannelImage3DT<double> imgData;
  158. semseg->make3DImage ( filelist, imgData );
  159. Timer timer;
  160. timer.start();
  161. semseg->classify ( imgData, segresult, probabilities, filelist );
  162. timer.stop();
  163. fprintf ( stderr, "testSemanticSegmentation: Segmentation finished in %f seconds!\n", timer.getLastAbsolute() );
  164. // save to file
  165. for ( int z = 0; z < segresult.channels(); z++ )
  166. {
  167. std::string fname = StringTools::baseName ( filelist[z], false );
  168. if ( show_result || write_results )
  169. {
  170. NICE::ColorImage orig ( filelist[z] );
  171. NICE::ColorImage rgb;
  172. NICE::ColorImage rgb_gt;
  173. NICE::ColorImage ov_rgb;
  174. NICE::ColorImage ov_rgb_gt;
  175. for ( int y = 0 ; y < segresult.height(); y++ )
  176. {
  177. for ( int x = 0 ; x < segresult.width(); x++ )
  178. {
  179. lm.setPixel ( x, y, segresult.get ( x, y, ( uint ) z ) );
  180. if ( run_3dseg )
  181. lm_gt.setPixel ( x, y, gt.get ( x, y, ( uint ) z ) );
  182. }
  183. }
  184. // confusion matrix
  185. NICE::Matrix M ( classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1 );
  186. M.set ( 0 );
  187. updateMatrix ( lm, lm_gt, M, forbidden_classes );
  188. M_vec.push_back ( M );
  189. classNames.labelToRGB ( lm, rgb );
  190. classNames.labelToRGB ( lm_gt, rgb_gt );
  191. if (postProcessing)
  192. {
  193. // median filter
  194. for (int r = 0; r < 3; r++)
  195. {
  196. NICE::Image postIm(rgb.width(), rgb.height());
  197. NICE::median(*(rgb.getChannel(r)), &postIm, 1);
  198. for (int y = 0; y < rgb.height(); y++)
  199. for (int x = 0; x < rgb.width(); x++)
  200. rgb.setPixel(x,y,r, postIm.getPixelQuick(x,y));
  201. }
  202. }
  203. segmentToOverlay ( orig.getChannel(1), rgb, ov_rgb );
  204. segmentToOverlay ( orig.getChannel(1), rgb_gt, ov_rgb_gt );
  205. if ( write_results )
  206. {
  207. std::stringstream out;
  208. if ( output_postfix.size() > 0 )
  209. out << resultdir << "/" << fname << output_postfix;
  210. else
  211. out << resultdir << "/" << fname;
  212. cerr << "Writing to file " << out.str() << "_*." << output_type << endl;
  213. orig.write ( out.str() + "_orig." + output_type );
  214. rgb.write ( out.str() + "_result." + output_type );
  215. rgb_gt.write ( out.str() + "_groundtruth." + output_type );
  216. ov_rgb.write ( out.str() + "_overlay_res." + output_type );
  217. ov_rgb_gt.write ( out.str() + "_overlay_gt." + output_type );
  218. }
  219. if ( show_result )
  220. {
  221. #ifndef NOVISUAL
  222. showImage ( ov_rgb, "Result" );
  223. showImage ( ov_rgb_gt, "Groundtruth" );
  224. showImage ( orig, "Input" );
  225. #endif
  226. }
  227. }
  228. }
  229. // prepare for new 3d image
  230. filelist.clear();
  231. segresult.reInit(0,0,0);
  232. gt.reInit(0,0,0);
  233. depthCount = 0;
  234. idx++;
  235. // pb.update ( testFiles->count() );
  236. }
  237. }
  238. segresult.freeData();
  239. // pb.hide();
  240. long maxMemory;
  241. rs.getMaximumMemory ( maxMemory );
  242. cerr << "Maximum memory used: " << maxMemory << " KB" << 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. ofstream fout ( ( resultdir + "/res.txt" ).c_str(), ios::out );
  292. fout << "overall: " << overall << endl;
  293. fout << "Average Performance " << avg_perf / ( classes_trained ) << endl;
  294. fout << "Lower Bound " << 1.0 / classes_trained << endl;
  295. fprintf ( stderr, "overall: %f\n", overall );
  296. fprintf ( stderr, "Average Performance %f\n", avg_perf / ( classes_trained ) );
  297. fprintf(stderr, "Lower Bound %f\n", 1.0 / classes_trained);
  298. for ( int r = 0 ; r < ( int ) M.rows() ; r++ )
  299. {
  300. if ( ( classNames.existsClassno ( r ) ) && ( forbidden_classes.find ( r ) == forbidden_classes.end() ) )
  301. {
  302. std::string classname = classNames.text ( r );
  303. fout << classname.c_str() << ": " << M ( r, r ) << endl;
  304. fprintf ( stderr, "%s: %f\n", classname.c_str(), M ( r, r ) );
  305. }
  306. }
  307. fout.close();
  308. delete semseg;
  309. return 0;
  310. }