SemanticSegmentation.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**
  2. * @file SemanticSegmentation.cpp
  3. * @brief abstract interface for semantic segmentation algorithms
  4. * @author Erik Rodner, Alexander Freytag, Sven Sickert
  5. * @date 03/19/2009
  6. */
  7. #include "SemanticSegmentation.h"
  8. #include "vislearning/baselib/Preprocess.h"
  9. #include "vislearning/baselib/Globals.h"
  10. #include <iostream>
  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. coarseMode = conf->gB( "main", "coarse_mode", false );
  43. if ( imagetype_s == "rgb" )
  44. imagetype = IMAGETYPE_RGB;
  45. else if ( imagetype_s == "gray" )
  46. imagetype = IMAGETYPE_GRAY;
  47. else {
  48. fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
  49. exit ( -1 );
  50. }
  51. // dangerous!!!
  52. Preprocess::Init ( conf );
  53. }
  54. ///////////////////// ///////////////////// /////////////////////
  55. // SEGMENTATION STUFF
  56. ///////////////////// ///////////////////// /////////////////////
  57. void SemanticSegmentation::semanticseg (
  58. const std::string & filename,
  59. NICE::ImageT<int> & segresult,
  60. NICE::MultiChannelImageT<double> & probabilities )
  61. {
  62. Globals::setCurrentImgFN ( filename );
  63. CachedExample *ce;
  64. if ( imagetype == IMAGETYPE_RGB )
  65. {
  66. NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
  67. ce = new CachedExample ( img );
  68. } else {
  69. NICE::Image img = Preprocess::ReadImgAdv ( filename );
  70. ce = new CachedExample ( img );
  71. }
  72. this->semanticseg ( ce, segresult, probabilities );
  73. delete ce;
  74. }
  75. void SemanticSegmentation::semanticseg (
  76. const std::vector<std::string> & filelist,
  77. NICE::MultiChannelImageT<int> & segresult,
  78. NICE::MultiChannelImage3DT<double> & probabilities )
  79. {
  80. NICE::MultiChannelImage3DT<int> img;
  81. make3DImage( filelist, img );
  82. CachedExample *ce = new CachedExample (img);
  83. this->semanticseg ( ce, segresult, probabilities );
  84. delete ce;
  85. }
  86. void SemanticSegmentation::classify ( const std::vector<std::string> & filelist,
  87. NICE::MultiChannelImageT<int> & segresult,
  88. NICE::MultiChannelImage3DT<double> & probabilities )
  89. {
  90. for ( int it = 0; it < ( int ) filelist.size(); it++ )
  91. {
  92. NICE::MultiChannelImageT<double> probs;
  93. NICE::ImageT<int> res ( segresult.width(), segresult.height() );
  94. this->semanticseg( filelist[it], res, probs );
  95. probabilities.addChannel( probs );
  96. segresult.addChannel( res );
  97. }
  98. }
  99. ///////////////////// ///////////////////// /////////////////////
  100. // DATA CONVERSION
  101. ///////////////////// ///////////////////// /////////////////////
  102. void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
  103. {
  104. #ifdef DEBUG_PRINTS
  105. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  106. #endif
  107. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  108. {
  109. for ( int j = 0; j < ( int ) iter->second.size(); j++ )
  110. {
  111. Vector &tmp = * ( iter->second[j] );
  112. int dim = tmp.size();
  113. SparseVector *vec = new SparseVector ( dim );
  114. for ( int j = 0; j < dim; j++ )
  115. {
  116. if ( tmp[j] != 0.0 )
  117. {
  118. ( *vec ) [j] = tmp[j];
  119. }
  120. }
  121. Example ex;
  122. ex.svec = vec;
  123. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  124. }
  125. }
  126. lvec.clear();
  127. #ifdef DEBUG_PRINTS
  128. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  129. #endif
  130. }
  131. void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec, const bool & removeOldDataPointer )
  132. {
  133. #ifdef DEBUG_PRINTS
  134. cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
  135. #endif
  136. for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
  137. {
  138. for ( int j = 0; j < (int)iter->second.size(); j++ )
  139. {
  140. NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
  141. Example ex ( vec );
  142. examples.push_back ( pair<int, Example> ( iter->first, ex ) );
  143. }
  144. }
  145. if (!removeOldDataPointer)
  146. {
  147. //NOTE this is only useful, if our classifier does NOT need the data explicitely
  148. lvec.clear();
  149. }
  150. else
  151. {
  152. lvec.removePointersToDataWithoutDeletion();
  153. //after setting all the pointers to NULL, we can savely clear the LSet without deleting the previously
  154. //stored features, which might be needed somewhere else, e.g., in the VCNearestNeighbour
  155. lvec.clear();
  156. }
  157. #ifdef DEBUG_PRINTS
  158. cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
  159. #endif
  160. }
  161. void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSetVector &lvec )
  162. {
  163. #ifdef DEBUG_PRINTS
  164. cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
  165. #endif
  166. lvec.clear();
  167. for ( int i = 0; i < ( int ) examples.size(); i++ )
  168. {
  169. if ( examples[i].second.vec != NULL )
  170. {
  171. lvec.add ( examples[i].first, *examples[i].second.vec );
  172. delete examples[i].second.vec;
  173. examples[i].second.vec = NULL;
  174. }
  175. else
  176. {
  177. if ( examples[i].second.svec != NULL )
  178. {
  179. NICE::Vector v;
  180. examples[i].second.svec->convertToVectorT(v);
  181. lvec.add ( examples[i].first, v );
  182. delete examples[i].second.svec;
  183. examples[i].second.svec = NULL;
  184. }
  185. else
  186. {
  187. throw ( "no features for LabeledSet" );
  188. }
  189. }
  190. }
  191. examples.clear();
  192. #ifdef DEBUG_PRINTS
  193. cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
  194. #endif
  195. }
  196. void SemanticSegmentation::convertExamplesToVVector ( VVector &feats, Examples &examples, vector<int> &label )
  197. {
  198. #ifdef DEBUG_PRINTS
  199. cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
  200. #endif
  201. feats.clear();
  202. label.clear();
  203. for ( int i = 0; i < ( int ) examples.size(); i++ )
  204. {
  205. label.push_back ( examples[i].first );
  206. feats.push_back ( *examples[i].second.vec );
  207. delete examples[i].second.vec;
  208. examples[i].second.vec = NULL;
  209. }
  210. examples.clear();
  211. #ifdef DEBUG_PRINTS
  212. cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
  213. #endif
  214. }
  215. void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &examples, vector<int> &label )
  216. {
  217. #ifdef DEBUG_PRINTS
  218. cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
  219. #endif
  220. for ( int i = 0; i < ( int ) feats.size(); i++ )
  221. {
  222. NICE::Vector *v = new NICE::Vector ( feats[i] );
  223. Example ex ( v );
  224. ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
  225. examples.push_back ( pair<int, Example> ( label[i], ex ) );
  226. feats[i].clear();
  227. }
  228. feats.clear();
  229. label.clear();
  230. #ifdef DEBUG_PRINTS
  231. cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
  232. #endif
  233. }
  234. void SemanticSegmentation::setIterationCountSuffix( const int & _iterationCountSuffix)
  235. {
  236. this->iterationCountSuffix = _iterationCountSuffix;
  237. }
  238. void SemanticSegmentation::setClassNames ( const OBJREC::ClassNames * _classNames )
  239. {
  240. this->classNames = _classNames;
  241. }
  242. void SemanticSegmentation::getProbabilityMap ( const NICE::MultiChannelImage3DT<double> & prob )
  243. {
  244. std::string s;
  245. for ( int cl = 0; cl < prob.channels(); cl++ )
  246. for ( int z = 0; z < prob.depth(); z++ )
  247. {
  248. NICE::ColorImage img( prob.width(),prob.height() );
  249. NICE::ImageT<double> m = prob.getChannelT(z, cl);
  250. imageToPseudoColor(m, img);
  251. std::stringstream out;
  252. out << "probmap_s" << z << "_c" << cl << ".ppm";
  253. s = out.str();
  254. img.write( s );
  255. //showImage(img, "Probability map");
  256. //getchar();
  257. }
  258. }
  259. ///////////////////// INTERFACE PERSISTENT /////////////////////
  260. // interface specific methods for store and restore
  261. ///////////////////// INTERFACE PERSISTENT /////////////////////
  262. void SemanticSegmentation::restore ( std::istream & is, int format )
  263. {
  264. //delete everything we knew so far...
  265. this->clear();
  266. bool b_restoreVerbose ( false );
  267. #ifdef B_RESTOREVERBOSE
  268. b_restoreVerbose = true;
  269. #endif
  270. if ( is.good() )
  271. {
  272. if ( b_restoreVerbose )
  273. std::cerr << " restore SemanticSegmentation" << std::endl;
  274. std::string tmp;
  275. is >> tmp; //class name
  276. if ( ! this->isStartTag( tmp, "SemanticSegmentation" ) )
  277. {
  278. std::cerr << " WARNING - attempt to restore SemanticSegmentation, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  279. throw;
  280. }
  281. is.precision (numeric_limits<double>::digits10 + 1);
  282. bool b_endOfBlock ( false ) ;
  283. while ( !b_endOfBlock )
  284. {
  285. is >> tmp; // start of block
  286. if ( this->isEndTag( tmp, "SemanticSegmentation" ) )
  287. {
  288. b_endOfBlock = true;
  289. continue;
  290. }
  291. tmp = this->removeStartTag ( tmp );
  292. if ( b_restoreVerbose )
  293. std::cerr << " currently restore section " << tmp << " in SemanticSegmentation" << std::endl;
  294. if ( tmp.compare("classNames") == 0 )
  295. {
  296. //dirty solution to circumvent the const-flag
  297. const_cast<ClassNames*>(this->classNames)->restore ( is, format );
  298. is >> tmp; // end of block
  299. tmp = this->removeEndTag ( tmp );
  300. }
  301. else if ( tmp.compare("imagetype") == 0 )
  302. {
  303. unsigned int ui_imagetyp;
  304. is >> ui_imagetyp;
  305. this->imagetype = static_cast<IMAGETYP> ( ui_imagetyp );
  306. is >> tmp; // end of block
  307. tmp = this->removeEndTag ( tmp );
  308. }
  309. else if ( tmp.compare("iterationCountSuffix") == 0 )
  310. {
  311. is >> this->iterationCountSuffix;
  312. is >> tmp; // end of block
  313. tmp = this->removeEndTag ( tmp );
  314. }
  315. else
  316. {
  317. std::cerr << "WARNING -- unexpected SemanticSegmentation object -- " << tmp << " -- for restoration... aborting" << std::endl;
  318. throw;
  319. }
  320. }
  321. }
  322. else
  323. {
  324. std::cerr << "SemanticSegmentation::restore -- InStream not initialized - restoring not possible!" << std::endl;
  325. throw;
  326. }
  327. //TODO check whether we also have to do something linke Preprocess::Init ( conf );
  328. }
  329. void SemanticSegmentation::store ( std::ostream & os, int format ) const
  330. {
  331. if (os.good())
  332. {
  333. // show starting point
  334. os << this->createStartTag( "SemanticSegmentation" ) << std::endl;
  335. os.precision (numeric_limits<double>::digits10 + 1);
  336. os << this->createStartTag( "classNames" ) << std::endl;
  337. this->classNames->store ( os, format );
  338. os << this->createEndTag( "classNames" ) << std::endl;
  339. //
  340. os << this->createStartTag( "imagetype" ) << std::endl;
  341. os << imagetype << std::endl;
  342. os << this->createEndTag( "imagetype" ) << std::endl;
  343. //
  344. os << this->createStartTag( "iterationCountSuffix" ) << std::endl;
  345. os << iterationCountSuffix << std::endl;
  346. os << this->createEndTag( "iterationCountSuffix" ) << std::endl;
  347. // done
  348. os << this->createEndTag( "SemanticSegmentation" ) << std::endl;
  349. }
  350. else
  351. {
  352. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  353. }
  354. }
  355. void SemanticSegmentation::clear ()
  356. {
  357. //TODO
  358. }