SemanticSegmentation.cpp 9.5 KB

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