RelativeLocationPrior.cpp 12 KB

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