KSquare Utilities
Model.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 
3 #include <exception>
4 #include <fstream>
5 #include <iomanip>
6 #include <iostream>
7 #include <math.h>
8 #include <set>
9 #include <sstream>
10 #include <stdio.h>
11 #include <string>
12 #include <vector>
13 #include "MemoryDebug.h"
14 using namespace std;
15 
16 
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 "ClassProb.h"
26 #include "FactoryFVProducer.h"
27 #include "FeatureEncoder2.h"
28 #include "FeatureNumList.h"
29 #include "FeatureVector.h"
30 #include "Model.h"
31 #include "ModelOldSVM.h"
32 #include "ModelSvmBase.h"
33 #include "ModelKnn.h"
34 #include "ModelUsfCasCor.h"
35 #include "ModelDual.h"
36 #include "ModelParam.h"
37 #include "ModelParamDual.h"
38 #include "ModelParamOldSVM.h"
39 #include "NormalizationParms.h"
40 using namespace KKMLL;
41 
42 
44  alreadyNormalized (false),
45  classes (NULL),
46  classesIndex (NULL),
47  classProbs (NULL),
48  crossClassProbTable (NULL),
50  encoder (NULL),
51  factoryFVProducer (NULL),
52  fileDesc (NULL),
53  name (),
54  normParms (NULL),
55  numOfClasses (0),
56  param (NULL),
58  trainExamples (NULL),
59  trianingPrepTime (0.0),
60  trainingTime (0.0),
61  trainingTimeStart (0.0),
62  validModel (true),
63  votes (NULL),
64  weOwnTrainExamples (false)
65 {
66 }
67 
68 
69 Model::Model (const Model& _model):
70  alreadyNormalized (false),
71  classes (NULL),
72  classesIndex (NULL),
73  classProbs (NULL),
74  crossClassProbTable (NULL),
76  encoder (NULL),
78  fileDesc (_model.fileDesc),
79  name (_model.name),
80  normParms (NULL),
82  param (NULL),
84  trainExamples (NULL),
85  trianingPrepTime (0.0),
86  trainingTime (_model.trainingTime),
87  trainingTimeStart (_model.trainingTime),
88  validModel (_model.validModel),
89  votes (NULL),
90  weOwnTrainExamples (false)
91 {
92  numOfClasses = 0;
93  if (_model.param != NULL)
94  param = _model.param->Duplicate ();
95 
96  if (_model.classes)
97  {
98  classes = new MLClassList (*_model.classes);
99  numOfClasses = classes->QueueSize ();
100 
102  }
103 
104  if (_model.classesIndex)
106 
107 
108  if (_model.encoder)
109  encoder = new FeatureEncoder2 (*_model.encoder);
110 }
111 
112 
113 
114 /**
115  *@brief Use this when you are planning on creating a empty model without parameters.
116  */
117 Model::Model (FactoryFVProducerPtr _factoryFVProducer):
118  alreadyNormalized (false),
119  classes (NULL),
120  classesIndex (NULL),
121  classProbs (NULL),
122  crossClassProbTable (NULL),
124  encoder (NULL),
125  factoryFVProducer (_factoryFVProducer),
126  fileDesc (NULL),
127  name (),
128  normParms (NULL),
129  numOfClasses (0),
130  param (NULL),
132  trainExamples (NULL),
133  trianingPrepTime (0.0),
134  trainingTime (0.0),
135  trainingTimeStart (0.0),
136  validModel (true),
137  votes (NULL),
138  weOwnTrainExamples (false)
139 {
141 }
142 
143 
144 
145 /**
146  *@brief Creates a new svm model from the provided example data
147  *@param[in] _name
148  *@param[in] _param Will make own local copy.
149  *@param[in] _fileDesc A description of the data file.
150  *@param[in] _log A log-file stream. All important events will be output to this stream
151  */
152 Model::Model (const KKStr& _name,
153  const ModelParam& _param, // Create new model from
154  FactoryFVProducerPtr _factoryFVProducer
155  ):
156  alreadyNormalized (false),
157  classes (NULL),
158  classesIndex (NULL),
159  classProbs (NULL),
160  crossClassProbTable (NULL),
162  encoder (NULL),
163  factoryFVProducer (_factoryFVProducer),
164  fileDesc (NULL),
165  name (_name),
166  normParms (NULL),
167  numOfClasses (0),
168  param (NULL),
170  trainExamples (NULL),
171  trianingPrepTime (0.0),
172  trainingTime (0.0),
173  trainingTimeStart (0.0),
174  validModel (true),
175  votes (NULL),
176  weOwnTrainExamples (false)
177 {
179  param = _param.Duplicate ();
180 }
181 
182 
183 
184 
185 /**
186  * @brief Frees any memory allocated by, and owned by the Model
187  */
189 {
191 
192  delete classesIndex; classesIndex = NULL;
193  delete classes; classes = NULL;
194  delete encoder; encoder = NULL;
195  delete normParms; normParms = NULL;
196 
197  if (weOwnTrainExamples)
198  {
199  delete trainExamples;
200  trainExamples = NULL;
201  }
202 
203  delete param;
204  param = NULL;
205 }
206 
207 
209 {
210  kkint32 memoryConsumedEstimated = sizeof (Model) + rootFileName.MemoryConsumedEstimated ();
211  if (classes) memoryConsumedEstimated += classes->MemoryConsumedEstimated ();
212  if (classesIndex) memoryConsumedEstimated += classesIndex->MemoryConsumedEstimated ();
213  if (classProbs) memoryConsumedEstimated += numOfClasses * sizeof (double);
214  if (crossClassProbTable) memoryConsumedEstimated += (numOfClasses * sizeof (double*) + numOfClasses * numOfClasses * sizeof (double));
215  if (encoder) memoryConsumedEstimated += encoder->MemoryConsumedEstimated ();
216  if (normParms) memoryConsumedEstimated += normParms->MemoryConsumedEstimated ();
217  if (param) memoryConsumedEstimated += param->MemoryConsumedEstimated ();
218  if (votes) memoryConsumedEstimated += numOfClasses * sizeof (kkint32);
219 
220  if (weOwnTrainExamples && (trainExamples != NULL))
221  memoryConsumedEstimated += trainExamples->MemoryConsumedEstimated ();
222 
223  return memoryConsumedEstimated;
224 }
225 
226 
227 
228 MLClassListPtr Model::MLClassesNewInstance () const
229 {
230  if (classes)
231  return new MLClassList (*classes);
232  else
233  return NULL;
234 } /* MLClassesNewInstance */
235 
236 
237 
238 
239 void Model::AddErrorMsg (const KKStr& errMsg,
240  kkint32 lineNum
241  )
242 {
243  errors.push_back (errMsg);
244 }
245 
246 
247 
249 {
250  return ModelTypeStr () + "(" + Name () + ")";
251 }
252 
253 
254 
255 
257 {
258  if (_modelingType == ModelTypes::Null) return "NULL";
259  else if (_modelingType == ModelTypes::OldSVM) return "OldSVM";
260  else if (_modelingType == ModelTypes::SvmBase) return "SvmBase";
261  else if (_modelingType == ModelTypes::KNN) return "KNN";
262  else if (_modelingType == ModelTypes::UsfCasCor) return "UsfCasCor";
263  else if (_modelingType == ModelTypes::Dual) return "Dual";
264  else
265  return "NULL";
266 } /* ModelingMethodToStr */
267 
268 
269 
270 
271 Model::ModelTypes Model::ModelTypeFromStr (const KKStr& _modelingTypeStr)
272 {
273  if (_modelingTypeStr.EqualIgnoreCase ("OldSVM") ||
274  _modelingTypeStr.EqualIgnoreCase ("One_Level")) return ModelTypes::OldSVM;
275  else if (_modelingTypeStr.EqualIgnoreCase ("SvmBase")) return ModelTypes::SvmBase;
276  else if (_modelingTypeStr.EqualIgnoreCase ("KNN")) return ModelTypes::KNN;
277  else if (_modelingTypeStr.EqualIgnoreCase ("UsfCasCor")) return ModelTypes::UsfCasCor;
278  else if (_modelingTypeStr.EqualIgnoreCase ("Dual")) return ModelTypes::Dual;
279 
280  else
281  return ModelTypes::Null;
282 } /* ModelingMethodFromStr */
283 
284 
285 
286 
287 ModelPtr Model::CreateAModel (ModelTypes _modelType,
288  const KKStr& _name,
289  const ModelParam& _param,
290  FactoryFVProducerPtr _factoryFVProducer,
291  VolConstBool& _cancelFlag,
292  RunLog& _log
293  )
294 {
295  ModelPtr model = NULL;
296  try
297  {
298  switch (_modelType)
299  {
300  case ModelTypes::OldSVM:
301  model = new ModelOldSVM (_name, dynamic_cast<const ModelParamOldSVM&> (_param), _factoryFVProducer);
302  break;
303 
304  case ModelTypes::SvmBase:
305  model = new ModelSvmBase (_name, dynamic_cast<const ModelParamSvmBase&> (_param), _factoryFVProducer);
306  break;
307 
308  case ModelTypes::KNN:
309  model = new ModelKnn (_name, dynamic_cast<const ModelParamKnn&> (_param), _factoryFVProducer);
310  break;
311 
313  model = new ModelUsfCasCor (_name, dynamic_cast<const ModelParamUsfCasCor&> (_param), _factoryFVProducer);
314  break;
315 
316  case ModelTypes::Dual:
317  model = new ModelDual (_name, dynamic_cast<const ModelParamDual&> (_param), _factoryFVProducer);
318  break;
319 
320  default:
321  KKStr errMsg = "Model::CreateAModel ***ERROR*** Invalid _modelType[" + KKB::StrFromInt16((kkint16)_modelType) + "].";
322  _log.Level (-1) << endl << errMsg << endl << endl;
323  throw KKException(errMsg);
324  } /* end of switch */
325  }
326  catch (const KKException& e)
327  {
328  delete model; model = NULL;
329  throw KKException ("Model::CreateAModel Exception calling constructor.", e);
330  }
331  catch (...)
332  {
333  delete model; model = NULL;
334  throw KKException ("Model::CreateAModel Exception calling constructor. No info provided.");
335  }
336 
337  return model;
338 } /* CreateAModel */
339 
340 
341 
342 
344 {
345  kkint32 x = 0;
346  kkint32 y = 0;
347 
349 
350  if (classes == NULL)
351  {
352  cerr << endl << endl
353  << "Model::AllocatePredictionVariables ***ERROR*** (classes == NULL)" << endl
354  << endl;
355 
356  validModel = false;
357  return;
358  }
359 
360  numOfClasses = classes->QueueSize ();
361 
362  if (numOfClasses > 0)
363  {
365  classProbs = new double[numOfClasses];
366  votes = new kkint32 [numOfClasses];
367  crossClassProbTable = new double*[numOfClasses];
368  for (x = 0; x < numOfClasses; x++)
369  {
370  classProbs [x] = 0.0;
371  votes [x] = 0;
372  crossClassProbTable[x] = new double[numOfClasses];
373  for (y = 0; y < numOfClasses; y++)
374  crossClassProbTable[x][y] = 0.0f;
375  }
376  }
377 } /* AllocatePredictionVariables*/
378 
379 
380 
381 
382 
384 {
385  kkint32 x = 0;
387  {
388  for (x = 0; x < numOfClasses; x++)
389  {
390  delete [] crossClassProbTable[x];
391  crossClassProbTable[x] = NULL;
392  }
393  delete[] crossClassProbTable;
394  crossClassProbTable = NULL;
395  }
396 
397  delete[] classProbs;
398  classProbs = NULL;
399 
400  delete[] votes;
401  votes = NULL;
402 }
403 
404 
405 
406 const FeatureEncoder2& Model::Encoder () const
407 {
408  if (!encoder)
409  throw KKException ("Model::GetFeatureNums 'encoder == NULL'.");
410  return *encoder;
411 }
412 
413 
414 
416 {
417  if (!param)
418  throw KKException ("Model::GetFeatureNums 'param == NULL'.");
420 }
421 
422 
424 {
425  if (!param)
426  throw KKException ("Model::NormalizeNominalAttributes 'param == NULL'.");
427 
429  return true;
430  else
431  return false;
432 } /* NormalizeNominalAttributes */
433 
434 
435 
437 {
438  if (!param)
439  throw KKException ("Model::GetFeatureNums 'param'.");
441 }
442 
443 
444 
446 {
447  trainingTimeStart = osGetSystemTimeUsed ();
448 }
449 
450 
451 
453 {
454  double trainingTimeEnd = osGetSystemTimeUsed ();
455  trainingTime = trainingTimeEnd - trainingTimeStart;
456  if (trainingTime < 0.0)
457  trainingTime += (24.0 * 60.0 * 60.0);
458 }
459 
460 
461 
462 
463 /**
464  *@brief Performs operations such as FeatureEncoding, and Normalization; the actual training
465  * of models occurs in the specific implementation of 'Model'.
466  */
467 void Model::TrainModel (FeatureVectorListPtr _trainExamples,
468  bool _alreadyNormalized,
469  bool _takeOwnership, /*!< True = Model will take ownership of these examples */
470  VolConstBool& _cancelFlag,
471  RunLog& _log
472  )
473 {
474  _log.Level (10) << "Model::TrainModel Preparing for training of Model[" << Name () << "] Examples[" << _trainExamples->QueueSize () << "]" << endl;
475 
476  double prepStartTime = osGetSystemTimeUsed ();
477 
478  if (_trainExamples == NULL)
479  {
480  KKStr errMsg = "ModelSvmBase::TrainModel (_trainExamples == NULL)";
481  _log.Level (-1) << endl << endl << errMsg << endl << endl;
482  throw KKException (errMsg);
483  }
484 
485  if (_trainExamples->QueueSize () < 2)
486  {
487  KKStr errMsg = "ModelSvmBase::TrainModel (_trainExamples == NULL)";
488  _log.Level (-1) << endl << endl << errMsg << endl << endl;
489  throw KKException (errMsg);
490  }
491 
492  weOwnTrainExamples = _takeOwnership;
493  trainExamples = _trainExamples;
494  alreadyNormalized = _alreadyNormalized;
495  _trainExamples = NULL;
496 
497  delete classes;
500 
501  delete classesIndex;
503 
506 
507  if (!_alreadyNormalized)
508  {
509  if (!weOwnTrainExamples)
510  {
511  // Since we do not own the WE will have to duplicate the trainExamples list and its contents before we normalize the data.
512  FeatureVectorListPtr temp = trainExamples->Duplicate (true);
513  weOwnTrainExamples = true;
514  trainExamples = temp;
515  }
516  else if (!trainExamples->Owner ())
517  {
518  // Even though we own 'trainExamples' we do not own its contents; so we will need to create a new list and own those contents.
519  FeatureVectorListPtr temp = trainExamples->Duplicate (true);
520  weOwnTrainExamples = true;
521  delete trainExamples;
522  trainExamples = temp;
523  }
524  delete normParms;
527  }
528 
530  {
531  // There is nothing for us to do.
532  return;
533  }
534 
536  {
537  if (!encoder)
539 
540  FeatureVectorListPtr encodedImages = encoder->EncodeAllExamples (trainExamples);
541  if (weOwnTrainExamples)
542  {
543  delete trainExamples;
544  trainExamples = NULL;
545  }
546 
547  trainExamples = encodedImages;
548  weOwnTrainExamples = true;
549  }
550 
552 
553  double prepEndTime = osGetSystemTimeUsed ();
554  trianingPrepTime = prepEndTime - prepStartTime;
555  if (trianingPrepTime < 0.0)
556  trianingPrepTime += (24.0 * 60.0 * 60.0);
557 
558  _log.Level (40) << "Model::TrainModel Exiting." << endl;
559 } /* TrainModel */
560 
561 
562 
563 
564 
565 /**
566  *@brief Every prediction method in every class that is inherited from this class should call
567  * this method before performing there prediction. Such things as Normalization and
568  * Feature Encoding.
569  *@param[in] fv Feature vector of example that needs to be prepared.
570  *@param[out] newExampleCreated Indicates if either Feature Encoding and/or Normalization needed
571  * to be done. If neither then the original instance is returned. If Yes then
572  * a new instance which the caller will have to be delete will be returned.
573  */
574 FeatureVectorPtr Model::PrepExampleForPrediction (FeatureVectorPtr fv,
575  bool& newExampleCreated
576  )
577 {
578  FeatureVectorPtr oldFV = NULL;
579  newExampleCreated = false;
580  if ((!alreadyNormalized) && (normParms))
581  {
582  oldFV = fv;
584  if (newExampleCreated)
585  {
586  delete oldFV;
587  oldFV = NULL;
588  }
589  newExampleCreated = true;
590  }
591 
592  // I do not believe we need the encoder at this point. At least not for the Features Selected part. Maybe the conversion from minimal fields will make sense.
593  if (encoder)
594  {
595  oldFV = fv;
597  if (newExampleCreated)
598  {
599  delete oldFV;
600  oldFV = NULL;
601  }
602  newExampleCreated = true;
603  }
604 
605  return fv;
606 } /* PrepExampleForPrediction */
607 
608 
609 
610 
611 /**
612  * Will normalize probabilities such that the sum of all equal 1.0 and no one probability will be less than 'minProbability'.
613  */
615  double* probabilities,
616  double minProbability
617  )
618 {
619  double sumGreaterOrEqualMin = 0.0;
620  kkint32 numLessThanMin = 0;
621 
622  kkint32 x = 0;
623  for (x = 0; x < numClasses; ++x)
624  {
625  if (probabilities[x] < minProbability)
626  ++numLessThanMin;
627  else
628  sumGreaterOrEqualMin += probabilities[x];
629  }
630 
631  double probLessMinTotal = numLessThanMin * minProbability;
632  double probLeftToAllocate = 1.0 - probLessMinTotal;
633 
634  for (x = 0; x < numClasses; ++x)
635  {
636  if (probabilities[x] < minProbability)
637  probabilities[x] = minProbability;
638  else
639  probabilities[x] = (probabilities[x] / sumGreaterOrEqualMin) * probLeftToAllocate;
640  }
641 } /* NormalizeProbabilitiesWithAMinumum */
642 
643 
644 
645 
646 /**
647  @brief Reduces the Training Images down to the size dictated by the 'examplesPerClass' parameter.
648  */
650 {
651  kkint32 examplesPerClass = param->ExamplesPerClass ();
652  kkuint32 zed = 0;
653 
654  if (examplesPerClass < 0)
655  examplesPerClass = int32_max;
656 
657  bool reductionNeeded = false;
658 
659  {
660  // First lets see if reduction is even necessary.
662  if (!stats)
663  {
664  log.Level (-1) << endl
665  << "Model::ReduceTrainExamples ***ERROR*** can not retrieve Class Stat's for training data." << endl
666  << endl;
667  validModel = false;
668  return;
669  }
670 
671  for (zed = 0; (zed < stats->size ()) && (!reductionNeeded); zed++)
672  {
673  if (stats->IdxToPtr (zed)->Count () > (kkuint32)examplesPerClass)
674  reductionNeeded = true;
675  }
676 
677  delete stats;
678  stats = NULL;
679  }
680 
681  if (!reductionNeeded)
682  {
683  log.Level (20) << "Model::ReduceTrainExamples Was not needed. No classes exceeded 'examplesPerClass'." << endl;
684  return;
685  }
686 
687  FeatureVectorListPtr reducedSet = trainExamples->ManufactureEmptyList (false);
688  FeatureVectorListPtr deleteSet = trainExamples->ManufactureEmptyList (false); // Examples that we do not use will need to be deleted.
689  MLClassList::iterator idx;
690 
691  for (idx = classes->begin (); idx != classes->end (); idx++)
692  {
693  MLClassPtr ic = *idx;
694  FeatureVectorListPtr examplesThisClass = trainExamples->ExtractExamplesForAGivenClass (ic);
695  if ((!examplesThisClass) || (examplesThisClass->size () < 1))
696  {
697  log.Level (-1) << endl
698  << "Model::ReduceTrainExamples ***ERROR*** No Training Examples for class[" << ic->Name () << "]." << endl
699  << endl;
700  continue;
701  }
702 
703  if (examplesThisClass->size () <= (kkuint32)examplesPerClass)
704  {
705  reducedSet->AddQueue (*examplesThisClass);
706  }
707  else
708  {
709  examplesThisClass->RandomizeOrder ();
710  zed = 0;
711  while (zed < (kkuint32)examplesPerClass)
712  {
713  reducedSet->PushOnBack (examplesThisClass->IdxToPtr (zed));
714  zed++;
715  }
716  while (zed < examplesThisClass->size ())
717  {
718  deleteSet->PushOnBack (examplesThisClass->IdxToPtr (zed));
719  zed++;
720  }
721  }
722 
723  delete examplesThisClass;
724  examplesThisClass = NULL;
725  }
726 
727  if (weOwnTrainExamples)
728  {
729  trainExamples->Owner (false);
730  delete trainExamples;
731  trainExamples = reducedSet;
732  reducedSet = NULL;
733 
734  trainExamples->Owner (true);
735  deleteSet->Owner (true);
736  delete deleteSet;
737  deleteSet = NULL;
738  }
739  else
740  {
741  // Since we are replacing 'trainExamples' with 'reducedSet' we will now own 'trainExamples' but not its contents.
742  reducedSet->Owner (false);
743  weOwnTrainExamples = true;
744  trainExamples = reducedSet;
745  reducedSet = NULL;
746  deleteSet->Owner (false);
747  delete deleteSet;
748  deleteSet = NULL;
749  }
750 } /* ReduceTrainExamples */
751 
752 
753 
754 
756  double** crossClassProbTable, /**< two dimension matrix that needs to be classes.QueueSize () squared. */
757  RunLog& log
758  )
759 {
760  if (classes.QueueSize () != crossClassProbTableSize)
761  {
762  // There Class List does not have the same number of entries as our 'CrossProbTable'
763  log.Level (-1) << endl
764  << "SVMModel::RetrieveCrossProbTable ***ERROR***" << endl
765  << " classes.QueueSize ()[" << classes.QueueSize () << "] != crossClassProbTableSize[" << crossClassProbTableSize << "]" << endl
766  << endl;
767  return;
768  }
769 
770  kkint32* indexTable = new kkint32[classes.QueueSize ()];
771  kkint32 x, y;
772  for (x = 0; x < classes.QueueSize (); x++)
773  {
774  for (y = 0; y < classes.QueueSize (); y++)
775  crossClassProbTable[x][y] = 0.0;
776 
777  indexTable[x] = classesIndex->GetClassIndex (classes.IdxToPtr (x));
778  if (indexTable[x] < 0)
779  {
780  log.Level (-1) << endl
781  << "SVMModel::RetrieveCrossProbTable ***WARNING***" << endl
782  << endl
783  << " Class Index[" << x << "] Name[" << classes[x].Name () << "]" << endl
784  << " will populate this index with zeros." << endl
785  << endl;
786  }
787  }
788 
789  if (classes.QueueSize () != crossClassProbTableSize)
790  {
791  log.Level (-1) << endl
792  << "SVMModel::RetrieveCrossProbTable ***ERROR***" << endl
793  << " 'classes.QueueSize () != crossClassProbTableSize'" << endl
794  << endl;
795  return;
796  }
797 
798 
799  // x,y = 'Callers' Class Indexes..
800  // xIdx, yIdx = 'SVMNodel' Class Indexed.
801  for (x = 0; x < classes.QueueSize (); x++)
802  {
803  kkint32 xIdx = indexTable[x];
804  if (xIdx >= 0)
805  {
806  for (y = 0; y < classes.QueueSize (); y++)
807  {
808  kkint32 yIdx = indexTable[y];
809  if (yIdx >= 0)
810  {
811  if ((x != xIdx) || (y != yIdx))
812  {
813  //kak I just added this check to see when this situation actually occurs.
814  kkint32 zed = 111;
815  }
816 
817  crossClassProbTable[x][y] = this->crossClassProbTable[xIdx][yIdx];
818  }
819  }
820  }
821  }
822 
823  delete[] indexTable; indexTable = NULL;
824  return;
825 } /* RetrieveCrossProbTable */
826 
827 
828 
829 
830 void Model::ProbabilitiesByClassDual (FeatureVectorPtr example,
831  KKStr& classifier1Desc,
832  KKStr& classifier2Desc,
833  ClassProbListPtr& classifier1Results,
834  ClassProbListPtr& classifier2Results,
835  RunLog& log
836  )
837 {
838  delete classifier1Results; classifier1Results = NULL;
839  delete classifier2Results; classifier2Results = NULL;
840 
841  classifier1Desc = Description ();
842  classifier2Desc = Description ();
843 
844  classifier1Results = ProbabilitiesByClass (example, log);
845  if (classifier1Results)
846  classifier2Results = new ClassProbList (*classifier1Results);
847 } /* ProbabilitiesByClassDual */
848 
849 
850 
851 
852 
853 
854 void Model::WriteModelXMLFields (ostream& o) const
855 {
856  //timeSaved = osGetLocalDateTime ();
857  ModelTypeStr ().WriteXML ("ModelType", o);
858  Name ().WriteXML ("Name", o);
859  rootFileName.WriteXML ("RootFileName", o);
861  if (classesIndex)
862  classesIndex->WriteXML ("ClassesIndex", o);
863 
864 
865  if (factoryFVProducer)
866  XmlElementKKStr::WriteXML (factoryFVProducer->Name (), "FvFactoryProducer", o);
867 
868  if (fileDesc)
869  fileDesc->WriteXML ("FileDesc", o);
870 
871  if (param)
872  param->WriteXML ("Param", o);
873 
874  timeSaved.YYYY_MM_DD_HH_MM_SS ().WriteXML ("TimeSaved", o);
875  XmlElementDouble::WriteXML (trainingTime, "TrainingTime", o);
876  XmlElementBool::WriteXML (alreadyNormalized, "AlreadyNormalized", o);
877  if (normParms)
878  normParms->WriteXML ("NormParms", o);
879 } /* WriteModelXMLFields */
880 
881 
882 
883 
885  RunLog& log
886  )
887 {
888  const KKStr& varName = t->VarName ();
890  {
891  bool tokenFound = true;
892 
893  XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
894 
895  const KKStr& varName = e->VarName ();
896 
897  if (varName.EqualIgnoreCase ("ModelType"))
898  {
900  {
901  KKStr errMsg (128);
902  errMsg << "Model::ReadXMLModelToken ***ERROR*** Wrong ModelType encountered; Expected[" << ModelTypeStr () << "] "
903  << "ModelType Specified[" << e->ToKKStr () << "].";
904 
905  log.Level (-1) << endl << errMsg << endl << endl;
906  AddErrorMsg (errMsg, 0);
907  }
908  }
909 
910  else if (varName.EqualIgnoreCase ("Name"))
911  name = e->ToKKStr ();
912 
913  else if (varName.EqualIgnoreCase ("RootFileName"))
915 
916  else if ((varName.EqualIgnoreCase ("Classes")) && (typeid(*e) == typeid (XmlElementMLClassNameList)))
917  {
918  delete classes;
920  }
921 
922  else if (varName.EqualIgnoreCase ("ClassesIndex") && (typeid(*e) == typeid (XmlElementMLClassIndexList)))
923  {
924  delete classesIndex;
925  classesIndex = dynamic_cast<XmlElementMLClassIndexListPtr>(t)->TakeOwnership ();
926  }
927 
928  else if (varName.EqualIgnoreCase ("FvFactoryProducer"))
929  {
930  factoryFVProducer = FactoryFVProducer::LookUpFactory (e->ToKKStr ());
931  }
932 
933  else if (varName.EqualIgnoreCase ("FileDesc") && (typeid(*e) == typeid (XmlElementFileDesc)))
934  {
935  fileDesc = dynamic_cast<XmlElementFileDescPtr>(t)->Value ();
936  }
937 
938  else if (varName.EqualIgnoreCase ("Param"))
939  {
940  XmlElementModelParamPtr xmlParameterElement = dynamic_cast<XmlElementModelParamPtr> (e);
941  if (xmlParameterElement)
942  {
943  delete param;
944  param = xmlParameterElement->TakeOwnership ();
945  }
946  else
947  {
948  KKStr errMsg (128);
949  errMsg << "Model::ReadXMLModelToken ***ERROR*** ModelParam variable 'param' not defined correctly.";
950  log.Level (-1) << endl << errMsg << endl << endl;
951  AddErrorMsg (errMsg, 0);
952  }
953  }
954 
955  else if (varName.EqualIgnoreCase ("TimeSaved"))
956  timeSaved = DateTime (e->ToKKStr ());
957 
958  else if (varName.EqualIgnoreCase ("TrainingTime"))
959  trainingTime = e->ToDouble ();
960 
961  else if (varName.EqualIgnoreCase ("AlreadyNormalized"))
962  alreadyNormalized = e->ToBool ();
963 
964  else if ((varName.EqualIgnoreCase ("NormParms")) && (typeid (*e) == typeid (XmlElementNormalizationParms)))
965  {
966  delete normParms;
967  normParms = dynamic_cast<XmlElementNormalizationParmsPtr> (e)->TakeOwnership ();
968  }
969 
970  else
971  {
972  tokenFound = false;
973  }
974 
975  if (tokenFound)
976  {
977  delete t;
978  t = NULL;
979  }
980  }
981 
982  return t;
983 } /* ReadXMLModelToken */
984 
985 
986 
988 {
989  if (!param)
990  {
991  KKStr errMsg = "Model::ReadXMLModelPost ***ERROR*** 'param' not defined.";
992  AddErrorMsg (errMsg, 0);
993  log.Level (-1) << endl << errMsg << endl << endl;
994  }
995 
996  else if (!param->ValidParam ())
997  {
998  KKStr errMsg = "Model::ReadXMLModelPost ***ERROR*** 'Param' is NOT Valid .";
999  AddErrorMsg (errMsg, 0);
1000  log.Level (-1) << endl << errMsg << endl << endl;
1001  }
1002 
1003  if (!classes)
1004  {
1005  KKStr errMsg = "Model::ReadXMLModelPost ***ERROR*** 'classes' not defined.";
1006  AddErrorMsg (errMsg, 0);
1007  log.Level (-1) << endl << errMsg << endl << endl;
1008  }
1009 
1010  if (!fileDesc)
1011  {
1012  KKStr errMsg = "Model::ReadXMLModelPost ***ERROR*** 'fileDesc' not defined.";
1013  AddErrorMsg (errMsg, 0);
1014  log.Level (-1) << endl << errMsg << endl << endl;
1015  }
1016 
1017  if (errors.size () > 0)
1018  {
1019  log.Level (-1) << "Model::ReadXMLModelPost Errors were detected; model is INVALID." << endl;
1020  validModel = false;
1021  }
1022  else
1023  {
1024  AllocatePredictionVariables ();
1025  }
1026 
1027 } /* ReadXMLModelPost */
ModelOldSVM(const KKStr &_name, const ModelParamOldSVM &_param, FactoryFVProducerPtr _factoryFVProducer)
Creates a new svm model from the provided example (example) data.
Definition: ModelOldSVM.cpp:62
__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
const KKStr & Name() const
Definition: Model.h:178
void DeAllocateSpace()
Definition: Model.cpp:383
kkint32 MemoryConsumedEstimated() const
Base class to all Learning Algorithms.
Definition: Model.h:82
FeatureEncoder2(const ModelParam &_param, FileDescPtr _fileDesc)
Constructs a Feature Encoder object.
kkint32 * votes
Definition: Model.h:428
void NormalizeExamples(FeatureVectorListPtr examples, RunLog &log)
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
bool alreadyNormalized
Definition: Model.h:388
KKException(const char *_exceptionStr, const KKException &_innerException)
Definition: KKException.cpp:74
static ModelPtr CreateAModel(ModelTypes _modelType, const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer, VolConstBool &_cancelFlag, RunLog &_log)
A factory method that will instantiate the appropriate class of training model based off &#39;_modelType&#39;...
Definition: Model.cpp:287
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
__int32 kkint32
Definition: KKBaseTypes.h:88
Maintains a list of classes and their associated integer index.
Definition: MLClass.h:459
static KKStr ModelTypeToStr(ModelTypes _modelingType)
Definition: Model.cpp:256
virtual FeatureVectorListPtr Duplicate(bool _owner) const
Creates a duplicate of list using the same container.
virtual KKStr ToKKStr() const
Definition: XmlStream.h:314
virtual kkint32 MemoryConsumedEstimated() const
NormalizationParmsPtr normParms
Definition: Model.h:408
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 FeatureNumListConstPtr GetFeatureNums() const
Definition: Model.cpp:415
MLClassIndexList(const MLClassIndexList &_list)
Definition: MLClass.cpp:1532
MLClassListPtr ExtractListOfClasses() const
kkint32 MemoryConsumedEstimated() const
FeatureVectorListPtr trainExamples
Definition: Model.h:424
static void WriteXML(const MLClassList &mlClassList, const KKStr &varName, std::ostream &o)
Definition: MLClass.cpp:1499
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
Definition: Model.cpp:239
kkint32 MemoryConsumedEstimated() const
Definition: MLClass.cpp:616
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
FactoryFVProducerPtr factoryFVProducer
Definition: Model.h:404
virtual EncodingMethodType EncodingMethod() const
Definition: ModelParam.h:111
double * classProbs
Definition: Model.h:394
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: MLClass.cpp:1733
virtual void WriteXML(const KKStr &varName, std::ostream &o) const =0
static ModelTypes ModelTypeFromStr(const KKStr &_modelingTypeStr)
Definition: Model.cpp:271
static void WriteXML(const bool b, const KKStr &varName, std::ostream &o)
Definition: XmlStream.cpp:1035
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
virtual FeatureVectorListPtr ManufactureEmptyList(bool _owner) const
Creates an instance of a Empty FeatureVectorList.
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
virtual ModelParamPtr Duplicate() const =0
kkint32 numOfClasses
Definition: Model.h:410
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
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
MLClassIndexList(const MLClassList &_classes)
Definition: MLClass.cpp:1544
FeatureVectorPtr ToNormalized(FeatureVectorPtr example) const
KKTHread * KKTHreadPtr
Model * ModelPtr
Definition: Model.h:85
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
NormalizationParms(const ModelParam &_param, FeatureVectorList &_examples, RunLog &_log)
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
XmlElement * XmlElementPtr
Definition: XmlStream.h:21
virtual kkint32 MemoryConsumedEstimated() const
Definition: ModelParam.cpp:87
virtual FeatureNumListConstPtr SelectedFeatures() const
Definition: ModelParam.h:116
FeatureVectorListPtr ExtractExamplesForAGivenClass(MLClassPtr _mlClass, kkint32 _maxToExtract=-1, float _minSize=-1.0f) const
FeatureEncoder2(const FeatureEncoder2 &_encoder)
MLClassListPtr TakeOwnership()
Definition: MLClass.cpp:1491
bool validModel
Definition: Model.h:426
This class encapsulates are the information necessary to build a UsfCasCor class. ...
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
void WriteXML(const KKStr &varName, std::ostream &o) const
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2) const
ModelKnn(const KKStr &_name, const ModelParamKnn &_param, FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelKnn.cpp:52
ModelUsfCasCor(const KKStr &_name, const ModelParamUsfCasCor &_param, FactoryFVProducerPtr _factoryFVProducer)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ClassStatisticListPtr GetClassStatistics() const
Returns the number of FeatureVectors per class.
virtual bool NormalizeNominalAttributes() const
Definition: Model.cpp:423
virtual void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
Definition: Model.cpp:755
ClassProbList(const ClassProbList &pairList)
Definition: ClassProb.cpp:55
void NormalizeProbabilitiesWithAMinumum(kkint32 numClasses, double *probabilities, double minProbability)
Definition: Model.cpp:614
virtual const KKStr & VarName() const
Definition: XmlStream.cpp:794
ClassStatisticList * ClassStatisticListPtr
virtual void ProbabilitiesByClassDual(FeatureVectorPtr example, KKStr &classifier1Desc, KKStr &classifier2Desc, ClassProbListPtr &classifier1Results, ClassProbListPtr &classifier2Results, RunLog &log)
Only applied to ModelDual classifier.
Definition: Model.cpp:830
KKStr YYYY_MM_DD_HH_MM_SS() const
Definition: DateTime.cpp:1216
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
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:4420
void ReduceTrainExamples(RunLog &log)
Reduces the Training Images down to the size dictated by the &#39;examplesPerClass&#39; parameter.
Definition: Model.cpp:649
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
Will implement the Dual Classifier Model.
Definition: ModelDual.h:66
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)=0
virtual ModelTypes ModelType() const =0
MLClassList(const MLClassList &_mlClasses)
Copy constructor; will copy list but not own the contents.
Definition: MLClass.cpp:570
double osGetSystemTimeUsed()
Returns the number of CPU seconds used by current process.
double ** crossClassProbTable
Definition: Model.h:396
FileDescPtr fileDesc
Definition: Model.h:406
virtual KKStr ModelTypeStr() const
Definition: Model.h:176
KKException(const char *_exceptionStr)
Definition: KKException.cpp:38
Normalization Parameters; calculation and implementation.
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)
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
KKStr StrFromInt16(kkint16 i)
Definition: KKStr.cpp:5152
void TrainingTimeStart()
Derived classes call this method to start the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:445
kkint32 MemoryConsumedEstimated() const
Definition: MLClass.cpp:1569
const FeatureEncoder2 & Encoder() const
Definition: Model.cpp:406
void ReadXMLModelPost(RunLog &log)
Definition: Model.cpp:987
virtual ~Model()
Frees any memory allocated by, and owned by the Model.
Definition: Model.cpp:188
ModelSvmBase(const KKStr &_name, const ModelParamSvmBase &_param, FactoryFVProducerPtr _factoryFVProducer)
virtual TokenTypes TokenType()=0
Model(const Model &_madel)
Copy Constructor.
Definition: Model.cpp:69
kkint32 crossClassProbTableSize
Definition: Model.h:398
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
Maintains a list of MLClass instances.
Definition: MLClass.h:233
Will only write the ClassName rather than complete MLClass instances.
Definition: MLClass.h:580
FeatureEncoder2Ptr encoder
Definition: Model.h:400
bool weOwnTrainExamples
Definition: Model.h:430
#define int32_max
Definition: KKBaseTypes.h:119
virtual KKStr Description() const
Definition: Model.cpp:248
Abstract Base class for Machine Learning parameters.
Definition: ModelParam.h:35
virtual FileDescPtr FileDesc() const =0
virtual kkint32 ExamplesPerClass() const
Definition: ModelParam.h:113
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: FileDesc.cpp:875
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
FeatureVectorPtr EncodeAExample(FeatureVectorPtr src) const
MLClassIndexListPtr classesIndex
Definition: Model.h:392
ModelDual(const KKStr &_name, const ModelParamDual &_param, FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelDual.cpp:67
FeatureVectorListPtr EncodeAllExamples(const FeatureVectorListPtr srcData)
MLClassListPtr classes
Definition: Model.h:390
virtual bool ValidParam() const
Definition: ModelParam.h:117
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
Definition: Model.cpp:117
void AllocatePredictionVariables()
Definition: Model.cpp:343
XmlElementMLClassNameList * XmlElementMLClassNameListPtr
Definition: MLClass.h:603
MLClassListPtr MLClassesNewInstance() const
Definition: Model.cpp:228
virtual FeatureNumListConstPtr SelectedFeatures() const
Definition: Model.cpp:436
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163