SemSegLocal.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @file SemSegLocal.cpp
  3. * @brief semantic segmentation using image patches only
  4. * @author Erik Rodner
  5. * @date 05/08/2008
  6. */
  7. #ifdef NOVISUAL
  8. #include <objrec/nice_nonvis.h>
  9. #else
  10. #include <objrec/nice.h>
  11. #endif
  12. #include <iostream>
  13. #include "SemSegLocal.h"
  14. #include "objrec/cbaselib/CachedExample.h"
  15. #include "objrec/classifier/fpclassifier/randomforest/FPCRandomForests.h"
  16. #include "objrec/features/fpfeatures/PixelPairFeature.h"
  17. #include "SemSegTools.h"
  18. using namespace OBJREC;
  19. using namespace std;
  20. using namespace NICE;
  21. SemSegLocal::SemSegLocal( const Config *conf,
  22. const MultiDataset *md )
  23. : SemanticSegmentation ( conf, &(md->getClassNames("train")) )
  24. {
  25. save_cache = conf->gB("FPCPixel", "save_cache", true );
  26. read_cache = conf->gB("FPCPixel", "read_cache", false );
  27. cache = conf->gS("FPCPixel", "cache", "fpc.data" );
  28. fpc = new FPCRandomForests ( conf, "FPCPixel" );
  29. fpc->setMaxClassNo ( classNames->getMaxClassno() );
  30. if ( read_cache ) {
  31. fprintf (stderr, "LocSSimpleFP:: Reading classifier data from %s\n", cache.c_str() );
  32. fpc->read ( cache );
  33. fprintf (stderr, "LocSSimpleFP:: successfully read\n" );
  34. } else {
  35. train ( conf, md );
  36. }
  37. }
  38. void SemSegLocal::train ( const Config *conf, const MultiDataset *md )
  39. {
  40. Examples examples;
  41. vector<CachedExample *> imgexamples;
  42. SemSegTools::collectTrainingExamples (
  43. conf,
  44. "FPCPixel", // config section for grid settings
  45. *((*md)["train"]),
  46. *classNames,
  47. examples,
  48. imgexamples );
  49. assert ( examples.size() > 0 );
  50. FeaturePool fp;
  51. PixelPairFeature hf (conf);
  52. hf.explode ( fp );
  53. fpc->train ( fp, examples );
  54. // clean up memory !!
  55. for ( vector<CachedExample *>::iterator i = imgexamples.begin();
  56. i != imgexamples.end();
  57. i++ )
  58. delete ( *i );
  59. if ( save_cache ) {
  60. fpc->save ( cache );
  61. }
  62. fp.destroy();
  63. }
  64. SemSegLocal::~SemSegLocal()
  65. {
  66. if ( fpc != NULL )
  67. delete fpc;
  68. }
  69. void SemSegLocal::semanticseg ( CachedExample *ce,
  70. // refactor-nice.pl: check this substitution
  71. // old: Image & segresult,
  72. NICE::Image & segresult,
  73. GenericImage<double> & probabilities )
  74. {
  75. // for speed optimization
  76. FPCRandomForests *fpcrf = dynamic_cast<FPCRandomForests *> ( fpc );
  77. int xsize, ysize;
  78. ce->getImageSize ( xsize, ysize );
  79. probabilities.reInit ( xsize, ysize, classNames->getMaxClassno()+1, true/*allocMem*/ );
  80. segresult.resize(xsize, ysize);
  81. Example pce ( ce, 0, 0 );
  82. long int offset = 0;
  83. for ( int y = 0 ; y < ysize ; y++ )
  84. for ( int x = 0 ; x < xsize ; x++,offset++ )
  85. {
  86. pce.x = x ; pce.y = y;
  87. ClassificationResult r = fpcrf->classify ( pce );
  88. segresult.setPixel(x,y,r.classno);
  89. for ( int i = 0 ; i < (int)probabilities.numChannels; i++ )
  90. probabilities.data[i][offset] = r.scores[i];
  91. }
  92. }