SemSegTools.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * @file SemSegTools.cpp
  3. * @brief tools for semantic segmentation
  4. * @author Erik Rodner, Sven Sickert
  5. * @date 03/19/2009
  6. */
  7. #include <iostream>
  8. #include <iomanip>
  9. #include "SemSegTools.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. #undef DEBUG_LOCALIZATION
  14. #undef DEBUG
  15. void SemSegTools::segmentToOverlay (
  16. const NICE::Image *orig,
  17. const NICE::ColorImage & segment,
  18. NICE::ColorImage & result )
  19. {
  20. int xsize = orig->width();
  21. int ysize = orig->height();
  22. result.resize( xsize, ysize );
  23. std::vector< NICE::MatrixT<double> > channelMat;
  24. double alpha = .3;
  25. for (int c = 0; c < 3; c++)
  26. {
  27. NICE::MatrixT<double> chan ( xsize, ysize );
  28. channelMat.push_back( chan );
  29. }
  30. for (int y = 0; y < ysize; y++)
  31. for (int x = 0; x < xsize; x++)
  32. {
  33. uchar val = orig->getPixelQuick(x,y);
  34. for (int c = 0; c < 3; c++)
  35. channelMat[c](x,y) = alpha*(double)val
  36. + (1.0-alpha)*(double)segment.getPixel( x, y, c );
  37. }
  38. for (int y = 0; y < ysize; y++)
  39. for (int x = 0; x < xsize; x++)
  40. for (int c = 0; c < 3; c++)
  41. {
  42. int val = channelMat[c](x,y);
  43. result.setPixel( x, y, c, (uchar)val);
  44. }
  45. }
  46. void SemSegTools::updateConfusionMatrix(
  47. const ImageT<int> &img,
  48. const ImageT<int> &gt,
  49. Matrix &M,
  50. const std::set<int> &forbiddenClasses,
  51. map<int,int> & classMapping )
  52. {
  53. double subsamplex = gt.width() / ( double ) img.width();
  54. double subsampley = gt.height() / ( double ) img.height();
  55. for ( int y = 0 ; y < gt.height() ; y++ )
  56. for ( int x = 0 ; x < gt.width() ; x++ )
  57. {
  58. int xx = ( int ) ( x / subsamplex );
  59. int yy = ( int ) ( y / subsampley );
  60. if ( xx < 0 ) xx = 0;
  61. if ( yy < 0 ) yy = 0;
  62. if ( xx > img.width() - 1 ) xx = img.width() - 1;
  63. if ( yy > img.height() - 1 ) yy = img.height() - 1;
  64. int cimg = img.getPixel ( xx, yy );
  65. int gimg = gt.getPixel ( x, y );
  66. if ( forbiddenClasses.find ( gimg ) == forbiddenClasses.end() )
  67. {
  68. M ( classMapping[gimg], classMapping[cimg] ) ++;
  69. }
  70. }
  71. }
  72. void SemSegTools::computeClassificationStatistics(
  73. Matrix &confMat,
  74. const ClassNames &classNames,
  75. const std::set<int> &forbiddenClasses,
  76. map<int,int> & classMappingInv )
  77. {
  78. std::cout << "\nPERFORMANCE" << std::endl;
  79. std::cout << "###########\n" << std::endl;
  80. double overallTrue = 0.0;
  81. double sumAll = 0.0;
  82. // overall recognition rate
  83. for ( int r = 0; r < (int) confMat.rows(); r++ )
  84. for ( int c = 0; c < (int) confMat.cols(); c++ )
  85. {
  86. if ( r == c )
  87. overallTrue += confMat( r, c );
  88. sumAll += confMat( r, c );
  89. }
  90. overallTrue /= sumAll;
  91. double truePos = (double)confMat(1,1);
  92. //double trueNeg = (double)confMat(0,0);
  93. double falsePos = (double)confMat(0,1);
  94. double falseNeg = (double)confMat(1,0);
  95. // binary classification metrics
  96. if ( classMappingInv.size() == 2 )
  97. {
  98. double precision = truePos / (truePos+falsePos);
  99. double recall = truePos / (truePos+falseNeg);
  100. double f1score = 2.0*(precision*recall)/(precision+recall);
  101. std::cout << "\nPrecision: " << precision;
  102. std::cout << "\nRecall: " << recall;
  103. std::cout << "\nF1Score: " << f1score;
  104. }
  105. // normalizing confMat using rows
  106. for ( int r = 0 ; r < (int) confMat.rows() ; r++ )
  107. {
  108. double sum = 0.0;
  109. for ( int c = 0 ; c < (int) confMat.cols() ; c++ )
  110. sum += confMat ( r, c );
  111. if ( std::fabs ( sum ) > 1e-4 )
  112. for ( int c = 0 ; c < (int) confMat.cols() ; c++ )
  113. confMat ( r, c ) /= sum;
  114. }
  115. // printing confusion matrix
  116. short int printWidth = 16;
  117. std::cout.precision(6);
  118. std::cout << std::setw(printWidth) << "";
  119. for (int r = 0; r < (int) confMat.rows(); r++)
  120. {
  121. int cl = classMappingInv[r];
  122. if ( classNames.existsClassno ( cl )
  123. && ( forbiddenClasses.find ( cl ) == forbiddenClasses.end() ) )
  124. {
  125. std::string cname = classNames.text ( cl );
  126. std::cout << std::setw(printWidth) << cname.c_str();
  127. }
  128. }
  129. std::cout << std::endl;
  130. for (int r = 0; r < (int) confMat.rows(); r++)
  131. {
  132. int cl = classMappingInv[r];
  133. if ( classNames.existsClassno ( cl )
  134. && ( forbiddenClasses.find ( cl ) == forbiddenClasses.end() ) )
  135. {
  136. std::string cname = classNames.text ( cl );
  137. std::cout << std::setw(printWidth) << cname.c_str();
  138. for (int c = 0; c < (int) confMat.cols(); c++)
  139. std::cout << std::setw(printWidth) << std::fixed << confMat (r, c);
  140. std::cout << std::endl;
  141. }
  142. }
  143. // print classification statistics
  144. std::cout << "\nOverall Recogntion Rate: " << overallTrue;
  145. std::cout << "\nAverage Recogntion Rate: " << confMat.trace() / (double)classMappingInv.size();
  146. std::cout << "\nLower Bound: " << 1.0 /(double)classMappingInv.size();
  147. std::cout << std::endl;
  148. }
  149. void SemSegTools::saveResultsToImageFile(
  150. const Config *conf,
  151. const string &section,
  152. const ColorImage &orig,
  153. const ColorImage &gtruth,
  154. const ColorImage &segment,
  155. const string &file )
  156. {
  157. std::string resultDir = conf->gS ( section, "resultdir", "." );
  158. std::string outputType = conf->gS ( section, "output_type", "ppm" );
  159. std::string outputPostfix = conf->gS ( section, "output_postfix", "" );
  160. NICE::ColorImage overlaySegment, overlayGTruth;
  161. NICE::Image* origGrey = orig.getChannel(1);
  162. segmentToOverlay( origGrey, segment, overlaySegment );
  163. segmentToOverlay( origGrey, gtruth, overlayGTruth );
  164. std::stringstream out;
  165. out << resultDir << "/" << file << outputPostfix;
  166. #ifdef DEBUG
  167. std::cout << "Writing to file " << out.str() << "_*." << outputType << std::endl;
  168. #endif
  169. orig.write ( out.str() + "_orig." + outputType );
  170. segment.write ( out.str() + "_result." + outputType );
  171. gtruth.write ( out.str() + "_groundtruth." + outputType );
  172. overlaySegment.write ( out.str() + "_overlay_res." + outputType );
  173. overlayGTruth.write ( out.str() + "_overlay_gt." + outputType );
  174. }
  175. void SemSegTools::collectTrainingExamples (
  176. const Config * conf,
  177. const std::string & section,
  178. const LabeledSet & train,
  179. const ClassNames & cn,
  180. Examples & examples,
  181. vector<CachedExample *> & imgexamples )
  182. {
  183. assert ( train.count() > 0 );
  184. examples.clear();
  185. imgexamples.clear();
  186. int grid_size_x = conf->gI(section, "grid_size_x", 5 );
  187. int grid_size_y = conf->gI(section, "grid_size_y", 5 );
  188. int grid_border_x = conf->gI(section, "grid_border_x", 20 );
  189. int grid_border_y = conf->gI(section, "grid_border_y", 20 );
  190. std::string selection = conf->gS(section, "train_selection" );
  191. set<int> classnoSelection;
  192. cn.getSelection ( selection, classnoSelection );
  193. bool useExcludedAsBG = conf->gB(section, "use_excluded_as_background", false );
  194. int backgroundClassNo = 0;
  195. if ( useExcludedAsBG )
  196. {
  197. backgroundClassNo = cn.classno("various");
  198. assert ( backgroundClassNo >= 0 );
  199. }
  200. LOOP_ALL_S (train)
  201. {
  202. EACH_INFO(image_classno,imgInfo);
  203. std::string imgfn = imgInfo.img();
  204. if ( ! imgInfo.hasLocalizationInfo() ) {
  205. std::cerr << "WARNING: NO localization info found for "
  206. << imgfn << " !" << std::endl;
  207. continue;
  208. }
  209. int xsize, ysize;
  210. CachedExample *ce = new CachedExample ( imgfn );
  211. ce->getImageSize ( xsize, ysize );
  212. imgexamples.push_back ( ce );
  213. const LocalizationResult *locResult = imgInfo.localization();
  214. if ( locResult->size() <= 0 ) {
  215. std::cerr << "WARNING: NO ground truth polygons found for "
  216. << imgfn << " !" << std::endl;
  217. continue;
  218. }
  219. std::cerr << "SemSegTools: Collecting pixel examples from localization info: "
  220. << imgfn << std::endl;
  221. NICE::Image pixelLabels (xsize, ysize);
  222. pixelLabels.set(0);
  223. locResult->calcLabeledImage ( pixelLabels, cn.getBackgroundClass() );
  224. #ifdef DEBUG_LOCALIZATION
  225. NICE::Image img (imgfn);
  226. showImage(img);
  227. showImage(pixelLabels);
  228. #endif
  229. Example pce ( ce, 0, 0 );
  230. for ( int x = 0 ; x < xsize ; x += grid_size_x )
  231. for ( int y = 0 ; y < ysize ; y += grid_size_y )
  232. {
  233. if ( (x >= grid_border_x) &&
  234. ( y >= grid_border_y ) && ( x < xsize - grid_border_x ) &&
  235. ( y < ysize - grid_border_x ) )
  236. {
  237. pce.x = x; pce.y = y;
  238. int classno = pixelLabels.getPixel(x,y);
  239. if ( classnoSelection.find(classno) != classnoSelection.end() ) {
  240. examples.push_back ( pair<int, Example> (
  241. classno,
  242. pce // FIXME: offset handling
  243. ) );
  244. } else if ( useExcludedAsBG ) {
  245. examples.push_back ( pair<int, Example> (
  246. backgroundClassNo,
  247. pce // FIXME: offset handling
  248. ) );
  249. }
  250. }
  251. }
  252. }
  253. std::cerr << "total number of examples: " << (int)examples.size() << std::endl;
  254. }