testSemanticSegmentation.cpp 11 KB

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