testSemanticSegmentation3D.cpp 9.3 KB

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