SemSegContextTree3D.cpp 66 KB

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