SemSegCsurka.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  1. #include <sstream>
  2. #include <iostream>
  3. #include "SemSegCsurka.h"
  4. #include "vislearning/baselib/ICETools.h"
  5. #include "core/image/FilterT.h"
  6. #include "semseg/semseg/postsegmentation/PSSImageLevelPrior.h"
  7. using namespace std;
  8. using namespace NICE;
  9. using namespace OBJREC;
  10. #undef DEBUG_CSURK
  11. #undef UNCERTAINTY
  12. // #define UNCERTAINTY
  13. SemSegCsurka::SemSegCsurka ( const Config *conf,
  14. const MultiDataset *md )
  15. : SemanticSegmentation ( conf, & ( md->getClassNames ( "train" ) ) )
  16. {
  17. this->conf = conf;
  18. opSiftImpl = conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
  19. readfeat = conf->gB ( "Descriptor", "read", true );
  20. writefeat = conf->gB ( "Descriptor", "write", true );
  21. #ifdef DEBUG_CSURK
  22. clog << "[log] SemSegCsurka::SemSegCsurka: OppenentSift implemenation: " << opSiftImpl << endl;
  23. #endif
  24. save_cache = conf->gB ( "FPCPixel", "save_cache", true );
  25. read_cache = conf->gB ( "FPCPixel", "read_cache", false );
  26. cache = conf->gS ( "cache", "root", "" );
  27. sigmaweight = conf->gD ( "SemSegCsurka", "sigmaweight", 0.6 );
  28. dim = conf->gI ( "SemSegCsurka", "pcadim", 50 );
  29. usepca = conf->gB ( "SemSegCsurka", "usepca", true );
  30. calcpca = conf->gB ( "SemSegCsurka", "calcpca", false );
  31. usegmm = conf->gB ( "SemSegCsurka", "usegmm", false );
  32. norm = conf->gB ( "SemSegCsurka", "normalize", false );
  33. usefisher = conf->gB ( "SemSegCsurka", "usefisher", false );
  34. dogmm = conf->gB ( "SemSegCsurka", "dogmm", false );
  35. gaussians = conf->gI ( "SemSegCsurka", "gaussians", 50 );
  36. usekmeans = conf->gB ( "SemSegCsurka", "usekmeans", false );
  37. kmeansfeat = conf->gI ( "SemSegCsurka", "kmeansfeat", 50 );
  38. kmeanshard = conf->gB ( "SemSegCsurka", "kmeanshard", false );
  39. cname = conf->gS ( "SemSegCsurka", "classifier", "RandomForests" );
  40. anteil = conf->gD ( "SemSegCsurka", "anteil", 1.0 );
  41. userellocprior = conf->gB ( "SemSegCsurka", "rellocfeat", false );
  42. bool usesrg = conf->gB ( "SemSegCsurka", "usesrg", false );
  43. useregions = conf->gB ( "SemSegCsurka", "useregions", true );
  44. savesteps = conf->gB ( "SemSegCsurka", "savesteps", true );
  45. bool usegcopt = conf->gB ( "SemSegCsurka", "usegcopt", false );
  46. bestclasses = conf->gI ( "SemSegCsurka", "bestclasses", 0 );
  47. smoothhl = conf->gB ( "SemSegCsurka", "smoothhl", false );
  48. smoothfactor = conf->gD ( "SemSegCsurka", "smoothfactor", 1.0 );
  49. usecolorfeats = conf->gB ( "SemSegCsurka", "usecolorfeats", false );
  50. string rsMethod = conf->gS ( "SemSegCsurka", "segmentation", "meanshift" );
  51. g = NULL;
  52. k = NULL;
  53. relloc = NULL;
  54. srg = NULL;
  55. gcopt = NULL;
  56. if ( !useregions && ( userellocprior || usesrg ) )
  57. {
  58. cerr << "relative location priors and super region growing are just supported in combination with useregions" << endl;
  59. exit ( 1 );
  60. }
  61. if ( usepca )
  62. pca = PCA ( dim );
  63. RegionSegmentationMethod * tmpseg;
  64. if ( rsMethod == "meanshift" )
  65. tmpseg = new RSMeanShift ( conf );
  66. else
  67. tmpseg = new RSGraphBased ( conf );
  68. if ( save_cache )
  69. seg = new RSCache ( conf, tmpseg );
  70. else
  71. seg = tmpseg;
  72. if ( userellocprior )
  73. relloc = new RelativeLocationPrior ( conf );
  74. else
  75. relloc = NULL;
  76. #ifdef NICE_USELIB_ICE
  77. if ( usesrg )
  78. srg = new PPSuperregion ( conf );
  79. else
  80. srg = NULL;
  81. #else
  82. srg = NULL;
  83. #endif
  84. if ( usegcopt )
  85. gcopt = new PPGraphCut ( conf );
  86. else
  87. gcopt = NULL;
  88. classifier = NULL;
  89. vclassifier = NULL;
  90. if ( cname == "RandomForests" )
  91. classifier = new FPCRandomForests ( conf, "ClassifierForest" );
  92. else if ( cname == "SMLR" )
  93. classifier = new FPCSMLR ( conf, "ClassifierSMLR" );
  94. else if ( cname == "GPHIK" )
  95. classifier = new FPCGPHIK ( conf, "ClassiferGPHIK" );
  96. else
  97. vclassifier = GenericClassifierSelection::selectVecClassifier ( conf, "main" );
  98. //classifier = new FPCSparseMultinomialLogisticRegression(conf, "ClassifierSMLR");
  99. if ( classifier != NULL )
  100. classifier->setMaxClassNo ( classNames->getMaxClassno() );
  101. else
  102. vclassifier->setMaxClassNo ( classNames->getMaxClassno() );
  103. cn = md->getClassNames ( "train" );
  104. if ( read_cache )
  105. {
  106. fprintf ( stderr, "SemSegCsurka:: Reading classifier data from %s\n", ( cache + "/fpcrf.data" ).c_str() );
  107. if ( classifier != NULL )
  108. classifier->read ( cache + "/fpcrf.data" );
  109. else
  110. vclassifier->read ( cache + "/veccl.data" );
  111. if ( usepca )
  112. {
  113. std::string filename = cache + "/pca";
  114. pca.read ( filename );
  115. }
  116. if ( usegmm )
  117. {
  118. g = new GMM ( conf, gaussians );
  119. if ( !g->loadData ( cache + "/gmm" ) )
  120. {
  121. cerr << "SemSegCsurka:: no gmm file found" << endl;
  122. exit ( -1 );
  123. }
  124. }
  125. else {
  126. g = NULL;
  127. }
  128. if ( usekmeans )
  129. {
  130. k = new KMeansOnline ( gaussians );
  131. }
  132. fprintf ( stderr, "SemSegCsurka:: successfully read\n" );
  133. std::string filename = cache + "/rlp";
  134. FILE *value;
  135. value = fopen ( filename.c_str(), "r" );
  136. if ( value == NULL )
  137. {
  138. trainpostprocess ( md );
  139. }
  140. else
  141. {
  142. if ( userellocprior )
  143. {
  144. relloc->read ( filename );
  145. }
  146. }
  147. filename = cache + "/srg";
  148. value = fopen ( filename.c_str(), "r" );
  149. if ( value == NULL )
  150. {
  151. trainpostprocess ( md );
  152. }
  153. else
  154. {
  155. if ( srg != NULL )
  156. {
  157. srg->read ( filename );
  158. }
  159. }
  160. }
  161. else
  162. {
  163. train ( md );
  164. }
  165. }
  166. SemSegCsurka::~SemSegCsurka()
  167. {
  168. // clean-up
  169. if ( classifier != NULL )
  170. delete classifier;
  171. if ( vclassifier != NULL )
  172. delete vclassifier;
  173. if ( seg != NULL )
  174. delete seg;
  175. g = NULL;
  176. if ( g != NULL )
  177. delete g;
  178. }
  179. void SemSegCsurka::normalize ( Examples &ex )
  180. {
  181. assert ( ex.size() > 0 );
  182. if ( vecmin.size() == 0 )
  183. {
  184. for ( int j = 0; j < ( int ) ex[0].second.vec->size(); j++ )
  185. {
  186. double maxv = -numeric_limits<int>::max();
  187. double minv = numeric_limits<int>::max();
  188. for ( int i = 0; i < ( int ) ex.size(); i++ )
  189. {
  190. maxv = std::max ( maxv, ( *ex[i].second.vec ) [j] );
  191. minv = std::min ( minv, ( *ex[i].second.vec ) [j] );
  192. }
  193. vecmin.push_back ( minv );
  194. vecmax.push_back ( maxv );
  195. }
  196. }
  197. for ( int i = 0; i < ( int ) ex.size(); i++ )
  198. {
  199. for ( int j = 0; j < ( int ) ex[i].second.vec->size(); j++ )
  200. {
  201. ( *ex[i].second.vec ) [j] = ( ( *ex[i].second.vec ) [j] - vecmin[j] ) / ( vecmax[j] - vecmin[j] );
  202. }
  203. }
  204. return;
  205. }
  206. void SemSegCsurka::convertLowToHigh ( Examples &ex, double reduce )
  207. {
  208. cout << "converting low-level features to high-level features" << endl;
  209. if ( reduce >= 1.0 )
  210. {
  211. for ( int i = 0; i < ( int ) ex.size(); i++ )
  212. {
  213. SparseVector *f = new SparseVector();
  214. if ( usekmeans )
  215. {
  216. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  217. }
  218. else
  219. {
  220. if ( usefisher )
  221. g->getFisher ( *ex[i].second.vec, *f );
  222. else
  223. g->getProbs ( *ex[i].second.vec, *f );
  224. }
  225. delete ex[i].second.vec;
  226. ex[i].second.vec = NULL;
  227. ex[i].second.svec = f;
  228. }
  229. }
  230. else
  231. {
  232. srand ( time ( NULL ) );
  233. vector<bool> del ( ex.size(), false );
  234. cout << "Example size old " << ex.size() << endl;
  235. //#pragma omp parallel for
  236. for ( int i = 0; i < ( int ) ex.size(); i++ )
  237. {
  238. double rval = ( double ) rand() / ( double ) RAND_MAX;
  239. if ( rval < reduce )
  240. {
  241. SparseVector *f = new SparseVector();
  242. if ( usekmeans )
  243. k->getDist ( *ex[i].second.vec, *f, kmeansfeat, kmeanshard );
  244. else
  245. {
  246. if ( usefisher )
  247. g->getFisher ( *ex[i].second.vec, *f );
  248. else
  249. g->getProbs ( *ex[i].second.vec, *f );
  250. }
  251. delete ex[i].second.vec;
  252. ex[i].second.vec = NULL;
  253. ex[i].second.svec = f;
  254. }
  255. else
  256. {
  257. del[i] = true;
  258. }
  259. }
  260. for ( int i = ( int ) del.size() - 1; i >= 0; i-- )
  261. {
  262. if ( del[i] )
  263. {
  264. ex.erase ( ex.begin() + i );
  265. }
  266. }
  267. cerr << "Example size new " << ex.size() << endl;
  268. }
  269. cerr << "converting low-level features to high-level features finished" << endl;
  270. }
  271. void SemSegCsurka::smoothHL ( Examples ex )
  272. {
  273. if ( !smoothhl )
  274. return;
  275. assert ( ex.size() > 1 );
  276. long long int minx = numeric_limits<long long int>::max();
  277. long long int miny = numeric_limits<long long int>::max();
  278. long long int maxx = -numeric_limits<long long int>::max();
  279. long long int maxy = -numeric_limits<long long int>::max();
  280. long long int distx = numeric_limits<long long int>::max();
  281. long long int disty = numeric_limits<long long int>::max();
  282. set<double> scales;
  283. for ( int i = 0; i < ( int ) ex.size(); i++ )
  284. {
  285. scales.insert ( ex[i].second.scale );
  286. }
  287. map<double, int> scalepos;
  288. int it = 0;
  289. for ( set<double>::const_iterator iter = scales.begin(); iter != scales.end(); ++iter, ++it )
  290. {
  291. scalepos.insert ( make_pair ( *iter, it ) );
  292. }
  293. for ( int i = 0; i < ( int ) ex.size(); i++ )
  294. {
  295. if ( minx < numeric_limits<int>::max() && ex[i].second.x - minx > 0 )
  296. distx = std::min ( distx, ex[i].second.x - minx );
  297. if ( miny < numeric_limits<int>::max() && ex[i].second.y - miny > 0 )
  298. disty = std::min ( disty, ex[i].second.y - miny );
  299. minx = std::min ( ( long long int ) ex[i].second.x, minx );
  300. maxx = std::max ( ( long long int ) ex[i].second.x, maxx );
  301. miny = std::min ( ( long long int ) ex[i].second.y, miny );
  302. maxy = std::max ( ( long long int ) ex[i].second.y, maxy );
  303. }
  304. distx = abs ( distx );
  305. int xsize = ( maxx - minx ) / distx + 1;
  306. int ysize = ( maxy - miny ) / disty + 1;
  307. double valx = ( ( double ) xsize - 1 ) / ( double ) ( maxx - minx );
  308. double valy = ( ( double ) ysize - 1 ) / ( double ) ( maxy - miny );
  309. //double sigma = smoothfactor;
  310. double sigma = std::max ( xsize, ysize ) * smoothfactor;
  311. //double sigma = 0.2;
  312. cout << "sigma1: " << sigma << endl;
  313. vector<NICE::FloatImage> imgv;
  314. vector<NICE::FloatImage> gaussImgv;
  315. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  316. {
  317. NICE::FloatImage img ( xsize, ysize );
  318. NICE::FloatImage gaussImg ( xsize, ysize );
  319. imgv.push_back ( img );
  320. gaussImgv.push_back ( gaussImg );
  321. }
  322. for ( int d = 0; d < ex[0].second.svec->getDim(); d++ )
  323. {
  324. //TODO: max und min dynamisches bestimmen
  325. for ( int i = 0; i < ( int ) scalepos.size(); i++ )
  326. {
  327. imgv[i].set ( 0.0 );
  328. gaussImgv[i].set ( 0.0 );
  329. }
  330. for ( int i = 0; i < ( int ) ex.size(); i++ )
  331. {
  332. int xpos = ( ex[i].second.x - minx ) * valx;
  333. int ypos = ( ex[i].second.y - miny ) * valy;
  334. double val = ex[i].second.svec->get ( d );
  335. imgv[scalepos[ex[i].second.scale]].setPixel ( xpos, ypos, val );
  336. }
  337. /*
  338. for(int y = 0; y < ysize; y++)
  339. {
  340. for(int x = 0; x < xsize; x++)
  341. {
  342. // refactor-nice.pl: check this substitution
  343. // old: double val = GetValD(img,x,y);
  344. double val = img.getPixel(x,y);
  345. double c = 0.0;
  346. if(val == 0.0)
  347. {
  348. if(x > 0)
  349. {
  350. // refactor-nice.pl: check this substitution
  351. // old: val+=GetValD(img,x-1,y);
  352. val+=img.getPixel(x-1,y);
  353. c+=1.0;
  354. }
  355. if(y > 0)
  356. {
  357. // refactor-nice.pl: check this substitution
  358. // old: val+=GetValD(img,x,y-1);
  359. val+=img.getPixel(x,y-1);
  360. c+=1.0;
  361. }
  362. if(x < xsize-1)
  363. {
  364. // refactor-nice.pl: check this substitution
  365. // old: val+=GetValD(img,x+1,y);
  366. val+=img.getPixel(x+1,y);
  367. c+=1.0;
  368. }
  369. if(y < ysize-1)
  370. {
  371. // refactor-nice.pl: check this substitution
  372. // old: val+=GetValD(img,x,y+1);
  373. val+=img.getPixel(x,y+1);
  374. c+=1.0;
  375. }
  376. // refactor-nice.pl: check this substitution
  377. // old: PutValD(img,x,y,val/c);
  378. img.setPixel(x,y,val/c);
  379. }
  380. }
  381. }*/
  382. for ( int i = 0; i < ( int ) imgv.size(); i++ )
  383. {
  384. NICE::FilterT<float, float, float> filt;
  385. filt.filterGaussSigmaApproximate ( imgv[i], sigma, &gaussImgv[i] );
  386. }
  387. for ( int i = 0; i < ( int ) ex.size(); i++ )
  388. {
  389. int xpos = ( ex[i].second.x - minx ) * valx;
  390. int ypos = ( ex[i].second.y - miny ) * valy;
  391. // refactor-nice.pl: check this substitution
  392. // old: double val = GetValD ( gaussImgv[scalepos[ex[i].second.scale]], xpos, ypos );
  393. double val = gaussImgv[scalepos[ex[i].second.scale]].getPixel ( xpos, ypos );
  394. if ( fabs ( val ) < 1e-7 )
  395. {
  396. if ( ex[i].second.svec->get ( d ) != 0.0 )
  397. {
  398. ex[i].second.svec->erase ( d );
  399. }
  400. }
  401. else
  402. {
  403. ( *ex[i].second.svec ) [d] = val;
  404. }
  405. }
  406. }
  407. }
  408. void SemSegCsurka::initializePCA ( Examples &ex )
  409. {
  410. #ifdef DEBUG
  411. cerr << "start computing pca" << endl;
  412. #endif
  413. std::string filename = cache + "/pca";
  414. FILE *value;
  415. value = fopen ( filename.c_str(), "r" );
  416. if ( value == NULL || calcpca )
  417. {
  418. srand ( time ( NULL ) );
  419. int featsize = ( int ) ex.size();
  420. int maxfeatures = dim * 10;
  421. int olddim = ex[0].second.vec->size();
  422. maxfeatures = std::min ( maxfeatures, featsize );
  423. NICE::Matrix features ( maxfeatures, olddim );
  424. for ( int i = 0; i < maxfeatures; i++ )
  425. {
  426. int k = rand() % featsize;
  427. int vsize = ( int ) ex[k].second.vec->size();
  428. for ( int j = 0; j < vsize; j++ )
  429. {
  430. features ( i, j ) = ( * ( ex[k].second.vec ) ) [j];
  431. }
  432. }
  433. pca.calculateBasis ( features, dim );
  434. if ( save_cache )
  435. pca.save ( filename );
  436. }
  437. else
  438. {
  439. cout << "readpca: " << filename << endl;
  440. pca.read ( filename );
  441. cout << "end" << endl;
  442. }
  443. #ifdef DEBUG
  444. cerr << "finished computing pca" << endl;
  445. #endif
  446. }
  447. void SemSegCsurka::doPCA ( Examples &ex )
  448. {
  449. cout << "converting features using pca starts" << endl;
  450. std::string savedir = cname = conf->gS ( "cache", "root", "/dev/null/" );
  451. std::string shortf = ex.filename;
  452. if ( string::npos != ex.filename.rfind ( "/" ) )
  453. shortf = ex.filename.substr ( ex.filename.rfind ( "/" ) );
  454. std::string filename = savedir + "/pcasave/" + shortf;
  455. std::string syscall = "mkdir " + savedir + "/pcasave";
  456. system ( syscall.c_str() );
  457. cout << "filename: " << filename << endl;
  458. if ( !FileMgt::fileExists ( filename ) || calcpca )
  459. {
  460. ofstream ofStream;
  461. //Opens the file binary
  462. ofStream.open ( filename.c_str(), fstream::out | fstream::binary );
  463. for ( int k = 0; k < ( int ) ex.size(); k++ )
  464. {
  465. NICE::Vector tmp = pca.getFeatureVector ( * ( ex[k].second.vec ), true );
  466. delete ex[k].second.vec;
  467. for ( int d = 0; d < ( int ) tmp.size(); d++ )
  468. ofStream.write ( ( char* ) &tmp[d], sizeof ( double ) );
  469. ex[k].second.vec = new NICE::Vector ( tmp );
  470. }
  471. ofStream.close();
  472. cout << endl;
  473. }
  474. else
  475. {
  476. ifstream ifStream;
  477. ifStream.open ( filename.c_str(), std::fstream::in | std::fstream::binary );
  478. for ( int k = 0; k < ( int ) ex.size(); k++ )
  479. {
  480. NICE::Vector tmp = NICE::Vector ( dim );
  481. delete ex[k].second.vec;
  482. for ( int d = 0; d < dim; d++ )
  483. ifStream.read ( ( char* ) &tmp[d], sizeof ( double ) );
  484. ex[k].second.vec = new NICE::Vector ( tmp );
  485. }
  486. ifStream.close();
  487. }
  488. cout << "converting features using pca finished" << endl;
  489. }
  490. void SemSegCsurka::train ( const MultiDataset *md )
  491. {
  492. /*die einzelnen Trainingsschritte
  493. 1. auf allen Trainingsbilder SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  494. 2. PCA anwenden
  495. 3. aus diesen ein GMM erstellen
  496. 4. für jedes SIFT-Merkmal einen Vektor erstellen, der an der Stelle i die Wahrscheinlichkeit enthällt zur Verteilung i des GMM, Zur Zeit mit BoV-Alternative durch Moosman06 erledigt
  497. 5. diese Vektoren in einem diskriminitativen Klassifikator ( z.B. SLR oder Randomized Forests) zusammen mit ihrer Klassenzugehörigkeit anlernen
  498. */
  499. #ifdef DEBUG
  500. cerr << "SemSegCsurka:: training starts" << endl;
  501. #endif
  502. Examples examples;
  503. examples.filename = "training";
  504. // Welche Opponentsift Implementierung soll genutzt werden ?
  505. LocalFeatureRepresentation *cSIFT = NULL;
  506. LocalFeatureRepresentation *writeFeats = NULL;
  507. LocalFeatureRepresentation *readFeats = NULL;
  508. LocalFeatureRepresentation *getFeats = NULL;
  509. if ( opSiftImpl == "NICE" )
  510. {
  511. cSIFT = new LFonHSG ( conf, "HSGtrain" );
  512. }
  513. else if ( opSiftImpl == "VANDESANDE" )
  514. {
  515. // the used features
  516. cSIFT = new LFColorSande ( conf, "LFColorSandeTrain" );
  517. }
  518. else
  519. {
  520. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  521. }
  522. getFeats = cSIFT;
  523. if ( writefeat )
  524. {
  525. // write the features to a file, if there isn't any to read
  526. writeFeats = new LFWriteCache ( conf, cSIFT );
  527. getFeats = writeFeats;
  528. }
  529. if ( readfeat )
  530. {
  531. // read the features from a file
  532. if ( writefeat )
  533. {
  534. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  535. }
  536. else
  537. {
  538. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  539. }
  540. getFeats = readFeats;
  541. }
  542. // additional Colorfeatures
  543. LocalFeatureColorWeijer lcw ( conf );
  544. int lfdimension = -1;
  545. const LabeledSet train = * ( *md ) ["train"];
  546. const LabeledSet *trainp = &train;
  547. ////////////////////////
  548. // Merkmale berechnen //
  549. ////////////////////////
  550. std::string forbidden_classes_s = conf->gS ( "analysis", "donttrain", "" );
  551. if ( forbidden_classes_s == "" )
  552. {
  553. forbidden_classes_s = conf->gS ( "analysis", "forbidden_classes", "" );
  554. }
  555. cn.getSelection ( forbidden_classes_s, forbidden_classes );
  556. cerr << "forbidden: " << forbidden_classes_s << endl;
  557. ProgressBar pb ( "Local Feature Extraction" );
  558. pb.show();
  559. int imgnb = 0;
  560. LOOP_ALL_S ( *trainp )
  561. {
  562. //EACH_S(classno, currentFile);
  563. EACH_INFO ( classno, info );
  564. pb.update ( trainp->count() );
  565. NICE::ColorImage img;
  566. std::string currentFile = info.img();
  567. CachedExample *ce = new CachedExample ( currentFile );
  568. const LocalizationResult *locResult = info.localization();
  569. if ( locResult->size() <= 0 )
  570. {
  571. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  572. currentFile.c_str() );
  573. continue;
  574. }
  575. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  576. currentFile.c_str() );
  577. int xsize, ysize;
  578. ce->getImageSize ( xsize, ysize );
  579. NICE::Image pixelLabels ( xsize, ysize );
  580. pixelLabels.set ( 0 );
  581. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  582. try {
  583. img = ColorImage ( currentFile );
  584. } catch ( Exception ) {
  585. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  586. continue;
  587. }
  588. Globals::setCurrentImgFN ( currentFile );
  589. VVector features;
  590. VVector cfeatures;
  591. VVector positions;
  592. NICE::ColorImage cimg ( currentFile );
  593. getFeats->extractFeatures ( img, features, positions );
  594. #ifdef DEBUG_CSURK
  595. cout << "[log] SemSegCsruka::train -> " << currentFile << " an " << positions.size() << " Positionen wurden Features (Anz = " << features.size() << ") " << endl;
  596. cout << "mit einer Dimension von " << features[ 0].size() << " extrahiert." << endl;
  597. #endif
  598. if ( usecolorfeats )
  599. lcw.getDescriptors ( cimg, cfeatures, positions );
  600. int j = 0;
  601. for ( VVector::const_iterator i = features.begin();
  602. i != features.end();
  603. i++, j++ )
  604. {
  605. const NICE::Vector & x = *i;
  606. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  607. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  608. continue;
  609. if ( lfdimension < 0 )
  610. lfdimension = ( int ) x.size();
  611. else
  612. assert ( lfdimension == ( int ) x.size() );
  613. NICE::Vector *v = new NICE::Vector ( x );
  614. if ( usecolorfeats && !usepca )
  615. v->append ( cfeatures[j] );
  616. Example example ( v );
  617. example.position = imgnb;
  618. examples.push_back (
  619. pair<int, Example> ( classno, example ) );
  620. }
  621. features.clear();
  622. positions.clear();
  623. delete ce;
  624. imgnb++;
  625. }
  626. pb.hide();
  627. //////////////////
  628. // PCA anwenden //
  629. //////////////////
  630. if ( usepca )
  631. {
  632. if ( !read_cache )
  633. {
  634. initializePCA ( examples );
  635. }
  636. doPCA ( examples );
  637. lfdimension = dim;
  638. }
  639. /////////////////////////////////////////////////////
  640. // Low-Level Features in High-Level transformieren //
  641. /////////////////////////////////////////////////////
  642. int hlfdimension = lfdimension;
  643. if ( norm )
  644. normalize ( examples );
  645. if ( usegmm )
  646. {
  647. if ( !usepca && !norm )
  648. normalize ( examples );
  649. g = new GMM ( conf, gaussians );
  650. if ( dogmm || !g->loadData ( cache + "/gmm" ) )
  651. {
  652. g->computeMixture ( examples );
  653. if ( save_cache )
  654. g->saveData ( cache + "/gmm" );
  655. }
  656. hlfdimension = gaussians;
  657. if ( usefisher )
  658. hlfdimension = gaussians * 2 * dim;
  659. }
  660. if ( usekmeans )
  661. {
  662. if ( !usepca || norm )
  663. normalize ( examples );
  664. k = new KMeansOnline ( gaussians );
  665. k->cluster ( examples );
  666. hlfdimension = gaussians;
  667. }
  668. if ( usekmeans || usegmm )
  669. {
  670. examples.clear();
  671. pb.reset ( "Local Feature Extraction" );
  672. lfdimension = -1;
  673. pb.update ( trainp->count() );
  674. LOOP_ALL_S ( *trainp )
  675. {
  676. EACH_INFO ( classno, info );
  677. pb.update ( trainp->count() );
  678. NICE::ColorImage img;
  679. std::string currentFile = info.img();
  680. CachedExample *ce = new CachedExample ( currentFile );
  681. const LocalizationResult *locResult = info.localization();
  682. if ( locResult->size() <= 0 )
  683. {
  684. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  685. currentFile.c_str() );
  686. continue;
  687. }
  688. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  689. currentFile.c_str() );
  690. int xsize, ysize;
  691. ce->getImageSize ( xsize, ysize );
  692. NICE::Image pixelLabels ( xsize, ysize );
  693. pixelLabels.set ( 0 );
  694. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  695. try {
  696. img = ColorImage ( currentFile );
  697. }
  698. catch ( Exception ) {
  699. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  700. continue;
  701. }
  702. Globals::setCurrentImgFN ( currentFile );
  703. VVector features;
  704. VVector cfeatures;
  705. VVector positions;
  706. NICE::ColorImage cimg ( currentFile );
  707. getFeats->extractFeatures ( img, features, positions );
  708. if ( usecolorfeats )
  709. lcw.getDescriptors ( cimg, cfeatures, positions );
  710. int j = 0;
  711. Examples tmpex;
  712. for ( VVector::const_iterator i = features.begin();
  713. i != features.end();
  714. i++, j++ )
  715. {
  716. const NICE::Vector & x = *i;
  717. classno = pixelLabels.getPixel ( ( int ) positions[j][0], ( int ) positions[j][1] );
  718. if ( forbidden_classes.find ( classno ) != forbidden_classes.end() )
  719. continue;
  720. if ( lfdimension < 0 )
  721. lfdimension = ( int ) x.size();
  722. else
  723. assert ( lfdimension == ( int ) x.size() );
  724. NICE::Vector *v = new NICE::Vector ( x );
  725. if ( usecolorfeats )
  726. v->append ( cfeatures[j] );
  727. Example example ( v );
  728. example.position = imgnb;
  729. example.x = ( int ) positions[j][0];
  730. example.y = ( int ) positions[j][1];
  731. example.scale = positions[j][2];
  732. tmpex.push_back ( pair<int, Example> ( classno, example ) );
  733. }
  734. tmpex.filename = currentFile;
  735. if ( usepca )
  736. {
  737. doPCA ( tmpex );
  738. }
  739. convertLowToHigh ( tmpex, anteil );
  740. smoothHL ( tmpex );
  741. for ( int i = 0; i < ( int ) tmpex.size(); i++ )
  742. {
  743. examples.push_back ( pair<int, Example> ( tmpex[i].first, tmpex[i].second ) );
  744. }
  745. tmpex.clear();
  746. features.clear();
  747. positions.clear();
  748. delete ce;
  749. imgnb++;
  750. }
  751. pb.hide();
  752. }
  753. ////////////////////////////
  754. // Klassifikator anlernen //
  755. ////////////////////////////
  756. FeaturePool fp;
  757. Feature *f;
  758. if ( usegmm || usekmeans )
  759. f = new SparseVectorFeature ( hlfdimension );
  760. else
  761. f = new VectorFeature ( hlfdimension );
  762. f->explode ( fp );
  763. delete f;
  764. if ( usecolorfeats && ! ( usekmeans || usegmm ) )
  765. {
  766. int dimension = hlfdimension + 11;
  767. for ( int i = hlfdimension ; i < dimension ; i++ )
  768. {
  769. VectorFeature *f = new VectorFeature ( dimension );
  770. f->feature_index = i;
  771. fp.addFeature ( f, 1.0 / dimension );
  772. }
  773. }
  774. /*
  775. cout << "train classifier" << endl;
  776. fp.store(cout);
  777. getchar();
  778. for(int z = 0; z < examples.size(); z++)
  779. {
  780. cout << "examples.size() " << examples.size() << endl;
  781. cout << "class: " << examples[z].first << endl;
  782. cout << *examples[z].second.vec << endl;
  783. getchar();
  784. }*/
  785. if ( classifier != NULL )
  786. classifier->train ( fp, examples );
  787. else
  788. {
  789. LabeledSetVector lvec;
  790. convertExamplesToLSet ( examples, lvec );
  791. vclassifier->teach ( lvec );
  792. if ( usegmm )
  793. convertLSetToSparseExamples ( examples, lvec );
  794. else
  795. convertLSetToExamples ( examples, lvec );
  796. vclassifier->finishTeaching();
  797. }
  798. fp.destroy();
  799. if ( save_cache )
  800. {
  801. if ( classifier != NULL )
  802. classifier->save ( cache + "/fpcrf.data" );
  803. else
  804. vclassifier->save ( cache + "/veccl.data" );
  805. }
  806. ////////////
  807. //clean up//
  808. ////////////
  809. for ( int i = 0; i < ( int ) examples.size(); i++ )
  810. {
  811. examples[i].second.clean();
  812. }
  813. examples.clear();
  814. if ( cSIFT != NULL )
  815. delete cSIFT;
  816. if ( writeFeats != NULL )
  817. delete writeFeats;
  818. if ( readFeats != NULL )
  819. delete readFeats;
  820. getFeats = NULL;
  821. trainpostprocess ( md );
  822. cerr << "SemSeg training finished" << endl;
  823. }
  824. void SemSegCsurka::trainpostprocess ( const MultiDataset *md )
  825. {
  826. cout << "start postprocess" << endl;
  827. ////////////////////////////
  828. // Postprocess trainieren //
  829. ////////////////////////////
  830. const LabeledSet train = * ( *md ) ["train"];
  831. const LabeledSet *trainp = &train;
  832. if ( userellocprior || srg != NULL || gcopt != NULL )
  833. {
  834. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior || srg != NULL || gcopt !=NULL )" << endl;
  835. if ( userellocprior )
  836. relloc->setClassNo ( cn.numClasses() );
  837. if ( gcopt != NULL )
  838. {
  839. gcopt->setClassNo ( cn.numClasses() );
  840. }
  841. ProgressBar pb ( "learn relative location prior maps" );
  842. pb.show();
  843. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  844. {
  845. EACH_INFO ( classno, info );
  846. pb.update ( trainp->count() );
  847. NICE::ColorImage img;
  848. std::string currentFile = info.img();
  849. Globals::setCurrentImgFN ( currentFile );
  850. CachedExample *ce = new CachedExample ( currentFile );
  851. const LocalizationResult *locResult = info.localization();
  852. if ( locResult->size() <= 0 )
  853. {
  854. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  855. currentFile.c_str() );
  856. continue;
  857. }
  858. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  859. currentFile.c_str() );
  860. int xsize, ysize;
  861. ce->getImageSize ( xsize, ysize );
  862. NICE::Image pixelLabels ( xsize, ysize );
  863. pixelLabels.set ( 0 );
  864. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  865. try {
  866. img = ColorImage ( currentFile );
  867. }
  868. catch ( Exception )
  869. {
  870. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  871. continue;
  872. }
  873. //Regionen ermitteln
  874. NICE::Matrix mask;
  875. int regionsize = seg->segRegions ( img, mask );
  876. #ifdef DEBUG_CSURK
  877. Image overlay ( img.width(), img.height() );
  878. double maxval = -numeric_limits<double>::max();
  879. for ( int y = 0; y < img.height(); y++ )
  880. {
  881. for ( int x = 0; x < img.width(); x++ )
  882. {
  883. int val = ( ( int ) mask ( x, y ) + 1 ) % 256;
  884. overlay.setPixel ( x, y, val );
  885. maxval = std::max ( mask ( x, y ), maxval );
  886. }
  887. }
  888. cout << maxval << " different regions found" << endl;
  889. NICE::showImageOverlay ( img, overlay, "Segmentation Result" );
  890. #endif
  891. Examples regions;
  892. vector<vector<int> > hists;
  893. for ( int i = 0; i < regionsize; i++ )
  894. {
  895. Example tmp;
  896. regions.push_back ( pair<int, Example> ( 0, tmp ) );
  897. vector<int> hist ( cn.numClasses(), 0 );
  898. hists.push_back ( hist );
  899. }
  900. for ( int x = 0; x < xsize; x++ )
  901. {
  902. for ( int y = 0; y < ysize; y++ )
  903. {
  904. int numb = mask ( x, y );
  905. regions[numb].second.x += x;
  906. regions[numb].second.y += y;
  907. regions[numb].second.weight += 1.0;
  908. hists[numb][pixelLabels.getPixel ( x,y ) ]++;
  909. }
  910. }
  911. for ( int i = 0; i < regionsize; i++ )
  912. {
  913. regions[i].second.x /= ( int ) regions[i].second.weight;
  914. regions[i].second.y /= ( int ) regions[i].second.weight;
  915. int maxval = -numeric_limits<int>::max();
  916. int maxpos = -1;
  917. int secondpos = -1;
  918. for ( int k = 0; k < ( int ) hists[i].size(); k++ )
  919. {
  920. if ( maxval < hists[i][k] )
  921. {
  922. maxval = hists[i][k];
  923. secondpos = maxpos;
  924. maxpos = k;
  925. }
  926. }
  927. if ( cn.text ( maxpos ) == "various" )
  928. regions[i].first = secondpos;
  929. else
  930. regions[i].first = maxpos;
  931. }
  932. if ( userellocprior )
  933. relloc->trainPriorsMaps ( regions, xsize, ysize );
  934. if ( srg != NULL )
  935. srg->trainShape ( regions, mask );
  936. if ( gcopt != NULL )
  937. gcopt->trainImage ( regions, mask );
  938. delete ce;
  939. }
  940. pb.hide();
  941. if ( userellocprior )
  942. relloc->finishPriorsMaps ( cn );
  943. if ( srg != NULL )
  944. srg->finishShape ( cn );
  945. if ( gcopt != NULL )
  946. gcopt->finishPP ( cn );
  947. }
  948. if ( userellocprior )
  949. {
  950. clog << "[log] SemSegCsurka::trainpostprocess: if ( userellocprior )" << endl;
  951. ProgressBar pb ( "learn relative location classifier" );
  952. pb.show();
  953. int nummer = 0;
  954. LOOP_ALL_S ( *trainp ) // für alle Bilder den ersten Klassifikationsschritt durchführen um den zweiten Klassifikator anzutrainieren
  955. {
  956. //EACH_S(classno, currentFile);
  957. EACH_INFO ( classno, info );
  958. nummer++;
  959. pb.update ( trainp->count() );
  960. NICE::Image img;
  961. std::string currentFile = info.img();
  962. CachedExample *ce = new CachedExample ( currentFile );
  963. const LocalizationResult *locResult = info.localization();
  964. if ( locResult->size() <= 0 )
  965. {
  966. fprintf ( stderr, "WARNING: NO ground truth polygons found for %s !\n",
  967. currentFile.c_str() );
  968. continue;
  969. }
  970. fprintf ( stderr, "SemSegCsurka: Collecting pixel examples from localization info: %s\n",
  971. currentFile.c_str() );
  972. int xsize, ysize;
  973. ce->getImageSize ( xsize, ysize );
  974. NICE::Image pixelLabels ( xsize, ysize );
  975. pixelLabels.set ( 0 );
  976. locResult->calcLabeledImage ( pixelLabels, ( *classNames ).getBackgroundClass() );
  977. try {
  978. img = Preprocess::ReadImgAdv ( currentFile.c_str() );
  979. }
  980. catch ( Exception )
  981. {
  982. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  983. continue;
  984. }
  985. Globals::setCurrentImgFN ( currentFile );
  986. NICE::ImageT<int> segresult;
  987. NICE::MultiChannelImageT<double> probabilities ( xsize, ysize, classno );
  988. Examples regions;
  989. NICE::Matrix mask;
  990. if ( savesteps )
  991. {
  992. std::ostringstream s1;
  993. s1 << cache << "/rlpsave/" << nummer;
  994. std::string filename = s1.str();
  995. s1 << ".probs";
  996. std::string fn2 = s1.str();
  997. FILE *file;
  998. file = fopen ( filename.c_str(), "r" );
  999. if ( file == NULL )
  1000. {
  1001. //berechnen
  1002. classifyregions ( ce, segresult, probabilities, regions, mask );
  1003. //schreiben
  1004. ofstream fout ( filename.c_str(), ios::app );
  1005. fout << regions.size() << endl;
  1006. for ( int i = 0; i < ( int ) regions.size(); i++ )
  1007. {
  1008. regions[i].second.store ( fout );
  1009. fout << regions[i].first << endl;
  1010. }
  1011. fout.close();
  1012. probabilities.store ( fn2 );
  1013. }
  1014. else
  1015. {
  1016. //lesen
  1017. ifstream fin ( filename.c_str() );
  1018. int size;
  1019. fin >> size;
  1020. for ( int i = 0; i < size; i++ )
  1021. {
  1022. Example ex;
  1023. ex.restore ( fin );
  1024. int tmp;
  1025. fin >> tmp;
  1026. regions.push_back ( pair<int, Example> ( tmp, ex ) );
  1027. }
  1028. fin.close();
  1029. probabilities.restore ( fn2 );
  1030. }
  1031. }
  1032. else
  1033. {
  1034. classifyregions ( ce, segresult, probabilities, regions, mask );
  1035. }
  1036. relloc->trainClassifier ( regions, probabilities );
  1037. delete ce;
  1038. }
  1039. relloc->finishClassifier();
  1040. pb.hide();
  1041. relloc->save ( cache + "/rlp" );
  1042. }
  1043. cout << "finished postprocess" << endl;
  1044. }
  1045. void SemSegCsurka::classifyregions ( CachedExample *ce, NICE::ImageT<int> & segresult, NICE::MultiChannelImageT<double> & probabilities, Examples &Regionen, NICE::Matrix & mask )
  1046. {
  1047. /* die einzelnen Testschritte:
  1048. 1.x auf dem Testbild alle SIFT Merkmale an den Gitterpunkten bei allen Auflösungen bestimmen
  1049. 2.x für jedes SIFT-Merkmal einen Vektor erstellen, der an der Stelle i die Wahrscheinlichkeit enthällt zur Verteilung i des GMM
  1050. 3.x diese Vektoren klassifizieren, so dass für jede Klasse die Wahrscheinlichkeit gespeichert wird
  1051. 4.x für jeden Pixel die Wahrscheinlichkeiten mitteln aus allen Patches, in denen der Pixel vorkommt
  1052. 5.x das Originalbild in homogene Bereiche segmentieren
  1053. 6.x die homogenen Bereiche bekommen die gemittelten Wahrscheinlichkeiten ihrer Pixel
  1054. 7. (einzelne Klassen mit einem globalen Klassifikator ausschließen)
  1055. 8.x jeder Pixel bekommt die Klasse seiner Region zugeordnet
  1056. */
  1057. clog << "[log] SemSegCsruka::classifyregions" << endl;
  1058. int xsize, ysize;
  1059. ce->getImageSize ( xsize, ysize );
  1060. probabilities.reInit ( xsize, ysize, classNames->getMaxClassno() + 1 );
  1061. clog << "[log] SemSegCsruka::classifyregions: probabilities.channels() = " << probabilities.channels() << endl;
  1062. segresult.resize ( xsize, ysize );
  1063. Examples pce;
  1064. // Welche Opponentsift Implementierung soll genutzt werden ?
  1065. LocalFeatureRepresentation *cSIFT = NULL;
  1066. LocalFeatureRepresentation *writeFeats = NULL;
  1067. LocalFeatureRepresentation *readFeats = NULL;
  1068. LocalFeatureRepresentation *getFeats = NULL;
  1069. if ( opSiftImpl == "NICE" )
  1070. {
  1071. cSIFT = new LFonHSG ( conf, "HSGtest" );
  1072. }
  1073. else if ( opSiftImpl == "VANDESANDE" )
  1074. {
  1075. // the used features
  1076. cSIFT = new LFColorSande ( conf, "LFColorSandeTest" );
  1077. }
  1078. else
  1079. {
  1080. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  1081. }
  1082. getFeats = cSIFT;
  1083. if ( writefeat )
  1084. {
  1085. // write the features to a file, if there isn't any to read
  1086. writeFeats = new LFWriteCache ( conf, cSIFT );
  1087. getFeats = writeFeats;
  1088. }
  1089. if ( readfeat )
  1090. {
  1091. // read the features from a file
  1092. if ( writefeat )
  1093. {
  1094. readFeats = new LFReadCache ( conf, writeFeats, -1 );
  1095. }
  1096. else
  1097. {
  1098. readFeats = new LFReadCache ( conf, cSIFT, -1 );
  1099. }
  1100. getFeats = readFeats;
  1101. }
  1102. // additional Colorfeatures
  1103. LocalFeatureColorWeijer lcw ( conf );
  1104. NICE::ColorImage img;
  1105. std::string currentFile = Globals::getCurrentImgFN();
  1106. try
  1107. {
  1108. img = ColorImage ( currentFile );
  1109. }
  1110. catch ( Exception )
  1111. {
  1112. cerr << "SemSegCsurka: error opening image file <" << currentFile << ">" << endl;
  1113. }
  1114. VVector features;
  1115. VVector cfeatures;
  1116. VVector positions;
  1117. getFeats->extractFeatures ( img, features, positions );
  1118. if ( usecolorfeats )
  1119. lcw.getDescriptors ( img, cfeatures, positions );
  1120. set<double> scales;
  1121. int j = 0;
  1122. int lfdimension = -1;
  1123. for ( VVector::const_iterator i = features.begin();
  1124. i != features.end();
  1125. i++, j++ )
  1126. {
  1127. const NICE::Vector & x = *i;
  1128. if ( lfdimension < 0 ) lfdimension = ( int ) x.size();
  1129. else assert ( lfdimension == ( int ) x.size() );
  1130. NICE::Vector *v = new NICE::Vector ( x );
  1131. if ( usecolorfeats )
  1132. v->append ( cfeatures[j] );
  1133. Example tmp = Example ( v );
  1134. tmp.x = ( int ) positions[j][0];
  1135. tmp.y = ( int ) positions[j][1];
  1136. tmp.width = ( int ) ( 16.0 * positions[j][2] );
  1137. tmp.height = tmp.width;
  1138. tmp.scale = positions[j][2];
  1139. scales.insert ( positions[j][2] );
  1140. pce.push_back ( pair<int, Example> ( 0, tmp ) );
  1141. }
  1142. //////////////////
  1143. // PCA anwenden //
  1144. //////////////////
  1145. pce.filename = currentFile;
  1146. if ( usepca )
  1147. {
  1148. doPCA ( pce );
  1149. lfdimension = dim;
  1150. }
  1151. //////////////////
  1152. // BoV anwenden //
  1153. //////////////////
  1154. if ( norm )
  1155. normalize ( pce );
  1156. if ( usegmm || usekmeans )
  1157. {
  1158. if ( !usepca && !norm )
  1159. normalize ( pce );
  1160. convertLowToHigh ( pce );
  1161. smoothHL ( pce );
  1162. lfdimension = gaussians;
  1163. }
  1164. /////////////////////////////////////////
  1165. // Wahrscheinlichkeitskarten erstellen //
  1166. /////////////////////////////////////////
  1167. int klassen = probabilities.channels();
  1168. NICE::MultiChannelImageT<double> preMap ( xsize, ysize, klassen*scales.size() );
  1169. // initialisieren
  1170. for ( int y = 0 ; y < ysize ; y++ )
  1171. for ( int x = 0 ; x < xsize ; x++ )
  1172. {
  1173. // alles zum Hintergrund machen
  1174. segresult.setPixel ( x, y, 0 );
  1175. // Die Wahrscheinlichkeitsmaps auf 0 initialisieren
  1176. for ( int i = 0 ; i < ( int ) probabilities.channels(); i++ )
  1177. {
  1178. probabilities[i](x,y) = 0.0;
  1179. }
  1180. for ( int j = 0; j < ( int ) preMap.channels(); j++ )
  1181. {
  1182. preMap[j](x,y) = 0.0;
  1183. }
  1184. }
  1185. // Die Wahrscheinlichkeitsmaps mit den einzelnen Wahrscheinlichkeiten je Skalierung füllen
  1186. int scalesize = scales.size();
  1187. // Globale Häufigkeiten akkumulieren
  1188. FullVector fV ( ( int ) probabilities.channels() );
  1189. for ( int i = 0; i < fV.size(); i++ )
  1190. fV[i] = 0.0;
  1191. // read allowed classes
  1192. string cndir = conf->gS ( "SemSegCsurka", "cndir", "" );
  1193. int classes = ( int ) probabilities.channels();
  1194. vector<int> useclass ( classes, 1 );
  1195. std::vector< std::string > list;
  1196. StringTools::split ( currentFile, '/', list );
  1197. string orgname = list.back();
  1198. if ( cndir != "" )
  1199. {
  1200. useclass = vector<int> ( classes, 0 );
  1201. ifstream infile ( ( cndir + "/" + orgname + ".dat" ).c_str() );
  1202. while ( !infile.eof() && infile.good() )
  1203. {
  1204. int tmp;
  1205. infile >> tmp;
  1206. if ( tmp >= 0 && tmp < classes )
  1207. {
  1208. useclass[tmp] = 1;
  1209. }
  1210. }
  1211. }
  1212. #ifdef UNCERTAINTY
  1213. std::vector<FloatImage> uncert;
  1214. std::vector<FloatImage> gpUncertainty;
  1215. std::vector<FloatImage> gpMean;
  1216. std::vector<FloatImage> gpMeanRatio;
  1217. std::vector<FloatImage> gpWeightAll;
  1218. std::vector<FloatImage> gpWeightRatio;
  1219. // std::vector<FloatImage> gpImpactAll;
  1220. // std::vector<FloatImage> gpImpactRatio;
  1221. //pre-allocate storage -- one image per scale and method
  1222. for(int s = 0; s < scalesize; s++)
  1223. {
  1224. uncert.push_back(FloatImage(xsize, ysize));
  1225. uncert[s].set(0.0);
  1226. gpUncertainty.push_back(FloatImage(xsize, ysize));
  1227. gpMean.push_back(FloatImage(xsize, ysize));
  1228. gpMeanRatio.push_back(FloatImage(xsize, ysize));
  1229. gpWeightAll.push_back(FloatImage(xsize, ysize));
  1230. gpWeightRatio.push_back(FloatImage(xsize, ysize));
  1231. /* gpImpactAll.push_back(FloatImage(xsize, ysize));
  1232. gpImpactRatio.push_back(FloatImage(xsize, ysize)); */
  1233. gpUncertainty[s].set(0.0);
  1234. gpMean[s].set(0.0);
  1235. gpMeanRatio[s].set(0.0);
  1236. gpWeightAll[s].set(0.0);
  1237. gpWeightRatio[s].set(0.0);
  1238. // gpImpactAll[s].set(0.0);
  1239. // gpImpactRatio[s].set(0.0);
  1240. }
  1241. ColorImage imgrgb ( xsize, ysize );
  1242. std::string s;
  1243. std::stringstream out;
  1244. std::vector< std::string > list2;
  1245. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1246. out << "uncertainty/" << list2.back();
  1247. double maxu = -numeric_limits<double>::max();
  1248. double minu = numeric_limits<double>::max();
  1249. double gpNoise = conf->gD("GPHIK", "noise", 0.01);
  1250. #endif
  1251. #ifdef UNCERTAINTY
  1252. std::cerr << "compute values for uncertainty stuff as well" << std::endl;
  1253. #endif
  1254. if ( classifier != NULL )
  1255. {
  1256. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen: classifier != NULL" << endl;
  1257. #pragma omp parallel for
  1258. for ( int s = 0; s < scalesize; s++ )
  1259. {
  1260. #pragma omp parallel for
  1261. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1262. {
  1263. ClassificationResult r = classifier->classify ( pce[i].second );
  1264. #ifdef UNCERTAINTY
  1265. //we need this if we want to compute GP-AL-measure lateron
  1266. double minMeanAbs ( numeric_limits<double>::max() );
  1267. double maxMeanAbs ( 0.0 );
  1268. double sndMaxMeanAbs ( 0.0 );
  1269. #endif
  1270. for ( int j = 0 ; j < r.scores.size(); j++ )
  1271. {
  1272. if ( useclass[j] == 0 )
  1273. continue;
  1274. fV[j] += r.scores[j];
  1275. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1276. #ifdef UNCERTAINTY
  1277. //check whether we found a class with higher smaller abs mean than the current minimum
  1278. if (abs(r.scores[j]) < minMeanAbs)
  1279. minMeanAbs = abs(r.scores[j]);
  1280. //check for larger abs mean as well
  1281. if (abs(r.scores[j]) > maxMeanAbs)
  1282. {
  1283. sndMaxMeanAbs = maxMeanAbs;
  1284. maxMeanAbs = abs(r.scores[j]);
  1285. }
  1286. // and also for the second highest mean of all classes
  1287. else if (abs(r.scores[j]) > sndMaxMeanAbs)
  1288. {
  1289. sndMaxMeanAbs = abs(r.scores[j]);
  1290. }
  1291. #endif
  1292. }
  1293. /*if(r.uncertainty < 0.0)
  1294. {
  1295. cerr << "uncertainty: " << r.uncertainty << endl;
  1296. pce[i].second.svec->store(cerr);
  1297. cerr << endl;
  1298. exit(-1);
  1299. }*/
  1300. #ifdef UNCERTAINTY
  1301. uncert[s] ( pce[i].second.x, pce[i].second.y ) = r.uncertainty;
  1302. maxu = std::max ( r.uncertainty, maxu );
  1303. minu = std::min ( r.uncertainty, minu );
  1304. double firstTerm (1.0 / sqrt(r.uncertainty+gpNoise));
  1305. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  1306. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  1307. gpUncertainty[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  1308. // compute results when we take the lowest mean value of all classes
  1309. gpMean[s] ( pce[i].second.x, pce[i].second.y ) = minMeanAbs;
  1310. //look at the difference in the absolut mean values for the most plausible class
  1311. // and the second most plausible class
  1312. gpMeanRatio[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs - sndMaxMeanAbs;
  1313. //compute the weight in the alpha-vector for every sample after assuming it to be
  1314. // added to the training set.
  1315. // Thereby, we measure its "importance" for the current model
  1316. //
  1317. //double firstTerm is already computed
  1318. //
  1319. //the second term is only needed when computing impacts
  1320. //double secondTerm; //this is the nasty guy :/
  1321. //--- compute the third term
  1322. // this is the difference between predicted label and GT label
  1323. std::vector<double> diffToPositive; diffToPositive.clear();
  1324. std::vector<double> diffToNegative; diffToNegative.clear();
  1325. double diffToNegativeSum(0.0);
  1326. for ( int j = 0 ; j < r.scores.size(); j++ )
  1327. {
  1328. if ( useclass[j] == 0 )
  1329. continue;
  1330. // look at the difference to plus 1
  1331. diffToPositive.push_back(abs(r.scores[j] - 1));
  1332. // look at the difference to -1
  1333. diffToNegative.push_back(abs(r.scores[j] + 1));
  1334. //sum up the difference to -1
  1335. diffToNegativeSum += abs(r.scores[j] - 1);
  1336. }
  1337. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1338. //and use this as the third term for this specific class.
  1339. //the final value is obtained by minimizing over all classes
  1340. //
  1341. // originally, we minimize over all classes after building the final score
  1342. // however, the first and the second term do not depend on the choice of
  1343. // y*, therefore we minimize here already
  1344. double thirdTerm (numeric_limits<double>::max()) ;
  1345. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1346. {
  1347. double tmpVal ( diffToPositive[tmpCnt] + (diffToNegativeSum-diffToNegative[tmpCnt]) );
  1348. if (tmpVal < thirdTerm)
  1349. thirdTerm = tmpVal;
  1350. }
  1351. gpWeightAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm;
  1352. //now look on the ratio of the resulting weights for the most plausible
  1353. // against the second most plausible class
  1354. double thirdTermMostPlausible ( 0.0 ) ;
  1355. double thirdTermSecondMostPlausible ( 0.0 ) ;
  1356. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1357. {
  1358. if (diffToPositive[tmpCnt] > thirdTermMostPlausible)
  1359. {
  1360. thirdTermSecondMostPlausible = thirdTermMostPlausible;
  1361. thirdTermMostPlausible = diffToPositive[tmpCnt];
  1362. }
  1363. else if (diffToPositive[tmpCnt] > thirdTermSecondMostPlausible)
  1364. {
  1365. thirdTermSecondMostPlausible = diffToPositive[tmpCnt];
  1366. }
  1367. }
  1368. //compute the resulting score
  1369. gpWeightRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm;
  1370. //finally, look for this feature how it would affect to whole model (summarized by weight-vector alpha), if we would
  1371. //use it as an additional training example
  1372. //TODO this would be REALLY computational demanding. Do we really want to do this?
  1373. // gpImpactAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm*secondTerm;
  1374. // gpImpactRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm*secondTerm;
  1375. #endif
  1376. }
  1377. }
  1378. }
  1379. else
  1380. {
  1381. //#pragma omp parallel for
  1382. for ( int s = 0; s < scalesize; s++ )
  1383. {
  1384. //#pragma omp parallel for
  1385. for ( int i = s; i < ( int ) pce.size(); i += scalesize )
  1386. {
  1387. ClassificationResult r = vclassifier->classify ( * ( pce[i].second.vec ) );
  1388. #ifdef UNCERTAINTY
  1389. //we need this if we want to compute GP-AL-measure lateron
  1390. double minMeanAbs ( numeric_limits<double>::max() );
  1391. double maxMeanAbs ( 0.0 );
  1392. double sndMaxMeanAbs ( 0.0 );
  1393. #endif
  1394. for ( int j = 0 ; j < ( int ) r.scores.size(); j++ )
  1395. {
  1396. if ( useclass[j] == 0 )
  1397. continue;
  1398. fV[j] += r.scores[j];
  1399. preMap.set ( pce[i].second.x, pce[i].second.y, r.scores[j], j + s*klassen );
  1400. #ifdef UNCERTAINTY
  1401. //check whether we found a class with higher smaller abs mean than the current minimum
  1402. if (abs(r.scores[j]) < minMeanAbs)
  1403. minMeanAbs = abs(r.scores[j]);
  1404. //check for larger abs mean as well
  1405. if (abs(r.scores[j]) > maxMeanAbs)
  1406. {
  1407. sndMaxMeanAbs = maxMeanAbs;
  1408. maxMeanAbs = abs(r.scores[j]);
  1409. }
  1410. // and also for the second highest mean of all classes
  1411. else if (abs(r.scores[j]) > sndMaxMeanAbs)
  1412. {
  1413. sndMaxMeanAbs = abs(r.scores[j]);
  1414. }
  1415. #endif
  1416. }
  1417. #ifdef UNCERTAINTY
  1418. uncert[s] ( pce[i].second.x, pce[i].second.y ) = r.uncertainty;
  1419. maxu = std::max ( r.uncertainty, maxu );
  1420. minu = std::min ( r.uncertainty, minu );
  1421. double firstTerm (1.0 / sqrt(r.uncertainty+gpNoise));
  1422. //compute the heuristic GP-UNCERTAINTY, as proposed by Kapoor et al. in IJCV 2010
  1423. // GP-UNCERTAINTY : |mean| / sqrt(var^2 + gpnoise^2)
  1424. gpUncertainty[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs*firstTerm; //firstTerm = 1.0 / sqrt(r.uncertainty+gpNoise))
  1425. // compute results when we take the lowest mean value of all classes
  1426. gpMean[s] ( pce[i].second.x, pce[i].second.y ) = minMeanAbs;
  1427. //look at the difference in the absolut mean values for the most plausible class
  1428. // and the second most plausible class
  1429. gpMeanRatio[s] ( pce[i].second.x, pce[i].second.y ) = maxMeanAbs - sndMaxMeanAbs;
  1430. //compute the weight in the alpha-vector for every sample after assuming it to be
  1431. // added to the training set.
  1432. // Thereby, we measure its "importance" for the current model
  1433. //
  1434. //double firstTerm is already computed
  1435. //
  1436. //the second term is only needed when computing impacts
  1437. //double secondTerm; //this is the nasty guy :/
  1438. //--- compute the third term
  1439. // this is the difference between predicted label and GT label
  1440. std::vector<double> diffToPositive; diffToPositive.clear();
  1441. std::vector<double> diffToNegative; diffToNegative.clear();
  1442. double diffToNegativeSum(0.0);
  1443. for ( int j = 0 ; j < fV.size(); j++ )
  1444. {
  1445. if ( useclass[j] == 0 )
  1446. continue;
  1447. // look at the difference to plus 1
  1448. diffToPositive.push_back(abs(r.scores[j] - 1));
  1449. // look at the difference to -1
  1450. diffToNegative.push_back(abs(r.scores[j] + 1));
  1451. //sum up the difference to -1
  1452. diffToNegativeSum += abs(r.scores[j] - 1);
  1453. }
  1454. //let's subtract for every class its diffToNegative from the sum, add its diffToPositive,
  1455. //and use this as the third term for this specific class.
  1456. //the final value is obtained by minimizing over all classes
  1457. //
  1458. // originally, we minimize over all classes after building the final score
  1459. // however, the first and the second term do not depend on the choice of
  1460. // y*, therefore we minimize here already
  1461. double thirdTerm (numeric_limits<double>::max()) ;
  1462. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1463. {
  1464. double tmpVal ( diffToPositive[tmpCnt] + (diffToNegativeSum-diffToNegative[tmpCnt]) );
  1465. if (tmpVal < thirdTerm)
  1466. thirdTerm = tmpVal;
  1467. }
  1468. gpWeightAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm;
  1469. //now look on the ratio of the resulting weights for the most plausible
  1470. // against the second most plausible class
  1471. double thirdTermMostPlausible ( 0.0 ) ;
  1472. double thirdTermSecondMostPlausible ( 0.0 ) ;
  1473. for(uint tmpCnt = 0; tmpCnt < diffToPositive.size(); tmpCnt++)
  1474. {
  1475. if (diffToPositive[tmpCnt] > thirdTermMostPlausible)
  1476. {
  1477. thirdTermSecondMostPlausible = thirdTermMostPlausible;
  1478. thirdTermMostPlausible = diffToPositive[tmpCnt];
  1479. }
  1480. else if (diffToPositive[tmpCnt] > thirdTermSecondMostPlausible)
  1481. {
  1482. thirdTermSecondMostPlausible = diffToPositive[tmpCnt];
  1483. }
  1484. }
  1485. //compute the resulting score
  1486. gpWeightRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm;
  1487. //finally, look for this feature how it would affect to whole model (summarized by weight-vector alpha), if we would
  1488. //use it as an additional training example
  1489. //TODO this would be REALLY computational demanding. Do we really want to do this?
  1490. // gpImpactAll[s] ( pce[i].second.x, pce[i].second.y ) = thirdTerm*firstTerm*secondTerm;
  1491. // gpImpactRatio[s] ( pce[i].second.x, pce[i].second.y ) = (thirdTermMostPlausible - thirdTermSecondMostPlausible)*firstTerm*secondTerm;
  1492. #endif
  1493. }
  1494. }
  1495. }
  1496. #ifdef UNCERTAINTY
  1497. std::cerr << "uncertainty values and derived scores successfully computed" << std::endl;
  1498. #endif
  1499. #ifdef UNCERTAINTY
  1500. cout << "maxvdirect: " << maxu << " minvdirect: " << minu << endl;
  1501. //pre-allocate the image for filtering lateron
  1502. FloatImage gaussUncert ( xsize, ysize );
  1503. //just store the first scale
  1504. ICETools::convertToRGB ( uncert[0], imgrgb );
  1505. imgrgb.write ( out.str() + "rough.ppm" );
  1506. //pre-allocate memory for filtering of scales
  1507. FloatImage gaussGPUncertainty ( xsize, ysize );
  1508. FloatImage gaussGPMean ( xsize, ysize );
  1509. FloatImage gaussGPMeanRatio( xsize, ysize );
  1510. FloatImage gaussGPWeightAll ( xsize, ysize );
  1511. FloatImage gaussGPWeightRatio ( xsize, ysize );
  1512. //just store the first scale for every method
  1513. ICETools::convertToRGB ( gpUncertainty[0], imgrgb );
  1514. imgrgb.write ( out.str() + "gpUncertainty.ppm" );
  1515. ICETools::convertToRGB ( gpMean[0], imgrgb );
  1516. imgrgb.write ( out.str() + "gpMean.ppm" );
  1517. ICETools::convertToRGB ( gpMeanRatio[0], imgrgb );
  1518. imgrgb.write ( out.str() + "gpMeanRatio.ppm" );
  1519. ICETools::convertToRGB ( gpWeightAll[0], imgrgb );
  1520. imgrgb.write ( out.str() + "gpWeightAll.ppm" );
  1521. ICETools::convertToRGB ( gpWeightRatio[0], imgrgb );
  1522. imgrgb.write ( out.str() + "gpWeightRatio.ppm" );
  1523. #endif
  1524. vector<double> scalesVec;
  1525. for ( set<double>::const_iterator iter = scales.begin();
  1526. iter != scales.end();
  1527. ++iter )
  1528. {
  1529. scalesVec.push_back ( *iter );
  1530. }
  1531. #undef VISSEMSEG
  1532. #ifdef VISSEMSEG
  1533. for ( int j = 0 ; j < ( int ) preMap.channels(); j++ )
  1534. {
  1535. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  1536. NICE::Matrix tmp ( preMap.ysize, preMap.xsize );
  1537. double maxval = 0.0;
  1538. for ( int y = 0; y < preMap.ysize; y++ )
  1539. for ( int x = 0; x < preMap.xsize; x++ )
  1540. {
  1541. double val = preMap.get ( x, y, j );
  1542. tmp ( y, x ) = val;
  1543. maxval = std::max ( val, maxval );
  1544. }
  1545. NICE::ColorImage imgrgb ( preMap.xsize, preMap.ysize );
  1546. ICETools::convertToRGB ( tmp, imgrgb );
  1547. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  1548. //Show ( ON, imgrgb, cn.text ( j ) );
  1549. //showImage(imgrgb, "Ergebnis");
  1550. std::string s;
  1551. std::stringstream out;
  1552. out << "tmpprebmap" << j << ".ppm";
  1553. s = out.str();
  1554. imgrgb.writePPM ( s );
  1555. //getchar();
  1556. }
  1557. #endif
  1558. // Gaußfiltern
  1559. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> Gaussfiltern" << endl;
  1560. for ( int s = 0; s < scalesize; s++ )
  1561. {
  1562. double sigma = sigmaweight * 16.0 * scalesVec[s];
  1563. cerr << "sigma: " << sigma << endl;
  1564. #pragma omp parallel for
  1565. for ( int i = 0; i < klassen; i++ )
  1566. {
  1567. if ( forbidden_classes.find ( i ) != forbidden_classes.end() )
  1568. {
  1569. continue;
  1570. }
  1571. int pos = i + s * klassen;
  1572. double maxval = preMap[pos](0,0);
  1573. double minval = maxval;
  1574. for ( int y = 0; y < ysize; y++ )
  1575. {
  1576. for ( int x = 0; x < xsize; x++ )
  1577. {
  1578. maxval = std::max ( maxval, preMap[pos](x,y) );
  1579. minval = std::min ( minval, preMap[pos](x,y) );
  1580. }
  1581. }
  1582. NICE::FloatImage dblImg ( xsize, ysize );
  1583. NICE::FloatImage gaussImg ( xsize, ysize );
  1584. for ( int y = 0; y < ysize; y++ )
  1585. {
  1586. for ( int x = 0; x < xsize; x++ )
  1587. {
  1588. dblImg.setPixel ( x, y, preMap[pos](x,y) );
  1589. }
  1590. }
  1591. NICE::FilterT<float, float, float> filt;
  1592. filt.filterGaussSigmaApproximate ( dblImg, sigma, &gaussImg );
  1593. for ( int y = 0; y < ysize; y++ )
  1594. {
  1595. for ( int x = 0; x < xsize; x++ )
  1596. {
  1597. preMap[pos](x,y) = gaussImg.getPixel ( x, y );
  1598. }
  1599. }
  1600. }
  1601. #ifdef UNCERTAINTY
  1602. filterGaussSigmaApproximate<float, float, float> ( uncert[s], sigma, &gaussUncert );
  1603. uncert[s] = gaussUncert;
  1604. //apply the gauss-filtering to all scales of every method
  1605. filterGaussSigmaApproximate<float, float, float> ( gpUncertainty[s], sigma, &gaussGPUncertainty );
  1606. filterGaussSigmaApproximate<float, float, float> ( gpMean[s], sigma, &gaussGPMean );
  1607. filterGaussSigmaApproximate<float, float, float> ( gpMeanRatio[s], sigma, &gaussGPMeanRatio );
  1608. filterGaussSigmaApproximate<float, float, float> ( gpWeightAll[s], sigma, &gaussGPWeightAll );
  1609. filterGaussSigmaApproximate<float, float, float> ( gpWeightRatio[s], sigma, &gaussGPWeightRatio );
  1610. gpUncertainty[s] = gaussGPUncertainty;
  1611. gpMean[s] = gaussGPMean;
  1612. gpMeanRatio[s] = gaussGPMeanRatio;
  1613. gpWeightAll[s] = gaussGPWeightAll;
  1614. gpWeightRatio[s] = gaussGPWeightRatio;
  1615. #endif
  1616. }
  1617. // Zusammenfassen und auswerten
  1618. clog << "[log] SemSegCsruka::classifyregions: Wahrscheinlichkeitskarten erstellen -> zusammenfassen und auswerten" << endl;
  1619. //#pragma omp parallel for
  1620. for ( int x = 0; x < xsize; x++ )
  1621. {
  1622. for ( int y = 0; y < ysize; y++ )
  1623. {
  1624. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1625. {
  1626. double prob = 0.0;
  1627. for ( int s = 0; s < ( int ) scalesize; s++ )
  1628. {
  1629. prob += preMap.get ( x, y, j + s * klassen );
  1630. }
  1631. double val = prob / ( double ) ( scalesize );
  1632. probabilities.set ( x, y, val, j );
  1633. }
  1634. }
  1635. }
  1636. #ifdef UNCERTAINTY
  1637. for ( int x = 0; x < xsize; x++ )
  1638. {
  1639. for ( int y = 0; y < ysize; y++ )
  1640. {
  1641. for ( int s = 0; s < ( int ) scalesize; s++ )
  1642. {
  1643. gaussUncert(x,y) += uncert[s](x,y);
  1644. //and for the other methods as well
  1645. gaussGPUncertainty(x,y) += gpUncertainty[s](x,y);
  1646. gaussGPMean(x,y) += gpMean[s](x,y);
  1647. gaussGPMeanRatio(x,y) += gpMeanRatio[s](x,y);
  1648. gaussGPWeightAll(x,y) += gpWeightAll[s](x,y);
  1649. gaussGPWeightRatio(x,y) += gpWeightRatio[s](x,y);
  1650. }
  1651. gaussUncert(x,y)/=scalesize;
  1652. //and for the other methods as well
  1653. gaussGPUncertainty(x,y)/=scalesize;
  1654. gaussGPMean(x,y)/=scalesize;
  1655. gaussGPMeanRatio(x,y)/=scalesize;
  1656. gaussGPWeightAll(x,y)/=scalesize;
  1657. gaussGPWeightRatio(x,y)/=scalesize;
  1658. }
  1659. }
  1660. maxu = -numeric_limits<double>::max();
  1661. minu = numeric_limits<double>::max();
  1662. for ( int y = 0; y < ysize; y++ )
  1663. {
  1664. for ( int x = 0; x < xsize; x++ )
  1665. {
  1666. double val = uncert[0] ( x, y );
  1667. maxu = std::max ( val, maxu );
  1668. minu = std::min ( val, minu );
  1669. }
  1670. }
  1671. cout << "maxvo = " << maxu << " minvo = " << minu << endl;
  1672. maxu = -numeric_limits<float>::max();
  1673. minu = numeric_limits<float>::max();
  1674. for ( int y = 0; y < ysize; y++ )
  1675. {
  1676. for ( int x = 0; x < xsize; x++ )
  1677. {
  1678. double val = gaussUncert ( x, y );
  1679. maxu = std::max ( val, maxu );
  1680. minu = std::min ( val, minu );
  1681. }
  1682. }
  1683. cout << "maxvf = " << maxu << " minvf = " << minu << endl;
  1684. gaussUncert(0,0) = 0.0;
  1685. gaussUncert(0,1) = 0.04;
  1686. ICETools::convertToRGB ( gaussUncert, imgrgb );
  1687. imgrgb.write ( out.str() + "filtered.ppm" );
  1688. ICETools::convertToRGB ( gaussGPUncertainty, imgrgb );
  1689. imgrgb.write ( out.str() + "gpUncertaintyFiltered.ppm" );
  1690. ICETools::convertToRGB ( gaussGPMean, imgrgb );
  1691. imgrgb.write ( out.str() + "gpMeanFiltered.ppm" );
  1692. ICETools::convertToRGB ( gaussGPMeanRatio, imgrgb );
  1693. imgrgb.write ( out.str() + "gpMeanRatioFiltered.ppm" );
  1694. ICETools::convertToRGB ( gaussGPWeightAll, imgrgb );
  1695. imgrgb.write ( out.str() + "gpWeightAllFiltered.ppm" );
  1696. ICETools::convertToRGB ( gaussGPWeightRatio, imgrgb );
  1697. imgrgb.write ( out.str() + "gpWeightRatioFiltered.ppm" );
  1698. #endif
  1699. #undef VISSEMSEG
  1700. #ifdef VISSEMSEG
  1701. std::string s;
  1702. std::stringstream out;
  1703. std::vector< std::string > list2;
  1704. StringTools::split ( Globals::getCurrentImgFN (), '/', list2 );
  1705. out << "probmaps/" << list2.back() << ".probs";
  1706. s = out.str();
  1707. probabilities.store ( s );
  1708. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1709. {
  1710. cout << "klasse: " << j << endl;//" " << cn.text ( j ) << endl;
  1711. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1712. double maxval = 0.0;
  1713. for ( int y = 0; y < probabilities.ysize; y++ )
  1714. for ( int x = 0; x < probabilities.xsize; x++ )
  1715. {
  1716. double val = probabilities.get ( x, y, j );
  1717. tmp ( y, x ) = val;
  1718. maxval = std::max ( val, maxval );
  1719. }
  1720. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1721. ICETools::convertToRGB ( tmp, imgrgb );
  1722. cout << "maxval = " << maxval << " for class " << j << endl; //cn.text ( j ) << endl;
  1723. //Show ( ON, imgrgb, cn.text ( j ) );
  1724. //showImage(imgrgb, "Ergebnis");
  1725. std::string s;
  1726. std::stringstream out;
  1727. out << "tmp" << j << ".ppm";
  1728. s = out.str();
  1729. imgrgb.writePPM ( s );
  1730. //getchar();
  1731. }
  1732. #endif
  1733. if ( useregions )
  1734. {
  1735. if ( bestclasses > 0 )
  1736. {
  1737. PSSImageLevelPrior pss ( 0, bestclasses, 0.2 );
  1738. pss.setPrior ( fV );
  1739. pss.postprocess ( segresult, probabilities );
  1740. }
  1741. //Regionen ermitteln
  1742. int regionsize = seg->segRegions ( img, mask );
  1743. Regionen.clear();
  1744. vector<vector <double> > regionprob;
  1745. #ifdef UNCERTAINTY
  1746. std::vector<double> regionUncert;
  1747. std::vector<double> regionGPUncertainty;
  1748. std::vector<double> regionGPMean;
  1749. std::vector<double> regionGPMeanRatio;
  1750. std::vector<double> regionGPWeightAll;
  1751. std::vector<double> regionGPWeightRatio;
  1752. #endif
  1753. // Wahrscheinlichkeiten für jede Region initialisieren
  1754. for ( int i = 0; i < regionsize; i++ )
  1755. {
  1756. vector<double> tmp;
  1757. for ( int j = 0; j < ( int ) probabilities.channels(); j++ )
  1758. {
  1759. tmp.push_back ( 0.0 );
  1760. }
  1761. regionprob.push_back ( tmp );
  1762. Regionen.push_back ( pair<int, Example> ( 0, Example() ) );
  1763. #ifdef UNCERTAINTY
  1764. regionUncert.push_back ( 0.0 );
  1765. regionGPUncertainty.push_back ( 0.0 );
  1766. regionGPMean.push_back ( 0.0 );
  1767. regionGPMeanRatio.push_back ( 0.0 );
  1768. regionGPWeightAll.push_back ( 0.0 );
  1769. regionGPWeightRatio.push_back ( 0.0 );
  1770. #endif
  1771. }
  1772. // Wahrscheinlichkeiten für Regionen bestimmen
  1773. for ( int x = 0; x < xsize; x++ )
  1774. {
  1775. for ( int y = 0; y < ysize; y++ )
  1776. {
  1777. int pos = mask ( x, y );
  1778. Regionen[pos].second.weight += 1.0;
  1779. Regionen[pos].second.x += x;
  1780. Regionen[pos].second.y += y;
  1781. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1782. {
  1783. double val = probabilities.get ( x, y, j );
  1784. regionprob[pos][j] += val;
  1785. }
  1786. #ifdef UNCERTAINTY
  1787. regionUncert[pos] += gaussUncert ( x, y );
  1788. regionGPUncertainty[pos] += gaussGPUncertainty ( x, y );
  1789. regionGPMean[pos] += gaussGPMean ( x, y );
  1790. regionGPMeanRatio[pos] += gaussGPMeanRatio ( x, y );
  1791. regionGPWeightAll[pos] += gaussGPWeightAll ( x, y );
  1792. regionGPWeightRatio[pos] += gaussGPWeightRatio ( x, y );
  1793. #endif
  1794. }
  1795. }
  1796. /*
  1797. cout << "regions: " << regionsize << endl;
  1798. cout << "outfeats: " << endl;
  1799. for(int j = 0; j < regionprob.size(); j++)
  1800. {
  1801. for(int i = 0; i < regionprob[j].size(); i++)
  1802. {
  1803. cout << regionprob[j][i] << " ";
  1804. }
  1805. cout << endl;
  1806. }
  1807. cout << endl;
  1808. getchar();*/
  1809. // beste Wahrscheinlichkeit je Region wählen
  1810. for ( int i = 0; i < regionsize; i++ )
  1811. {
  1812. if ( Regionen[i].second.weight > 0 )
  1813. {
  1814. Regionen[i].second.x /= ( int ) Regionen[i].second.weight;
  1815. Regionen[i].second.y /= ( int ) Regionen[i].second.weight;
  1816. }
  1817. double maxval = -numeric_limits<double>::max();
  1818. int maxpos = 0;
  1819. for ( int j = 0 ; j < ( int ) regionprob[i].size(); j++ )
  1820. {
  1821. if ( forbidden_classes.find ( j ) != forbidden_classes.end() )
  1822. continue;
  1823. regionprob[i][j] /= Regionen[i].second.weight;
  1824. if ( maxval < regionprob[i][j] )
  1825. {
  1826. maxval = regionprob[i][j];
  1827. maxpos = j;
  1828. }
  1829. probabilities.set ( Regionen[i].second.x, Regionen[i].second.y, regionprob[i][j], j );
  1830. }
  1831. Regionen[i].first = maxpos;
  1832. #ifdef UNCERTAINTY
  1833. regionUncert[i] /= Regionen[i].second.weight;
  1834. regionGPUncertainty[i] /= Regionen[i].second.weight;
  1835. regionGPMean[i] /= Regionen[i].second.weight;
  1836. regionGPMeanRatio[i] /= Regionen[i].second.weight;
  1837. regionGPWeightAll[i] /= Regionen[i].second.weight;
  1838. regionGPWeightRatio[i] /= Regionen[i].second.weight;
  1839. #endif
  1840. }
  1841. // Pixel jeder Region labeln
  1842. for ( int y = 0; y < ( int ) mask.cols(); y++ )
  1843. {
  1844. for ( int x = 0; x < ( int ) mask.rows(); x++ )
  1845. {
  1846. int pos = mask ( x, y );
  1847. segresult.setPixel ( x, y, Regionen[pos].first );
  1848. #ifdef UNCERTAINTY
  1849. gaussUncert ( x, y ) = regionUncert[pos];
  1850. gaussGPUncertainty ( x, y ) = regionGPUncertainty[pos];
  1851. gaussGPMean ( x, y ) = regionGPMean[pos];
  1852. gaussGPMeanRatio ( x, y ) = regionGPMeanRatio[pos];
  1853. gaussGPWeightAll ( x, y ) = regionGPWeightAll[pos];
  1854. gaussGPWeightRatio ( x, y ) = regionGPWeightRatio[pos];
  1855. #endif
  1856. }
  1857. }
  1858. #ifdef UNCERTAINTY
  1859. maxu = -numeric_limits<float>::max();
  1860. minu = numeric_limits<float>::max();
  1861. for ( int y = 0; y < ysize; y++ )
  1862. {
  1863. for ( int x = 0; x < xsize; x++ )
  1864. {
  1865. //float val = uncert(x,y);
  1866. double val = gaussUncert ( x, y );
  1867. maxu = std::max ( val, maxu );
  1868. minu = std::min ( val, minu );
  1869. }
  1870. }
  1871. cout << "maxvr = " << maxu << " minvr = " << minu << endl;
  1872. // uncert(0,0) = 1;
  1873. // uncert(0,1) = 0;
  1874. ICETools::convertToRGB ( gaussUncert, imgrgb );
  1875. imgrgb.write ( out.str() + "region.ppm" );
  1876. ICETools::convertToRGB ( gaussGPUncertainty, imgrgb );
  1877. imgrgb.write ( out.str() + "gpUncertaintyRegion.ppm" );
  1878. ICETools::convertToRGB ( gaussGPMean, imgrgb );
  1879. imgrgb.write ( out.str() + "gpMeanRegion.ppm" );
  1880. ICETools::convertToRGB ( gaussGPMeanRatio, imgrgb );
  1881. imgrgb.write ( out.str() + "gpMeanRatioRegion.ppm" );
  1882. ICETools::convertToRGB ( gaussGPWeightAll, imgrgb );
  1883. imgrgb.write ( out.str() + "gpWeightAllRegion.ppm" );
  1884. ICETools::convertToRGB ( gaussGPWeightRatio, imgrgb );
  1885. imgrgb.write ( out.str() + "gpWeightRatioRegion.ppm" );
  1886. #endif
  1887. #undef WRITEREGIONS
  1888. #ifdef WRITEREGIONS
  1889. RegionGraph rg;
  1890. seg->getGraphRepresentation ( img, mask, rg );
  1891. for ( uint pos = 0; pos < regionprob.size(); pos++ )
  1892. {
  1893. rg[pos]->setProbs ( regionprob[pos] );
  1894. }
  1895. std::string s;
  1896. std::stringstream out;
  1897. std::vector< std::string > list;
  1898. StringTools::split ( Globals::getCurrentImgFN (), '/', list );
  1899. out << "rgout/" << list.back() << ".graph";
  1900. string writefile = out.str();
  1901. rg.write ( writefile );
  1902. #endif
  1903. }
  1904. else
  1905. {
  1906. PSSImageLevelPrior pss ( 1, 4, 0.2 );
  1907. pss.setPrior ( fV );
  1908. pss.postprocess ( segresult, probabilities );
  1909. }
  1910. // Saubermachen:
  1911. clog << "[log] SemSegCsurka::classifyregions: sauber machen" << endl;
  1912. for ( int i = 0; i < ( int ) pce.size(); i++ )
  1913. {
  1914. pce[i].second.clean();
  1915. }
  1916. pce.clear();
  1917. if ( cSIFT != NULL )
  1918. delete cSIFT;
  1919. if ( writeFeats != NULL )
  1920. delete writeFeats;
  1921. if ( readFeats != NULL )
  1922. delete readFeats;
  1923. getFeats = NULL;
  1924. }
  1925. void SemSegCsurka::semanticseg (
  1926. CachedExample *ce,
  1927. NICE::ImageT<int> & segresult,
  1928. NICE::MultiChannelImageT<double> & probabilities )
  1929. {
  1930. Examples regions;
  1931. NICE::Matrix regionmask;
  1932. classifyregions ( ce, segresult, probabilities, regions, regionmask );
  1933. if ( userellocprior || srg != NULL || gcopt != NULL )
  1934. {
  1935. if ( userellocprior )
  1936. relloc->postprocess ( regions, probabilities );
  1937. if ( srg != NULL )
  1938. srg->optimizeShape ( regions, regionmask, probabilities );
  1939. if ( gcopt != NULL )
  1940. gcopt->optimizeImage ( regions, regionmask, probabilities );
  1941. // Pixel jeder Region labeln
  1942. for ( int y = 0; y < ( int ) regionmask.cols(); y++ )
  1943. {
  1944. for ( int x = 0; x < ( int ) regionmask.rows(); x++ )
  1945. {
  1946. int pos = regionmask ( x, y );
  1947. segresult.setPixel ( x, y, regions[pos].first );
  1948. }
  1949. }
  1950. }
  1951. #ifndef NOVISUAL
  1952. #undef VISSEMSEG
  1953. #ifdef VISSEMSEG
  1954. // showImage(img);
  1955. for ( int j = 0 ; j < ( int ) probabilities.channels(); j++ )
  1956. {
  1957. cout << "klasse: " << j << " " << cn.text ( j ) << endl;
  1958. NICE::Matrix tmp ( probabilities.ysize, probabilities.xsize );
  1959. double maxval = -numeric_limits<double>::max();
  1960. for ( int y = 0; y < probabilities.ysize; y++ )
  1961. for ( int x = 0; x < probabilities.xsize; x++ )
  1962. {
  1963. double val = probabilities.get ( x, y, j );
  1964. tmp ( y, x ) = val;
  1965. maxval = std::max ( val, maxval );
  1966. }
  1967. NICE::ColorImage imgrgb ( probabilities.xsize, probabilities.ysize );
  1968. ICETools::convertToRGB ( tmp, imgrgb );
  1969. cout << "maxval = " << maxval << " for class " << cn.text ( j ) << endl;
  1970. Show ( ON, imgrgb, cn.text ( j ) );
  1971. imgrgb.Write ( "tmp.ppm" );
  1972. getchar();
  1973. }
  1974. #endif
  1975. #endif
  1976. }