testSemanticSegmentation3D.cpp 9.4 KB

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