testSemanticSegmentation3D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "semseg/semseg/SemSegContextTree3D.h"
  16. #include "semseg/semseg/SemSegTools.h"
  17. #include "core/basics/ResourceStatistics.h"
  18. #include "core/image/Morph.h"
  19. #include <fstream>
  20. #include <vector>
  21. #undef DEBUG
  22. using namespace OBJREC;
  23. using namespace NICE;
  24. using namespace std;
  25. void startClassification (SemanticSegmentation *semseg,
  26. std::vector< NICE::Matrix > & M_vec,
  27. const Config & conf,
  28. const LabeledSet* testFiles,
  29. const ClassNames & classNames,
  30. const set<int> & forbidden_classes,
  31. map<int,int> & classMapping,
  32. const string & resultdir,
  33. const bool doCrossVal)
  34. {
  35. bool show_results = conf.gB ( "debug", "show_results", false );
  36. bool write_results = conf.gB ( "debug", "write_results", false );
  37. bool writeProbMaps = conf.gB ( "debug", "write_prob_maps", false );
  38. if (doCrossVal)
  39. write_results = false;
  40. bool run_3Dseg = conf.gB( "SSContextTree", "run_3dseg", false);
  41. bool postProcessing = conf.gB( "main", "post_process", false);
  42. string output_type = conf.gS ( "debug", "output_type", "ppm" );
  43. string output_postfix = conf.gS ( "debug", "output_postfix", "" );
  44. vector< int > zsizeVec;
  45. semseg->getDepthVector ( testFiles, zsizeVec, run_3Dseg );
  46. int depthCount = 0, idx = 0;
  47. vector< string > filelist;
  48. NICE::MultiChannelImageT<int> segresult;
  49. NICE::MultiChannelImageT<int> gt;
  50. for (LabeledSet::const_iterator it = testFiles->begin(); it != testFiles->end(); it++)
  51. {
  52. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  53. jt != it->second.end(); jt++)
  54. {
  55. ImageInfo & info = *(*jt);
  56. std::string file = info.img();
  57. filelist.push_back ( file );
  58. depthCount++;
  59. NICE::ImageT<int> lm;
  60. NICE::ImageT<int> lm_gt;
  61. if ( info.hasLocalizationInfo() )
  62. {
  63. const LocalizationResult *l_gt = info.localization();
  64. lm.resize ( l_gt->xsize, l_gt->ysize );
  65. lm.set ( 0 );
  66. lm_gt.resize ( l_gt->xsize, l_gt->ysize );
  67. lm_gt.set ( 0 );
  68. l_gt->calcLabeledImage ( lm, classNames.getBackgroundClass() );
  69. #ifdef DEBUG
  70. cout << "testSemanticSegmentation3D: Generating Labeled NICE::Image (Ground-Truth)" << endl;
  71. #endif
  72. l_gt->calcLabeledImage ( lm_gt, classNames.getBackgroundClass() );
  73. }
  74. segresult.addChannel ( lm );
  75. gt.addChannel ( lm_gt );
  76. int depthBoundary = 0;
  77. if ( run_3Dseg )
  78. {
  79. depthBoundary = zsizeVec[idx];
  80. }
  81. if ( depthCount < depthBoundary ) continue;
  82. NICE::MultiChannelImage3DT<double> probabilities;
  83. semseg->classify ( filelist, segresult, probabilities );
  84. // save to file
  85. for ( int z = 0; z < segresult.channels(); z++ )
  86. {
  87. std::string fname = StringTools::baseName ( filelist[z], false );
  88. if ( show_results || write_results )
  89. {
  90. NICE::ColorImage orig ( filelist[z] );
  91. NICE::ColorImage rgb;
  92. NICE::ColorImage rgb_gt;
  93. NICE::ColorImage ov_rgb;
  94. NICE::ColorImage ov_rgb_gt;
  95. for ( int y = 0 ; y < orig.height(); y++ )
  96. {
  97. for ( int x = 0 ; x < orig.width(); x++ )
  98. {
  99. lm.setPixel ( x, y, segresult.get ( x, y, ( uint ) z ) );
  100. if ( run_3Dseg )
  101. lm_gt.setPixel ( x, y, gt.get ( x, y, ( uint ) z ) );
  102. }
  103. }
  104. // confusion matrix
  105. NICE::Matrix M ( classMapping.size(), classMapping.size() );
  106. M.set ( 0 );
  107. SemSegTools::updateConfusionMatrix ( lm, lm_gt, M, forbidden_classes,
  108. classMapping );
  109. M_vec.push_back ( M );
  110. classNames.labelToRGB ( lm, rgb );
  111. classNames.labelToRGB ( lm_gt, rgb_gt );
  112. if (postProcessing)
  113. {
  114. // median filter
  115. for (int r = 0; r < 3; r++)
  116. {
  117. NICE::Image postIm(rgb.width(), rgb.height());
  118. NICE::median(*(rgb.getChannel(r)), &postIm, 1);
  119. for (int y = 0; y < rgb.height(); y++)
  120. for (int x = 0; x < rgb.width(); x++)
  121. rgb.setPixel(x,y,r, postIm.getPixelQuick(x,y));
  122. }
  123. }
  124. if ( write_results )
  125. {
  126. SemSegTools::segmentToOverlay ( orig.getChannel(1), rgb, ov_rgb );
  127. SemSegTools::segmentToOverlay ( orig.getChannel(1), rgb_gt, ov_rgb_gt );
  128. std::stringstream out;
  129. if ( output_postfix.size() > 0 )
  130. out << resultdir << "/" << fname << output_postfix;
  131. else
  132. out << resultdir << "/" << fname;
  133. #ifdef DEBUG
  134. cout << "Writing to file " << out.str() << "_*." << output_type << endl;
  135. #endif
  136. orig.write ( out.str() + "_orig." + output_type );
  137. rgb.write ( out.str() + "_result." + output_type );
  138. rgb_gt.write ( out.str() + "_groundtruth." + output_type );
  139. ov_rgb.write ( out.str() + "_overlay_res." + output_type );
  140. ov_rgb_gt.write ( out.str() + "_overlay_gt." + output_type );
  141. // write Probability maps
  142. if (writeProbMaps)
  143. {
  144. NICE::ColorImage prob_map( probabilities.width(), probabilities.height() );
  145. prob_map.set(0,0,0);
  146. int iNumChannels = probabilities.channels();
  147. for ( int idxProbMap = 0; idxProbMap < iNumChannels; idxProbMap++)
  148. {
  149. for ( int y = 0 ; y < probabilities.height(); y++ )
  150. {
  151. for ( int x = 0 ; x < probabilities.width(); x++ )
  152. {
  153. double probVal = probabilities.get( x, y, z, idxProbMap ) * 255.0;
  154. int tmp = round(probVal);
  155. for ( int c = 0 ; c < 3 ; c++ )
  156. prob_map.setPixel( x, y, c, tmp );
  157. }
  158. }
  159. std::stringstream ssFileProbMap;
  160. //ssFileProbMap << out.str() << "_probs." << "c" << idxProbMap << "." << output_type;
  161. ssFileProbMap << out.str() << "_probs." << "c-" << classNames.code( idxProbMap ) << "." << output_type;
  162. //classNames
  163. prob_map.write ( ssFileProbMap.str() );
  164. }
  165. }
  166. }
  167. }
  168. }
  169. // prepare for new 3d image
  170. filelist.clear();
  171. segresult.reInit(0,0,0);
  172. gt.reInit(0,0,0);
  173. depthCount = 0;
  174. idx++;
  175. }
  176. }
  177. segresult.freeData();
  178. }
  179. /**
  180. test semantic segmentation routines
  181. */
  182. int main ( int argc, char **argv )
  183. {
  184. std::set_terminate ( __gnu_cxx::__verbose_terminate_handler );
  185. Config conf ( argc, argv );
  186. ResourceStatistics rs;
  187. /*---------------CONFIGURATION---------------*/
  188. bool doCrossVal = conf.gB ( "debug", "do_crossval", false );
  189. string resultdir = conf.gS ( "debug", "resultdir", "." );
  190. /*-------------------------------------------*/
  191. #ifdef DEBUG
  192. cerr << "Writing Results to " << resultdir << endl;
  193. #endif
  194. std::vector< NICE::Matrix > M_vec;
  195. MultiDataset md ( &conf );
  196. const ClassNames & classNames = md.getClassNames ( "train" );
  197. set<int> forbidden_classes;
  198. classNames.getSelection ( conf.gS ( "analysis", "forbidden_classes", "" ),
  199. forbidden_classes );
  200. vector<bool> usedClasses ( classNames.numClasses(), true );
  201. for ( set<int>::const_iterator it = forbidden_classes.begin();
  202. it != forbidden_classes.end(); ++it)
  203. {
  204. usedClasses [ *it ] = false;
  205. }
  206. map<int,int> classMapping, classMappingInv;
  207. int j = 0;
  208. for ( int i = 0; i < usedClasses.size(); i++ )
  209. if (usedClasses[i])
  210. {
  211. classMapping[i] = j;
  212. classMappingInv[j] = i;
  213. j++;
  214. }
  215. // initialize semantic segmentation method
  216. SemanticSegmentation *semseg = NULL;
  217. // TRAINING AND TESTING
  218. if (!doCrossVal)
  219. {
  220. semseg = new SemSegContextTree3D ( &conf, &classNames );
  221. // STANDARD EVALUATION
  222. cout << "\nTRAINING" << endl;
  223. cout << "########\n" << endl;
  224. semseg->train( &md );
  225. cout << "\nCLASSIFICATION" << endl;
  226. cout << "##############\n" << endl;
  227. const LabeledSet *testFiles = md["test"];
  228. startClassification (semseg, M_vec, conf, testFiles, classNames,
  229. forbidden_classes, classMapping, resultdir, doCrossVal );
  230. delete semseg;
  231. }
  232. else
  233. {
  234. // CROSS-VALIDATION
  235. for (int cval = 1; cval <= 10; cval++)
  236. {
  237. semseg = new SemSegContextTree3D ( &conf, &classNames );
  238. stringstream ss;
  239. ss << cval;
  240. string cvaltrain = "train_cv" + ss.str();
  241. string cvaltest = "test_cv" + ss.str();
  242. cout << "\nTRAINING " << cval << endl;
  243. cout << "###########\n" << endl;
  244. const LabeledSet *trainFiles = md[cvaltrain];
  245. semseg->train( trainFiles );
  246. cout << "\nCLASSIFICATION " << cval << endl;
  247. cout << "#################\n" << endl;
  248. const LabeledSet *testFiles = md[cvaltest];
  249. startClassification (semseg, M_vec, conf, testFiles, classNames,
  250. forbidden_classes, classMapping, resultdir, doCrossVal );
  251. delete semseg;
  252. }
  253. }
  254. cout << "\nSTATISTICS" << endl;
  255. cout << "##########\n" << endl;
  256. long maxMemory;
  257. double userCPUTime, sysCPUTime;
  258. rs.getStatistics ( maxMemory, userCPUTime, sysCPUTime );
  259. cout << "Memory (max): " << maxMemory << " KB" << endl;
  260. cout << "CPU Time (user): " << userCPUTime << " seconds" << endl;
  261. cout << "CPU Time (sys): " << sysCPUTime << " seconds" << endl;
  262. NICE::Matrix M ( classMapping.size(), classMapping.size() );
  263. M.set ( 0 );
  264. for ( int s = 0; s < ( int ) M_vec.size(); s++ )
  265. {
  266. NICE::Matrix M_tmp = M_vec[s];
  267. for ( int r = 0; r < ( int ) M_tmp.rows(); r++ )
  268. for ( int c = 0; c < ( int ) M_tmp.cols(); c++ )
  269. M ( r, c ) += M_tmp ( r, c );
  270. }
  271. // evaluation & analysis
  272. SemSegTools::computeClassificationStatistics(
  273. M, classNames, forbidden_classes, classMappingInv );
  274. return 0;
  275. }