GenericClusterAlgorithmSelection.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file GenericClusterAlgorithmSelection.h
  3. * @brief This class provides a generic chooser function for different clustering techniques
  4. * @date 14-06-2013 (dd-mm-yyyy)
  5. * @author Alexander Freytag
  6. */
  7. #ifndef _NICE_GENERICCLUSTERALGORITHMSELECTION_INCLUDE
  8. #define _NICE_GENERICCLUSTERALGORITHMSELECTION_INCLUDE
  9. #include <iostream>
  10. //abstract base class
  11. #include "vislearning/math/cluster/ClusterAlgorithm.h"
  12. //derived specializations
  13. #include "vislearning/math/cluster/GMM.h"
  14. #include "vislearning/math/cluster/KMeans.h"
  15. #include "vislearning/math/cluster/KMeansHeuristic.h"
  16. #include "vislearning/math/cluster/KMeansMatlab.h"
  17. #include "vislearning/math/cluster/KMedian.h"
  18. #include "vislearning/math/cluster/RandomClustering.h"
  19. #include "vislearning/math/cluster/SpectralCluster.h"
  20. namespace OBJREC {
  21. /** @class GenericClusterAlgorithmSelection
  22. * @brief This class provides a generic chooser function for different clustering techniques
  23. * The abstract base class is ClusterAlgorithm
  24. * @date 14-06-2013 (dd-mm-yyyy)
  25. * @author Alexander Freytag
  26. */
  27. class GenericClusterAlgorithmSelection
  28. {
  29. public:
  30. /**
  31. * @brief This methode switches between the different clustering techniques.
  32. * @param[in] conf - A pointer to the given configfile, which should contain "section" - "clusterTechnique"
  33. * @param[in] section - This string defines the value for "section" in the configfile.
  34. * @return ClusterAlgorithm* - The ClusterAlgorithm to cluster samples according to the selected clustering technique.
  35. * @date 14-06-2013 (dd-mm-yyyy)
  36. * @author Alexander Freytag
  37. */
  38. static
  39. OBJREC::ClusterAlgorithm *selectClusterAlgorithm ( const NICE::Config *conf, std::string section = "clustering" )
  40. {
  41. // return value
  42. OBJREC::ClusterAlgorithm *clusterAlgo = NULL;
  43. // string which defines the clustering technique
  44. std::string clusterTechnique = conf->gS(section, "clusterTechnique", "");
  45. std::cerr << "clusterTechnique: " << clusterTechnique << std::endl;
  46. if ( ( clusterTechnique == "kmeans" ) || ( clusterTechnique == "KMeans" ) )
  47. {
  48. clusterAlgo = new OBJREC::KMeans ( conf );
  49. }
  50. else if ( ( clusterTechnique == "kmeansHeuristic" )|| ( clusterTechnique == "KMeansHeuristic" ) )
  51. {
  52. clusterAlgo = new OBJREC::KMeansHeuristic ( conf );
  53. }
  54. else if ( ( clusterTechnique == "kmeansMatlab" )|| ( clusterTechnique == "KMeansMatlab" ) )
  55. {
  56. clusterAlgo = new OBJREC::KMeansMatlab ( conf );
  57. }
  58. else if ( ( clusterTechnique == "kmedian" )|| ( clusterTechnique == "KMedian" ) )
  59. {
  60. clusterAlgo = new OBJREC::KMedian ( conf );
  61. }
  62. else if ( clusterTechnique == "GMM" )
  63. {
  64. clusterAlgo = new OBJREC::GMM ( conf );
  65. }
  66. else if ( ( clusterTechnique == "spectral" )|| ( clusterTechnique == "SpectralCluster" ) )
  67. {
  68. clusterAlgo = new OBJREC::SpectralCluster ( conf );
  69. }
  70. else if ( clusterTechnique == "RandomClustering" )
  71. {
  72. clusterAlgo = new OBJREC::RandomClustering ( conf );
  73. }
  74. else
  75. {
  76. //default: random clustering - it is easy, fast, does not need extra memory, and is still better than a NULL pointer
  77. std::cerr << "Unknown cluster algorithm selected, use random clustering instead" << std::endl;
  78. clusterAlgo = new OBJREC::RandomClustering ( conf );
  79. }
  80. return clusterAlgo;
  81. };
  82. static
  83. void restoreClusterAlgorithm ( ClusterAlgorithm * _clusterAlgo, std::istream & is, int format = 0 )
  84. {
  85. if ( is.good() )
  86. {
  87. if ( _clusterAlgo != NULL )
  88. delete _clusterAlgo;
  89. std::string className;
  90. is >> className; //class name
  91. if ( className == "<KMeans>" )
  92. {
  93. _clusterAlgo = new OBJREC::KMeans();
  94. }
  95. else if ( className == "<KMeansHeuristic>" )
  96. {
  97. _clusterAlgo = new OBJREC::KMeansHeuristic();
  98. }
  99. else if ( className == "<KMeansMatlab>" )
  100. {
  101. _clusterAlgo = new OBJREC::KMeansMatlab();
  102. }
  103. else if ( className == "<KMedian>" )
  104. {
  105. _clusterAlgo = new OBJREC::KMedian();
  106. }
  107. else if ( className == "<GMM>" )
  108. {
  109. _clusterAlgo = new OBJREC::GMM();
  110. }
  111. else if ( className == "<SpectralCluster>" )
  112. {
  113. _clusterAlgo = new OBJREC::SpectralCluster();
  114. }
  115. else if ( className == "<RandomClustering>" )
  116. {
  117. _clusterAlgo = new OBJREC::RandomClustering();
  118. }
  119. else
  120. {
  121. fthrow ( NICE::Exception, "GenericClusterAlgorithmSelection::restoreClusterAlgo -- class name " << className << "unknown. Aborting." );
  122. }
  123. //undo reading of class name
  124. for ( uint i = 0; i < className.size(); i++)
  125. {
  126. is.unget();
  127. }
  128. //now, call the restore method of the underlying object
  129. //NOTE this could be also done externally, leaving only the actual instantiation of the derived objects here
  130. _clusterAlgo->restore ( is );
  131. }
  132. else
  133. {
  134. fthrow ( NICE::Exception, "GenericClusterAlgorithmSelection::restoreClusterAlgo -- InStream not initialized - restoring not possible!" );
  135. }
  136. };
  137. };
  138. }
  139. #endif