testSemSegObliqueTrees.cpp 4.0 KB

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