testSemanticSegmentation.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Beispielhafter Aufruf: BUILD_x86_64/progs/testSemanticSegmentation -config <CONFIGFILE>
  2. /**
  3. * @file testSemanticSegmentation.cpp
  4. * @brief test semantic segmentation routines
  5. * @author Erik Rodner
  6. * @date 03/20/2008
  7. */
  8. #ifdef NICE_USELIB_OPENMP
  9. #include <omp.h>
  10. #endif
  11. #include <objrec/baselib/Config.h>
  12. #include <objrec/baselib/StringTools.h>
  13. #include <objrec/baselib/ICETools.h>
  14. #include <objrec-froehlichexp/semseg/SemanticSegmentation.h>
  15. #include <objrec-froehlichexp/semseg/SemSegLocal.h>
  16. #include <objrec-froehlichexp/semseg/SemSegSTF.h>
  17. #include <objrec-froehlichexp/semseg/SemSegCsurka.h>
  18. #include <objrec-froehlichexp/semseg/SemSegCsurka2.h>
  19. #include <objrec-froehlichexp/semseg/SemSegRegionBased.h>
  20. #include <objrec-froehlichexp/semseg/SemSegContextTree.h>
  21. #include <fstream>
  22. using namespace OBJREC;
  23. using namespace NICE;
  24. using namespace std;
  25. void updateMatrix(const NICE::Image & img, const NICE::Image & gt,
  26. NICE::Matrix & M, const set<int> & forbidden_classes)
  27. {
  28. double subsamplex = gt.width() / (double)img.width();
  29. double subsampley = gt.height() / (double)img.height();
  30. for (int y = 0 ; y < gt.height() ; y++)
  31. for (int x = 0 ; x < gt.width() ; x++)
  32. {
  33. int xx = (int)(x / subsamplex);
  34. int yy = (int)(y / subsampley);
  35. if (xx < 0) xx = 0;
  36. if (yy < 0) yy = 0;
  37. if (xx > img.width() - 1) xx = img.width() - 1;
  38. if (yy > img.height() - 1) yy = img.height() - 1;
  39. int cimg = img.getPixel(xx, yy);
  40. int gimg = gt.getPixel(x, y);
  41. if (forbidden_classes.find(gimg) == forbidden_classes.end())
  42. {
  43. M(gimg, cimg)++;
  44. }
  45. }
  46. }
  47. /**
  48. test semantic segmentation routines
  49. */
  50. int main(int argc, char **argv)
  51. {
  52. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  53. Config conf(argc, argv);
  54. bool show_result = conf.gB("debug", "show_results", false);
  55. bool write_results = conf.gB("debug", "write_results", false);
  56. bool write_results_pascal = conf.gB("debug", "write_results_pascal", false);
  57. std::string resultdir = conf.gS("debug", "resultdir", ".");
  58. if (write_results)
  59. {
  60. cerr << "Writing Results to " << resultdir << endl;
  61. }
  62. MultiDataset md(&conf);
  63. const ClassNames & classNames = md.getClassNames("train");
  64. string method = conf.gS("main","method","SSCsurka");
  65. SemanticSegmentation *semseg = NULL;
  66. if(method == "SSCsurka")
  67. {
  68. semseg = new SemSegCsurka ( &conf, &md);
  69. }
  70. else if(method == "SSContext")
  71. {
  72. semseg = new SemSegContextTree ( &conf, &md);
  73. }
  74. //SemanticSegmentation *semseg = new SemSegLocal ( &conf, &md );
  75. //SemanticSegmentation *semseg = new SemSegSTF ( &conf, &md );
  76. //SemanticSegmentation *semseg = new SemSegRegionBased(&conf, &md);
  77. const LabeledSet *testFiles = md["test"];
  78. NICE::Matrix M(classNames.getMaxClassno() + 1, classNames.getMaxClassno() + 1);
  79. M.set(0);
  80. set<int> forbidden_classes;
  81. std::string forbidden_classes_s = conf.gS("analysis", "forbidden_classes", "");
  82. classNames.getSelection(forbidden_classes_s, forbidden_classes);
  83. ProgressBar pb("Semantic Segmentation Analysis");
  84. pb.show();
  85. int fileno = 0;
  86. LOOP_ALL_S(*testFiles)
  87. {
  88. EACH_INFO(classno, info);
  89. std::string file = info.img();
  90. NICE::Image lm;
  91. GenericImage<double> probabilities;
  92. if (info.hasLocalizationInfo())
  93. {
  94. const LocalizationResult *l_gt = info.localization();
  95. lm.resize(l_gt->xsize, l_gt->ysize);
  96. lm.set(0);
  97. l_gt->calcLabeledImage(lm, classNames.getBackgroundClass());
  98. }
  99. semseg->semanticseg(file, lm, probabilities);
  100. fprintf(stderr, "testSemanticSegmentation: Segmentation finished !\n");
  101. NICE::Image lm_gt;
  102. if (info.hasLocalizationInfo())
  103. {
  104. const LocalizationResult *l_gt = info.localization();
  105. lm_gt.resize(l_gt->xsize, l_gt->ysize);
  106. lm_gt.set(0);
  107. fprintf(stderr, "testSemanticSegmentation: Generating Labeled NICE::Image (Ground-Truth)\n");
  108. l_gt->calcLabeledImage(lm_gt, classNames.getBackgroundClass());
  109. }
  110. std::string fname = StringTools::baseName(file, false);
  111. if (write_results_pascal)
  112. {
  113. NICE::Image pascal_lm(lm.width(), lm.height());
  114. int backgroundClass = classNames.getBackgroundClass();
  115. for (int y = 0 ; y < lm.height(); y++)
  116. for (int x = 0 ; x < lm.width(); x++)
  117. {
  118. int v = lm.getPixel(x, y);
  119. if (v == backgroundClass)
  120. pascal_lm.setPixel(x, y, 255);
  121. else
  122. pascal_lm.setPixel(x, y, 255 - v - 1);
  123. }
  124. char filename[1024];
  125. char *format = (char *)"pgm";
  126. sprintf(filename, "%s/%s.%s", resultdir.c_str(), fname.c_str(), format);
  127. pascal_lm.write(filename);
  128. }
  129. if (show_result || write_results)
  130. {
  131. NICE::ColorImage orig(file);
  132. NICE::ColorImage rgb;
  133. NICE::ColorImage rgb_gt;
  134. classNames.labelToRGB(lm, rgb);
  135. classNames.labelToRGB(lm_gt, rgb_gt);
  136. if (write_results)
  137. {
  138. char filename[1024];
  139. char *format = (char *)"ppm";
  140. sprintf(filename, "%06d.%s", fileno, format);
  141. std::string origfilename = resultdir + "/orig_" + string(filename);
  142. cerr << "Writing to file " << origfilename << endl;
  143. orig.write(origfilename);
  144. rgb.write(resultdir + "/result_" + string(filename));
  145. rgb_gt.write(resultdir + "/groundtruth_" + string(filename));
  146. }
  147. if (show_result)
  148. {
  149. #ifndef NOVISUAL
  150. showImage(rgb, "Result");
  151. showImage(rgb_gt, "Groundtruth");
  152. showImage(orig, "Input");
  153. #endif
  154. }
  155. }
  156. //#pragma omp critical
  157. updateMatrix(lm, lm_gt, M, forbidden_classes);
  158. cerr << M << endl;
  159. fileno++;
  160. pb.update(testFiles->count());
  161. }
  162. pb.hide();
  163. double overall = 0.0;
  164. double sumall = 0.0;
  165. for(int r = 0; r < (int)M.rows(); r++)
  166. {
  167. for(int c = 0; c < (int)M.cols(); c++)
  168. {
  169. if(r == c)
  170. overall += M(r,c);
  171. sumall += M(r,c);
  172. }
  173. }
  174. overall /= sumall;
  175. // normalizing M using rows
  176. for (int r = 0 ; r < (int)M.rows() ; r++)
  177. {
  178. double sum = 0.0;
  179. for (int c = 0 ; c < (int)M.cols() ; c++)
  180. sum += M(r, c);
  181. if (fabs(sum) > 1e-4)
  182. for (int c = 0 ; c < (int)M.cols() ; c++)
  183. M(r, c) /= sum;
  184. }
  185. cerr << M << endl;
  186. double avg_perf = 0.0;
  187. int classes_trained = 0;
  188. for (int r = 0 ; r < (int)M.rows() ; r++)
  189. {
  190. if ((classNames.existsClassno(r)) && (forbidden_classes.find(r) == forbidden_classes.end()))
  191. {
  192. avg_perf += M(r, r);
  193. classes_trained++;
  194. }
  195. }
  196. if (write_results)
  197. {
  198. ofstream fout((resultdir + "/res.txt").c_str(), ios::out);
  199. fout << "overall: " << overall << endl;
  200. fout << "Average Performance " << avg_perf / (classes_trained) << endl;
  201. fout << "Lower Bound " << 1.0 / classes_trained << endl;
  202. for (int r = 0 ; r < (int)M.rows() ; r++)
  203. {
  204. if ((classNames.existsClassno(r)) && (forbidden_classes.find(r) == forbidden_classes.end()))
  205. {
  206. std::string classname = classNames.text(r);
  207. fout << classname.c_str() << ": " << M(r, r) << endl;
  208. }
  209. }
  210. fout.close();
  211. }
  212. fprintf(stderr, "overall: %f\n", overall);
  213. fprintf(stderr, "Average Performance %f\n", avg_perf / (classes_trained));
  214. //fprintf(stderr, "Lower Bound %f\n", 1.0 / classes_trained);
  215. for (int r = 0 ; r < (int)M.rows() ; r++)
  216. {
  217. if ((classNames.existsClassno(r)) && (forbidden_classes.find(r) == forbidden_classes.end()))
  218. {
  219. std::string classname = classNames.text(r);
  220. fprintf(stderr, "%s: %f\n", classname.c_str(), M(r, r));
  221. }
  222. }
  223. delete semseg;
  224. return 0;
  225. }