testSemSegObliqueTrees.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @file testSemSegConvTrees.cpp
  3. * @brief test semantic segmentation routines of the ConvTree method
  4. * @author Sven Sickert
  5. * @date 10/20/2014
  6. */
  7. #include "core/basics/StringTools.h"
  8. #include "core/basics/ResourceStatistics.h"
  9. #include "core/basics/Timer.h"
  10. #include "core/image/Morph.h"
  11. #include "semseg/semseg/SemSegObliqueTree.h"
  12. #include "semseg/semseg/SemSegTools.h"
  13. #include <fstream>
  14. #include <vector>
  15. using namespace OBJREC;
  16. int main ( int argc, char **argv )
  17. {
  18. // variables
  19. NICE::Config conf (argc, argv );
  20. NICE::ResourceStatistics rs;
  21. bool postProcessing = conf.gB( "SemSegObliqueTree", "post_process", false);
  22. MultiDataset md ( &conf );
  23. const ClassNames & classNames = md.getClassNames ( "train" );
  24. const LabeledSet *testFiles = md["test"];
  25. std::set<int> forbiddenClasses;
  26. classNames.getSelection ( conf.gS ( "analysis", "forbidden_classes", "" ),
  27. forbiddenClasses );
  28. std::vector<bool> usedClasses ( classNames.numClasses(), true );
  29. for ( std::set<int>::const_iterator it = forbiddenClasses.begin();
  30. it != forbiddenClasses.end(); ++it)
  31. {
  32. usedClasses [ *it ] = false;
  33. }
  34. std::map<int,int> classMapping;
  35. int j = 0;
  36. for ( int i = 0; i < usedClasses.size(); i++ )
  37. if (usedClasses[i])
  38. {
  39. classMapping[i] = j;
  40. j++;
  41. }
  42. NICE::Matrix M ( classMapping.size(), classMapping.size() );
  43. M.set( 0 );
  44. // initialize semantic segmentation method
  45. SemanticSegmentation *semseg = NULL;
  46. // setup actual segmentation method
  47. semseg = new SemSegObliqueTree ( &conf, &classNames );
  48. // training
  49. std::cout << "\nTRAINING" << std::endl;
  50. std::cout << "########\n" << std::endl;
  51. semseg->train( &md );
  52. // testing
  53. NICE::Timer timer;
  54. std::cout << "\nCLASSIFICATION" << std::endl;
  55. std::cout << "##############\n" << std::endl;
  56. for (LabeledSet::const_iterator it = testFiles->begin(); it != testFiles->end(); it++)
  57. {
  58. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  59. jt != it->second.end(); jt++)
  60. {
  61. ImageInfo & info = *(*jt);
  62. std::string file = info.img();
  63. NICE::ImageT<int> segresult, gtruth;
  64. if ( info.hasLocalizationInfo() )
  65. {
  66. const LocalizationResult *l_gt = info.localization();
  67. segresult.resize ( l_gt->xsize, l_gt->ysize );
  68. segresult.set( 0 );
  69. gtruth.resize( l_gt->xsize, l_gt->ysize );
  70. gtruth.set ( 0 );
  71. l_gt->calcLabeledImage ( gtruth, classNames.getBackgroundClass() );
  72. }
  73. else
  74. {
  75. std::cerr << "testSemSegConvTrees: WARNING: NO localization info found for "
  76. << file << std::endl;
  77. }
  78. // actual testing
  79. NICE::MultiChannelImageT<double> probabilities;
  80. timer.start();
  81. semseg->semanticseg( file, segresult, probabilities );
  82. timer.stop();
  83. std::cout << "Time for Classification: " << timer.getLastAbsolute()
  84. << "\n\n";
  85. // post processing results
  86. if (postProcessing)
  87. {
  88. std::cerr << "testSemSegConvTrees: WARNING: Post processing not yet supported."
  89. << std::endl;
  90. }
  91. // updating confusion matrix
  92. SemSegTools::updateConfusionMatrix ( segresult, gtruth, M,
  93. forbiddenClasses, classMapping );
  94. // saving results to image file
  95. NICE::ColorImage rgb;
  96. NICE::ColorImage rgb_gt;
  97. NICE::ColorImage orig ( file );
  98. classNames.labelToRGB( segresult, rgb);
  99. classNames.labelToRGB( gtruth, rgb_gt);
  100. std::string fname = NICE::StringTools::baseName ( file, false );
  101. SemSegTools::saveResultsToImageFile(
  102. &conf, "analysis", orig, rgb_gt, rgb, fname );
  103. }
  104. }
  105. // evaluation & analysis
  106. SemSegTools::computeClassificationStatistics( M, classNames, forbiddenClasses);
  107. // Cleaning up
  108. delete semseg;
  109. }