KSquare Utilities
ModelKnn.h
Go to the documentation of this file.
1 #ifndef _MODELKNN_
2 #define _MODELKNN_
3 
4 #include "FeatureVector.h"
5 #include "Model.h"
6 #include "ModelParamKnn.h"
7 
8 namespace KKMLL
9 {
10  class ModelKnn: public Model
11  {
12  public:
14 
15  ModelKnn ();
16 
17  ModelKnn (FactoryFVProducerPtr _factoryFVProducer);
18 
19  ModelKnn (const KKStr& _name,
20  const ModelParamKnn& _param, // Create new model from
21  FactoryFVProducerPtr _factoryFVProducer
22  );
23 
24  ModelKnn (const ModelKnn& _madel);
25 
26  virtual ~ModelKnn();
27 
28 
29  virtual
30  ModelKnnPtr Duplicate () const;
31 
32  virtual ModelTypes ModelType () const {return ModelTypes::KNN;}
33 
34 
35  ModelParamKnnPtr Param ();
36 
37 
38  virtual
39  MLClassPtr Predict (FeatureVectorPtr example,
40  RunLog& log
41  );
42 
43 
44  virtual
45  void Predict (FeatureVectorPtr example,
46  MLClassPtr knownClass,
47  MLClassPtr& predClass1,
48  MLClassPtr& predClass2,
49  kkint32& predClass1Votes,
50  kkint32& predClass2Votes,
51  double& probOfKnownClass,
52  double& predClass1Prob,
53  double& predClass2Prob,
54  kkint32& numOfWinners,
55  bool& knownClassOneOfTheWinners,
56  double& breakTie,
57  RunLog& log
58  );
59 
60 
61  virtual
62  ClassProbListPtr ProbabilitiesByClass (FeatureVectorPtr example,
63  RunLog& log
64  );
65 
66  virtual
67  void ProbabilitiesByClass (FeatureVectorPtr example,
68  const MLClassList& _mlClasses,
69  kkint32* _votes,
70  double* _probabilities,
71  RunLog& log
72  );
73 
74 
75  /**
76  *@brief Derives predicted probabilities by class.
77  *@details Will get the probabilities assigned to each class by the classifier. The '_mlClasses' parameter
78  * dictates the order of the classes. That is the probabilities for any given index in '_probabilities'
79  * will be for the class specified in the same index in '_mlClasses'.
80  *@param[in] _example FeatureVector object to calculate predicted probabilities for.
81  *@param[in] _mlClasses List image classes that caller is aware of. This should be the same list that was
82  * used when constructing this Model object. The list must be the same but not
83  * necessarily in the same order as when Model was 1st constructed.
84  *@param[out] _probabilities An array that must be as big as the number of classes as in
85  * mlClasses. The probability of class in mlClasses[x] will be returned
86  * in probabilities[x].
87  */
88  virtual
89  void ProbabilitiesByClass (FeatureVectorPtr _example,
90  const MLClassList& _mlClasses,
91  double* _probabilities,
92  RunLog& _log
93  );
94 
95 
96  virtual void TrainModel (FeatureVectorListPtr _trainExamples,
97  bool _alreadyNormalized,
98  bool _takeOwnership, /**< Model will take ownership of these examples */
99  VolConstBool& _cancelFlag,
100  RunLog& _log
101  );
102 
103 
104  void ReadXML (XmlStream& s,
105  XmlTagConstPtr tag,
106  VolConstBool& cancelFlag,
107  RunLog& log
108  );
109 
110 
111  void WriteXML (const KKStr& varName,
112  ostream& o
113  ) const;
114 
115 
116  private:
117  ModelParamKnnPtr param; /**< We will NOT own this instance; it will point to same instance defined in parent class Model. */
118  }; /* ModelKnn */
119 
120  typedef ModelKnn::ModelKnnPtr ModelKnnPtr;
121 
122 
125 }
126 
127 
128 #endif
Base class to all Learning Algorithms.
Definition: Model.h:82
XmlElementModelTemplate< ModelKnn > XmlElementModelKnn
Definition: ModelKnn.h:123
__int32 kkint32
Definition: KKBaseTypes.h:88
ModelKnn(const ModelKnn &_madel)
Definition: ModelKnn.cpp:65
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 ...
Definition: ModelKnn.cpp:180
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
Definition: ModelKnn.cpp:123
void WriteXML(const KKStr &varName, ostream &o) const
Definition: ModelKnn.cpp:202
virtual ModelKnnPtr Duplicate() const
Definition: ModelKnn.cpp:82
ModelParamKnnPtr Param()
Definition: ModelKnn.cpp:88
virtual ~ModelKnn()
Definition: ModelKnn.cpp:74
virtual ModelTypes ModelType() const
Definition: ModelKnn.h:32
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 ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Derives predicted probabilities by class.
Definition: ModelKnn.cpp:169
ModelKnn(const KKStr &_name, const ModelParamKnn &_param, FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelKnn.cpp:52
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
virtual MLClassPtr Predict(FeatureVectorPtr example, RunLog &log)
Definition: ModelKnn.cpp:94
ModelKnn(FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelKnn.cpp:43
XmlElementModelKnn * XmlElementModelKnnPtr
Definition: ModelKnn.h:124
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 ...
Definition: ModelKnn.cpp:225
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 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)
Definition: ModelKnn.cpp:103
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
Definition: ModelKnn.cpp:159
Maintains a list of MLClass instances.
Definition: MLClass.h:233
ModelKnn * ModelKnnPtr
Definition: ModelKnn.h:13
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163