KSquare Utilities
ModelSvmBase.h
Go to the documentation of this file.
1 #ifndef _MODELSVMBASE_
2 #define _MODELSVMBASE_
3 //***********************************************************************
4 //* ModelSvmBase *
5 //* *
6 //* This will be the base model for implementations that utilize *
7 //* the Support Vector Machine. *
8 //***********************************************************************
9 
10 
11 #include "RunLog.h"
12 #include "KKStr.h"
13 
14 #include "Model.h"
15 #include "ModelParam.h"
16 #include "ModelParamSvmBase.h"
17 #include "svm2.h"
18 
19 
20 namespace KKMLL
21 {
22  class ModelSvmBase: public Model
23  {
24  public:
25 
27 
28 
29  ModelSvmBase ();
30 
31  ModelSvmBase (FactoryFVProducerPtr _factoryFVProducer);
32 
33  ModelSvmBase (const KKStr& _name,
34  const ModelParamSvmBase& _param, // Create new model from
35  FactoryFVProducerPtr _factoryFVProducer
36  );
37 
38  ModelSvmBase (const ModelSvmBase& _model);
39 
40  virtual ~ModelSvmBase ();
41 
42  virtual KKStr Description () const;
43 
44  virtual ModelSvmBasePtr Duplicate () const;
45 
46  virtual kkint32 MemoryConsumedEstimated () const;
47 
48  virtual ModelTypes ModelType () const {return ModelTypes::SvmBase;}
49 
50  virtual kkint32 NumOfSupportVectors () const;
51 
52  ModelParamSvmBasePtr Param ();
53 
54  virtual MLClassPtr Predict (FeatureVectorPtr image,
55  RunLog& log
56  );
57 
58  virtual
59  void Predict (FeatureVectorPtr example,
60  MLClassPtr knownClass,
61  MLClassPtr& predClass1,
62  MLClassPtr& predClass2,
63  kkint32& predClass1Votes,
64  kkint32& predClass2Votes,
65  double& probOfKnownClass,
66  double& predClass1Prob,
67  double& predClass2Prob,
68  kkint32& numOfWinners,
69  bool& knownClassOneOfTheWinners,
70  double& breakTie,
71  RunLog& log
72  );
73 
74 
75  virtual
76  ClassProbListPtr ProbabilitiesByClass (FeatureVectorPtr example,
77  RunLog& log
78  );
79 
80 
81 
82  virtual
83  void ProbabilitiesByClass (FeatureVectorPtr example,
84  const MLClassList& _mlClasses,
85  kkint32* _votes,
86  double* _probabilities,
87  RunLog& _log
88  );
89 
90  /**
91  *@brief Derives predicted probabilities by class.
92  *@details Will get the probabilities assigned to each class by the classifier. The
93  * '_mlClasses' parameter dictates the order of the classes. That is the
94  * probabilities for any given index in '_probabilities' will be for the class
95  * specified in the same index in '_mlClasses'.
96  *@param[in] _example FeatureVector object to calculate predicted probabilities for.
97  *@param[in] _mlClasses List image classes that caller is aware of. This should be the
98  * same list that was used when constructing this Model object. The list must
99  * be the same but not necessarily in the same order as when Model was 1st
100  * constructed.
101  *@param[out] _probabilities An array that must be as big as the number of classes as in
102  * mlClasses. The probability of class in mlClasses[x] will be returned
103  * in probabilities[x].
104  */
105  virtual
106  void ProbabilitiesByClass (FeatureVectorPtr _example,
107  const MLClassList& _mlClasses,
108  double* _probabilities,
109  RunLog& _log
110  );
111 
112 
113  virtual
114  void RetrieveCrossProbTable (MLClassList& classes,
115  double** crossProbTable, /**< two dimension matrix that needs to be classes.QueueSize () squared. */
116  RunLog& log
117  );
118 
119 
120 
121  virtual void TrainModel (FeatureVectorListPtr _trainExamples,
122  bool _alreadyNormalized,
123  bool _takeOwnership, /*!< Model will take ownership of these examples */
124  VolConstBool& _cancelFlag,
125  RunLog& _log
126  );
127 
128 
129  virtual void ReadXML (XmlStream& s,
130  XmlTagConstPtr tag,
131  VolConstBool& cancelFlag,
132  RunLog& log
133  );
134 
135 
136  virtual void WriteXML (const KKStr& varName,
137  ostream& o
138  ) const;
139 
140 
141 
142  protected:
143  SVM289_MFS::Svm_Model* svmModel;
144  ModelParamSvmBasePtr param; /*!< We will NOT own this instance. It will point to same instance defined in parent class Model. */
145  }; /* ModelSvmBase */
146 
147  typedef ModelSvmBase::ModelSvmBasePtr ModelSvmBasePtr;
148 
149 
152 } /* namespace KKMLL */
153 
154 #endif
Base class to all Learning Algorithms.
Definition: Model.h:82
__int32 kkint32
Definition: KKBaseTypes.h:88
virtual void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
virtual ModelSvmBasePtr Duplicate() const
virtual ModelTypes ModelType() const
Definition: ModelSvmBase.h:48
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &_log)
XmlElementModelSvmBase * XmlElementModelSvmBasePtr
Definition: ModelSvmBase.h:151
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
To be implemented by derived classes; the parent classes fields will be updated by the derived class ...
virtual void Predict(FeatureVectorPtr example, MLClassPtr knownClass, MLClassPtr &predClass1, MLClassPtr &predClass2, kkint32 &predClass1Votes, kkint32 &predClass2Votes, double &probOfKnownClass, double &predClass1Prob, double &predClass2Prob, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners, double &breakTie, RunLog &log)
ModelSvmBase(FactoryFVProducerPtr _factoryFVProducer)
XmlElementModelTemplate< ModelSvmBase > XmlElementModelSvmBase
Definition: ModelSvmBase.h:150
virtual KKStr Description() const
virtual void WriteXML(const KKStr &varName, ostream &o) const
virtual MLClassPtr Predict(FeatureVectorPtr image, RunLog &log)
ModelParamSvmBasePtr Param()
virtual ~ModelSvmBase()
Frees any memory allocated by, and owned by the ModelSvmBase.
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
virtual void TrainModel(FeatureVectorListPtr _trainExamples, bool _alreadyNormalized, bool _takeOwnership, VolConstBool &_cancelFlag, RunLog &_log)
Performs operations such as FeatureEncoding, and Normalization. The actual training of models occurs ...
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ModelParamSvmBasePtr param
Definition: ModelSvmBase.h:144
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Derives predicted probabilities by class.
virtual kkint32 MemoryConsumedEstimated() const
ModelSvmBase(const ModelSvmBase &_model)
ModelSvmBase * ModelSvmBasePtr
Definition: ModelSvmBase.h:26
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
ModelSvmBase(const KKStr &_name, const ModelParamSvmBase &_param, FactoryFVProducerPtr _factoryFVProducer)
Maintains a list of MLClass instances.
Definition: MLClass.h:233
virtual kkint32 NumOfSupportVectors() const
SVM289_MFS::Svm_Model * svmModel
Definition: ModelSvmBase.h:143
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163