testSemanticSegmentation.cpp 6.8 KB

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