KSquare Utilities
ModelSvmBase.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <string>
4 #include <iostream>
5 #include <fstream>
6 #include <math.h>
7 #include <vector>
8 #include <sstream>
9 #include <iomanip>
10 #include <set>
11 #include <vector>
12 #include "MemoryDebug.h"
13 using namespace std;
14 
15 
16 #include "GlobalGoalKeeper.h"
17 #include "KKBaseTypes.h"
18 #include "KKException.h"
19 #include "OSservices.h"
20 #include "RunLog.h"
21 #include "KKStr.h"
22 using namespace KKB;
23 
24 
25 #include "ModelSvmBase.h"
26 #include "ClassProb.h"
27 #include "FeatureNumList.h"
28 #include "FeatureVector.h"
29 #include "svm2.h"
30 using namespace KKMLL;
31 
32 
33 
34 
36  Model (),
37  param (NULL),
38  svmModel (NULL)
39 {
40 }
41 
42 
44  Model (_factoryFVProducer),
45  param (NULL),
46  svmModel (NULL)
47 {
48 }
49 
50 
51 
53  const ModelParamSvmBase& _param, // Create new model from
54  FactoryFVProducerPtr _factoryFVProducer
55  ):
56  Model (_name, _param, _factoryFVProducer),
57  param (NULL),
58  svmModel (NULL)
59 {
60  param = dynamic_cast<ModelParamSvmBasePtr> (Model::param);
61 }
62 
63 
64 
65 
67  Model (_model),
68  param (NULL),
69  svmModel (NULL)
70 {
71  param = dynamic_cast<ModelParamSvmBasePtr> (Model::param);
72  if (_model.svmModel)
73  {
74  svmModel = new SVM289_MFS::Svm_Model (*_model.svmModel, fileDesc);
75  }
76 }
77 
78 
79 
80 
81 
82 /**
83  * @brief Frees any memory allocated by, and owned by the ModelSvmBase
84  */
86 {
87  // The base class owns param, so we do not delete it.
88  // delete param;
89  if (svmModel)
90  {
91  svm_destroy_model (svmModel); // 'svm_destroy_model' will also delete 'svmModel'.
92  delete svmModel;
93  svmModel = NULL;
94  }
95 }
96 
97 
98 
100 {
101  kkint32 memoryConsumedEstimated = Model::MemoryConsumedEstimated () +
102  sizeof (SVM289_MFS::Svm_Model*) +
103  sizeof (ModelParamSvmBasePtr);
104 
105  if (svmModel)
106  memoryConsumedEstimated += svmModel->MemoryConsumedEstimated ();
107  return memoryConsumedEstimated;
108 }
109 
110 
111 
112 
113 ModelSvmBasePtr ModelSvmBase::Duplicate () const
114 {
115  return new ModelSvmBase (*this);
116 }
117 
118 
119 
120 
122 {
123  KKStr result = "SvmBase(" + Name () + ")";
124 
125  if (param)
126  {
127  const SVM289_MFS::svm_parameter& svmParam = param->SvmParam ();
128 
129  //result << " " << MachineTypeToStr (svmParam.MachineType ())
130  // << " " << SelectionMethodToStr (svmParam.SelectionMethod ());
131  }
132  return result;
133 }
134 
135 
136 
137 ModelParamSvmBasePtr ModelSvmBase::Param ()
138 {
139  return param;
140 }
141 
142 
143 
145  bool _alreadyNormalized,
146  bool _takeOwnership, /*!< Model will take ownership of these examples */
147  VolConstBool& _cancelFlag,
148  RunLog& _log
149  )
150 {
151  _log.Level (10) << "ModelSvmBase::TrainModel[" << param->FileName () << "]." << endl;
152 
153  if (param == NULL)
154  {
155  validModel = false;
156  KKStr errMsg = "ModelSvmBase::TrainModel (param == NULL)";
157  _log.Level (-1) << endl << endl << errMsg << endl << endl;
158  throw KKException (errMsg);
159  }
160 
161  if (svmModel)
162  {
164  delete svmModel;
165  svmModel = NULL;
166  }
167 
168  try
169  {
170  Model::TrainModel (_trainExamples, _alreadyNormalized, _takeOwnership, _cancelFlag, _log);
171  }
172  catch (const KKException& e)
173  {
174  validModel = false;
175  KKStr errMsg = "ModelSvmBase::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
176  _log.Level (-1) << endl << errMsg << endl << e.ToString () << endl << endl;
177  throw KKException (errMsg, e);
178  }
179  catch (const exception& e2)
180  {
181  validModel = false;
182  KKStr errMsg = "ModelSvmBase::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
183  _log.Level (-1) << endl << errMsg << endl << e2.what () << endl << endl;
184  throw KKException (errMsg, e2);
185  }
186  catch (...)
187  {
188  validModel = false;
189  KKStr errMsg = "ModelSvmBase::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
190  _log.Level (-1) << endl << errMsg << endl << endl;
191  throw KKException (errMsg);
192  }
193 
194 
195  // 'Model::TrainModel' Will have performed any Feature Encoding that needed to be done.
196  // Also the data structures 'classes', 'encoder', and 'fileDesc' will have been built.
197  // 'classes' will already be sorted in name order.
198  // The Prediction variables 'probabilities', 'votes', and 'crossClassProbTable' will
199  // have been built.
200 
201 
202  // Build the Label array that libSVM expects.
203  float* y = new float[trainExamples->QueueSize ()];
204  {
205  for (kkint32 labelIndex = 0; labelIndex < trainExamples->QueueSize (); labelIndex++)
206  {
207  kkint16 label = classesIndex->GetClassIndex (trainExamples->IdxToPtr (labelIndex)->MLClass ());
208  if (label < 0)
209  {
210  _log.Level (-1) << endl << " ModelSvmBase::TrainModel ***ERROR*** Label computed to -1; should not be able to happen." << endl << endl;
211  }
212  y[labelIndex] = (float)label;
213  }
214  }
215 
217  delete[] y; y = NULL;
218 
219  try
220  {
222  svmModel = SVM289_MFS::svm_train (prob, param->SvmParam (), _log);
224  }
225  catch (const std::exception& e)
226  {
227  validModel = false;
228  KKStr errMsg = "ModelSvmBase::TrainModel ***ERROR*** Exception occurred in 'SVM289_MFS::svm_train' building training model[" + rootFileName + "].";
229  errMsg << endl << " Exception[" << e.what () << "]";
230  _log.Level (-1) << endl << endl << errMsg << endl << endl;
231  throw KKException (errMsg);
232  }
233  catch (...)
234  {
235  validModel = false;
236  KKStr errMsg = "ModelSvmBase::TrainModel ***ERROR*** Exception occurred in 'SVM289_MFS::svm_train' building training model[" + rootFileName + "].";
237  _log.Level (-1) << endl << endl << errMsg << endl << endl;
238  throw KKException (errMsg);
239  }
240 
241  if (svmModel == NULL)
242  {
243  validModel = false;
244  KKStr errMsg = "ModelSvmBase::TrainModel ***ERROR*** Building 'LibSVM' training model[" + rootFileName + "].";
245  _log.Level (-1) << endl << endl << errMsg << endl << endl;
246  throw KKException (errMsg);
247  }
248 } /* TrainModel */
249 
250 
251 
252 MLClassPtr ModelSvmBase::Predict (FeatureVectorPtr example,
253  RunLog& log
254  )
255 {
256  if (!svmModel)
257  {
258  log.Level (-1) << endl << endl << "ModelSvmBase::Predict ***ERROR*** (svmModel == NULL)" << endl << endl;
259  return NULL;
260  }
261 
262  if (!classesIndex)
263  {
264  log.Level (-1) << endl << endl << "ModelSvmBase::Predict ***ERROR*** (classesIndex == NULL)" << endl << endl;
265  return NULL;
266  }
267 
268  bool newExampleCreated = false;
269  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
270  double y = (kkint32)SVM289_MFS::svm_predict (svmModel, *encodedExample);
271  if (newExampleCreated)
272  {
273  delete encodedExample;
274  encodedExample = NULL;
275  }
276 
277  kkint16 label = (kkint16)y;
278 
279  MLClassPtr ic = classesIndex->GetMLClass (label);
280  if (!ic)
281  log.Level (-1) << endl << endl << "ModelSvmBase::Predict ***ERROR*** Label[" << y << "] Returned from the SVM was not in the ClassAssignments list." << endl << endl;
282 
283  return ic;
284 } /* Predict */
285 
286 
287 
288 
290  MLClassPtr knownClass,
291  MLClassPtr& predClass1,
292  MLClassPtr& predClass2,
293  kkint32& predClass1Votes,
294  kkint32& predClass2Votes,
295  double& probOfKnownClass,
296  double& predClass1Prob,
297  double& predClass2Prob,
298  kkint32& numOfWinners,
299  bool& knownClassOneOfTheWinners,
300  double& breakTie,
301  RunLog& log
302  )
303 {
304  if (!svmModel)
305  {
306  KKStr errMsg = "ModelSvmBase::Predict ***ERROR*** (svmModel == NULL)";
307  log.Level (-1) << endl << endl << errMsg << endl << endl;
308  throw KKException (errMsg);
309  }
310 
311  if (!classesIndex)
312  {
313  KKStr errMsg = "ModelSvmBase::Predict ***ERROR*** (classesIndex == NULL)";
314  log.Level (-1) << endl << endl << errMsg << endl << endl;
315  throw KKException (errMsg);
316  }
317 
318  kkint32 knownClassIdx = classesIndex->GetClassIndex (knownClass);
319 
320  bool newExampleCreated = false;
321  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
322 
323  double y = SVM289_MFS::svm_predict_probability (svmModel, *encodedExample, classProbs, votes);
324 
325  if (newExampleCreated)
326  {
327  delete encodedExample;
328  encodedExample = NULL;
329  }
330 
331  kkint32 maxIndex1 = 0;
332  kkint32 maxIndex2 = 0;
333 
334  double maxProb1 = -1.0;
335  double maxProb2 = -1.0;
336 
337  numOfWinners = 0;
338  kkint32 winnerNumVotes = 0;
339 
340  for (kkint32 idx = 0; idx < numOfClasses; ++idx)
341  {
342  if (classProbs[idx] > maxProb1)
343  {
344  maxProb2 = maxProb1;
345  maxIndex2 = maxIndex1;
346  maxProb1 = classProbs[idx];
347  maxIndex1 = idx;
348  }
349  else if (classProbs[idx] > maxProb2)
350  {
351  maxProb2 = classProbs[idx];
352  maxIndex2 = idx;
353  }
354 
355  if (votes[idx] > winnerNumVotes)
356  {
357  // We have a new winner.
358  knownClassOneOfTheWinners = false;
359  numOfWinners = 1;
360  winnerNumVotes = votes[idx];
361  if (knownClassIdx == idx)
362  knownClassOneOfTheWinners = true;
363  }
364  else if (votes[idx] == winnerNumVotes)
365  {
366  numOfWinners++;
367  if (knownClassIdx == idx)
368  knownClassOneOfTheWinners = true;
369  }
370  }
371 
372  predClass1 = classesIndex->GetMLClass (maxIndex1);
373  predClass2 = classesIndex->GetMLClass (maxIndex2);
374  predClass1Votes = votes[maxIndex1];
375  predClass2Votes = votes[maxIndex2];
376  predClass1Prob = maxProb1;
377  predClass2Prob = maxProb2;
378 
379  breakTie = predClass1Prob - predClass2Prob;
380 
381  if ((knownClassIdx < 0) || (knownClassIdx >= numOfClasses))
382  {
383  probOfKnownClass = 0.0;
384  knownClassOneOfTheWinners = false;
385  }
386  else
387  {
388  probOfKnownClass = (float)classProbs[knownClassIdx];
389  }
390 
391  return;
392 } /* Predict */
393 
394 
395 
396 
397 
399  RunLog& log
400  )
401 {
402  if (!svmModel)
403  {
404  KKStr errMsg = "ModelSvmBase::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
405  log.Level (-1) << endl << endl << errMsg << endl << endl;
406  throw KKException (errMsg);
407  }
408 
409  if (!classesIndex)
410  {
411  KKStr errMsg = "ModelSvmBase::ProbabilitiesByClass ***ERROR*** (classesIndex == NULL)";
412  log.Level (-1) << endl << endl << errMsg << endl << endl;
413  throw KKException (errMsg);
414  }
415 
416  bool newExampleCreated = false;
417  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
418 
419  double y = SVM289_MFS::svm_predict_probability (svmModel, *encodedExample, classProbs, votes);
420 
421  if (newExampleCreated)
422  {
423  delete encodedExample;
424  encodedExample = NULL;
425  }
426 
427  ClassProbListPtr results = new ClassProbList ();
428  kkint32 idx = 0;
429  for (idx = 0; idx < numOfClasses; idx++)
430  {
431  MLClassPtr ic = classesIndex->GetMLClass (idx);
432  results->PushOnBack (new ClassProb (ic, classProbs[idx], (float)votes[idx]));
433  }
434 
435  results->SortByVotes (true);
436 
437  return results;
438 } /* ProbabilitiesByClass */
439 
440 
441 
442 
444  const MLClassList& _mlClasses,
445  kkint32* _votes,
446  double* _probabilities,
447  RunLog& _log
448  )
449 {
450  if (!svmModel)
451  {
452  KKStr errMsg = "ModelSvmBase::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
453  _log.Level (-1) << endl << errMsg << endl << endl;
454  throw KKException (errMsg);
455  }
456 
457  if (!classesIndex)
458  {
459  KKStr errMsg = "ModelSvmBase::ProbabilitiesByClass ***ERROR*** (classesIndex == NULL)";
460  _log.Level (-1) << endl << errMsg << endl << endl;
461  throw KKException (errMsg);
462  }
463 
464  bool newExampleCreated = false;
465  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
466 
467  double y = SVM289_MFS::svm_predict_probability (svmModel, *encodedExample, classProbs, votes);
468 
469  if (newExampleCreated)
470  {
471  delete encodedExample;
472  encodedExample = NULL;
473  }
474 
475  kkuint32 idx;
476  for (idx = 0; idx < _mlClasses.size (); idx++)
477  {
478  MLClassPtr ic = _mlClasses.IdxToPtr (idx);
479  kkint32 classIndex = classesIndex->GetClassIndex (ic);
480  if ((classIndex < 0) || (classIndex >= (kkint32)numOfClasses))
481  {
482  KKStr errMsg = "ModelSvmBase::Predict ***ERROR*** ";
483  errMsg << "Class[" << ic->Name () << "] was asked for but is not defined in this instance of 'ModelSvmBase'.";
484  _log.Level (-1) << endl << errMsg << endl << endl;
485  _votes [idx] = 0;
486  _probabilities [idx] = 0.0f;
487  }
488  else
489  {
490  _votes [idx] = votes [classIndex];
491  _probabilities [idx] = classProbs [classIndex];
492  }
493  }
494 
495  return;
496 } /* ProbabilitiesByClass */
497 
498 
499 
500 
501 
503  const MLClassList& _mlClasses,
504  double* _probabilities,
505  RunLog& _log
506  )
507 {
508  if (!svmModel)
509  {
510  KKStr errMsg = "ModelSvmBase::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
511  _log.Level (-1) << endl << endl << errMsg << endl << endl;
512  throw KKException (errMsg);
513  }
514 
515  if (!classesIndex)
516  {
517  KKStr errMsg = "ModelSvmBase::ProbabilitiesByClass ***ERROR*** (classesIndex == NULL)";
518  _log.Level (-1) << endl << endl << errMsg << endl << endl;
519  throw KKException (errMsg);
520  }
521 
522  bool newExampleCreated = false;
523  FeatureVectorPtr encodedExample = PrepExampleForPrediction (_example, newExampleCreated);
524 
525  double y = SVM289_MFS::svm_predict_probability (svmModel, *encodedExample, classProbs, votes);
526 
527  if (newExampleCreated)
528  {
529  delete encodedExample;
530  encodedExample = NULL;
531  }
532 
533  kkuint32 idx;
534  for (idx = 0; idx < _mlClasses.size (); idx++)
535  {
536  MLClassPtr ic = _mlClasses.IdxToPtr (idx);
537  kkint32 classIndex = classesIndex->GetClassIndex (ic);
538  if ((classIndex < 0) || (classIndex >= (kkint32)numOfClasses))
539  {
540  KKStr errMsg = "ModelSvmBase::Predict ***ERROR*** ";
541  errMsg << "Class[" << ic->Name () << "] was asked for but is not defined in this instance of 'ModelSvmBase'.";
542  _log.Level (-1) << endl << endl << errMsg << endl << endl;
543  _probabilities [idx] = 0.0f;
544  }
545  else
546  {
547  _probabilities [idx] = classProbs [classIndex];
548  }
549  }
550 
551  return;
552 } /* ProbabilitiesByClass */
553 
554 
555 
556 
558  double** _crossProbTable, /**< Two dimension matrix that needs to be classes.QueueSize () squared. */
559  RunLog& _log
560  )
561 {
562  kkuint32 idx1, idx2;
563  VectorInt pairWiseIndexes (_classes.size (), 0);
564  for (idx1 = 0; idx1 < _classes.size (); idx1++)
565  {
566  MLClassPtr ic = _classes.IdxToPtr (idx1);
567  kkint32 pairWiseIndex = classesIndex->GetClassIndex (ic);
568  if ((pairWiseIndex < 0) || (pairWiseIndex >= (kkint32)numOfClasses))
569  {
570  KKStr errMsg = "ModelSvmBase::RetrieveCrossProbTable ***ERROR*** ";
571  errMsg << "Class[" << ic->Name () << "] was asked for but is not defined in this instance of 'ModelSvmBase'.";
572  _log.Level (-1) << endl << endl << errMsg << endl << endl;
573  pairWiseIndexes[idx1] = 0;
574  }
575  else
576  {
577  pairWiseIndexes[idx1] = pairWiseIndex;
578  }
579 
580  for (idx2 = 0; idx2 < _classes.size (); idx2++)
581  _crossProbTable[idx1][idx2] = 0.0;
582  }
583 
584  double** pairWiseProb = svmModel->PairwiseProb ();
585  if (!pairWiseProb)
586  return;
587 
588  for (idx1 = 0; idx1 < (_classes.size () - 1); idx1++)
589  {
590  kkint32 pairWiseIndex1 = pairWiseIndexes [idx1];
591  if ((pairWiseIndex1 >= 0) && (pairWiseIndex1 < (kkint32)numOfClasses))
592  {
593  for (idx2 = idx1 + 1; idx2 < _classes.size (); idx2++)
594  {
595  kkint32 pairWiseIndex2 = pairWiseIndexes [idx2];
596  if ((pairWiseIndex2 >= 0) && (pairWiseIndex2 < (kkint32)numOfClasses))
597  {
598  _crossProbTable[idx1][idx2] = pairWiseProb[pairWiseIndex1][pairWiseIndex2];
599  _crossProbTable[idx2][idx1] = pairWiseProb[pairWiseIndex2][pairWiseIndex1];
600  }
601  }
602  }
603  }
604 } /* RetrieveCrossProbTable */
605 
606 
607 
609 {
610  kkint32 numOfSupportVectors = 0;
611  if (!svmModel)
612  return 0;
613 
614  return svmModel->numSVs;
615 } /* NumOfSupportVectors */
616 
617 
618 
619 
620 
621 void ModelSvmBase::WriteXML (const KKStr& varName,
622  ostream& o
623  ) const
624 {
625  XmlTag startTag ("ModelSvmBase", XmlTag::TagTypes::tagStart);
626  if (!varName.Empty ())
627  startTag.AddAtribute ("VarName", varName);
628  startTag.WriteXML (o);
629 
630  WriteModelXMLFields (o); // Write the PArent class fields 1st.
631 
632  if (svmModel)
633  svmModel->WriteXML ("SvmModel", o);
634 
635  // Turns out the base class "Model" owns this data field and will also be writing it
636  // out so no point in re-reading again.
637  //if (param) param->WriteXML ("Param", o);
638 
639  XmlTag endTag ("ModelSvmBase", XmlTag::TagTypes::tagEnd);
640  endTag.WriteXML (o);
641  o << endl;
642 } /* WriteXML */
643 
644 
645 
646 
647 
649  XmlTagConstPtr tag,
650  VolConstBool& cancelFlag,
651  RunLog& log
652  )
653 {
654  delete svmModel;
655  svmModel = NULL;
656  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
657  while (t)
658  {
659  t = ReadXMLModelToken (t, log); // 1st see if the base class has this data field.
660  if (t)
661  {
662  if ((t->VarName ().EqualIgnoreCase ("SvmModel")) && (typeid(*t) == typeid(XmlElementSvm_Model)))
663  {
664  delete svmModel;
665  svmModel = dynamic_cast<XmlElementSvm_ModelPtr> (t)->TakeOwnership ();
666  }
667  else
668  {
669  KKStr errMsg (128);
670  errMsg << "ModelSvmBase::ReadXML ***ERROR*** Unexpected Token; Section:" << t->SectionName () << " VarName: " << t->VarName ();
671  AddErrorMsg (errMsg, 0);
672  log.Level (-1) << endl << errMsg << endl << endl;
673  }
674  }
675 
676  delete t;
677  t = s.GetNextToken (cancelFlag, log);
678  }
679  delete t;
680  t = NULL;
681 
682  if (!cancelFlag)
683  {
684  if (!param)
685  param = dynamic_cast<ModelParamSvmBasePtr> (Model::param);
686 
687  if (Model::param == NULL)
688  {
689  KKStr errMsg (128);
690  errMsg << "ModelSvmBase::ReadXML ***ERROR*** Base class 'Model' does not have 'param' defined.";
691  AddErrorMsg (errMsg, 0);
692  log.Level (-1) << endl << errMsg << endl << endl;
693  }
694 
695  else if (typeid (*Model::param) != typeid(ModelParamSvmBase))
696  {
697  KKStr errMsg (128);
698  errMsg << "ModelSvmBase::ReadXML ***ERROR*** Base class 'Model' param parameter is of the wrong type; found: " << Model::param->ModelParamTypeStr ();
699  AddErrorMsg (errMsg, 0);
700  log.Level (-1) << endl << errMsg << endl << endl;
701  }
702 
703  else
704  {
705  param = dynamic_cast<ModelParamSvmBasePtr> (Model::param);
706  }
707 
708  ReadXMLModelPost (log);
709  }
710 } /* ReadXML */
711 
712 
713 
714 XmlFactoryMacro(ModelSvmBase)
double svm_predict_probability(Svm_Model *model, const FeatureVector &x, double *prob_estimates, kkint32 *votes)
Definition: svm2.cpp:3988
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
XmlTag(const KKStr &_name, TagTypes _tagType)
Definition: XmlStream.cpp:586
const KKStr & Name() const
Definition: Model.h:178
Base class to all Learning Algorithms.
Definition: Model.h:82
kkint32 * votes
Definition: Model.h:428
__int32 kkint32
Definition: KKBaseTypes.h:88
Svm_Model * svm_train(const svm_problem &prob, const svm_parameter &param, RunLog &log)
Definition: svm2.cpp:3345
FeatureVector * FeatureVectorPtr
Definition: Model.h:44
virtual void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
virtual ModelSvmBasePtr Duplicate() const
KKException(const KKStr &_exceptionStr, const KKException &_innerException)
Definition: KKException.cpp:83
kkint16 GetClassIndex(MLClassPtr c)
Returns the corresponding index to the class &#39;c&#39;; if not in list will return -1.
Definition: MLClass.cpp:1621
XmlTokenPtr ReadXMLModelToken(XmlTokenPtr t, RunLog &log)
Will process any tokens that belong to &#39;ModelParam&#39; and return NULL ones that are not will be passed ...
Definition: Model.cpp:884
ModelParamPtr param
Definition: Model.h:412
virtual void PushOnBack(ClassProbPtr cp)
Definition: ClassProb.cpp:212
std::vector< int > VectorInt
Definition: KKBaseTypes.h:138
FeatureVectorListPtr trainExamples
Definition: Model.h:424
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &_log)
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
Definition: Model.cpp:239
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)
KKStr rootFileName
Definition: Model.h:414
void WriteModelXMLFields(std::ostream &o) const
The "WriteXML" method in Derived classes call this method to include the parents classes fields in th...
Definition: Model.cpp:854
const SVM289_MFS::svm_parameter & SvmParam()
double * classProbs
Definition: Model.h:394
ModelSvmBase(FactoryFVProducerPtr _factoryFVProducer)
Svm_Model(const Svm_Model &_model, FileDescPtr _fileDesc)
Definition: svm2.cpp:4089
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
kkint32 numOfClasses
Definition: Model.h:410
KKException(const KKStr &_exceptionStr, const std::exception &_innerException)
Definition: KKException.cpp:52
MLClassPtr GetMLClass(kkint16 classIndex)
Locates the MLClass that was assigned classIndex.
Definition: MLClass.cpp:1636
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
Every prediction method in every class that is inherited from this class should call this method befo...
Definition: Model.cpp:574
virtual KKStr Description() const
virtual void WriteXML(const KKStr &varName, ostream &o) const
KKTHread * KKTHreadPtr
virtual MLClassPtr Predict(FeatureVectorPtr image, RunLog &log)
ModelParamSvmBasePtr Param()
virtual ~ModelSvmBase()
Frees any memory allocated by, and owned by the ModelSvmBase.
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
double svm_predict(const struct Svm_Model *model, const FeatureVector &x)
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
double ** PairwiseProb()
Definition: svm2.cpp:4319
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
bool Empty() const
Definition: KKStr.h:241
virtual FeatureNumListConstPtr SelectedFeatures() const
Definition: ModelParam.h:116
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
bool validModel
Definition: Model.h:426
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 ...
void TrainingTimeEnd()
Derived classes call this method to stop the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:452
virtual kkint32 MemoryConsumedEstimated() const
Definition: Model.cpp:208
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
void svm_destroy_model(struct Svm_Model *&model)
Definition: svm2.cpp:4611
ModelParamSvmBasePtr param
Definition: ModelSvmBase.h:144
void SortByVotes(bool highToLow=true)
Definition: ClassProb.cpp:151
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Derives predicted probabilities by class.
const KKStr & Name() const
Definition: MLClass.h:154
Model(const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer)
Construct a instance of &#39;Model&#39; using the parameters specified in &#39;_param&#39;.
Definition: Model.cpp:152
virtual kkint32 MemoryConsumedEstimated() const
ModelSvmBase(const ModelSvmBase &_model)
svm_problem(const FeatureVectorList &_x, const float *_y, const FeatureNumList &_selFeatures)
Definition: svm2.cpp:130
FileDescPtr fileDesc
Definition: Model.h:406
kkint32 MemoryConsumedEstimated() const
Definition: svm2.cpp:4284
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
kkint32 numSVs
Definition: svm2.h:206
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 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: Model.cpp:467
void TrainingTimeStart()
Derived classes call this method to start the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:445
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
void ReadXMLModelPost(RunLog &log)
Definition: Model.cpp:987
virtual KKStr ModelParamTypeStr() const
Definition: ModelParam.h:108
ModelSvmBase(const KKStr &_name, const ModelParamSvmBase &_param, FactoryFVProducerPtr _factoryFVProducer)
Model(const Model &_madel)
Copy Constructor.
Definition: Model.cpp:69
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Definition: ClassProb.cpp:22
Maintains a list of MLClass instances.
Definition: MLClass.h:233
virtual void WriteXML(const KKStr &varName, ostream &o) const
Definition: svm2.cpp:4332
FeatureVectorList * FeatureVectorListPtr
Definition: Model.h:46
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
MLClassIndexListPtr classesIndex
Definition: Model.h:392
virtual kkint32 NumOfSupportVectors() const
FactoryFVProducer * FactoryFVProducerPtr
Definition: Model.h:75
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
Definition: Model.cpp:117
SVM289_MFS::Svm_Model * svmModel
Definition: ModelSvmBase.h:143
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163