testSemanticSegmentation3D.cpp 9.8 KB

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