KSquare Utilities
ModelOldSVM.h
Go to the documentation of this file.
1 #if !defined(_MODELOLDSVM_)
2 #define _MODELOLDSVM_
3 
4 
5 
6 #include "RunLog.h"
7 #include "KKStr.h"
8 
9 #include "svm.h"
10 #include "Model.h"
11 #include "SVMModel.h"
12 
13 
14 
15 namespace KKMLL
16 {
17  #if !defined(_CLASSASSIGNMENTS_)
18  class ClassAssignments;
20  #endif
21 
22 
23  #if !defined(_MODELPARAMOLDSVM_)
26 #endif
27 
28 
29  /**
30  *@class ModelOldSVM
31  *@brief A specialization of 'Model'; meant to Wrap the original version of 'SvmModel' class.
32  * It will allow us to use the original implementation using version 2.39 of LibSVM.
33  */
34  class ModelOldSVM: public Model
35  {
36  public:
38 
39  ModelOldSVM ();
40 
41  ModelOldSVM (FactoryFVProducerPtr _factoryFVProducer);
42 
43  /**
44  *@brief Creates a new svm model from the provided example (example) data
45  *@param[in] _name
46  *@param[in] _param The parameters for the svm, and for creating the model.
47  *@param[in] _factoryFVProducer Describes the FeactureVectorProducer, FileDesc, and FeatureVectorList derived classes needed.
48  */
49  ModelOldSVM (const KKStr& _name,
50  const ModelParamOldSVM& _param, // Create new model from
51  FactoryFVProducerPtr _factoryFVProducer
52  );
53 
54  ModelOldSVM (const ModelOldSVM& _madel);
55 
56  virtual ~ModelOldSVM ();
57 
58 
59  virtual
60  ModelOldSVMPtr Duplicate () const;
61 
62  virtual ModelTypes ModelType () const {return ModelTypes::OldSVM;}
63 
64  const ClassAssignments& Assignments () const;
65 
66  virtual
67  KKStr Description () const; /**< Return short user readable description of model. */
68 
70 
72  MLClassPtr class1,
73  MLClassPtr class2
74  );
75 
76  virtual
78 
79  virtual
80  bool NormalizeNominalAttributes () const;
81 
83 
84  void SupportVectorStatistics (kkint32& numSVs,
85  kkint32& totalNumSVs
86  );
87 
88  ModelParamOldSVMPtr Param () const;
89 
91 
92  SVMModelPtr SvmModel () const {return svmModel;}
93 
94 
95  virtual
96  MLClassPtr Predict (FeatureVectorPtr image,
97  RunLog& log
98  );
99 
100  virtual
101  void Predict (FeatureVectorPtr example,
102  MLClassPtr knownClass,
103  MLClassPtr& predClass1,
104  MLClassPtr& predClass2,
105  kkint32& predClass1Votes,
106  kkint32& predClass2Votes,
107  double& probOfKnownClass,
108  double& predClass1Prob,
109  double& predClass2Prob,
110  kkint32& numOfWinners,
111  bool& knownClassOneOfTheWinners,
112  double& breakTie,
113  RunLog& log
114  );
115 
116 
117  virtual void PredictRaw (FeatureVectorPtr example,
118  MLClassPtr& predClass,
119  double& dist
120  );
121 
122 
123  virtual
125  RunLog& log
126  );
127 
128 
129 
130 
131 
132  /**
133  *@brief Will get the probabilities assigned to each class.
134  *@param[in] example unknown example that we want to get predicted probabilities for.
135  *@param[in] _ImageClasses List classes that caller is aware of. This should be the same list that
136  * was used when constructing this ModelOldSVM object. The list must be the
137  * same but not necessarily in the same order as when ModelOldSVM was 1st
138  * constructed. The ordering of this instance dictates how the parameters
139  * '_votes' and '_probabilities' are populated.
140  *@param[out] _votes Number of votes for each class
141  *@param[out] _probabilities An array that must be as big as the number of classes in _mlClasses.
142  * The probability of class in _mlClasses[x] will be returned in _probabilities[x].
143  *@param[in] log Logger.
144  */
145  virtual
147  const MLClassList& _mlClasses,
148  kkint32* _votes,
149  double* _probabilities,
150  RunLog& log
151  );
152 
153 
154  /**
155  *@brief Retrieves probabilities assigned to each class.
156  *@param[in] _example unknown example that we want to get predicted probabilities for.
157  *@param[in] _ImageClasses List classes that caller is aware of. This should be the same list that
158  * was used when constructing this ModelOldSVM object. The list must be the
159  * same but not necessarily in the same order as when ModelOldSVM was 1st
160  * constructed.
161  *@param[out] _probabilities An array that must be as big as the number of classes in _mlClasses.
162  * The probability of class in _mlClasses[x] will be returned in _probabilities[x].
163  *@param[in] _log Logger to send messages to.
164  */
165  virtual
166  void ProbabilitiesByClass (FeatureVectorPtr _example,
167  const MLClassList& _mlClasses,
168  double* _probabilities,
169  RunLog& _log
170  );
171 
172 
173 
174  /**
175  *@brief For a given two class pair return the names of the 'numToFind' worst S/V's.
176  *@details This method will iterate through all the S/V's removing them one at a
177  * time and re-compute the decision boundary and probability. It will then
178  * return the S/V's that when removed improve the probability in 'c1's
179  * the most.
180  *@param[in] example Example that was classified incorrectly.
181  *@param[in] numToFind The number of the worst examples you are looking for.
182  *@param[in] c1 Class that the 'example; parameter should have been classed as.
183  *@param[in] c2 Class that it was classified as.
184  */
187  MLClassPtr c1,
188  MLClassPtr c2
189  );
190 
191 
192  /**
193  *@brief For a given two class pair return the names of the 'numToFind' worst S/V's.
194  *@details This method will iterate through all the S/V's removing them one at a
195  * time and retraining a new SVM and then comparing with the new prediction results.
196  *@param[in] example Example that was classified incorrectly.
197  *@param[in] numToFind The number of the worst examples you are looking for.
198  *@param[in] c1 Class that the 'example; parameter should have been classed as.
199  *@param[in] c2 Class that it was classified as.
200  */
203  MLClassPtr c1,
204  MLClassPtr c2
205  );
206 
207 
208  /**
209  *@brief ModelOldSVM Specific 'PrepExampleForPrediction' will only normalize data.
210  *@param[in] fv Feature vector of example that needs to be prepared.
211  *@param[out] newExampleCreated Indicates if either Feature Encoding and/or Normalization needed
212  * to be done. If neither then the original instance is returned. If Yes then
213  * a new instance which the caller will have to be delete will be returned.
214  */
215  virtual
217  bool& newExampleCreated
218  );
219 
220  std::vector<KKStr> SupportVectorNames () const;
221 
222 
224  MLClassPtr c2
225  ) const;
226 
227 
228  void RetrieveCrossProbTable (MLClassList& classes,
229  double** crossProbTable, /**< two dimension matrix that needs to be classes.QueueSize () squared. */
230  RunLog& log
231  );
232 
233 
234  /**
235  * @brief Use given training data to create a trained Model that can be used for classifying examples.
236  * @param[in] _trainExamples The example data we will be building the model from.
237  * @param[in] _alreadyNormalized Specifies weather the training data has already been normalized.
238  * @param[in] _takeOwnership If true this instance will take ownership of '_trainExamples' and delete it when done with it.
239  * @param[in] _cancelFlag Monitored; when set to True will end processing and return to caller.
240  * @param[in] _log Logger to send messages to. */
241  virtual void TrainModel (FeatureVectorListPtr _trainExamples,
242  bool _alreadyNormalized,
243  bool _takeOwnership, /*!< Model will take ownership of these examples */
244  VolConstBool& _cancelFlag,
245  RunLog& _log
246  );
247 
248 
249  virtual void ReadXML (XmlStream& s,
250  XmlTagConstPtr tag,
251  VolConstBool& cancelFlag,
252  RunLog& log
253  );
254 
255 
256  virtual void WriteXML (const KKStr& varName,
257  std::ostream& o
258  ) const;
259 
260  private:
261  ClassAssignmentsPtr assignments;
262  SVMModelPtr svmModel;
263  };
264 
265 
266  typedef ModelOldSVM::ModelOldSVMPtr ModelOldSVMPtr;
267 
268 
271 
272 } /* namespace KKMLL */
273 
274 
275 
276 #endif
ModelOldSVM(const KKStr &_name, const ModelParamOldSVM &_param, FactoryFVProducerPtr _factoryFVProducer)
Creates a new svm model from the provided example (example) data.
Definition: ModelOldSVM.cpp:62
Base class to all Learning Algorithms.
Definition: Model.h:82
virtual bool NormalizeNominalAttributes() const
ClassAssignments * ClassAssignmentsPtr
SVMModel * SVMModelPtr
Definition: SVMModel.h:557
std::vector< ProbNamePair > FindWorstSupportVectors(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
std::vector< KKStr > SupportVectorNames() const
SVM_SelectionMethod
Definition: SVMparam.h:34
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureVector * FeatureVectorPtr
Definition: Model.h:44
virtual ~ModelOldSVM()
Definition: ModelOldSVM.cpp:87
SVMModelPtr SvmModel() const
Definition: ModelOldSVM.h:92
ModelParamOldSVMPtr Param() const
ModelOldSVM(FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelOldSVM.cpp:52
FeatureNumListConstPtr GetFeatureNums() const
ModelOldSVM * ModelOldSVMPtr
Definition: ModelOldSVM.h:37
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 ...
void SupportVectorStatistics(kkint32 &numSVs, kkint32 &totalNumSVs)
virtual void PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
Will get the probabilities assigned to each class.
XmlElementModelOldSVM * XmlElementModelOldSVMPtr
Definition: ModelOldSVM.h:270
virtual ModelOldSVMPtr Duplicate() const
FeatureNumListConstPtr GetFeatureNums(FileDescPtr filedesc, MLClassPtr class1, MLClassPtr class2)
virtual ModelTypes ModelType() const
Definition: ModelOldSVM.h:62
kkint32 NumOfSupportVectors() const
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
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)
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
Binds MLClass objects to the appropriate number that the Learning Algorithm expects.
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2) const
std::vector< ProbNamePair > FindWorstSupportVectors2(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ClassProbList * ClassProbListPtr
Definition: Classifier2.h:30
virtual KKStr Description() const
virtual void TrainModel(FeatureVectorListPtr _trainExamples, bool _alreadyNormalized, bool _takeOwnership, VolConstBool &_cancelFlag, RunLog &_log)
Use given training data to create a trained Model that can be used for classifying examples...
FileDesc * FileDescPtr
SVM_SelectionMethod SelectionMethod() const
ModelParamOldSVM * ModelParamOldSVMPtr
Definition: ModelOldSVM.h:24
const ClassAssignments & Assignments() const
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
ModelOldSVM(const ModelOldSVM &_madel)
Definition: ModelOldSVM.cpp:75
virtual MLClassPtr Predict(FeatureVectorPtr image, RunLog &log)
XmlElementModelTemplate< ModelOldSVM > XmlElementModelOldSVM
Definition: ModelOldSVM.h:269
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
ModelOldSVM Specific &#39;PrepExampleForPrediction&#39; will only normalize data.
FeatureNumListConst * FeatureNumListConstPtr
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)
Maintains a list of MLClass instances.
Definition: MLClass.h:233
FeatureVectorList * FeatureVectorListPtr
Definition: Model.h:46
FactoryFVProducer * FactoryFVProducerPtr
Definition: Model.h:75
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Retrieves probabilities assigned to each class.
virtual kkint32 MemoryConsumedEstimated() const
Definition: ModelOldSVM.cpp:97
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163