SemSegContextTree3D.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. #include "SemSegContextTree3D.h"
  2. #include "SemSegTools.h"
  3. #include <core/basics/FileName.h>
  4. #include <core/basics/numerictools.h>
  5. #include <core/basics/quadruplet.h>
  6. #include <core/basics/StringTools.h>
  7. #include <core/basics/Timer.h>
  8. #include <core/basics/vectorio.h>
  9. #include <core/image/FilterT.h>
  10. #include <core/image/Morph.h>
  11. #include <core/imagedisplay/ImageDisplay.h>
  12. #include <vislearning/baselib/cc.h>
  13. #include <vislearning/baselib/Globals.h>
  14. #include <vislearning/baselib/ICETools.h>
  15. #include <vislearning/cbaselib/CachedExample.h>
  16. #include <vislearning/cbaselib/PascalResults.h>
  17. #include <segmentation/RSGraphBased.h>
  18. #include <segmentation/RSMeanShift.h>
  19. #include <segmentation/RSSlic.h>
  20. #include <omp.h>
  21. #include <time.h>
  22. #include <iostream>
  23. #define VERBOSE
  24. #undef DEBUG
  25. #undef VISUALIZE
  26. #undef WRITEREGIONS
  27. using namespace OBJREC;
  28. using namespace std;
  29. using namespace NICE;
  30. //###################### CONSTRUCTORS #########################//
  31. SemSegContextTree3D::SemSegContextTree3D () : SemanticSegmentation ()
  32. {
  33. this->lfcw = NULL;
  34. this->firstiteration = true;
  35. this->run3Dseg = false;
  36. this->maxSamples = 2000;
  37. this->minFeats = 50;
  38. this->maxDepth = 10;
  39. this->windowSize = 15;
  40. this->featsPerSplit = 200;
  41. this->useShannonEntropy = true;
  42. this->nbTrees = 10;
  43. this->randomTests = 10;
  44. this->useAltTristimulus = false;
  45. this->useGradient = true;
  46. this->useWeijer = false;
  47. this->useAdditionalLayer = false;
  48. this->useHoiemFeatures = false;
  49. this->useCategorization = false;
  50. this->cndir = "";
  51. this->fasthik = NULL;
  52. this->saveLoadData = false;
  53. this->fileLocation = "tmp.txt";
  54. this->pixelWiseLabeling = true;
  55. this->segmentation = NULL;
  56. this->useFeat0 = true;
  57. this->useFeat1 = false;
  58. this->useFeat2 = true;
  59. this->useFeat3 = true;
  60. this->useFeat4 = false;
  61. this->labelIncrement = 1;
  62. if (coarseMode)
  63. this->labelIncrement = 6;
  64. srand(time(NULL));
  65. }
  66. SemSegContextTree3D::SemSegContextTree3D (
  67. const Config *conf,
  68. const ClassNames *classNames )
  69. : SemanticSegmentation ( conf, classNames )
  70. {
  71. this->conf = conf;
  72. string section = "SSContextTree";
  73. string featsec = "Features";
  74. this->lfcw = NULL;
  75. this->firstiteration = true;
  76. this->run3Dseg = conf->gB ( section, "run_3dseg", false );
  77. this->maxSamples = conf->gI ( section, "max_samples", 2000 );
  78. this->minFeats = conf->gI ( section, "min_feats", 50 );
  79. this->maxDepth = conf->gI ( section, "max_depth", 10 );
  80. this->windowSize = conf->gI ( section, "window_size", 15 );
  81. this->featsPerSplit = conf->gI ( section, "feats_per_split", 200 );
  82. this->useShannonEntropy = conf->gB ( section, "use_shannon_entropy", true );
  83. this->nbTrees = conf->gI ( section, "amount_trees", 10 );
  84. this->randomTests = conf->gI ( section, "random_tests", 10 );
  85. this->useAltTristimulus = conf->gB ( featsec, "use_alt_trist", false );
  86. this->useGradient = conf->gB ( featsec, "use_gradient", true );
  87. this->useWeijer = conf->gB ( featsec, "use_weijer", true );
  88. this->useAdditionalLayer = conf->gB ( featsec, "use_additional_layer", false );
  89. this->useHoiemFeatures = conf->gB ( featsec, "use_hoiem_features", false );
  90. this->useCategorization = conf->gB ( section, "use_categorization", false );
  91. this->cndir = conf->gS ( "SSContextTree", "cndir", "" );
  92. this->saveLoadData = conf->gB ( "debug", "save_load_data", false );
  93. this->fileLocation = conf->gS ( "debug", "datafile", "tmp.txt" );
  94. this->pixelWiseLabeling = conf->gB ( section, "pixelWiseLabeling", false );
  95. if (coarseMode)
  96. this->labelIncrement = conf->gI ( section, "label_increment", 6 );
  97. else
  98. this->labelIncrement = 1;
  99. if ( useCategorization && cndir == "" )
  100. this->fasthik = new FPCGPHIK ( conf );
  101. else
  102. this->fasthik = NULL;
  103. if ( useWeijer )
  104. this->lfcw = new LocalFeatureColorWeijer ( conf );
  105. this->classnames = (*classNames);
  106. string forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  107. classnames.getSelection ( forbidden_classes_s, forbidden_classes );
  108. // feature types
  109. this->useFeat0 = conf->gB ( section, "use_feat_0", true); // pixel pair features
  110. this->useFeat1 = conf->gB ( section, "use_feat_1", false); // region feature
  111. this->useFeat2 = conf->gB ( section, "use_feat_2", true); // integral features
  112. this->useFeat3 = conf->gB ( section, "use_feat_3", true); // integral contex features
  113. this->useFeat4 = conf->gB ( section, "use_feat_4", false); // pixel pair context features
  114. string segmentationtype = conf->gS ( section, "segmentation_type", "none" );
  115. if ( segmentationtype == "meanshift" )
  116. this->segmentation = new RSMeanShift ( conf );
  117. else if ( segmentationtype == "felzenszwalb" )
  118. this->segmentation = new RSGraphBased ( conf );
  119. else if ( segmentationtype == "slic" )
  120. this->segmentation = new RSSlic ( conf );
  121. else if ( segmentationtype == "none" )
  122. {
  123. this->segmentation = NULL;
  124. this->pixelWiseLabeling = true;
  125. this->useFeat1 = false;
  126. }
  127. else
  128. throw ( "no valid segmenation_type\n please choose between none, meanshift, slic and felzenszwalb\n" );
  129. if ( useFeat0 )
  130. this->featTypes.push_back(0);
  131. if ( useFeat1 )
  132. this->featTypes.push_back(1);
  133. if ( useFeat2 )
  134. this->featTypes.push_back(2);
  135. if ( useFeat3 )
  136. this->featTypes.push_back(3);
  137. if ( useFeat4 )
  138. this->featTypes.push_back(4);
  139. srand(time(NULL));
  140. this->initOperations();
  141. }
  142. //###################### DESTRUCTORS ##########################//
  143. SemSegContextTree3D::~SemSegContextTree3D()
  144. {
  145. }
  146. //#################### MEMBER FUNCTIONS #######################//
  147. void SemSegContextTree3D::initOperations()
  148. {
  149. this->ops.push_back ( new SimpleOperationPool ( conf ) );
  150. this->ops.push_back ( new RegionOperationPool ( conf ) );
  151. this->ops.push_back ( new RectangleOperationPool ( conf ) );
  152. this->ops.push_back ( new RectangleOperationPool ( conf, true ) );
  153. this->ops.push_back ( new SimpleOperationPool ( conf, true ) );
  154. for ( unsigned short i = 0; i < ops.size(); i++ )
  155. ops[i]->getOperations();
  156. }
  157. double SemSegContextTree3D::getBestSplit (
  158. std::vector<NICE::MultiChannelImage3DT<double> > &feats,
  159. std::vector<NICE::MultiChannelImage3DT<unsigned short int> > &nodeIndices,
  160. const std::vector<NICE::MultiChannelImageT<int> > &labels,
  161. int node,
  162. Operation3D *&splitop,
  163. double &splitval,
  164. const int &tree,
  165. vector<vector<vector<double> > > &regionProbs )
  166. {
  167. Timer t;
  168. t.start();
  169. int imgCount = 0;
  170. try
  171. {
  172. imgCount = ( int ) feats.size();
  173. }
  174. catch ( Exception )
  175. {
  176. cerr << "no features computed?" << endl;
  177. }
  178. double bestig = -numeric_limits< double >::max();
  179. splitop = NULL;
  180. splitval = -1.0;
  181. vector<quadruplet<int,int,int,int> > selFeats;
  182. map<int, int> e;
  183. int featcounter = forest[tree][node].featcounter;
  184. if ( featcounter < minFeats )
  185. {
  186. return 0.0;
  187. }
  188. vector<double> fraction ( a.size(), 0.0 );
  189. for ( uint i = 0; i < fraction.size(); i++ )
  190. {
  191. if ( forbidden_classes.find ( labelmapback[i] ) != forbidden_classes.end() )
  192. fraction[i] = 0;
  193. else
  194. fraction[i] = ( ( double ) maxSamples ) / ( ( double ) featcounter * a[i] * a.size() );
  195. }
  196. featcounter = 0;
  197. for ( int iCounter = 0; iCounter < imgCount; iCounter++ )
  198. {
  199. int xsize = ( int ) nodeIndices[iCounter].width();
  200. int ysize = ( int ) nodeIndices[iCounter].height();
  201. int zsize = ( int ) nodeIndices[iCounter].depth();
  202. for ( int x = 0; x < xsize; x++ )
  203. for ( int y = 0; y < ysize; y++ )
  204. for ( int z = 0; z < zsize; z++ )
  205. {
  206. if ( nodeIndices[iCounter].get ( x, y, z, tree ) == node )
  207. {
  208. int cn = labels[iCounter].get ( x, y, ( uint ) z );
  209. double randD = ( double ) rand() / ( double ) RAND_MAX;
  210. if ( labelmap.find ( cn ) == labelmap.end() )
  211. continue;
  212. if ( randD < fraction[labelmap[cn]] )
  213. {
  214. quadruplet<int,int,int,int> quad( iCounter, x, y, z );
  215. featcounter++;
  216. selFeats.push_back ( quad );
  217. e[cn]++;
  218. }
  219. }
  220. }
  221. }
  222. // global entropy
  223. double globent = 0.0;
  224. for ( map<int, int>::iterator mapit = e.begin() ; mapit != e.end(); mapit++ )
  225. {
  226. double p = ( double ) ( *mapit ).second / ( double ) featcounter;
  227. globent += p * log2 ( p );
  228. }
  229. globent = -globent;
  230. if ( globent < 0.5 )
  231. return 0.0;
  232. // pointers to all randomly chosen features
  233. std::vector<Operation3D*> featsel;
  234. for ( int i = 0; i < featsPerSplit; i++ )
  235. {
  236. int x1, x2, y1, y2, z1, z2, ft;
  237. do
  238. {
  239. ft = featTypes[ (int)(rand() % featTypes.size()) ];
  240. }
  241. while ( channelsPerType[ft].size() == 0 );
  242. /* random window positions */
  243. x1 = ( int ) ( rand() % windowSize ) - windowSize / 2 ;
  244. x2 = ( int ) ( rand() % windowSize ) - windowSize / 2 ;
  245. y1 = ( int ) ( rand() % windowSize ) - windowSize / 2 ;
  246. y2 = ( int ) ( rand() % windowSize ) - windowSize / 2 ;
  247. z1 = ( int ) ( rand() % windowSize ) - windowSize / 2 ;
  248. z2 = ( int ) ( rand() % windowSize ) - windowSize / 2 ;
  249. /* random feature maps (channels) */
  250. int f1, f2;
  251. f1 = ( int ) ( rand() % channelsPerType[ft].size() );
  252. f1 = channelsPerType[ft][f1];
  253. f2 = ( int ) ( rand() % classNames->numClasses() );
  254. /* random extraction method (operation) */
  255. int o = ( int ) ( rand() % ops[ft]->pool.size() );
  256. Operation3D *op = ops[ft]->pool[o]->clone();
  257. op->set ( x1, y1, z1, x2, y2, z2, f1, f2, ft );
  258. op->setWSize( windowSize );
  259. featsel.push_back ( op );
  260. }
  261. // do actual split tests
  262. for ( int f = 0; f < featsPerSplit; f++ )
  263. {
  264. double l_bestig = -numeric_limits< double >::max();
  265. double l_splitval = -1.0;
  266. vector<double> vals;
  267. double maxval = -numeric_limits<double>::max();
  268. double minval = numeric_limits<double>::max();
  269. int counter = 0;
  270. for ( vector<quadruplet<int,int,int,int> >::const_iterator it = selFeats.begin();
  271. it != selFeats.end(); it++ )
  272. {
  273. Features feat;
  274. feat.feats = &feats[ ( *it ).first ];
  275. feat.rProbs = &regionProbs[ ( *it ).first ];
  276. assert ( forest.size() > ( uint ) tree );
  277. assert ( forest[tree][0].dist.size() > 0 );
  278. double val = 0.0;
  279. val = featsel[f]->getVal ( feat, ( *it ).second, ( *it ).third, ( *it ).fourth );
  280. if ( !isfinite ( val ) )
  281. {
  282. #ifdef DEBUG
  283. cerr << "feat " << feat.feats->width() << " " << feat.feats->height() << " " << feat.feats->depth() << endl;
  284. cerr << "non finite value " << val << " for " << featsel[f]->writeInfos() << endl << (*it).second << " " << (*it).third << " " << (*it).fourth << endl;
  285. #endif
  286. val = 0.0;
  287. }
  288. vals.push_back ( val );
  289. maxval = std::max ( val, maxval );
  290. minval = std::min ( val, minval );
  291. }
  292. if ( minval == maxval )
  293. continue;
  294. // split values
  295. for ( int run = 0 ; run < randomTests; run++ )
  296. {
  297. // choose threshold randomly
  298. double sval = 0.0;
  299. sval = ( (double) rand() / (double) RAND_MAX*(maxval-minval) ) + minval;
  300. map<int, int> eL, eR;
  301. int counterL = 0, counterR = 0;
  302. counter = 0;
  303. for ( vector<quadruplet<int,int,int,int> >::const_iterator it2 = selFeats.begin();
  304. it2 != selFeats.end(); it2++, counter++ )
  305. {
  306. int cn = labels[ ( *it2 ).first ].get ( ( *it2 ).second, ( *it2 ).third, ( *it2 ).fourth );
  307. //cout << "vals[counter2] " << vals[counter2] << " val: " << val << endl;
  308. if ( vals[counter] < sval )
  309. {
  310. //left entropie:
  311. eL[cn] = eL[cn] + 1;
  312. counterL++;
  313. }
  314. else
  315. {
  316. //right entropie:
  317. eR[cn] = eR[cn] + 1;
  318. counterR++;
  319. }
  320. }
  321. double leftent = 0.0;
  322. for ( map<int, int>::iterator mapit = eL.begin() ; mapit != eL.end(); mapit++ )
  323. {
  324. double p = ( double ) ( *mapit ).second / ( double ) counterL;
  325. leftent -= p * log2 ( p );
  326. }
  327. double rightent = 0.0;
  328. for ( map<int, int>::iterator mapit = eR.begin() ; mapit != eR.end(); mapit++ )
  329. {
  330. double p = ( double ) ( *mapit ).second / ( double ) counterR;
  331. rightent -= p * log2 ( p );
  332. }
  333. //cout << "rightent: " << rightent << " leftent: " << leftent << endl;
  334. double pl = ( double ) counterL / ( double ) ( counterL + counterR );
  335. //information gain
  336. double ig = globent - ( 1.0 - pl ) * rightent - pl * leftent;
  337. //double ig = globent - rightent - leftent;
  338. if ( useShannonEntropy )
  339. {
  340. double esplit = - ( pl * log ( pl ) + ( 1 - pl ) * log ( 1 - pl ) );
  341. ig = 2 * ig / ( globent + esplit );
  342. }
  343. if ( ig > l_bestig )
  344. {
  345. l_bestig = ig;
  346. l_splitval = sval;
  347. }
  348. }
  349. if ( l_bestig > bestig )
  350. {
  351. bestig = l_bestig;
  352. splitop = featsel[f];
  353. splitval = l_splitval;
  354. }
  355. }
  356. #ifdef DEBUG
  357. cout << "globent: " << globent << " bestig " << bestig << " splitval: " << splitval << endl;
  358. #endif
  359. return bestig;
  360. }
  361. inline double SemSegContextTree3D::getMeanProb (
  362. const int &x,
  363. const int &y,
  364. const int &z,
  365. const int &channel,
  366. const MultiChannelImage3DT<unsigned short int> &nodeIndices )
  367. {
  368. double val = 0.0;
  369. for ( int tree = 0; tree < nbTrees; tree++ )
  370. {
  371. val += forest[tree][nodeIndices.get ( x,y,z,tree ) ].dist[channel];
  372. }
  373. return val / ( double ) nbTrees;
  374. }
  375. void SemSegContextTree3D::updateProbabilityMaps (
  376. const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices,
  377. NICE::MultiChannelImage3DT<double> &feats,
  378. int firstChannel )
  379. {
  380. int xsize = feats.width();
  381. int ysize = feats.height();
  382. int zsize = feats.depth();
  383. int classes = ( int ) labelmap.size();
  384. // integral images for context channels (probability maps for each class)
  385. #pragma omp parallel for
  386. for ( int c = 0; c < classes; c++ )
  387. {
  388. for ( int z = 0; z < zsize; z++ )
  389. for ( int y = 0; y < ysize; y++ )
  390. for ( int x = 0; x < xsize; x++ )
  391. {
  392. double val = getMeanProb ( x, y, z, c, nodeIndices );
  393. if (useFeat3 || useFeat4)
  394. feats ( x, y, z, firstChannel + c ) = val;
  395. }
  396. feats.calcIntegral ( firstChannel + c );
  397. }
  398. }
  399. inline double computeWeight ( const int &d, const int &dim )
  400. {
  401. if (d == 0)
  402. return 0.0;
  403. else
  404. return 1.0 / ( pow ( 2, ( double ) ( dim - d + 1 ) ) );
  405. }
  406. void SemSegContextTree3D::train ( const MultiDataset *md )
  407. {
  408. const LabeledSet *trainp = ( *md ) ["train"];
  409. if ( saveLoadData )
  410. {
  411. if ( FileMgt::fileExists ( fileLocation ) )
  412. read ( fileLocation );
  413. else
  414. {
  415. train ( trainp );
  416. write ( fileLocation );
  417. }
  418. }
  419. else
  420. {
  421. train ( trainp );
  422. }
  423. }
  424. void SemSegContextTree3D::train ( const LabeledSet * trainp )
  425. {
  426. int shortsize = numeric_limits<short>::max();
  427. Timer timer;
  428. timer.start();
  429. vector<int> zsizeVec;
  430. SemSegTools::getDepthVector ( trainp, zsizeVec, run3Dseg );
  431. //FIXME: memory usage
  432. vector<MultiChannelImage3DT<double> > allfeats; // Feature Werte
  433. vector<MultiChannelImage3DT<unsigned short int> > nodeIndices; // Zuordnung Knoten/Baum für jeden Pixel
  434. vector<MultiChannelImageT<int> > labels;
  435. // für externen Klassifikator
  436. vector<SparseVector*> globalCategorFeats;
  437. vector<map<int,int> > classesPerImage;
  438. vector<vector<int> > rSize; // anzahl der pixel je region
  439. vector<int> amountRegionpI; // ANZAHL der regionen pro bild (von unsupervised segmentation)
  440. int imgCounter = 0;
  441. int amountPixels = 0;
  442. // How many channels of non-integral type do we have?
  443. if ( imagetype == IMAGETYPE_RGB )
  444. rawChannels = 3;
  445. else
  446. rawChannels = 1;
  447. if ( useGradient )
  448. {
  449. if ( run3Dseg )
  450. rawChannels *= 4; // gx, gy, gz
  451. else
  452. rawChannels *= 3; // gx, gy
  453. }
  454. if ( useWeijer ) // Weijer Colornames
  455. rawChannels += 11;
  456. if ( useHoiemFeatures ) // geometrische Kontextmerkmale
  457. rawChannels += 8;
  458. if ( useAdditionalLayer ) // beliebige Merkmale in extra Bilddateien
  459. rawChannels += 1;
  460. ///////////////////////////// read input data /////////////////////////////////
  461. ///////////////////////////////////////////////////////////////////////////////
  462. int depthCount = 0;
  463. vector< string > filelist;
  464. NICE::MultiChannelImageT<int> pixelLabels;
  465. std::map<int, bool> labelExist;
  466. for (LabeledSet::const_iterator it = trainp->begin(); it != trainp->end(); it++)
  467. {
  468. for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
  469. jt != it->second.end(); jt++)
  470. {
  471. int classno = it->first;
  472. ImageInfo & info = *(*jt);
  473. std::string file = info.img();
  474. filelist.push_back ( file );
  475. depthCount++;
  476. const LocalizationResult *locResult = info.localization();
  477. // getting groundtruth
  478. NICE::ImageT<int> pL;
  479. pL.resize ( locResult->xsize, locResult->ysize );
  480. pL.set ( 0 );
  481. locResult->calcLabeledImage ( pL, ( *classNames ).getBackgroundClass() );
  482. pixelLabels.addChannel ( pL );
  483. if ( locResult->size() <= 0 )
  484. {
  485. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  486. file.c_str() );
  487. continue;
  488. }
  489. fprintf ( stderr, "SSContext: Collecting pixel examples from localization info: %s\n", file.c_str() );
  490. int depthBoundary = 0;
  491. if ( run3Dseg )
  492. {
  493. depthBoundary = zsizeVec[imgCounter];
  494. }
  495. if ( depthCount < depthBoundary ) continue;
  496. // all image slices collected -> make a 3d image
  497. NICE::MultiChannelImage3DT<double> imgData;
  498. make3DImage ( filelist, imgData );
  499. int xsize = imgData.width();
  500. int ysize = imgData.height();
  501. int zsize = imgData.depth();
  502. amountPixels += xsize * ysize * zsize;
  503. MultiChannelImageT<int> tmpMat ( xsize, ysize, ( uint ) zsize );
  504. labels.push_back ( tmpMat );
  505. nodeIndices.push_back ( MultiChannelImage3DT<unsigned short int> ( xsize, ysize, zsize, nbTrees ) );
  506. nodeIndices[imgCounter].setAll ( 0 );
  507. int amountRegions;
  508. // convert color to L*a*b, add selected feature channels
  509. addFeatureMaps ( imgData, filelist, amountRegions );
  510. allfeats.push_back(imgData);
  511. if ( useFeat1 )
  512. {
  513. amountRegionpI.push_back ( amountRegions );
  514. rSize.push_back ( vector<int> ( amountRegions, 0 ) );
  515. }
  516. if ( useCategorization )
  517. {
  518. globalCategorFeats.push_back ( new SparseVector() );
  519. classesPerImage.push_back ( map<int,int>() );
  520. }
  521. for ( int x = 0; x < xsize; x++ )
  522. for ( int y = 0; y < ysize; y++ )
  523. for ( int z = 0; z < zsize; z++ )
  524. {
  525. if ( useFeat1 )
  526. rSize[imgCounter][allfeats[imgCounter] ( x, y, z, rawChannels ) ]++;
  527. if ( run3Dseg )
  528. classno = pixelLabels ( x, y, ( uint ) z );
  529. else
  530. classno = pL.getPixelQuick ( x,y );
  531. labels[imgCounter].set ( x, y, classno, ( uint ) z );
  532. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  533. continue;
  534. labelExist[classno] = true;
  535. if ( useCategorization )
  536. classesPerImage[imgCounter][classno] = 1;
  537. }
  538. filelist.clear();
  539. pixelLabels.reInit ( 0,0,0 );
  540. depthCount = 0;
  541. imgCounter++;
  542. }
  543. }
  544. int classes = 0;
  545. for ( map<int, bool>::const_iterator mapit = labelExist.begin();
  546. mapit != labelExist.end(); mapit++ )
  547. {
  548. labelmap[mapit->first] = classes;
  549. labelmapback[classes] = mapit->first;
  550. classes++;
  551. }
  552. ////////////////////////// channel type configuration /////////////////////////
  553. ///////////////////////////////////////////////////////////////////////////////
  554. unsigned char shift = 0;
  555. std::vector<int> rawChannelsIdx, numClassesIdx;
  556. int idx = 0;
  557. for ( int i = 0; i < rawChannels; i++, idx++ )
  558. rawChannelsIdx.push_back ( idx );
  559. for ( int i = 0; i < classes; i++, idx++ )
  560. numClassesIdx.push_back ( idx );
  561. /** Type 0: single pixel & pixel-comparison features on gray value channels */
  562. // actual values derived from integral values
  563. channelsPerType.push_back ( rawChannelsIdx );
  564. /** Type 1: region channel with unsupervised segmentation */
  565. if ( useFeat1 )
  566. {
  567. channelsPerType.push_back ( vector<int>(1, rawChannels) );
  568. shift = 1;
  569. }
  570. else
  571. channelsPerType.push_back ( vector<int>() );
  572. /** Type 2: rectangular and Haar-like features on gray value integral channels */
  573. if ( useFeat2 )
  574. channelsPerType.push_back ( rawChannelsIdx );
  575. else
  576. channelsPerType.push_back ( vector<int>() );
  577. /** Type 3: type 2 features on integral probability channels (context) */
  578. if ( useFeat3 )
  579. channelsPerType.push_back ( numClassesIdx );
  580. else
  581. channelsPerType.push_back ( vector<int>() );
  582. /** Type 4: type 0 features on probability channels (context) */
  583. // Type 4 channels are now INTEGRAL
  584. // This remains for compatibility reasons.
  585. if ( useFeat4 )
  586. channelsPerType.push_back ( numClassesIdx );
  587. else
  588. channelsPerType.push_back ( vector<int>() );
  589. ///////////////////////////////////////////////////////////////////////////////
  590. ///////////////////////////////////////////////////////////////////////////////
  591. vector<vector<vector<double> > > regionProbs;
  592. if ( useFeat1 )
  593. {
  594. for ( int i = 0; i < imgCounter; i++ )
  595. {
  596. regionProbs.push_back ( vector<vector<double> > ( amountRegionpI[i], vector<double> ( classes, 0.0 ) ) );
  597. }
  598. }
  599. //balancing
  600. a = vector<double> ( classes, 0.0 );
  601. int selectionCounter = 0;
  602. for ( int iCounter = 0; iCounter < imgCounter; iCounter++ )
  603. {
  604. int xsize = ( int ) nodeIndices[iCounter].width();
  605. int ysize = ( int ) nodeIndices[iCounter].height();
  606. int zsize = ( int ) nodeIndices[iCounter].depth();
  607. for ( int x = 0; x < xsize; x++ )
  608. for ( int y = 0; y < ysize; y++ )
  609. for ( int z = 0; z < zsize; z++ )
  610. {
  611. int cn = labels[iCounter] ( x, y, ( uint ) z );
  612. if ( labelmap.find ( cn ) == labelmap.end() )
  613. continue;
  614. a[labelmap[cn]] ++;
  615. selectionCounter++;
  616. }
  617. }
  618. for ( int i = 0; i < ( int ) a.size(); i++ )
  619. a[i] /= ( double ) selectionCounter;
  620. #ifdef VERBOSE
  621. cout << "\nDistribution:" << endl;
  622. for ( int i = 0; i < ( int ) a.size(); i++ )
  623. cout << "class '" << classNames->code(labelmapback[i]) << "': "
  624. << a[i] << endl;
  625. #endif
  626. depth = 0;
  627. uniquenumber = 0;
  628. //initialize random forest
  629. for ( int t = 0; t < nbTrees; t++ )
  630. {
  631. vector<TreeNode> singletree;
  632. singletree.push_back ( TreeNode() );
  633. singletree[0].dist = vector<double> ( classes, 0.0 );
  634. singletree[0].depth = depth;
  635. singletree[0].featcounter = amountPixels;
  636. singletree[0].nodeNumber = uniquenumber;
  637. uniquenumber++;
  638. forest.push_back ( singletree );
  639. }
  640. vector<int> startnode ( nbTrees, 0 );
  641. bool noNewSplit = false;
  642. timer.stop();
  643. cout << "\nTime for Pre-Processing: " << timer.getLastAbsolute() << " seconds\n" << endl;
  644. //////////////////////////// train the classifier ///////////////////////////
  645. /////////////////////////////////////////////////////////////////////////////
  646. timer.start();
  647. while ( !noNewSplit && (depth < maxDepth) )
  648. {
  649. depth++;
  650. #ifdef DEBUG
  651. cout << "depth: " << depth << endl;
  652. #endif
  653. noNewSplit = true;
  654. vector<MultiChannelImage3DT<unsigned short int> > lastNodeIndices = nodeIndices;
  655. vector<vector<vector<double> > > lastRegionProbs = regionProbs;
  656. if ( useFeat1 )
  657. for ( int i = 0; i < imgCounter; i++ )
  658. {
  659. int numRegions = (int) regionProbs[i].size();
  660. for ( int r = 0; r < numRegions; r++ )
  661. for ( int c = 0; c < classes; c++ )
  662. regionProbs[i][r][c] = 0.0;
  663. }
  664. // initialize & update context channels
  665. for ( int i = 0; i < imgCounter; i++)
  666. if ( useFeat3 || useFeat4 )
  667. this->updateProbabilityMaps ( nodeIndices[i], allfeats[i], rawChannels + shift );
  668. #ifdef VERBOSE
  669. Timer timerDepth;
  670. timerDepth.start();
  671. #endif
  672. double weight = computeWeight ( depth, maxDepth )
  673. - computeWeight ( depth - 1, maxDepth );
  674. #pragma omp parallel for
  675. // for each tree
  676. for ( int tree = 0; tree < nbTrees; tree++ )
  677. {
  678. const int t = ( int ) forest[tree].size();
  679. const int s = startnode[tree];
  680. startnode[tree] = t;
  681. double bestig;
  682. // for each node
  683. for ( int node = s; node < t; node++ )
  684. {
  685. if ( !forest[tree][node].isleaf && forest[tree][node].left < 0 )
  686. {
  687. // find best split
  688. Operation3D *splitfeat = NULL;
  689. double splitval;
  690. bestig = getBestSplit ( allfeats, lastNodeIndices, labels, node,
  691. splitfeat, splitval, tree, lastRegionProbs );
  692. forest[tree][node].feat = splitfeat;
  693. forest[tree][node].decision = splitval;
  694. // split the node
  695. if ( splitfeat != NULL )
  696. {
  697. noNewSplit = false;
  698. int left;
  699. #pragma omp critical
  700. {
  701. left = forest[tree].size();
  702. forest[tree].push_back ( TreeNode() );
  703. forest[tree].push_back ( TreeNode() );
  704. }
  705. int right = left + 1;
  706. forest[tree][node].left = left;
  707. forest[tree][node].right = right;
  708. forest[tree][left].init( depth, classes, uniquenumber);
  709. int leftu = uniquenumber;
  710. uniquenumber++;
  711. forest[tree][right].init( depth, classes, uniquenumber);
  712. int rightu = uniquenumber;
  713. uniquenumber++;
  714. #pragma omp parallel for
  715. for ( int i = 0; i < imgCounter; i++ )
  716. {
  717. int xsize = nodeIndices[i].width();
  718. int ysize = nodeIndices[i].height();
  719. int zsize = nodeIndices[i].depth();
  720. for ( int x = 0; x < xsize; x++ )
  721. {
  722. for ( int y = 0; y < ysize; y++ )
  723. {
  724. for ( int z = 0; z < zsize; z++ )
  725. {
  726. if ( nodeIndices[i].get ( x, y, z, tree ) == node )
  727. {
  728. // get feature value
  729. Features feat;
  730. feat.feats = &allfeats[i];
  731. feat.rProbs = &lastRegionProbs[i];
  732. double val = 0.0;
  733. val = splitfeat->getVal ( feat, x, y, z );
  734. if ( !isfinite ( val ) ) val = 0.0;
  735. #pragma omp critical
  736. {
  737. int curLabel = labels[i] ( x, y, ( uint ) z );
  738. // traverse to left child
  739. if ( val < splitval )
  740. {
  741. nodeIndices[i].set ( x, y, z, left, tree );
  742. if ( labelmap.find ( curLabel ) != labelmap.end() )
  743. forest[tree][left].dist[labelmap[curLabel]]++;
  744. forest[tree][left].featcounter++;
  745. if ( useCategorization && leftu < shortsize )
  746. ( *globalCategorFeats[i] ) [leftu]+=weight;
  747. }
  748. // traverse to right child
  749. else
  750. {
  751. nodeIndices[i].set ( x, y, z, right, tree );
  752. if ( labelmap.find ( curLabel ) != labelmap.end() )
  753. forest[tree][right].dist[labelmap[curLabel]]++;
  754. forest[tree][right].featcounter++;
  755. if ( useCategorization && rightu < shortsize )
  756. ( *globalCategorFeats[i] ) [rightu]+=weight;
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }
  764. // normalize distributions in child leaves
  765. double lcounter = 0.0, rcounter = 0.0;
  766. for ( int c = 0; c < (int)forest[tree][left].dist.size(); c++ )
  767. {
  768. if ( forbidden_classes.find ( labelmapback[c] ) != forbidden_classes.end() )
  769. {
  770. forest[tree][left].dist[c] = 0;
  771. forest[tree][right].dist[c] = 0;
  772. }
  773. else
  774. {
  775. forest[tree][left].dist[c] /= a[c];
  776. lcounter += forest[tree][left].dist[c];
  777. forest[tree][right].dist[c] /= a[c];
  778. rcounter += forest[tree][right].dist[c];
  779. }
  780. }
  781. assert ( lcounter > 0 && rcounter > 0 );
  782. for ( int c = 0; c < classes; c++ )
  783. {
  784. forest[tree][left].dist[c] /= lcounter;
  785. forest[tree][right].dist[c] /= rcounter;
  786. }
  787. }
  788. else
  789. {
  790. forest[tree][node].isleaf = true;
  791. }
  792. }
  793. }
  794. }
  795. if ( useFeat1 )
  796. {
  797. for ( int i = 0; i < imgCounter; i++ )
  798. {
  799. int xsize = nodeIndices[i].width();
  800. int ysize = nodeIndices[i].height();
  801. int zsize = nodeIndices[i].depth();
  802. #pragma omp parallel for
  803. // set region probability distribution
  804. for ( int x = 0; x < xsize; x++ )
  805. {
  806. for ( int y = 0; y < ysize; y++ )
  807. {
  808. for ( int z = 0; z < zsize; z++ )
  809. {
  810. for ( int tree = 0; tree < nbTrees; tree++ )
  811. {
  812. int node = nodeIndices[i].get ( x, y, z, tree );
  813. for ( int c = 0; c < classes; c++ )
  814. {
  815. int r = (int) ( allfeats[i] ( x, y, z, rawChannels ) );
  816. regionProbs[i][r][c] += forest[tree][node].dist[c];
  817. }
  818. }
  819. }
  820. }
  821. }
  822. // normalize distribution
  823. int numRegions = (int) regionProbs[i].size();
  824. for ( int r = 0; r < numRegions; r++ )
  825. {
  826. for ( int c = 0; c < classes; c++ )
  827. {
  828. regionProbs[i][r][c] /= ( double ) ( rSize[i][r] );
  829. }
  830. }
  831. }
  832. }
  833. if ( firstiteration ) firstiteration = false;
  834. #ifdef VERBOSE
  835. timerDepth.stop();
  836. cout << "Depth " << depth << ": " << timerDepth.getLastAbsolute() << " seconds" <<endl;
  837. #endif
  838. lastNodeIndices.clear();
  839. lastRegionProbs.clear();
  840. }
  841. timer.stop();
  842. cout << "Time for Learning: " << timer.getLastAbsolute() << " seconds\n" << endl;
  843. //////////////////////// classification using HIK ///////////////////////////
  844. /////////////////////////////////////////////////////////////////////////////
  845. if ( useCategorization && fasthik != NULL )
  846. {
  847. timer.start();
  848. uniquenumber = std::min ( shortsize, uniquenumber );
  849. for ( uint i = 0; i < globalCategorFeats.size(); i++ )
  850. {
  851. globalCategorFeats[i]->setDim ( uniquenumber );
  852. globalCategorFeats[i]->normalize();
  853. }
  854. std::map< uint, NICE::Vector > ys;
  855. uint cCounter = 0;
  856. for ( std::map<int,int>::const_iterator it = labelmap.begin();
  857. it != labelmap.end(); it++, cCounter++ )
  858. {
  859. ys[cCounter] = NICE::Vector ( globalCategorFeats.size() );
  860. for ( int i = 0; i < imgCounter; i++ )
  861. {
  862. if ( classesPerImage[i].find ( it->first ) != classesPerImage[i].end() )
  863. {
  864. ys[cCounter][i] = 1;
  865. }
  866. else
  867. {
  868. ys[cCounter][i] = -1;
  869. }
  870. }
  871. }
  872. fasthik->train( reinterpret_cast<vector<const NICE::SparseVector *>&>(globalCategorFeats), ys);
  873. timer.stop();
  874. cerr << "Time for Categorization: " << timer.getLastAbsolute() << " seconds\n" << endl;
  875. }
  876. #ifdef VERBOSE
  877. cout << "\nFEATURE USAGE" << endl;
  878. cout << "#############\n" << endl;
  879. // amount of used features per feature type
  880. std::map<int, int> featTypeCounter;
  881. for ( int tree = 0; tree < nbTrees; tree++ )
  882. {
  883. int t = ( int ) forest[tree].size();
  884. for ( int node = 0; node < t; node++ )
  885. {
  886. if ( !forest[tree][node].isleaf && forest[tree][node].left != -1 )
  887. {
  888. featTypeCounter[ forest[tree][node].feat->getFeatType() ] += 1;
  889. }
  890. }
  891. }
  892. cout << "Types:" << endl;
  893. for ( map<int, int>::const_iterator it = featTypeCounter.begin(); it != featTypeCounter.end(); it++ )
  894. cout << it->first << ": " << it->second << endl;
  895. cout << "\nOperations - All:" << endl;
  896. // used operations
  897. vector<int> opOverview ( NBOPERATIONS, 0 );
  898. // relative use of context vs raw features per tree level
  899. vector<vector<double> > contextOverview ( maxDepth, vector<double> ( 2, 0.0 ) );
  900. for ( int tree = 0; tree < nbTrees; tree++ )
  901. {
  902. int t = ( int ) forest[tree].size();
  903. for ( int node = 0; node < t; node++ )
  904. {
  905. #ifdef DEBUG
  906. printf ( "tree[%i]: left: %i, right: %i", node, forest[tree][node].left, forest[tree][node].right );
  907. #endif
  908. if ( !forest[tree][node].isleaf && forest[tree][node].left != -1 )
  909. {
  910. #ifdef DEBUG
  911. cout << forest[tree][node].feat->writeInfos() << endl;
  912. #endif
  913. opOverview[ forest[tree][node].feat->getOps() ]++;
  914. contextOverview[forest[tree][node].depth][ ( int ) forest[tree][node].feat->getContext() ]++;
  915. }
  916. #ifdef DEBUG
  917. for ( int d = 0; d < ( int ) forest[tree][node].dist.size(); d++ )
  918. cout << " " << forest[tree][node].dist[d];
  919. cout << endl;
  920. #endif
  921. }
  922. }
  923. // amount of used features per operation type
  924. cout << "\nOperations - Summary:" << endl;
  925. for ( int t = 0; t < ( int ) opOverview.size(); t++ )
  926. {
  927. cout << "Ops " << t << ": " << opOverview[ t ] << endl;
  928. }
  929. // ratio of used context features per depth level
  930. cout << "\nContext-Ratio:" << endl;
  931. for ( int d = 0; d < maxDepth; d++ )
  932. {
  933. double sum = contextOverview[d][0] + contextOverview[d][1];
  934. if ( sum == 0 )
  935. sum = 1;
  936. contextOverview[d][0] /= sum;
  937. contextOverview[d][1] /= sum;
  938. cout << "Depth [" << d+1 << "] Normal: " << contextOverview[d][0] << " Context: " << contextOverview[d][1] << endl;
  939. }
  940. #endif
  941. }
  942. void SemSegContextTree3D::addFeatureMaps (
  943. NICE::MultiChannelImage3DT<double> &imgData,
  944. const vector<string> &filelist,
  945. int &amountRegions )
  946. {
  947. int xsize = imgData.width();
  948. int ysize = imgData.height();
  949. int zsize = imgData.depth();
  950. amountRegions = 0;
  951. // RGB to Lab
  952. if ( imagetype == IMAGETYPE_RGB )
  953. {
  954. for ( int z = 0; z < zsize; z++ )
  955. for ( int y = 0; y < ysize; y++ )
  956. for ( int x = 0; x < xsize; x++ )
  957. {
  958. double R, G, B, X, Y, Z, L, a, b;
  959. R = ( double )imgData.get( x, y, z, 0 ) / 255.0;
  960. G = ( double )imgData.get( x, y, z, 1 ) / 255.0;
  961. B = ( double )imgData.get( x, y, z, 2 ) / 255.0;
  962. if ( useAltTristimulus )
  963. {
  964. ColorConversion::ccRGBtoXYZ( R, G, B, &X, &Y, &Z, 4 );
  965. ColorConversion::ccXYZtoCIE_Lab( X, Y, Z, &L, &a, &b, 4 );
  966. }
  967. else
  968. {
  969. ColorConversion::ccRGBtoXYZ( R, G, B, &X, &Y, &Z, 0 );
  970. ColorConversion::ccXYZtoCIE_Lab( X, Y, Z, &L, &a, &b, 0 );
  971. }
  972. imgData.set( x, y, z, L, 0 );
  973. imgData.set( x, y, z, a, 1 );
  974. imgData.set( x, y, z, b, 2 );
  975. }
  976. }
  977. else
  978. // normalize gray values to [0,1]
  979. {
  980. for ( int z = 0; z < zsize; z++ )
  981. for ( int y = 0; y < ysize; y++ )
  982. for ( int x = 0; x < xsize; x++ )
  983. {
  984. double val = imgData.get( x, y, z, 0 ) / 255.0;
  985. imgData.set( x, y, z, val, 0 );
  986. }
  987. }
  988. // Gradient layers
  989. if ( useGradient )
  990. {
  991. int currentsize = imgData.channels();
  992. imgData.addChannel ( 2*currentsize );
  993. // gradients for X and Y
  994. for ( int z = 0; z < zsize; z++ )
  995. for ( int c = 0; c < currentsize; c++ )
  996. {
  997. ImageT<double> tmp = imgData.getChannelT(z, c);
  998. ImageT<double> sobX( xsize, ysize );
  999. ImageT<double> sobY( xsize, ysize );
  1000. NICE::FilterT<double, double, double>::sobelX ( tmp, sobX );
  1001. NICE::FilterT<double, double, double>::sobelY ( tmp, sobY );
  1002. for ( int y = 0; y < ysize; y++ )
  1003. for ( int x = 0; x < xsize; x++ )
  1004. {
  1005. imgData.set( x, y, z, sobX.getPixelQuick(x,y), c+currentsize );
  1006. imgData.set( x, y, z, sobY.getPixelQuick(x,y), c+(currentsize*2) );
  1007. }
  1008. }
  1009. // gradients for Z
  1010. if ( run3Dseg )
  1011. {
  1012. imgData.addChannel ( currentsize );
  1013. for ( int x = 0; x < xsize; x++ )
  1014. for ( int c = 0; c < currentsize; c++ )
  1015. {
  1016. ImageT<double> tmp = imgData.getXSlice(x, c);
  1017. ImageT<double> sobZ( zsize, ysize );
  1018. NICE::FilterT<double, double, double>::sobelX ( tmp, sobZ );
  1019. for ( int y = 0; y < ysize; y++ )
  1020. for ( int z = 0; z < zsize; z++ )
  1021. imgData.set( x, y, z, sobZ.getPixelQuick(z,y), c+(currentsize*3) );
  1022. }
  1023. }
  1024. }
  1025. // Weijer color names
  1026. if ( useWeijer )
  1027. {
  1028. if ( imagetype == IMAGETYPE_RGB )
  1029. {
  1030. int currentsize = imgData.channels();
  1031. imgData.addChannel ( 11 );
  1032. for ( int z = 0; z < zsize; z++ )
  1033. {
  1034. NICE::ColorImage img = imgData.getColor ( z );
  1035. NICE::MultiChannelImageT<double> cfeats;
  1036. lfcw->getFeats ( img, cfeats );
  1037. for ( int c = 0; c < cfeats.channels(); c++)
  1038. for ( int y = 0; y < ysize; y++ )
  1039. for ( int x = 0; x < xsize; x++ )
  1040. imgData.set(x, y, z, cfeats.get(x,y,(uint)c), c+currentsize);
  1041. }
  1042. }
  1043. else
  1044. {
  1045. cerr << "Can't compute weijer features of a grayscale image." << endl;
  1046. }
  1047. }
  1048. // arbitrary additional layer as image
  1049. if ( useAdditionalLayer )
  1050. {
  1051. int currentsize = imgData.channels();
  1052. imgData.addChannel ( 1 );
  1053. for ( int z = 0; z < zsize; z++ )
  1054. {
  1055. vector<string> list;
  1056. StringTools::split ( filelist[z], '/', list );
  1057. string layerPath = StringTools::trim ( filelist[z], list.back() ) + "addlayer/" + list.back();
  1058. NICE::Image layer ( layerPath );
  1059. for ( int y = 0; y < ysize; y++ )
  1060. for ( int x = 0; x < xsize; x++ )
  1061. imgData.set(x, y, z, layer.getPixelQuick(x,y), currentsize);
  1062. }
  1063. }
  1064. // read the geometric cues produced by Hoiem et al.
  1065. if ( useHoiemFeatures )
  1066. {
  1067. string hoiemDirectory = conf->gS ( "Features", "hoiem_directory" );
  1068. // we could also give the following set as a config option
  1069. string hoiemClasses_s = "sky 000 090-045 090-090 090-135 090 090-por 090-sol";
  1070. vector<string> hoiemClasses;
  1071. StringTools::split ( hoiemClasses_s, ' ', hoiemClasses );
  1072. int currentsize = imgData.channels();
  1073. imgData.addChannel ( hoiemClasses.size() );
  1074. for ( int z = 0; z < zsize; z++ )
  1075. {
  1076. FileName fn ( filelist[z] );
  1077. fn.removeExtension();
  1078. FileName fnBase = fn.extractFileName();
  1079. for ( vector<string>::const_iterator i = hoiemClasses.begin(); i != hoiemClasses.end(); i++, currentsize++ )
  1080. {
  1081. string hoiemClass = *i;
  1082. FileName fnConfidenceImage ( hoiemDirectory + fnBase.str() + "_c_" + hoiemClass + ".png" );
  1083. if ( ! fnConfidenceImage.fileExists() )
  1084. {
  1085. fthrow ( Exception, "Unable to read the Hoiem geometric confidence image: " << fnConfidenceImage.str() << " (original image is " << filelist[z] << ")" );
  1086. }
  1087. else
  1088. {
  1089. Image confidenceImage ( fnConfidenceImage.str() );
  1090. if ( confidenceImage.width() != xsize || confidenceImage.height() != ysize )
  1091. {
  1092. fthrow ( Exception, "The size of the geometric confidence image does not match with the original image size: " << fnConfidenceImage.str() );
  1093. }
  1094. // copy standard image to double image
  1095. for ( int y = 0 ; y < confidenceImage.height(); y++ )
  1096. for ( int x = 0 ; x < confidenceImage.width(); x++ )
  1097. imgData ( x, y, z, currentsize ) = ( double ) confidenceImage ( x, y );
  1098. currentsize++;
  1099. }
  1100. }
  1101. }
  1102. }
  1103. // region feature (unsupervised segmentation)
  1104. int shift = 0;
  1105. if ( useFeat1 )
  1106. {
  1107. shift = 1;
  1108. MultiChannelImageT<int> regions;
  1109. regions.reInit( xsize, ysize, zsize );
  1110. amountRegions = segmentation->segRegions ( imgData, regions, imagetype );
  1111. int currentsize = imgData.channels();
  1112. imgData.addChannel ( 1 );
  1113. for ( int z = 0; z < ( int ) regions.channels(); z++ )
  1114. for ( int y = 0; y < regions.height(); y++ )
  1115. for ( int x = 0; x < regions.width(); x++ )
  1116. imgData.set ( x, y, z, regions ( x, y, ( uint ) z ), currentsize );
  1117. }
  1118. // convert raw channels to intergal channels
  1119. #pragma omp parallel for
  1120. for ( int i = 0; i < rawChannels; i++ )
  1121. imgData.calcIntegral ( i );
  1122. int classes = classNames->numClasses() - forbidden_classes.size();
  1123. if ( useFeat3 || useFeat4 )
  1124. imgData.addChannel ( classes );
  1125. }
  1126. void SemSegContextTree3D::classify (
  1127. const std::vector<std::string> & filelist,
  1128. NICE::MultiChannelImageT<int> & segresult,
  1129. NICE::MultiChannelImage3DT<double> & probabilities )
  1130. {
  1131. ///////////////////////// build MCI3DT from files ///////////////////////////
  1132. /////////////////////////////////////////////////////////////////////////////
  1133. NICE::MultiChannelImage3DT<double> imgData;
  1134. this->make3DImage( filelist, imgData );
  1135. int xsize = imgData.width();
  1136. int ysize = imgData.height();
  1137. int zsize = imgData.depth();
  1138. ////////////////////////// initialize variables /////////////////////////////
  1139. /////////////////////////////////////////////////////////////////////////////
  1140. firstiteration = true;
  1141. depth = 0;
  1142. // anytime classification ability
  1143. int classificationDepth = conf->gI( "SSContextTree", "classification_depth", maxDepth );
  1144. if (classificationDepth > maxDepth || classificationDepth < 1 )
  1145. classificationDepth = maxDepth;
  1146. Timer timer;
  1147. timer.start();
  1148. // classes occurred during training step
  1149. int classes = labelmapback.size();
  1150. // classes defined in config file
  1151. int numClasses = classNames->numClasses();
  1152. // class probabilities by pixel
  1153. probabilities.reInit ( xsize, ysize, zsize, numClasses );
  1154. probabilities.setAll ( 0 );
  1155. // class probabilities by region
  1156. vector<vector<double> > regionProbs;
  1157. // affiliation: pixel <-> (tree,node)
  1158. MultiChannelImage3DT<unsigned short int> nodeIndices ( xsize, ysize, zsize, nbTrees );
  1159. nodeIndices.setAll ( 0 );
  1160. // for categorization
  1161. SparseVector *globalCategorFeat;
  1162. globalCategorFeat = new SparseVector();
  1163. /////////////////////////// get feature values //////////////////////////////
  1164. /////////////////////////////////////////////////////////////////////////////
  1165. // Basic Features
  1166. int amountRegions;
  1167. addFeatureMaps ( imgData, filelist, amountRegions );
  1168. vector<int> rSize;
  1169. int shift = 0;
  1170. if ( useFeat1 )
  1171. {
  1172. shift = 1;
  1173. regionProbs = vector<vector<double> > ( amountRegions, vector<double> ( classes, 0.0 ) );
  1174. rSize = vector<int> ( amountRegions, 0 );
  1175. for ( int z = 0; z < zsize; z++ )
  1176. {
  1177. for ( int y = 0; y < ysize; y++ )
  1178. {
  1179. for ( int x = 0; x < xsize; x++ )
  1180. {
  1181. rSize[imgData ( x, y, z, rawChannels ) ]++;
  1182. }
  1183. }
  1184. }
  1185. }
  1186. ////////////////// traverse image example through trees /////////////////////
  1187. /////////////////////////////////////////////////////////////////////////////
  1188. bool noNewSplit = false;
  1189. for ( int d = 0; d < classificationDepth && !noNewSplit; d++ )
  1190. {
  1191. depth++;
  1192. vector<vector<double> > lastRegionProbs = regionProbs;
  1193. if ( useFeat1 )
  1194. {
  1195. int numRegions = ( int ) regionProbs.size();
  1196. for ( int r = 0; r < numRegions; r++ )
  1197. for ( int c = 0; c < classes; c++ )
  1198. regionProbs[r][c] = 0.0;
  1199. }
  1200. if ( depth < classificationDepth )
  1201. {
  1202. int firstChannel = rawChannels + shift;
  1203. if ( useFeat3 || useFeat4 )
  1204. this->updateProbabilityMaps ( nodeIndices, imgData, firstChannel );
  1205. }
  1206. double weight = computeWeight ( depth, maxDepth )
  1207. - computeWeight ( depth - 1, maxDepth );
  1208. noNewSplit = true;
  1209. int tree;
  1210. #pragma omp parallel for private(tree)
  1211. for ( tree = 0; tree < nbTrees; tree++ )
  1212. for ( int x = 0; x < xsize; x=x+labelIncrement )
  1213. for ( int y = 0; y < ysize; y=y+labelIncrement )
  1214. for ( int z = 0; z < zsize; z++ )
  1215. {
  1216. int node = nodeIndices.get ( x, y, z, tree );
  1217. if ( forest[tree][node].left > 0 )
  1218. {
  1219. noNewSplit = false;
  1220. Features feat;
  1221. feat.feats = &imgData;
  1222. feat.rProbs = &lastRegionProbs;
  1223. double val = forest[tree][node].feat->getVal ( feat, x, y, z );
  1224. if ( !isfinite ( val ) ) val = 0.0;
  1225. // traverse to left child
  1226. if ( val < forest[tree][node].decision )
  1227. {
  1228. int left = forest[tree][node].left;
  1229. for ( int n = 0; n < labelIncrement; n++ )
  1230. for ( int m = 0; m < labelIncrement; m++ )
  1231. if (x+m < xsize && y+n < ysize)
  1232. nodeIndices.set ( x+m, y+n, z, left, tree );
  1233. #pragma omp critical
  1234. {
  1235. if ( fasthik != NULL
  1236. && useCategorization
  1237. && forest[tree][left].nodeNumber < uniquenumber )
  1238. ( *globalCategorFeat ) [forest[tree][left].nodeNumber] += weight;
  1239. }
  1240. }
  1241. // traverse to right child
  1242. else
  1243. {
  1244. int right = forest[tree][node].right;
  1245. for ( int n = 0; n < labelIncrement; n++ )
  1246. for ( int m = 0; m < labelIncrement; m++ )
  1247. if (x+m < xsize && y+n < ysize)
  1248. nodeIndices.set ( x+m, y+n, z, right, tree );
  1249. #pragma omp critical
  1250. {
  1251. if ( fasthik != NULL
  1252. && useCategorization
  1253. && forest[tree][right].nodeNumber < uniquenumber )
  1254. ( *globalCategorFeat ) [forest[tree][right].nodeNumber] += weight;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. if ( useFeat1 )
  1260. {
  1261. int xsize = nodeIndices.width();
  1262. int ysize = nodeIndices.height();
  1263. int zsize = nodeIndices.depth();
  1264. #pragma omp parallel for
  1265. for ( int x = 0; x < xsize; x++ )
  1266. for ( int y = 0; y < ysize; y++ )
  1267. for ( int z = 0; z < zsize; z++ )
  1268. for ( int tree = 0; tree < nbTrees; tree++ )
  1269. {
  1270. int node = nodeIndices.get ( x, y, z, tree );
  1271. for ( uint c = 0; c < forest[tree][node].dist.size(); c++ )
  1272. {
  1273. int r = (int) imgData ( x, y, z, rawChannels );
  1274. regionProbs[r][c] += forest[tree][node].dist[c];
  1275. }
  1276. }
  1277. int numRegions = (int) regionProbs.size();
  1278. for ( int r = 0; r < numRegions; r++ )
  1279. for ( int c = 0; c < (int) classes; c++ )
  1280. regionProbs[r][c] /= ( double ) ( rSize[r] );
  1281. }
  1282. if ( (depth < classificationDepth) && firstiteration ) firstiteration = false;
  1283. }
  1284. vector<int> classesInImg;
  1285. if ( useCategorization )
  1286. {
  1287. if ( cndir != "" )
  1288. {
  1289. for ( int z = 0; z < zsize; z++ )
  1290. {
  1291. vector< string > list;
  1292. StringTools::split ( filelist[z], '/', list );
  1293. string orgname = list.back();
  1294. ifstream infile ( ( cndir + "/" + orgname + ".dat" ).c_str() );
  1295. while ( !infile.eof() && infile.good() )
  1296. {
  1297. int tmp;
  1298. infile >> tmp;
  1299. assert ( tmp >= 0 && tmp < numClasses );
  1300. classesInImg.push_back ( tmp );
  1301. }
  1302. }
  1303. }
  1304. else
  1305. {
  1306. globalCategorFeat->setDim ( uniquenumber );
  1307. globalCategorFeat->normalize();
  1308. ClassificationResult cr = fasthik->classify( globalCategorFeat);
  1309. for ( uint i = 0; i < ( uint ) classes; i++ )
  1310. {
  1311. cerr << cr.scores[i] << " ";
  1312. if ( cr.scores[i] > 0.0/*-0.3*/ )
  1313. {
  1314. classesInImg.push_back ( i );
  1315. }
  1316. }
  1317. }
  1318. cerr << "amount of classes: " << classes << " used classes: " << classesInImg.size() << endl;
  1319. }
  1320. if ( classesInImg.size() == 0 )
  1321. {
  1322. for ( uint i = 0; i < ( uint ) classes; i++ )
  1323. {
  1324. classesInImg.push_back ( i );
  1325. }
  1326. }
  1327. // final labeling step
  1328. if ( pixelWiseLabeling )
  1329. {
  1330. for ( int x = 0; x < xsize; x++ )
  1331. for ( int y = 0; y < ysize; y++ )
  1332. for ( int z = 0; z < zsize; z++ )
  1333. {
  1334. double maxProb = - numeric_limits<double>::max();
  1335. int maxClass = 0;
  1336. for ( uint c = 0; c < classesInImg.size(); c++ )
  1337. {
  1338. int i = classesInImg[c];
  1339. double curProb = getMeanProb ( x, y, z, i, nodeIndices );
  1340. probabilities.set ( x, y, z, curProb, labelmapback[i] );
  1341. if ( curProb > maxProb )
  1342. {
  1343. maxProb = curProb;
  1344. maxClass = labelmapback[i];
  1345. }
  1346. }
  1347. assert(maxProb <= 1);
  1348. // copy pixel labeling into segresults (output)
  1349. segresult.set ( x, y, maxClass, ( uint ) z );
  1350. }
  1351. #ifdef VISUALIZE
  1352. getProbabilityMap( probabilities );
  1353. #endif
  1354. }
  1355. else
  1356. {
  1357. // labeling by region
  1358. NICE::MultiChannelImageT<int> regions;
  1359. int xsize = imgData.width();
  1360. int ysize = imgData.height();
  1361. int zsize = imgData.depth();
  1362. regions.reInit ( xsize, ysize, zsize );
  1363. if ( useFeat1 )
  1364. {
  1365. for ( int z = 0; z < zsize; z++ )
  1366. for ( int y = 0; y < ysize; y++ )
  1367. for ( int x = 0; x < xsize; x++ )
  1368. regions.set ( x, y, imgData ( x, y, z, rawChannels ), ( uint ) z );
  1369. }
  1370. else
  1371. {
  1372. amountRegions = segmentation->segRegions ( imgData, regions, imagetype );
  1373. #ifdef DEBUG
  1374. for ( unsigned int z = 0; z < ( uint ) zsize; z++ )
  1375. {
  1376. NICE::Matrix regmask;
  1377. NICE::ColorImage colorimg ( xsize, ysize );
  1378. NICE::ColorImage marked ( xsize, ysize );
  1379. regmask.resize ( xsize, ysize );
  1380. for ( int y = 0; y < ysize; y++ )
  1381. {
  1382. for ( int x = 0; x < xsize; x++ )
  1383. {
  1384. regmask ( x,y ) = regions ( x,y,z );
  1385. colorimg.setPixelQuick ( x, y, 0, imgData.get ( x,y,z,0 ) );
  1386. colorimg.setPixelQuick ( x, y, 1, imgData.get ( x,y,z,0 ) );
  1387. colorimg.setPixelQuick ( x, y, 2, imgData.get ( x,y,z,0 ) );
  1388. }
  1389. }
  1390. vector<int> colorvals;
  1391. colorvals.push_back ( 255 );
  1392. colorvals.push_back ( 0 );
  1393. colorvals.push_back ( 0 );
  1394. segmentation->markContours ( colorimg, regmask, colorvals, marked );
  1395. std::vector<string> list;
  1396. StringTools::split ( filelist[z], '/', list );
  1397. string savePath = StringTools::trim ( filelist[z], list.back() ) + "marked/" + list.back();
  1398. marked.write ( savePath );
  1399. }
  1400. #endif
  1401. }
  1402. regionProbs.clear();
  1403. regionProbs = vector<vector<double> > ( amountRegions, vector<double> ( classes, 0.0 ) );
  1404. vector<vector<double> > regionProbsCount ( amountRegions, vector<double> ( classes, 0.0 ) );
  1405. vector<int> bestlabels ( amountRegions, labelmapback[classesInImg[0]] );
  1406. for ( int z = 0; z < zsize; z++ )
  1407. {
  1408. for ( int y = 0; y < ysize; y++ )
  1409. {
  1410. for ( int x = 0; x < xsize; x++ )
  1411. {
  1412. int r = regions ( x, y, ( uint ) z );
  1413. for ( uint i = 0; i < classesInImg.size(); i++ )
  1414. {
  1415. int c = classesInImg[i];
  1416. // get mean voting of all trees
  1417. regionProbs[r][c] += getMeanProb ( x, y, z, c, nodeIndices );
  1418. regionProbsCount[r][c]++;
  1419. }
  1420. }
  1421. }
  1422. }
  1423. for ( int r = 0; r < amountRegions; r++ )
  1424. for ( int c = 0; c < classes; c++ )
  1425. regionProbs[r][c] /= regionProbsCount[r][c];
  1426. for ( int r = 0; r < amountRegions; r++ )
  1427. {
  1428. double maxProb = regionProbs[r][classesInImg[0]];
  1429. bestlabels[r] = classesInImg[0];
  1430. for ( int c = 1; c < classes; c++ )
  1431. if ( maxProb < regionProbs[r][c] )
  1432. {
  1433. maxProb = regionProbs[r][c];
  1434. bestlabels[r] = c;
  1435. }
  1436. bestlabels[r] = labelmapback[bestlabels[r]];
  1437. }
  1438. // copy region labeling into segresults (output)
  1439. for ( int z = 0; z < zsize; z++ )
  1440. for ( int y = 0; y < ysize; y++ )
  1441. for ( int x = 0; x < xsize; x++ )
  1442. {
  1443. int r = regions ( x,y, (uint) z );
  1444. int l = bestlabels[ r ];
  1445. segresult.set ( x, y, l, (uint) z );
  1446. for ( int c = 0; c < classes; c++ )
  1447. {
  1448. double curProb = regionProbs[r][c];
  1449. probabilities.set( x, y, z, curProb, c );
  1450. }
  1451. }
  1452. #ifdef WRITEREGIONS
  1453. for ( int z = 0; z < zsize; z++ )
  1454. {
  1455. RegionGraph rg;
  1456. NICE::ColorImage img ( xsize,ysize );
  1457. if ( imagetype == IMAGETYPE_RGB )
  1458. {
  1459. img = imgData.getColor ( z );
  1460. }
  1461. else
  1462. {
  1463. NICE::Image gray = imgData.getChannel ( z );
  1464. for ( int y = 0; y < ysize; y++ )
  1465. {
  1466. for ( int x = 0; x < xsize; x++ )
  1467. {
  1468. int val = gray.getPixelQuick ( x,y );
  1469. img.setPixelQuick ( x, y, val, val, val );
  1470. }
  1471. }
  1472. }
  1473. Matrix regions_tmp ( xsize,ysize );
  1474. for ( int y = 0; y < ysize; y++ )
  1475. {
  1476. for ( int x = 0; x < xsize; x++ )
  1477. {
  1478. regions_tmp ( x,y ) = regions ( x,y, ( uint ) z );
  1479. }
  1480. }
  1481. segmentation->getGraphRepresentation ( img, regions_tmp, rg );
  1482. for ( uint pos = 0; pos < regionProbs.size(); pos++ )
  1483. {
  1484. rg[pos]->setProbs ( regionProbs[pos] );
  1485. }
  1486. std::string s;
  1487. std::stringstream out;
  1488. std::vector< std::string > list;
  1489. StringTools::split ( filelist[z], '/', list );
  1490. out << "rgout/" << list.back() << ".graph";
  1491. string writefile = out.str();
  1492. rg.write ( writefile );
  1493. }
  1494. #endif
  1495. }
  1496. timer.stop();
  1497. cout << "\nTime for Classification: " << timer.getLastAbsolute() << endl;
  1498. // CLEANING UP
  1499. // TODO: operations in "forest"
  1500. while( !ops.empty() )
  1501. {
  1502. OperationPool* op = ops.back();
  1503. op->clear();
  1504. ops.pop_back();
  1505. }
  1506. delete globalCategorFeat;
  1507. }
  1508. void SemSegContextTree3D::store ( std::ostream & os, int format ) const
  1509. {
  1510. os.precision ( numeric_limits<double>::digits10 + 1 );
  1511. os << nbTrees << endl;
  1512. classnames.store ( os );
  1513. map<int, int>::const_iterator it;
  1514. os << labelmap.size() << endl;
  1515. for ( it = labelmap.begin() ; it != labelmap.end(); it++ )
  1516. os << ( *it ).first << " " << ( *it ).second << endl;
  1517. os << labelmapback.size() << endl;
  1518. for ( it = labelmapback.begin() ; it != labelmapback.end(); it++ )
  1519. os << ( *it ).first << " " << ( *it ).second << endl;
  1520. int trees = forest.size();
  1521. os << trees << endl;
  1522. for ( int t = 0; t < trees; t++ )
  1523. {
  1524. int nodes = forest[t].size();
  1525. os << nodes << endl;
  1526. for ( int n = 0; n < nodes; n++ )
  1527. {
  1528. os << forest[t][n].left << " " << forest[t][n].right << " " << forest[t][n].decision << " " << forest[t][n].isleaf << " " << forest[t][n].depth << " " << forest[t][n].featcounter << " " << forest[t][n].nodeNumber << endl;
  1529. os << forest[t][n].dist << endl;
  1530. if ( forest[t][n].feat == NULL )
  1531. os << -1 << endl;
  1532. else
  1533. {
  1534. os << forest[t][n].feat->getOps() << endl;
  1535. forest[t][n].feat->store ( os );
  1536. }
  1537. }
  1538. }
  1539. vector<int> channelType;
  1540. if ( useFeat0 )
  1541. channelType.push_back(0);
  1542. if ( useFeat1 )
  1543. channelType.push_back(1);
  1544. if ( useFeat2 )
  1545. channelType.push_back(2);
  1546. if ( useFeat3 )
  1547. channelType.push_back(3);
  1548. if ( useFeat4 )
  1549. channelType.push_back(4);
  1550. os << channelType.size() << endl;
  1551. for ( int i = 0; i < ( int ) channelType.size(); i++ )
  1552. {
  1553. os << channelType[i] << " ";
  1554. }
  1555. os << endl;
  1556. os << rawChannels << endl;
  1557. os << uniquenumber << endl;
  1558. }
  1559. void SemSegContextTree3D::restore ( std::istream & is, int format )
  1560. {
  1561. is >> nbTrees;
  1562. classnames.restore ( is );
  1563. int lsize;
  1564. is >> lsize;
  1565. labelmap.clear();
  1566. for ( int l = 0; l < lsize; l++ )
  1567. {
  1568. int first, second;
  1569. is >> first;
  1570. is >> second;
  1571. labelmap[first] = second;
  1572. }
  1573. is >> lsize;
  1574. labelmapback.clear();
  1575. for ( int l = 0; l < lsize; l++ )
  1576. {
  1577. int first, second;
  1578. is >> first;
  1579. is >> second;
  1580. labelmapback[first] = second;
  1581. }
  1582. int trees;
  1583. is >> trees;
  1584. forest.clear();
  1585. for ( int t = 0; t < trees; t++ )
  1586. {
  1587. vector<TreeNode> tmptree;
  1588. forest.push_back ( tmptree );
  1589. int nodes;
  1590. is >> nodes;
  1591. for ( int n = 0; n < nodes; n++ )
  1592. {
  1593. TreeNode tmpnode;
  1594. forest[t].push_back ( tmpnode );
  1595. is >> forest[t][n].left;
  1596. is >> forest[t][n].right;
  1597. is >> forest[t][n].decision;
  1598. is >> forest[t][n].isleaf;
  1599. is >> forest[t][n].depth;
  1600. is >> forest[t][n].featcounter;
  1601. is >> forest[t][n].nodeNumber;
  1602. is >> forest[t][n].dist;
  1603. int feattype;
  1604. is >> feattype;
  1605. assert ( feattype < NBOPERATIONS );
  1606. forest[t][n].feat = NULL;
  1607. if ( feattype >= 0 )
  1608. {
  1609. for ( uint o = 0; o < ops.size(); o++ )
  1610. {
  1611. for ( uint o2 = 0; o2 < ops[o]->pool.size(); o2++ )
  1612. {
  1613. if ( forest[t][n].feat == NULL )
  1614. {
  1615. if ( ops[o]->pool[o2]->getOps() == feattype )
  1616. {
  1617. forest[t][n].feat = ops[o]->pool[o2]->clone();
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. }
  1623. assert ( forest[t][n].feat != NULL );
  1624. forest[t][n].feat->restore ( is );
  1625. forest[t][n].feat->setWSize ( windowSize );
  1626. }
  1627. }
  1628. }
  1629. // channel type configuration
  1630. int ctsize;
  1631. is >> ctsize;
  1632. for ( int i = 0; i < ctsize; i++ )
  1633. {
  1634. int tmp;
  1635. is >> tmp;
  1636. switch (tmp)
  1637. {
  1638. case 0: useFeat0 = true; break;
  1639. case 1: useFeat1 = true; break;
  1640. case 2: useFeat2 = true; break;
  1641. case 3: useFeat3 = true; break;
  1642. case 4: useFeat4 = true; break;
  1643. }
  1644. }
  1645. is >> rawChannels;
  1646. is >> uniquenumber;
  1647. }