SemSegContextTree3D.cpp 66 KB

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