SemanticSegmentation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @file SemanticSegmentation.cpp
  3. * @brief abstract interface for semantic segmentation algorithms
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 03/19/2009, latest update: 29-01-2014 (dd-mm-yyyy)
  6. */
  7. #include <iostream>
  8. #include "SemanticSegmentation.h"
  9. #include "vislearning/baselib/Preprocess.h"
  10. #include "vislearning/baselib/Globals.h"
  11. using namespace OBJREC;
  12. using namespace std;
  13. using namespace NICE;
  14. ///////////////////// ///////////////////// /////////////////////
  15. // CONSTRUCTORS / DESTRUCTORS
  16. ///////////////////// ///////////////////// /////////////////////
  17. SemanticSegmentation::SemanticSegmentation ( )
  18. : iterationCountSuffix(1)
  19. {
  20. this->imagetype = IMAGETYPE_RGB;
  21. this->classNames = new ClassNames();
  22. }
  23. SemanticSegmentation::SemanticSegmentation ( const Config *conf,
  24. const ClassNames *classNames )
  25. : iterationCountSuffix(1)
  26. {
  27. ///////////
  28. // same code as in empty constructor - duplication can be avoided with C++11 allowing for constructor delegation
  29. ///////////
  30. ///////////
  31. // here comes the new code part different from the empty constructor
  32. ///////////
  33. this->classNames = classNames;
  34. this->initFromConfig( conf );
  35. }
  36. SemanticSegmentation::~SemanticSegmentation()
  37. {
  38. }
  39. void SemanticSegmentation::initFromConfig(const Config* conf, const string& s_confSection)
  40. {
  41. std::string imagetype_s = conf->gS ( "main", "imagetype", "rgb" );
  42. if ( imagetype_s == "rgb" )
  43. imagetype = IMAGETYPE_RGB;
  44. else if ( imagetype_s == "gray" )
  45. imagetype = IMAGETYPE_GRAY;
  46. else {
  47. fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
  48. exit ( -1 );
  49. }
  50. // dangerous!!!
  51. Preprocess::Init ( conf );
  52. }
  53. ///////////////////// ///////////////////// /////////////////////
  54. // SEGMENTATION STUFF
  55. ///////////////////// ///////////////////// /////////////////////
  56. void SemanticSegmentation::semanticseg ( const std::string & filename,
  57. NICE::Image & segresult,
  58. NICE::MultiChannelImageT<double> & probabilities )
  59. {
  60. Globals::setCurrentImgFN ( filename );
  61. CachedExample *ce;
  62. if ( imagetype == IMAGETYPE_RGB )
  63. {
  64. NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
  65. ce = new CachedExample ( img );
  66. } else {
  67. NICE::Image img = Preprocess::ReadImgAdv ( filename );
  68. ce = new CachedExample ( img );
  69. }
  70. fprintf ( stderr, "Starting Semantic Segmentation !\n" );
  71. semanticseg ( ce, segresult, probabilities );
  72. delete ce;
  73. }
  74. ///////////////////// ///////////////////// /////////////////////
  75. // DATA CONVERSION
  76. ///////////////////// ///////////////////// /////////////////////
  77. void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
  78. {
  79. #ifdef DEBUG_PRINTS
  80. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  81. #endif
  82. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  83. {
  84. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  85. {
  86. Vector &tmp = * ( iter->second[j] );
  87. int dim = tmp.size();
  88. SparseVector *vec = new SparseVector ( dim );
  89. for ( int j = 0; j < dim; j++ )
  90. {
  91. if ( tmp[j] != 0.0 )
  92. {
  93. ( *vec ) [j] = tmp[j];
  94. }
  95. }
  96. Example ex;
  97. ex.svec = vec;
  98. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  99. }
  100. }
  101. lvec.clear();
  102. #ifdef DEBUG_PRINTS
  103. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  104. #endif
  105. }
  106. void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec, const bool & removeOldDataPointer )
  107. {
  108. #ifdef DEBUG_PRINTS
  109. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  110. #endif
  111. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  112. {
  113. for ( int j = 0; j < (int)iter->second.size(); j++ )
  114. {
  115. NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
  116. Example ex ( vec );
  117. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  118. }
  119. }
  120. if (!removeOldDataPointer)
  121. {
  122. //NOTE this is only useful, if our classifier does NOT need the data explicitely
  123. lvec.clear();
  124. }
  125. else
  126. {
  127. lvec.removePointersToDataWithoutDeletion();
  128. //after setting all the pointers to NULL, we can savely clear the LSet without deleting the previously
  129. //stored features, which might be needed somewhere else, e.g., in the VCNearestNeighbour
  130. lvec.clear();
  131. }
  132. #ifdef DEBUG_PRINTS
  133. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  134. #endif
  135. }
  136. void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSetVector &lvec )
  137. {
  138. #ifdef DEBUG_PRINTS
  139. cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
  140. #endif
  141. lvec.clear();
  142. for ( int i = 0; i < ( int ) examples.size(); i++ )
  143. {
  144. if ( examples[i].second.vec != NULL )
  145. {
  146. lvec.add ( examples[i].first, *examples[i].second.vec );
  147. delete examples[i].second.vec;
  148. examples[i].second.vec = NULL;
  149. }
  150. else
  151. {
  152. if ( examples[i].second.svec != NULL )
  153. {
  154. NICE::Vector v;
  155. examples[i].second.svec->convertToVectorT(v);
  156. lvec.add ( examples[i].first, v );
  157. delete examples[i].second.svec;
  158. examples[i].second.svec = NULL;
  159. }
  160. else
  161. {
  162. throw ( "no features for LabeledSet" );
  163. }
  164. }
  165. }
  166. examples.clear();
  167. #ifdef DEBUG_PRINTS
  168. cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
  169. #endif
  170. }
  171. void SemanticSegmentation::convertExamplesToVVector ( VVector &feats, Examples &examples, vector<int> &label )
  172. {
  173. #ifdef DEBUG_PRINTS
  174. cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
  175. #endif
  176. feats.clear();
  177. label.clear();
  178. for ( int i = 0; i < ( int ) examples.size(); i++ )
  179. {
  180. label.push_back ( examples[i].first );
  181. feats.push_back ( *examples[i].second.vec );
  182. delete examples[i].second.vec;
  183. examples[i].second.vec = NULL;
  184. }
  185. examples.clear();
  186. #ifdef DEBUG_PRINTS
  187. cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
  188. #endif
  189. }
  190. void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &examples, vector<int> &label )
  191. {
  192. #ifdef DEBUG_PRINTS
  193. cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
  194. #endif
  195. for ( int i = 0; i < ( int ) feats.size(); i++ )
  196. {
  197. NICE::Vector *v = new NICE::Vector ( feats[i] );
  198. Example ex ( v );
  199. ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
  200. examples.push_back ( pair<int, Example> ( label[i], ex ) );
  201. feats[i].clear();
  202. }
  203. feats.clear();
  204. label.clear();
  205. #ifdef DEBUG_PRINTS
  206. cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
  207. #endif
  208. }
  209. void SemanticSegmentation::setIterationCountSuffix( const int & _iterationCountSuffix)
  210. {
  211. this->iterationCountSuffix = _iterationCountSuffix;
  212. }
  213. void SemanticSegmentation::setClassNames ( const OBJREC::ClassNames * _classNames )
  214. {
  215. this->classNames = _classNames;
  216. }
  217. ///////////////////// INTERFACE PERSISTENT /////////////////////
  218. // interface specific methods for store and restore
  219. ///////////////////// INTERFACE PERSISTENT /////////////////////
  220. void SemanticSegmentation::restore ( std::istream & is, int format )
  221. {
  222. //delete everything we knew so far...
  223. this->clear();
  224. bool b_restoreVerbose ( false );
  225. #ifdef B_RESTOREVERBOSE
  226. b_restoreVerbose = true;
  227. #endif
  228. if ( is.good() )
  229. {
  230. if ( b_restoreVerbose )
  231. std::cerr << " restore SemanticSegmentation" << std::endl;
  232. std::string tmp;
  233. is >> tmp; //class name
  234. if ( ! this->isStartTag( tmp, "SemanticSegmentation" ) )
  235. {
  236. std::cerr << " WARNING - attempt to restore SemanticSegmentation, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  237. throw;
  238. }
  239. is.precision (numeric_limits<double>::digits10 + 1);
  240. bool b_endOfBlock ( false ) ;
  241. while ( !b_endOfBlock )
  242. {
  243. is >> tmp; // start of block
  244. if ( this->isEndTag( tmp, "SemanticSegmentation" ) )
  245. {
  246. b_endOfBlock = true;
  247. continue;
  248. }
  249. tmp = this->removeStartTag ( tmp );
  250. if ( b_restoreVerbose )
  251. std::cerr << " currently restore section " << tmp << " in SemanticSegmentation" << std::endl;
  252. if ( tmp.compare("classNames") == 0 )
  253. {
  254. //dirty solution to circumvent the const-flag
  255. const_cast<ClassNames*>(this->classNames)->restore ( is, format );
  256. is >> tmp; // end of block
  257. tmp = this->removeEndTag ( tmp );
  258. }
  259. else if ( tmp.compare("imagetype") == 0 )
  260. {
  261. unsigned int ui_imagetyp;
  262. is >> ui_imagetyp;
  263. this->imagetype = static_cast<IMAGETYP> ( ui_imagetyp );
  264. is >> tmp; // end of block
  265. tmp = this->removeEndTag ( tmp );
  266. }
  267. else if ( tmp.compare("iterationCountSuffix") == 0 )
  268. {
  269. is >> this->iterationCountSuffix;
  270. is >> tmp; // end of block
  271. tmp = this->removeEndTag ( tmp );
  272. }
  273. else
  274. {
  275. std::cerr << "WARNING -- unexpected SemanticSegmentation object -- " << tmp << " -- for restoration... aborting" << std::endl;
  276. throw;
  277. }
  278. }
  279. }
  280. else
  281. {
  282. std::cerr << "SemanticSegmentation::restore -- InStream not initialized - restoring not possible!" << std::endl;
  283. throw;
  284. }
  285. //TODO check whether we also have to do something linke Preprocess::Init ( conf );
  286. }
  287. void SemanticSegmentation::store ( std::ostream & os, int format ) const
  288. {
  289. if (os.good())
  290. {
  291. // show starting point
  292. os << this->createStartTag( "SemanticSegmentation" ) << std::endl;
  293. os.precision (numeric_limits<double>::digits10 + 1);
  294. os << this->createStartTag( "classNames" ) << std::endl;
  295. this->classNames->store ( os, format );
  296. os << this->createEndTag( "classNames" ) << std::endl;
  297. //
  298. os << this->createStartTag( "imagetype" ) << std::endl;
  299. os << imagetype << std::endl;
  300. os << this->createEndTag( "imagetype" ) << std::endl;
  301. //
  302. os << this->createStartTag( "iterationCountSuffix" ) << std::endl;
  303. os << iterationCountSuffix << std::endl;
  304. os << this->createEndTag( "iterationCountSuffix" ) << std::endl;
  305. // done
  306. os << this->createEndTag( "SemanticSegmentation" ) << std::endl;
  307. }
  308. else
  309. {
  310. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  311. }
  312. }
  313. void SemanticSegmentation::clear ()
  314. {
  315. //TODO
  316. }