testSemanticSegmentation.cpp 6.8 KB

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