SemanticSegmentation.cpp 10 KB

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