testSemanticSegmentation3D.cpp 9.3 KB

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