SemSegContextTree3D.cpp 67 KB

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