SemSegCsurka.cpp 68 KB

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