SemanticSegmentation.cpp 13 KB

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