SemSegNoveltyBinary.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @file SemSegNoveltyBinary.h
  3. * @brief semantic segmentation using the method from Csurka08
  4. * @author Björn Fröhlich, Alexander Freytag
  5. * @date 04/24/2009
  6. */
  7. #ifndef SemSegNoveltyBinaryINCLUDE
  8. #define SemSegNoveltyBinaryINCLUDE
  9. #include "SemanticSegmentation.h"
  10. #include "SemSegTools.h"
  11. #include "vislearning/classifier/classifierbase/FeaturePoolClassifier.h"
  12. #include "vislearning/classifier/genericClassifierSelection.h"
  13. #include "vislearning/features/localfeatures/LocalFeatureColorWeijer.h"
  14. #include "vislearning/cbaselib/ClassificationResults.h"
  15. #include "segmentation/RegionSegmentationMethod.h"
  16. /** @brief pixelwise labeling systems */
  17. namespace OBJREC {
  18. class SemSegNoveltyBinary : public SemanticSegmentation
  19. {
  20. protected:
  21. //! boolean whether to reuse segmentation results for single images in different runs
  22. bool reuseSegmentation;
  23. //! boolean whether to read the initial classifier from a file. If not, training will be performed
  24. bool read_classifier;
  25. //! boolean whether to save the final classifier or not
  26. bool save_classifier;
  27. //! The cached Data
  28. std::string cache;
  29. //! Classifier
  30. FeaturePoolClassifier *classifier;
  31. VecClassifier *vclassifier;
  32. //! feature extraction
  33. LocalFeatureColorWeijer *featExtract;
  34. //! Configuration File
  35. const NICE::Config *conf;
  36. //! distance between features for training
  37. int trainWsize;
  38. //! half of the window size for local features
  39. int whs;
  40. //! rectangle size for classification, 1 means pixelwise
  41. int testWSize;
  42. //! name of all classes
  43. ClassNames cn;
  44. //! low level Segmentation method
  45. RegionSegmentationMethod *regionSeg;
  46. //! set of forbidden/background classes for the initial training
  47. std::set<int> forbidden_classesTrain;
  48. //! set of forbidden/background classes for the whole process of learning over time
  49. std::set<int> forbidden_classesActiveLearning;
  50. //! store the class numbers currently used
  51. std::set<int> classesInUse;
  52. //! only needed for binary scenarios, index of the positive class
  53. int positiveClass;
  54. //! obviously, the number of classes used for training (i.e., classesInUse.size() )
  55. int numberOfClasses;
  56. //! where to save the resulting images (uncertainty and classification results)
  57. std::string resultdir;
  58. //! find the maximum uncertainty or not within the whole test set
  59. bool findMaximumUncert;
  60. //! image with most uncertain region
  61. NICE::ColorImage maskedImg;
  62. //! for debugging and visualization: show novelty images with and without region segmentation and the most novel region
  63. bool visualizeALimages;
  64. //! maximum uncertainty over all images, i.e., the novelty score of the most "novel" region of all test images
  65. double globalMaxUncert;
  66. //! determine whether a "novelty" method computes large scores for novel objects (e.g., variance), or small scores (e.g., min abs mean)
  67. bool mostNoveltyWithMaxScores;
  68. //! current examples for most uncertain region
  69. Examples newTrainExamples;
  70. //! contains filenames of images and indices of contained regions, that where already queried, to prevent them from being queried again
  71. std::map<std::string,std::set<int> > queriedRegions;
  72. std::pair<std::string, int> currentRegionToQuery;
  73. //! store the binary classification results from a single run to evaluate them with AUC lateron
  74. ClassificationResults resultsOfSingleRun;
  75. bool write_results;
  76. enum NoveltyMethod{
  77. GPVARIANCE, // novel = large variance
  78. GPUNCERTAINTY, //novel = small uncertainty (mean / var)
  79. GPMINMEAN, //novel = small mean
  80. GPMEANRATIO, //novel = small difference between mean of most plausible class and mean of snd
  81. // most plausible class (not useful in binary settings)
  82. GPWEIGHTALL, // novel = large weight in alpha vector after updating the model (can be predicted exactly)
  83. GPWEIGHTRATIO, // novel = small difference between weights for alpha vectors with assumptions of GT label to be the most
  84. // plausible against the second most plausible class
  85. RANDOM // query regions randomly
  86. };
  87. //! specify how "novelty" shall be computed, e.g., using GP-variance, GP-uncertainty, or predicted weight entries
  88. NoveltyMethod noveltyMethod;
  89. std::string noveltyMethodString;
  90. //! just store the name of our classifier
  91. std::string classifierString;
  92. inline void computeClassificationResults( const NICE::MultiChannelImageT<double> & feats,
  93. NICE::ImageT<int> & segresult,
  94. NICE::MultiChannelImageT<double> & probabilities,
  95. const int & xsize,
  96. const int & ysize,
  97. const int & featdim );
  98. void computeNoveltyByRandom( NICE::FloatImage & noveltyImage,
  99. const NICE::MultiChannelImageT<double> & feats,
  100. NICE::ImageT<int> & segresult,
  101. NICE::MultiChannelImageT<double> & probabilities,
  102. const int & xsize, const int & ysize, const int & featdim );
  103. void computeNoveltyByVariance( NICE::FloatImage & noveltyImage,
  104. const NICE::MultiChannelImageT<double> & feats,
  105. NICE::ImageT<int> & segresult,
  106. NICE::MultiChannelImageT<double> & probabilities,
  107. const int & xsize, const int & ysize, const int & featdim );
  108. void computeNoveltyByGPUncertainty ( NICE::FloatImage & noveltyImage,
  109. const NICE::MultiChannelImageT<double> & feats,
  110. NICE::ImageT<int> & segresult,
  111. NICE::MultiChannelImageT<double> & probabilities,
  112. const int & xsize, const int & ysize, const int & featdim );
  113. void computeNoveltyByGPMean ( NICE::FloatImage & noveltyImage,
  114. const NICE::MultiChannelImageT<double> & feats,
  115. NICE::ImageT<int> & segresult,
  116. NICE::MultiChannelImageT<double> & probabilities,
  117. const int & xsize, const int & ysize, const int & featdim );
  118. void computeNoveltyByGPMeanRatio ( NICE::FloatImage & noveltyImage,
  119. const NICE::MultiChannelImageT<double> & feats,
  120. NICE::ImageT<int> & segresult,
  121. NICE::MultiChannelImageT<double> & probabilities,
  122. const int & xsize, const int & ysize, const int & featdim );
  123. void computeNoveltyByGPWeightAll ( NICE::FloatImage & noveltyImage,
  124. const NICE::MultiChannelImageT<double> & feats,
  125. NICE::ImageT<int> & segresult,
  126. NICE::MultiChannelImageT<double> & probabilities,
  127. const int & xsize, const int & ysize, const int & featdim );
  128. void computeNoveltyByGPWeightRatio ( NICE::FloatImage & noveltyImage,
  129. const NICE::MultiChannelImageT<double> & feats,
  130. NICE::ImageT<int> & segresult,
  131. NICE::MultiChannelImageT<double> & probabilities,
  132. const int & xsize, const int & ysize, const int & featdim );
  133. public:
  134. /** constructor
  135. * @param conf needs a configfile
  136. * @param md and a MultiDataset (contains images and other things)
  137. */
  138. SemSegNoveltyBinary ( const NICE::Config *conf, const MultiDataset *md );
  139. /** simple destructor */
  140. virtual ~SemSegNoveltyBinary();
  141. /** The trainingstep
  142. * @param md and a MultiDataset (contains images and other things)
  143. */
  144. void train ( const MultiDataset *md );
  145. /** The main procedure. Input: Image, Output: Segmented Image with pixelwise labeles and the probabilities
  146. * @param ce image data
  147. * @param segresult result of the semantic segmentation with a label for each pixel
  148. * @param probabilities multi-channel image with one channel for each class and corresponding probabilities for each pixel
  149. */
  150. void semanticseg ( CachedExample *ce,
  151. NICE::ImageT<int> & segresult,
  152. NICE::MultiChannelImageT<double> & probabilities );
  153. void semanticseg ( CachedExample *ce,
  154. NICE::MultiChannelImageT<int> & segresult,
  155. NICE::MultiChannelImage3DT<double> & probabilities )
  156. {}
  157. /**
  158. * @brief visualize a specific region in the original image
  159. *
  160. * @param img input image
  161. * @param regions map of the regions
  162. * @param region visualize this region
  163. * @param outimage result
  164. * @return void
  165. **/
  166. void visualizeRegion(const NICE::ColorImage &img, const NICE::Matrix &regions, int region, NICE::ColorImage &outimage);
  167. /**
  168. * @brief Add a new example to the known training data
  169. *
  170. * @param newExample (NICE::Vector) the feature vector of the new examples
  171. * @param newClassNo (int) the corresponding GT class number
  172. * @return void
  173. **/
  174. void addNewExample(const NICE::Vector & newExample, const int & newClassNo);
  175. /**
  176. * @brief Add those examples, which belong to the most novel region seen so far
  177. *
  178. * @return void
  179. **/
  180. virtual void addNovelExamples();
  181. /**
  182. * @brief Get a pointer to the examples extracted from the most novel region seen so far
  183. *
  184. * @return Examples *
  185. **/
  186. virtual const Examples * getNovelExamples() const;
  187. /**
  188. * @brief Compute AUC scores from the results of the images computed so far
  189. *
  190. * @return double
  191. **/
  192. double getAUCPerformance() const;
  193. };
  194. } //namespace
  195. #endif