RelativeLocationPrior.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #include "RelativeLocationPrior.h"
  2. #include "objrec/fourier/FourierLibrary.h"
  3. #include "objrec/baselib/ICETools.h"
  4. using namespace std;
  5. using namespace NICE;
  6. using namespace OBJREC;
  7. RelativeLocationPrior::RelativeLocationPrior()
  8. {
  9. conf = new Config();
  10. mapsize = 200;
  11. }
  12. RelativeLocationPrior::RelativeLocationPrior(const Config *_conf):conf(_conf)
  13. {
  14. }
  15. void RelativeLocationPrior::setClassNo(int _classno)
  16. {
  17. classno = _classno;
  18. Init();
  19. }
  20. void RelativeLocationPrior::Init()
  21. {
  22. std::string section = "PostProcessRLP";
  23. mapsize = conf->gI(section, "mapsize", 200 );
  24. featdim = classno*3;
  25. //Priorsmaps erzeugen
  26. for(int i = 0; i < classno; i++)
  27. {
  28. GenericImage<double> *tmp = new GenericImage<double>(mapsize, mapsize, classno, true);
  29. tmp->setAll(0.0);
  30. priormaps.push_back(tmp);
  31. }
  32. }
  33. RelativeLocationPrior::~RelativeLocationPrior()
  34. {
  35. for(int i = 0; i < classno; i++)
  36. {
  37. delete priormaps[i];
  38. }
  39. }
  40. void RelativeLocationPrior::trainPriorsMaps(Examples &regions, int xsize, int ysize)
  41. {
  42. for(int j = 0; j < (int)regions.size(); j++)
  43. {
  44. for(int i = 0; i < (int)regions.size(); i++)
  45. {
  46. if(i == j)
  47. continue;
  48. int x = regions[i].second.x - regions[j].second.x;
  49. int y = regions[i].second.y - regions[j].second.y;
  50. convertCoords(x, xsize);
  51. convertCoords(y, ysize);
  52. priormaps[regions[i].first]->set(x, y, priormaps[regions[i].first]->get(x, y, regions[j].first)+1.0/*regions[j].second.weight*/, regions[j].first);
  53. }
  54. }
  55. }
  56. void RelativeLocationPrior::finishPriorsMaps(ClassNames &cn)
  57. {
  58. // Priormaps normalisieren
  59. double alpha = 5;
  60. for(int i = 0; i < classno; i++)
  61. {
  62. for(int j = 0; j < classno; j++)
  63. {
  64. double val = 0.0;
  65. for(int x = 0; x < mapsize; x++)
  66. {
  67. for(int y = 0; y < mapsize; y++)
  68. {
  69. val = std::max(val,priormaps[i]->get(x, y, j));
  70. }
  71. }
  72. if(val != 0.0)
  73. {
  74. for(int x = 0; x < mapsize; x++)
  75. {
  76. for(int y = 0; y < mapsize; y++)
  77. {
  78. double old = priormaps[i]->get(x, y, j);
  79. #undef DIRICHLET
  80. #ifdef DIRICHLET
  81. old = (old+alpha)/(val+classno*alpha);
  82. #else
  83. old /= val;
  84. #endif
  85. priormaps[i]->set(x, y, old, j);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. double sigma = 0.1*(double)mapsize; // 10% der Breite/Höhe der Maps
  92. // alle Priormaps weichzeichnen
  93. for(int j = 0; j < classno; j++)
  94. {
  95. for(int i = 0; i < classno; i++)
  96. {
  97. NICE::FloatImage tmp(mapsize, mapsize);
  98. tmp.set(0.0);
  99. for(int x = 0; x < mapsize; x++)
  100. {
  101. for(int y = 0; y < mapsize; y++)
  102. {
  103. tmp.setPixelQuick(x,y, priormaps[j]->get(x, y, i));
  104. }
  105. }
  106. NICE::FloatImage out;
  107. FourierLibrary::gaussFilterD(tmp, out, sigma);
  108. for(int x = 0; x < mapsize; x++)
  109. {
  110. for(int y = 0; y < mapsize; y++)
  111. {
  112. priormaps[j]->set(x, y, out.getPixel(x,y), i);
  113. }
  114. }
  115. }
  116. }
  117. // Summe aller Pixel an einer Position über jede Klasse = 1
  118. for(int i = 0; i < classno; i++)
  119. {
  120. for(int x = 0; x < mapsize; x++)
  121. {
  122. for(int y = 0; y < mapsize; y++)
  123. {
  124. double val = 0.0;
  125. for(int j = 0; j < classno; j++)
  126. {
  127. val += priormaps[i]->get(x, y, j);
  128. }
  129. if(val != 0.0)
  130. {
  131. for(int j = 0; j < classno; j++)
  132. {
  133. double old = priormaps[i]->get(x, y, j);
  134. old /= val;
  135. priormaps[i]->set(x, y, old, j);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. #undef VISDEBUG
  142. #ifdef VISDEBUG
  143. #ifndef NOVISUAL
  144. NICE::ColorImage rgbim((classno-1)*(mapsize+10), (classno-1)*(mapsize+10));
  145. double maxval = -numeric_limits<double>::max();
  146. double minval = numeric_limits<double>::max();
  147. for(int j = 0; j < classno; j++)
  148. {
  149. if(j == 6) continue;
  150. for(int i = 0; i < classno; i++)
  151. {
  152. if(i == 6) continue;
  153. for(int x = 0; x < mapsize; x++)
  154. {
  155. for(int y = 0; y < mapsize; y++)
  156. {
  157. double val = priormaps[j]->get(x, y, i);
  158. maxval = std::max(val, maxval);
  159. minval = std::min(val, minval);
  160. }
  161. }
  162. }
  163. }
  164. int jcounter = 0;
  165. for(int j = 0; j < classno; j++)
  166. {
  167. if(j == 6) continue;
  168. int icounter = 0;
  169. for(int i = 0; i < classno; i++)
  170. {
  171. if(i == 6) continue;
  172. NICE::FloatImage tmp(mapsize, mapsize);
  173. tmp.set(0.0);
  174. for(int x = 0; x < mapsize; x++)
  175. {
  176. for(int y = 0; y < mapsize; y++)
  177. {
  178. tmp.setPixel(x, y, priormaps[j]->get(x, y, i));
  179. }
  180. }
  181. tmp.setPixel(0,0,maxval);
  182. tmp.setPixel(0,1,minval);
  183. cout << "i: " << cn.text(i) << endl;
  184. NICE::ColorImage imgrgb2 (mapsize, mapsize);
  185. ICETools::convertToRGB(tmp, imgrgb2);
  186. imgrgb2.setPixel(0,0,2,imgrgb2.getPixel(1,0,2));
  187. imgrgb2.setPixel(0,1,2,imgrgb2.getPixel(1,1,2));
  188. imgrgb2.setPixel(0,0,0,imgrgb2.getPixel(1,0,0));
  189. imgrgb2.setPixel(0,1,0,imgrgb2.getPixel(1,1,0));
  190. imgrgb2.setPixel(0,0,1,imgrgb2.getPixel(1,0,1));
  191. imgrgb2.setPixel(0,1,1,imgrgb2.getPixel(1,1,1));
  192. for(int y = 0; y < mapsize; y++)
  193. {
  194. for(int x = 0; x < mapsize; x++)
  195. {
  196. rgbim.setPixel(x+jcounter*(mapsize+10),y+icounter*(mapsize+10),2,imgrgb2.getPixel(x,y,2));
  197. rgbim.setPixel(x+jcounter*(mapsize+10),y+icounter*(mapsize+10),0,imgrgb2.getPixel(x,y,0));
  198. rgbim.setPixel(x+jcounter*(mapsize+10),y+icounter*(mapsize+10),1,imgrgb2.getPixel(x,y,1));
  199. }
  200. }
  201. icounter++;
  202. }
  203. jcounter++;
  204. }
  205. rgbim.write("tmp.ppm");
  206. #endif
  207. #endif
  208. }
  209. void RelativeLocationPrior::trainClassifier(Examples &regions, GenericImage<double> & probabilities)
  210. {
  211. // für alle Regionen einen Merkmalsvektor erzeugen und diesen der Trainingsmenge hinzufügen
  212. getFeature(regions, probabilities);
  213. for(int i = 0; i < (int)regions.size(); i++)
  214. {
  215. trainingsdata.push_back(pair<int, Example>(regions[i].first, regions[i].second));
  216. regions[i].second.svec = NULL;
  217. }
  218. }
  219. void RelativeLocationPrior::finishClassifier()
  220. {
  221. //////////////////////////////
  222. // Klassifikatoren anlernen //
  223. //////////////////////////////
  224. FeaturePool fp;
  225. Feature *f = new SparseVectorFeature ( featdim );
  226. f->explode ( fp );
  227. delete f;
  228. //feature size
  229. int s = 3;
  230. classifiers.resize(classno);
  231. for(int i = 0; i < classno; i++)
  232. {
  233. classifiers[i] = SLR(conf, "ClassifierSMLR");
  234. Examples ex2;
  235. int countex = 0;
  236. for(int j = 0; j < (int)trainingsdata.size(); j++)
  237. {
  238. Example e;
  239. int z = 0;
  240. e.svec = new SparseVector(s+1);
  241. for(int k = i*s; k < i*s+s; k++, z++)
  242. {
  243. double val = trainingsdata[j].second.svec->get(k);
  244. if(val != 0.0)
  245. (*e.svec)[z] = val;
  246. }
  247. (*e.svec)[s] = 1.0;
  248. ex2.push_back ( pair<int, Example> ( trainingsdata[j].first, e ) );
  249. if(trainingsdata[j].first == i)
  250. countex++;
  251. }
  252. if(ex2.size() <= 2 || countex < 1)
  253. continue;
  254. classifiers[i].train(fp, ex2, i);
  255. for(int j = 0; j < (int)ex2.size(); j++)
  256. {
  257. delete ex2[j].second.svec;
  258. ex2[j].second.svec = NULL;
  259. }
  260. }
  261. trainingsdata.clear();
  262. }
  263. void RelativeLocationPrior::postprocess ( Examples &regions, GenericImage<double> & probabilities)
  264. {
  265. getFeature(regions, probabilities);
  266. int s = 3;
  267. for(int i = 0; i < (int) regions.size(); i++)
  268. {
  269. FullVector overall_distribution(classno+1);
  270. overall_distribution[classno] = 0.0;
  271. double maxp = -numeric_limits<double>::max();
  272. int bestclass = 0;
  273. double sum = 0.0;
  274. for(int c = 0; c < classno; c++)
  275. {
  276. Example e;
  277. int z = 0;
  278. e.svec = new SparseVector(s+1);
  279. for(int k = c*s; k < c*s+s; k++, z++)
  280. {
  281. double val = regions[i].second.svec->get(k);
  282. if(val != 0.0)
  283. (*e.svec)[z] = val;
  284. }
  285. (*e.svec)[s] = 1.0;
  286. overall_distribution[c] = classifiers[c].classify(e);
  287. sum += overall_distribution[c];
  288. if(maxp < overall_distribution[c])
  289. {
  290. bestclass = c;
  291. maxp = overall_distribution[c];
  292. }
  293. delete e.svec;
  294. e.svec = NULL;
  295. }
  296. for(int c = 0; c < classno; c++)
  297. {
  298. overall_distribution[c] /= sum;
  299. }
  300. ClassificationResult r = ClassificationResult( bestclass, overall_distribution );
  301. if(bestclass < 0)
  302. {
  303. regions[i].second.svec->store(cout);cout << endl;
  304. cout << "fehler: besclass=" << bestclass << endl;
  305. for(int j = 0; j < (int)probabilities.numChannels; j++)
  306. {
  307. cout << "j: " << j << " score: " << r.scores[j] << endl;
  308. }
  309. }
  310. regions[i].first = bestclass;
  311. }
  312. }
  313. void RelativeLocationPrior::convertCoords(int &x, int xsize)
  314. {
  315. x = (int)round((double(x)+(double)xsize)/(2.0*(double)xsize) * ((double)mapsize-1.0));
  316. x = std::min(x, mapsize-1);
  317. x = std::max(x,0);
  318. }
  319. void RelativeLocationPrior::getFeature(Examples &regions, GenericImage<double> & probabilities)
  320. {
  321. int xsize, ysize;
  322. xsize = probabilities.xsize;
  323. ysize = probabilities.ysize;
  324. // get best classes
  325. vector<int> bestclasses(regions.size(), -1);
  326. for(int r = 0; r < (int)regions.size(); r++)
  327. {
  328. double maxval = -numeric_limits<double>::max();
  329. for(int c = 0; c < (int)probabilities.numChannels; c++)
  330. {
  331. double val = probabilities.get(regions[r].second.x, regions[r].second.y, c);
  332. if(maxval < val)
  333. {
  334. bestclasses[r] = c;
  335. maxval = val;
  336. }
  337. }
  338. }
  339. vector<double> alpha;
  340. for(int r = 0; r < (int)regions.size(); r++)
  341. {
  342. double tmpalpha = probabilities.get(regions[r].second.x,regions[r].second.y,bestclasses[r]) *regions[r].second.weight;
  343. alpha.push_back(tmpalpha);
  344. }
  345. //erzeuge f_relloc
  346. vector<vector<double> > vother;
  347. vector<vector<double> > vself;
  348. for(int i = 0; i < (int)regions.size(); i++)
  349. {
  350. vector<double> v,w;
  351. vother.push_back(v);
  352. vself.push_back(w);
  353. for( int c = 0; c < classno; c++)
  354. {
  355. double tmp_vother = 0.0;
  356. double tmp_self = 0.0;
  357. for(int j = 0; j < (int)regions.size(); j++)
  358. {
  359. if(j == i)
  360. continue;
  361. int x = regions[i].second.x - regions[j].second.x;
  362. int y = regions[i].second.y - regions[j].second.y;
  363. convertCoords(x, xsize);
  364. convertCoords(y, ysize);
  365. double val = priormaps[c]->get(x, y, bestclasses[j]) * alpha[j]; ;
  366. if(bestclasses[j] == bestclasses[i])//Objektbestandteile
  367. {
  368. tmp_self += val;
  369. }
  370. else//Kontextinformationen
  371. {
  372. tmp_vother += val;
  373. }
  374. }
  375. if(fabs(tmp_self) < 10e-7)
  376. tmp_self = 10e-7;
  377. if(fabs(tmp_vother) < 10e-7)
  378. tmp_vother = 10e-7;
  379. vother[i].push_back(tmp_vother);
  380. vself[i].push_back(tmp_self);
  381. }
  382. }
  383. for(int r = 0; r < (int)regions.size(); r++)
  384. {
  385. if(regions[r].second.svec !=NULL)
  386. {
  387. delete regions[r].second.svec;
  388. regions[r].second.svec = NULL;
  389. }
  390. if(regions[r].second.vec !=NULL)
  391. {
  392. delete regions[r].second.vec;
  393. regions[r].second.vec = NULL;
  394. }
  395. regions[r].second.svec = new SparseVector(classno*3);
  396. int counter = 0;
  397. for (int i = 0; i < classno; i++)
  398. {
  399. //appearence feature (old probability for each class
  400. double fapp = log(probabilities.get(regions[r].second.x,regions[r].second.y,i));
  401. if(fabs(fapp) > 10e-7)
  402. (*(regions[r].second.svec))[counter] = fapp;
  403. counter++;
  404. double val = log(vother[r][i]);
  405. if(fabs(val) > 10e-7)
  406. (*(regions[r].second.svec))[counter] = val;
  407. counter++;
  408. val =log(vself[r][i]);
  409. if(fabs(val) > 10e-7)
  410. (*(regions[r].second.svec))[counter] = val;
  411. counter++;
  412. }
  413. }
  414. }
  415. void RelativeLocationPrior::restore (istream & is, int format)
  416. {
  417. is >> classno;
  418. is >> mapsize;
  419. is >> featdim;
  420. //Priorsmaps erzeugen
  421. for(int i = 0; i < classno; i++)
  422. {
  423. GenericImage<double> *tmp = new GenericImage<double>(mapsize, mapsize, classno, true);
  424. tmp->setAll(0.0);
  425. priormaps.push_back(tmp);
  426. }
  427. double val;
  428. for(int i = 0; i < classno; i++)
  429. {
  430. for(int j = 0; j < classno; j++)
  431. {
  432. for(int x = 0; x < mapsize; x++)
  433. {
  434. for(int y = 0; y < mapsize; y++)
  435. {
  436. is >> val;
  437. priormaps[i]->set(x, y, val, j);
  438. }
  439. }
  440. }
  441. }
  442. classifiers.resize(classno);
  443. for(int i = 0; i < classno; i++)
  444. {
  445. classifiers[i] = SLR();
  446. classifiers[i].restore(is, format);
  447. }
  448. }
  449. void RelativeLocationPrior::store (ostream & os, int format) const
  450. {
  451. os << classno << " ";
  452. os << mapsize << " ";
  453. os << featdim << endl;
  454. for(int i = 0; i < classno; i++)
  455. {
  456. for(int j = 0; j < classno; j++)
  457. {
  458. for(int x = 0; x < mapsize; x++)
  459. {
  460. for(int y = 0; y < mapsize; y++)
  461. {
  462. os << priormaps[i]->get(x, y, j) << " ";
  463. }
  464. }
  465. }
  466. }
  467. for(int i = 0; i < classno; i++)
  468. {
  469. classifiers[i].store(os, format);
  470. }
  471. }
  472. void RelativeLocationPrior::clear ()
  473. {
  474. }