testSemanticSegmentation3D.cpp 9.3 KB

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