testSemSegObliqueTrees.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. NICE::Matrix M ( classNames.getMaxClassno() + 1,
  29. classNames.getMaxClassno() + 1 );
  30. M.set( 0 );
  31. // initialize semantic segmentation method
  32. SemanticSegmentation *semseg = NULL;
  33. // setup actual segmentation method
  34. semseg = new SemSegObliqueTree ( &conf, &classNames );
  35. // training
  36. std::cout << "\nTRAINING" << std::endl;
  37. std::cout << "########\n" << std::endl;
  38. semseg->train( &md );
  39. // testing
  40. NICE::Timer timer;
  41. std::cout << "\nCLASSIFICATION" << std::endl;
  42. std::cout << "##############\n" << std::endl;
  43. for (LabeledSet::const_iterator it = testFiles->begin(); it != testFiles->end(); it++)
  44. {
  45. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  46. jt != it->second.end(); jt++)
  47. {
  48. ImageInfo & info = *(*jt);
  49. std::string file = info.img();
  50. NICE::Image segresult, gtruth;
  51. if ( info.hasLocalizationInfo() )
  52. {
  53. const LocalizationResult *l_gt = info.localization();
  54. segresult.resize ( l_gt->xsize, l_gt->ysize );
  55. segresult.set( 0 );
  56. gtruth.resize( l_gt->xsize, l_gt->ysize );
  57. gtruth.set ( 0 );
  58. l_gt->calcLabeledImage ( gtruth, classNames.getBackgroundClass() );
  59. }
  60. else
  61. {
  62. std::cerr << "testSemSegConvTrees: WARNING: NO localization info found for "
  63. << file << std::endl;
  64. }
  65. // actual testing
  66. NICE::MultiChannelImageT<double> probabilities;
  67. timer.start();
  68. semseg->semanticseg( file, segresult, probabilities );
  69. timer.stop();
  70. std::cout << "Time for Classification: " << timer.getLastAbsolute()
  71. << "\n\n";
  72. // post processing results
  73. if (postProcessing)
  74. {
  75. NICE::Image postIm(segresult.width(), segresult.height());
  76. NICE::median(segresult, &postIm, 1);
  77. segresult = postIm;
  78. }
  79. // updating confusion matrix
  80. SemSegTools::updateConfusionMatrix (
  81. segresult, gtruth, M, forbiddenClasses );
  82. // saving results to image file
  83. NICE::ColorImage rgb;
  84. NICE::ColorImage rgb_gt;
  85. NICE::ColorImage orig ( file );
  86. classNames.labelToRGB( segresult, rgb);
  87. classNames.labelToRGB( gtruth, rgb_gt);
  88. std::string fname = NICE::StringTools::baseName ( file, false );
  89. SemSegTools::saveResultsToImageFile(
  90. &conf, "analysis", orig, rgb_gt, rgb, fname );
  91. }
  92. }
  93. // evaluation & analysis
  94. SemSegTools::computeClassificationStatistics( M, classNames, forbiddenClasses);
  95. // Cleaning up
  96. delete semseg;
  97. }