testSemSegObliqueTrees.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. gtruth.resize( l_gt->xsize, l_gt->ysize );
  67. l_gt->calcLabeledImage ( gtruth, classNames.getBackgroundClass() );
  68. }
  69. else
  70. {
  71. std::cerr << "testSemSegConvTrees: WARNING: NO localization info found for "
  72. << file << std::endl;
  73. }
  74. segresult = gtruth;
  75. // actual testing
  76. NICE::MultiChannelImageT<double> probabilities;
  77. timer.start();
  78. semseg->semanticseg( file, segresult, probabilities );
  79. timer.stop();
  80. std::cout << "Time for Classification: " << timer.getLastAbsolute()
  81. << "\n\n";
  82. // updating confusion matrix
  83. SemSegTools::updateConfusionMatrix ( segresult, gtruth, M,
  84. forbiddenClasses, classMapping );
  85. // saving results to image file
  86. NICE::ColorImage rgb;
  87. NICE::ColorImage rgb_gt;
  88. NICE::ColorImage orig ( file );
  89. classNames.labelToRGB( segresult, rgb);
  90. classNames.labelToRGB( gtruth, rgb_gt);
  91. std::string fname = NICE::StringTools::baseName ( file, false );
  92. std::string outStr;
  93. SemSegTools::saveResultsToImageFile( &conf, "analysis", orig,
  94. rgb_gt, rgb, fname, outStr );
  95. }
  96. }
  97. // resource statistics
  98. SemSegTools::computeResourceStatistics ( rs );
  99. // evaluation & analysis
  100. SemSegTools::computeClassificationStatistics(
  101. M, classNames, forbiddenClasses, classMappingInv );
  102. // Cleaning up
  103. delete semseg;
  104. }