testSemanticSegmentation.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. cout << 1 << endl;
  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. cout << 7 << endl;
  152. }
  153. pb.hide();
  154. double overall = 0.0;
  155. double sumall = 0.0;
  156. for(int r = 0; r < (int)M.rows(); r++)
  157. {
  158. for(int c = 0; c < (int)M.cols(); c++)
  159. {
  160. if(r == c)
  161. overall += M(r,c);
  162. sumall += M(r,c);
  163. }
  164. }
  165. overall /= sumall;
  166. // normalizing M using rows
  167. for (int r = 0 ; r < (int)M.rows() ; r++)
  168. {
  169. double sum = 0.0;
  170. for (int c = 0 ; c < (int)M.cols() ; c++)
  171. sum += M(r, c);
  172. if (fabs(sum) > 1e-4)
  173. for (int c = 0 ; c < (int)M.cols() ; c++)
  174. M(r, c) /= sum;
  175. }
  176. cerr << M << endl;
  177. double avg_perf = 0.0;
  178. int classes_trained = 0;
  179. for (int r = 0 ; r < (int)M.rows() ; r++)
  180. {
  181. if ((classNames.existsClassno(r)) && (forbidden_classes.find(r) == forbidden_classes.end()))
  182. {
  183. avg_perf += M(r, r);
  184. classes_trained++;
  185. }
  186. }
  187. if (write_results)
  188. {
  189. ofstream fout((resultdir + "/res.txt").c_str(), ios::out);
  190. fout << "overall: " << overall << endl;
  191. fout << "Average Performance " << avg_perf / (classes_trained) << endl;
  192. fout << "Lower Bound " << 1.0 / classes_trained << endl;
  193. for (int r = 0 ; r < (int)M.rows() ; r++)
  194. {
  195. if ((classNames.existsClassno(r)) && (forbidden_classes.find(r) == forbidden_classes.end()))
  196. {
  197. std::string classname = classNames.text(r);
  198. fout << classname.c_str() << ": " << M(r, r) << endl;
  199. }
  200. }
  201. fout.close();
  202. }
  203. cerr << "overall: " << overall << endl;
  204. fprintf(stderr, "Average Performance %f\n", avg_perf / (classes_trained));
  205. //fprintf(stderr, "Lower Bound %f\n", 1.0 / classes_trained);
  206. for (int r = 0 ; r < (int)M.rows() ; r++)
  207. {
  208. if ((classNames.existsClassno(r)) && (forbidden_classes.find(r) == forbidden_classes.end()))
  209. {
  210. std::string classname = classNames.text(r);
  211. fprintf(stderr, "%s: %f\n", classname.c_str(), M(r, r));
  212. }
  213. }
  214. delete semseg;
  215. return 0;
  216. }