KSquare Utilities
Classifier2.h
Go to the documentation of this file.
1 #ifndef _CLASSIFIER2_
2 #define _CLASSIFIER2_
3 
4 
5 
6 #include "Application.h"
7 
8 
9 // Problem apperas to b in file below,
10 #include "Model.h"
11 
12 
13 
14 #include "ModelOldSVM.h"
15 #include "ModelParamOldSVM.h"
16 
17 
18 
19 #include "ModelParam.h"
20 #include "RunLog.h"
21 #include "SVMModel.h"
22 
23 
24 
25 namespace KKMLL
26 {
27 #if !defined(_CLASSPROB_)
28 class ClassProb;
29 typedef ClassProb* ClassProbPtr;
32 #endif
33 
34 
36 class FeatureVector;
38 #endif
39 
41 class FeatureVectorList;
43 #endif
44 
45 
46 #if !defined(_MLCLASS_)
47 class MLClass;
48 typedef MLClass* MLClassPtr;
49 
50 class MLClassList;
52 #endif
53 
54 
55 #if !defined(_TRAININGCONFIGURATION2_)
58 #endif
59 
60 
61 #if !defined(_TrainingProcess2_Defined_)
64 #endif
65 
66 
67 #if !defined(_TrainingProcess2List_Defined_)
70 #endif
71 
74 
76  {
77  public:
79 
81  RunLog& _log
82  );
83 
84  virtual ~Classifier2 ();
85 
86  bool Abort () const {return abort;}
87  const KKStr& ConfigRootName () const {return configRootName;}
89 
90  MLClassPtr ClassifyAExample (FeatureVector& example);
91 
92  void ClassifyAExample (FeatureVector& example,
93  MLClassPtr& predClass1,
94  MLClassPtr& predClass2,
95  kkint32& predClass1Votes,
96  kkint32& predClass2Votes,
97  double& knownClassProb,
98  double& predClass1Prob,
99  double& predClass2Prob,
100  kkint32& numOfWinners,
101  double& breakTie
102  );
103 
104  MLClassPtr ClassifyAExample (FeatureVector& example,
105  kkint32& numOfWinners,
106  bool& knownClassOneOfTheWinners
107  );
108 
109  MLClassPtr ClassifyAExample (FeatureVector& example,
110  double& probability,
111  kkint32& numOfWinners,
112  bool& knownClassOneOfTheWinners,
113  double& breakTie
114  );
115 
116  /**
117  *@brief For a given two class pair return the names of the 'numToFind' worst S/V's.
118  *@details This method will iterate through all the S/V's removing them one at a
119  * time and re-compute the decision boundary and probability. It will then
120  * return the S/V's that when removed improve the probability in 'c1's
121  * the most.
122  *@param[in] example The example that was classified incorrectly.
123  *@param[in] numToFind The number of the worst examples you are looking for.
124  *@param[in] c1 Class that the 'example; parameter should have been classed as.
125  *@param[in] c2 Class that it was classified as.
126  */
129  MLClassPtr c1,
130  MLClassPtr c2
131  );
132 
133 
134 
135  /**
136  *@brief For a given two class pair return the names of the 'numToFind' worst S/V's.
137  *@details This method will iterate through all the S/V's removing them one at a
138  * time and rebuild a new SVM then submit example for testing.
139  *@param[in] example The example that was classified incorrectly.
140  *@param[in] numToFind The number of the worst examples you are looking for.
141  *@param[in] c1 Class that the 'example; parameter should have been classed as.
142  *@param[in] c2 Class that it was classified as.
143  */
146  MLClassPtr c1,
147  MLClassPtr c2
148  );
149 
150 
151  virtual
153 
154 
155  void PredictRaw (FeatureVectorPtr example,
156  MLClassPtr & predClass,
157  double& dist
158  );
159 
160 
161 
162  /**
163  *@brief Returns the distribution of the training data used to build the classifier.
164  *@details The caller will NOT own this list. Ownership will remain with 'trainingProcess'
165  * member of this class.
166  */
167  ClassProbList const * PriorProbability () const;
168 
169 
170  /**
171  *@brief For a given feature vector return back the probabilities and votes for each class.
172  *@details
173  *@param classes [in] List of classes that we can be predicted for The ordering of 'votes' and 'probabilities' will be dictated by this list.
174  *@param example [in] Feature Vector to make prediction on.
175  *@param votes [out] Pointer to list of int's, must be as large as 'classes' The number of votes for each corresponding class will be stored hear.
176  *@param probabilities [out] Pointer to list of double's, must be as large as 'classes' The probability for each corresponding class will be stored hear.
177  */
178  void ProbabilitiesByClass (const MLClassList& classes,
179  FeatureVectorPtr example,
180  kkint32* votes,
181  double* probabilities
182  );
183 
184 
186 
187 
189  KKStr& classifier1Desc,
190  KKStr& classifier2Desc,
191  ClassProbListPtr& classifier1Results,
192  ClassProbListPtr& classifier2Results
193  );
194 
195 
196  void RetrieveCrossProbTable (MLClassList& classes,
197  double** crossProbTable // two dimension matrix that needs to be classes.QueueSize () squared.
198  );
199 
200 
202  MLClassPtr c2
203  );
204 
205 
206 
207  private:
208  typedef std::map<MLClassPtr, Classifier2Ptr> ClassClassifierIndexType;
209  typedef std::pair<MLClassPtr, Classifier2Ptr> ClassClassifierPair;
210  typedef std::multimap<Classifier2Ptr,MLClassPtr> ClassifierClassIndexType;
211  typedef std::pair<Classifier2Ptr,MLClassPtr> ClassifierClassPair;
212 
213 
214  void BuildSubClassifierIndex ();
215 
216  MLClassPtr ClassifyAImageOneLevel (FeatureVector& example);
217 
218  MLClassPtr ClassifyAImageOneLevel (FeatureVector& example,
219  kkint32& numOfWinners,
220  bool& knownClassOneOfTheWinners
221  );
222 
223 
224  MLClassPtr ClassifyAImageOneLevel (FeatureVector& example,
225  double& probability,
226  kkint32& numOfWinners,
227  bool& knownClassOneOfTheWinners,
228  double& breakTie
229  );
230 
231  Classifier2Ptr LookUpSubClassifietByClass (MLClassPtr c);
232 
233  MLClassListPtr PredictionsThatHaveSubClassifier (ClassProbListPtr predictions);
234 
235  ClassProbListPtr ProcessSubClassifersMethod1 (FeatureVectorPtr example,
236  ClassProbListPtr upperLevelPredictions
237  );
238 
239  ClassProbListPtr ProcessSubClassifersMethod2 (FeatureVectorPtr example,
240  ClassProbListPtr upperLevelPredictions
241  );
242 
243  ClassProbListPtr GetListOfPredictionsForClassifier (Classifier2Ptr classifier,
244  ClassProbListPtr predictions
245  );
246 
247 
248 
249  bool abort;
250 
251  KKStr configRootName; /**< Name from 'TrainingConfiguration2' instance that was used to build the
252  * 'TrainingProcess2'instance that this instance will refer to.
253  */
254 
255  bool featuresAlreadyNormalized;
256 
257  RunLog& log;
258 
259  MLClassListPtr mlClasses; /**< We will own the MLClass objects in this
260  * list. Will be originally populated by
261  * TrainingConfiguration2 construction.
262  */
263 
264  MLClassPtr noiseMLClass; /**< Point to class that represents Noise Images
265  * The object pointed to will also be included
266  * in mlClasses.
267  */
268 
269  Classifier2ListPtr subClassifiers;
270 
271  ClassClassifierIndexType classClassifierIndex;
272  ClassifierClassIndexType classifierClassIndex;
273 
274  ModelPtr trainedModel;
275 
276  ModelOldSVMPtr trainedModelOldSVM;
277 
278  SVMModelPtr trainedModelSVMModel;
279 
280  TrainingProcess2Ptr trainingProcess;
281 
282  MLClassPtr unKnownMLClass;
283  };
285 
286 #define _Classifier2_Defined_
287 
288 
289 
290 
292  {
293  public:
294  Classifier2List (bool _owner);
295  virtual ~Classifier2List ();
296 
297  Classifier2Ptr LookUpByName (const KKStr& rootName) const;
298  };
300 
301 #define _Classifier2List_Defined_
302 
303 
304 
305 } /* namespace KKMLL */
306 
307 
308 #endif
MLClassPtr ClassifyAExample(FeatureVector &example, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners)
SVMModel * SVMModelPtr
Definition: SVMModel.h:557
SVM_SelectionMethod SelectionMethod() const
SVM_SelectionMethod
Definition: SVMparam.h:34
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureVector * FeatureVectorPtr
Definition: Model.h:44
const KKStr & ConfigRootName() const
Definition: Classifier2.h:87
#define _FeatureVector_Defined_
Classifier2List(bool _owner)
void ProbabilitiesByClassDual(FeatureVectorPtr example, KKStr &classifier1Desc, KKStr &classifier2Desc, ClassProbListPtr &classifier1Results, ClassProbListPtr &classifier2Results)
Classifier2List * Classifier2ListPtr
Definition: Classifier2.h:72
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.
Classifier2Ptr LookUpByName(const KKStr &rootName) const
virtual kkint32 MemoryConsumedEstimated() const
ClassProbList const * PriorProbability() const
Returns the distribution of the training data used to build the classifier.
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2)
void PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
void ClassifyAExample(FeatureVector &example, MLClassPtr &predClass1, MLClassPtr &predClass2, kkint32 &predClass1Votes, kkint32 &predClass2Votes, double &knownClassProb, double &predClass1Prob, double &predClass2Prob, kkint32 &numOfWinners, double &breakTie)
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
#define _FeatureVectorList_Defined_
virtual ~Classifier2()
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
Classifier2(TrainingProcess2Ptr _trainer, RunLog &_log)
Definition: Classifier2.cpp:42
Classifier2 * Classifier2Ptr
Definition: Classifier2.h:78
TrainingProcess2 * TrainingProcess2Ptr
Definition: Classifier2.h:62
MLClassPtr ClassifyAExample(FeatureVector &example, double &probability, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners, double &breakTie)
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.
void ProbabilitiesByClass(const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
For a given feature vector return back the probabilities and votes for each class.
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)
ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example)
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable)
TrainingConfiguration2 * TrainingConfiguration2Ptr
Definition: Classifier2.h:56
Maintains a list of MLClass instances.
Definition: MLClass.h:233
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
Classifier2 * Classifier2Ptr
Definition: Classifier2.h:284
TrainingProcess2List * TrainingProcess2ListPtr
Definition: Classifier2.h:68
bool Abort() const
Definition: Classifier2.h:86
MLClassPtr ClassifyAExample(FeatureVector &example)
ClassProb * ClassProbPtr
Definition: Classifier2.h:28