SemSegNovelty.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <sstream>
  2. #include <iostream>
  3. #include "SemSegNovelty.h"
  4. #include "fast-hik/GPHIKClassifier.h"
  5. #include "vislearning/baselib/ICETools.h"
  6. #include "vislearning/baselib/Globals.h"
  7. #include "vislearning/features/fpfeatures/SparseVectorFeature.h"
  8. #include "core/basics/StringTools.h"
  9. #include "core/basics/Timer.h"
  10. using namespace std;
  11. using namespace NICE;
  12. using namespace OBJREC;
  13. SemSegNovelty::SemSegNovelty ( const Config *conf,
  14. const MultiDataset *md )
  15. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  16. {
  17. this->conf = conf;
  18. featExtract = new LFColorWeijer ( conf );
  19. save_cache = conf->gB ( "FPCPixel", "save_cache", true );
  20. read_cache = conf->gB ( "FPCPixel", "read_cache", false );
  21. uncertdir = conf->gS("debug", "uncertainty","uncertainty");
  22. cache = conf->gS ( "cache", "root", "" );
  23. classifier = new GPHIKClassifier ( conf, "ClassiferGPHIK" );;
  24. whs = conf->gI ( "SemSegNovelty", "window_size", 10 );
  25. featdist = conf->gI ( "SemSegNovelty", "grid", 10 );
  26. cn = md->getClassNames ( "train" );
  27. if ( read_cache )
  28. {
  29. string classifierdst = "/classifier.data";
  30. fprintf ( stderr, "SemSegNovelty:: Reading classifier data from %s\n", ( cache + classifierdst ).c_str() );
  31. try
  32. {
  33. if ( classifier != NULL )
  34. {
  35. classifier->read ( cache + classifierdst );
  36. }
  37. fprintf ( stderr, "SemSegNovelty:: successfully read\n" );
  38. }
  39. catch ( char *str )
  40. {
  41. cerr << "error reading data: " << str << endl;
  42. }
  43. }
  44. else
  45. {
  46. train ( md );
  47. }
  48. }
  49. SemSegNovelty::~SemSegNovelty()
  50. {
  51. // clean-up
  52. if ( classifier != NULL )
  53. delete classifier;
  54. if ( featExtract != NULL )
  55. delete featExtract;
  56. }
  57. void SemSegNovelty::train ( const MultiDataset *md )
  58. {
  59. const LabeledSet train = * ( *md ) ["train"];
  60. const LabeledSet *trainp = &train;
  61. ////////////////////////
  62. // feature extraction //
  63. ////////////////////////
  64. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  65. if ( forbidden_classes_s == "" )
  66. {
  67. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  68. }
  69. cn.getSelection ( forbidden_classes_s, forbidden_classes );
  70. cerr << "forbidden: " << forbidden_classes_s << endl;
  71. ProgressBar pb ( "Local Feature Extraction" );
  72. pb.show();
  73. int imgnb = 0;
  74. Examples examples;
  75. examples.filename = "training";
  76. int featdim = -1;
  77. LOOP_ALL_S ( *trainp )
  78. {
  79. //EACH_S(classno, currentFile);
  80. EACH_INFO ( classno, info );
  81. std::string currentFile = info.img();
  82. CachedExample *ce = new CachedExample ( currentFile );
  83. const LocalizationResult *locResult = info.localization();
  84. if ( locResult->size() <= 0 )
  85. {
  86. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  87. currentFile.c_str() );
  88. continue;
  89. }
  90. int xsize, ysize;
  91. ce->getImageSize ( xsize, ysize );
  92. Image labels ( xsize, ysize );
  93. labels.set ( 0 );
  94. locResult->calcLabeledImage ( labels, ( *classNames ).getBackgroundClass() );
  95. NICE::ColorImage img;
  96. try {
  97. img = ColorImage ( currentFile );
  98. } catch ( Exception ) {
  99. cerr << "SemSegNovelty: error opening image file <" << currentFile << ">" << endl;
  100. continue;
  101. }
  102. Globals::setCurrentImgFN ( currentFile );
  103. MultiChannelImageT<double> feats;
  104. // extract features
  105. featExtract->getFeats ( img, feats );
  106. featdim = feats.channels();
  107. // compute integral images
  108. for ( int c = 0; c < featdim; c++ )
  109. {
  110. feats.calcIntegral ( c );
  111. }
  112. for ( int y = 0; y < ysize; y += featdist )
  113. {
  114. for ( int x = 0; x < xsize; x += featdist )
  115. {
  116. int classno = labels ( x, y );
  117. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  118. continue;
  119. Example example;
  120. example.vec = NULL;
  121. example.svec = new SparseVector ( featdim );
  122. for ( int f = 0; f < featdim; f++ )
  123. {
  124. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  125. if ( val > 1e-10 )
  126. ( *example.svec ) [f] = val;
  127. }
  128. example.svec->normalize();
  129. example.position = imgnb;
  130. examples.push_back ( pair<int, Example> ( classno, example ) );
  131. }
  132. }
  133. delete ce;
  134. imgnb++;
  135. pb.update ( trainp->count() );
  136. }
  137. pb.hide();
  138. //////////////////////
  139. // train classifier //
  140. //////////////////////
  141. FeaturePool fp;
  142. Feature *f = new SparseVectorFeature ( featdim );
  143. f->explode ( fp );
  144. delete f;
  145. if ( classifier != NULL )
  146. classifier->train ( fp, examples );
  147. else
  148. {
  149. cerr << "no classifier selected?!" << endl;
  150. exit ( -1 );
  151. }
  152. fp.destroy();
  153. if ( save_cache )
  154. {
  155. if ( classifier != NULL )
  156. classifier->save ( cache + "/classifier.data" );
  157. }
  158. ////////////
  159. //clean up//
  160. ////////////
  161. for ( int i = 0; i < ( int ) examples.size(); i++ )
  162. {
  163. examples[i].second.clean();
  164. }
  165. examples.clear();
  166. cerr << "SemSeg training finished" << endl;
  167. }
  168. void SemSegNovelty::semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities )
  169. {
  170. Timer timer;
  171. timer.start();
  172. Examples examples;
  173. examples.filename = "testing";
  174. segresult.set ( 0 );
  175. int featdim = -1;
  176. std::string currentFile = Globals::getCurrentImgFN();
  177. int xsize, ysize;
  178. ce->getImageSize ( xsize, ysize );
  179. probabilities.reInit( xsize, ysize, cn.getMaxClassno()+1);
  180. probabilities.set ( 0.0 );
  181. NICE::ColorImage img;
  182. try {
  183. img = ColorImage ( currentFile );
  184. } catch ( Exception ) {
  185. cerr << "SemSegNovelty: error opening image file <" << currentFile << ">" << endl;
  186. return;
  187. }
  188. MultiChannelImageT<double> feats;
  189. // extract features
  190. featExtract->getFeats ( img, feats );
  191. featdim = feats.channels();
  192. // compute integral images
  193. for ( int c = 0; c < featdim; c++ )
  194. {
  195. feats.calcIntegral ( c );
  196. }
  197. FloatImage uncert ( xsize, ysize );
  198. uncert.set ( 0.0 );
  199. double maxunc = -numeric_limits<double>::max();
  200. timer.stop();
  201. cout << "first: " << timer.getLastAbsolute() << endl;
  202. timer.start();
  203. #pragma omp parallel for
  204. for ( int y = 0; y < ysize; y++ )
  205. {
  206. Example example;
  207. example.vec = NULL;
  208. example.svec = new SparseVector ( featdim );
  209. for ( int x = 0; x < xsize; x++ )
  210. {
  211. for ( int f = 0; f < featdim; f++ )
  212. {
  213. double val = feats.getIntegralValue ( x - whs, y - whs, x + whs, y + whs, f );
  214. if ( val > 1e-10 )
  215. ( *example.svec ) [f] = val;
  216. }
  217. example.svec->normalize();
  218. ClassificationResult cr = classifier->classify ( example );
  219. for ( int j = 0 ; j < cr.scores.size(); j++ )
  220. {
  221. probabilities ( x, y, j ) = cr.scores[j];
  222. }
  223. segresult ( x, y ) = cr.classno;
  224. if(maxunc < cr.uncertainty)
  225. maxunc = cr.uncertainty;
  226. uncert ( x, y ) = cr.uncertainty;
  227. example.svec->clear();
  228. }
  229. delete example.svec;
  230. example.svec = NULL;
  231. }
  232. cout << "maxunertainty: " << maxunc << endl;
  233. timer.stop();
  234. cout << "second: " << timer.getLastAbsolute() << endl;
  235. timer.start();
  236. ColorImage imgrgb ( xsize, ysize );
  237. std::stringstream out;
  238. std::vector< std::string > list2;
  239. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  240. out << uncertdir << "/" << list2.back();
  241. uncert.writeRaw(out.str()+".rawfloat");
  242. uncert(0,0) = 0.0;
  243. uncert(0,1) = 1.0;
  244. ICETools::convertToRGB ( uncert, imgrgb );
  245. imgrgb.write ( out.str() + "rough.png" );
  246. timer.stop();
  247. cout << "last: " << timer.getLastAbsolute() << endl;
  248. }