testSemSegObliqueTrees.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. MultiDataset md ( &conf );
  22. const ClassNames & classNames = md.getClassNames ( "train" );
  23. const LabeledSet *testFiles = md["test"];
  24. std::set<int> forbiddenClasses;
  25. classNames.getSelection ( conf.gS ( "analysis", "forbidden_classes", "" ),
  26. forbiddenClasses );
  27. std::vector<bool> usedClasses ( classNames.numClasses(), true );
  28. for ( std::set<int>::const_iterator it = forbiddenClasses.begin();
  29. it != forbiddenClasses.end(); ++it)
  30. {
  31. usedClasses [ *it ] = false;
  32. }
  33. std::map<int,int> classMapping, classMappingInv;
  34. int j = 0;
  35. for ( int i = 0; i < usedClasses.size(); i++ )
  36. if (usedClasses[i])
  37. {
  38. classMapping[i] = j;
  39. classMappingInv[j] = i;
  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. // updating confusion matrix
  86. SemSegTools::updateConfusionMatrix ( segresult, gtruth, M,
  87. forbiddenClasses, classMapping );
  88. // saving results to image file
  89. NICE::ColorImage rgb;
  90. NICE::ColorImage rgb_gt;
  91. NICE::ColorImage orig ( file );
  92. classNames.labelToRGB( segresult, rgb);
  93. classNames.labelToRGB( gtruth, rgb_gt);
  94. std::string fname = NICE::StringTools::baseName ( file, false );
  95. std::string outStr;
  96. SemSegTools::saveResultsToImageFile( &conf, "analysis", orig,
  97. rgb_gt, rgb, fname, outStr );
  98. }
  99. }
  100. // evaluation & analysis
  101. SemSegTools::computeClassificationStatistics(
  102. M, classNames, forbiddenClasses, classMappingInv );
  103. // Cleaning up
  104. delete semseg;
  105. }