35 #include "ModelDual.h" 36 #include "ModelParam.h" 37 #include "ModelParamDual.h" 38 #include "ModelParamOldSVM.h" 39 #include "NormalizationParms.h" 40 using namespace KKMLL;
59 trianingPrepTime (0.0),
61 trainingTimeStart (0.0),
85 trianingPrepTime (0.0),
86 trainingTime (_model.trainingTime),
87 trainingTimeStart (_model.trainingTime),
99 numOfClasses = classes->QueueSize ();
133 trianingPrepTime (0.0),
135 trainingTimeStart (0.0),
154 FactoryFVProducerPtr _factoryFVProducer
171 trianingPrepTime (0.0),
173 trainingTimeStart (0.0),
223 return memoryConsumedEstimated;
243 errors.push_back (errMsg);
290 FactoryFVProducerPtr _factoryFVProducer,
322 _log.Level (-1) << endl << errMsg << endl << endl;
328 delete model; model = NULL;
329 throw KKException ("Model::CreateAModel Exception calling constructor.", e
);
333 delete model; model = NULL;
334 throw KKException ("Model::CreateAModel Exception calling constructor. No info provided.");
353 <<
"Model::AllocatePredictionVariables ***ERROR*** (classes == NULL)" << endl
360 numOfClasses = classes->QueueSize ();
409 throw KKException ("Model::GetFeatureNums 'encoder == NULL'.");
418 throw KKException ("Model::GetFeatureNums 'param == NULL'.");
426 throw KKException ("Model::NormalizeNominalAttributes 'param == NULL'.");
455 trainingTime = trainingTimeEnd - trainingTimeStart;
456 if (trainingTime < 0.0)
457 trainingTime += (24.0 * 60.0 * 60.0);
468 bool _alreadyNormalized,
474 _log.Level (10) <<
"Model::TrainModel Preparing for training of Model[" << Name () <<
"] Examples[" << _trainExamples->QueueSize () <<
"]" << endl;
478 if (_trainExamples == NULL)
480 KKStr errMsg =
"ModelSvmBase::TrainModel (_trainExamples == NULL)";
481 _log.Level (-1) << endl << endl << errMsg << endl << endl;
485 if (_trainExamples->QueueSize () < 2)
487 KKStr errMsg =
"ModelSvmBase::TrainModel (_trainExamples == NULL)";
488 _log.Level (-1) << endl << endl << errMsg << endl << endl;
489 throw KKException (errMsg);
495 _trainExamples = NULL;
507 if (!_alreadyNormalized)
516 else if (!trainExamples->Owner ())
519 FeatureVectorListPtr temp = trainExamples->Duplicate (
true);
520 weOwnTrainExamples =
true;
521 delete trainExamples;
522 trainExamples = temp;
554 trianingPrepTime = prepEndTime - prepStartTime;
555 if (trianingPrepTime < 0.0)
556 trianingPrepTime += (24.0 * 60.0 * 60.0);
558 _log.Level (40) <<
"Model::TrainModel Exiting." << endl;
575 bool& newExampleCreated
578 FeatureVectorPtr oldFV = NULL;
579 newExampleCreated =
false;
584 if (newExampleCreated)
589 newExampleCreated =
true;
597 if (newExampleCreated)
602 newExampleCreated =
true;
615 double* probabilities,
616 double minProbability
619 double sumGreaterOrEqualMin = 0.0;
623 for (x = 0; x < numClasses; ++x)
625 if (probabilities[x] < minProbability)
628 sumGreaterOrEqualMin += probabilities[x];
631 double probLessMinTotal = numLessThanMin * minProbability;
632 double probLeftToAllocate = 1.0 - probLessMinTotal;
634 for (x = 0; x < numClasses; ++x)
636 if (probabilities[x] < minProbability)
637 probabilities[x] = minProbability;
639 probabilities[x] = (probabilities[x] / sumGreaterOrEqualMin) * probLeftToAllocate;
654 if (examplesPerClass < 0)
657 bool reductionNeeded =
false;
664 log.Level (-1) << endl
665 <<
"Model::ReduceTrainExamples ***ERROR*** can not retrieve Class Stat's for training data." << endl
671 for (zed = 0; (zed < stats->size ()) && (!reductionNeeded); zed++)
673 if (stats->IdxToPtr (zed)->Count () > (kkuint32)examplesPerClass)
674 reductionNeeded =
true;
681 if (!reductionNeeded)
683 log.Level (20) <<
"Model::ReduceTrainExamples Was not needed. No classes exceeded 'examplesPerClass'." << endl;
689 MLClassList::iterator idx;
691 for (idx = classes->begin (); idx != classes->end (); idx++)
693 MLClassPtr ic = *idx;
695 if ((!examplesThisClass) || (examplesThisClass->size () < 1))
697 log.Level (-1) << endl
698 <<
"Model::ReduceTrainExamples ***ERROR*** No Training Examples for class[" << ic->Name () <<
"]." << endl
703 if (examplesThisClass->size () <= (kkuint32)examplesPerClass)
705 reducedSet->AddQueue (*examplesThisClass);
709 examplesThisClass->RandomizeOrder ();
711 while (zed < (kkuint32)examplesPerClass)
713 reducedSet->PushOnBack (examplesThisClass->IdxToPtr (zed));
716 while (zed < examplesThisClass->size ())
718 deleteSet->PushOnBack (examplesThisClass->IdxToPtr (zed));
723 delete examplesThisClass;
724 examplesThisClass = NULL;
729 trainExamples->Owner (
false);
734 trainExamples->Owner (
true);
735 deleteSet->Owner (
true);
742 reducedSet->Owner (
false);
746 deleteSet->Owner (
false);
756 double** crossClassProbTable,
760 if (classes.QueueSize () != crossClassProbTableSize)
763 log.Level (-1) << endl
764 <<
"SVMModel::RetrieveCrossProbTable ***ERROR***" << endl
765 <<
" classes.QueueSize ()[" << classes.QueueSize () <<
"] != crossClassProbTableSize[" << crossClassProbTableSize <<
"]" << endl
772 for (x = 0; x < classes.QueueSize (); x++)
774 for (y = 0; y < classes.QueueSize (); y++)
775 crossClassProbTable[x][y] = 0.0;
777 indexTable[x] = classesIndex->GetClassIndex (classes.IdxToPtr (x));
778 if (indexTable[x] < 0)
780 log.Level (-1) << endl
781 <<
"SVMModel::RetrieveCrossProbTable ***WARNING***" << endl
783 <<
" Class Index[" << x <<
"] Name[" << classes[x].Name () <<
"]" << endl
784 <<
" will populate this index with zeros." << endl
789 if (classes.QueueSize () != crossClassProbTableSize)
791 log.Level (-1) << endl
792 <<
"SVMModel::RetrieveCrossProbTable ***ERROR***" << endl
793 <<
" 'classes.QueueSize () != crossClassProbTableSize'" << endl
801 for (x = 0; x < classes.QueueSize (); x++)
806 for (y = 0; y < classes.QueueSize (); y++)
811 if ((x != xIdx) || (y != yIdx))
823 delete[] indexTable; indexTable = NULL;
831 KKStr& classifier1Desc,
832 KKStr& classifier2Desc,
833 ClassProbListPtr& classifier1Results,
834 ClassProbListPtr& classifier2Results,
838 delete classifier1Results; classifier1Results = NULL;
839 delete classifier2Results; classifier2Results = NULL;
845 if (classifier1Results)
865 if (factoryFVProducer)
866 XmlElementKKStr::WriteXML (factoryFVProducer->Name (),
"FvFactoryProducer", o);
875 XmlElementDouble::WriteXML (trainingTime,
"TrainingTime", o);
891 bool tokenFound =
true;
902 errMsg <<
"Model::ReadXMLModelToken ***ERROR*** Wrong ModelType encountered; Expected[" <<
ModelTypeStr () <<
"] " 903 <<
"ModelType Specified[" << e
->ToKKStr () <<
"].";
905 log.Level (-1) << endl << errMsg << endl << endl;
922 else if (varName.EqualIgnoreCase (
"ClassesIndex") && (
typeid(*e) ==
typeid (XmlElementMLClassIndexList)))
925 classesIndex =
dynamic_cast<XmlElementMLClassIndexListPtr>(t)->TakeOwnership ();
928 else if (varName.EqualIgnoreCase (
"FvFactoryProducer"))
930 factoryFVProducer = FactoryFVProducer::LookUpFactory (e->ToKKStr ());
933 else if (varName.EqualIgnoreCase (
"FileDesc") && (
typeid(*e) ==
typeid (XmlElementFileDesc)))
935 fileDesc =
dynamic_cast<XmlElementFileDescPtr>(t)->Value ();
938 else if (varName.EqualIgnoreCase (
"Param"))
940 XmlElementModelParamPtr xmlParameterElement =
dynamic_cast<XmlElementModelParamPtr> (e);
941 if (xmlParameterElement)
944 param = xmlParameterElement->TakeOwnership ();
949 errMsg <<
"Model::ReadXMLModelToken ***ERROR*** ModelParam variable 'param' not defined correctly.";
950 log.Level (-1) << endl << errMsg << endl << endl;
951 AddErrorMsg (errMsg, 0);
955 else if (varName.EqualIgnoreCase (
"TimeSaved"))
956 timeSaved = DateTime (e->ToKKStr ());
958 else if (varName.EqualIgnoreCase (
"TrainingTime"))
959 trainingTime = e->ToDouble ();
961 else if (varName.EqualIgnoreCase (
"AlreadyNormalized"))
962 alreadyNormalized = e->ToBool ();
964 else if ((varName.EqualIgnoreCase (
"NormParms")) && (
typeid (*e) ==
typeid (XmlElementNormalizationParms)))
967 normParms =
dynamic_cast<XmlElementNormalizationParmsPtr> (e)->TakeOwnership ();
991 KKStr errMsg =
"Model::ReadXMLModelPost ***ERROR*** 'param' not defined.";
993 log.Level (-1) << endl << errMsg << endl << endl;
998 KKStr errMsg =
"Model::ReadXMLModelPost ***ERROR*** 'Param' is NOT Valid .";
1000 log.Level (-1) << endl << errMsg << endl << endl;
1005 KKStr errMsg =
"Model::ReadXMLModelPost ***ERROR*** 'classes' not defined.";
1007 log.Level (-1) << endl << errMsg << endl << endl;
1012 KKStr errMsg =
"Model::ReadXMLModelPost ***ERROR*** 'fileDesc' not defined.";
1014 log.Level (-1) << endl << errMsg << endl << endl;
1017 if (errors.size () > 0)
1019 log.Level (-1) <<
"Model::ReadXMLModelPost Errors were detected; model is INVALID." << endl;
1024 AllocatePredictionVariables ();
ModelOldSVM(const KKStr &_name, const ModelParamOldSVM &_param, FactoryFVProducerPtr _factoryFVProducer)
Creates a new svm model from the provided example (example) data.
__int16 kkint16
16 bit signed integer.
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for 'size' characters.
const KKStr & Name() const
kkint32 MemoryConsumedEstimated() const
Base class to all Learning Algorithms.
FeatureEncoder2(const ModelParam &_param, FileDescPtr _fileDesc)
Constructs a Feature Encoder object.
void NormalizeExamples(FeatureVectorListPtr examples, RunLog &log)
bool EqualIgnoreCase(const char *s2) const
KKException(const char *_exceptionStr, const KKException &_innerException)
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 '_modelType'...
kkint32 MemoryConsumedEstimated() const
Maintains a list of classes and their associated integer index.
static KKStr ModelTypeToStr(ModelTypes _modelingType)
virtual FeatureVectorListPtr Duplicate(bool _owner) const
Creates a duplicate of list using the same container.
virtual KKStr ToKKStr() const
virtual kkint32 MemoryConsumedEstimated() const
NormalizationParmsPtr normParms
XmlTokenPtr ReadXMLModelToken(XmlTokenPtr t, RunLog &log)
Will process any tokens that belong to 'ModelParam' and return NULL ones that are not will be passed ...
virtual FeatureNumListConstPtr GetFeatureNums() const
MLClassIndexList(const MLClassIndexList &_list)
MLClassListPtr ExtractListOfClasses() const
kkint32 MemoryConsumedEstimated() const
FeatureVectorListPtr trainExamples
static void WriteXML(const MLClassList &mlClassList, const KKStr &varName, std::ostream &o)
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
kkint32 MemoryConsumedEstimated() const
void WriteModelXMLFields(std::ostream &o) const
The "WriteXML" method in Derived classes call this method to include the parents classes fields in th...
FactoryFVProducerPtr factoryFVProducer
virtual EncodingMethodType EncodingMethod() const
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
virtual void WriteXML(const KKStr &varName, std::ostream &o) const =0
static ModelTypes ModelTypeFromStr(const KKStr &_modelingTypeStr)
static void WriteXML(const bool b, const KKStr &varName, std::ostream &o)
KKStr operator+(const char *right) const
virtual FeatureVectorListPtr ManufactureEmptyList(bool _owner) const
Creates an instance of a Empty FeatureVectorList.
unsigned __int32 kkuint32
virtual ModelParamPtr Duplicate() const =0
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
Every prediction method in every class that is inherited from this class should call this method befo...
KKStr & operator=(KKStr &&src)
MLClassIndexList(const MLClassList &_classes)
FeatureVectorPtr ToNormalized(FeatureVectorPtr example) const
KKStr operator+(const char *left, const KKStr &right)
NormalizationParms(const ModelParam &_param, FeatureVectorList &_examples, RunLog &_log)
KKStr(const KKStr &str)
Copy Constructor.
XmlElement * XmlElementPtr
virtual kkint32 MemoryConsumedEstimated() const
virtual FeatureNumListConstPtr SelectedFeatures() const
FeatureVectorListPtr ExtractExamplesForAGivenClass(MLClassPtr _mlClass, kkint32 _maxToExtract=-1, float _minSize=-1.0f) const
FeatureEncoder2(const FeatureEncoder2 &_encoder)
MLClassListPtr TakeOwnership()
This class encapsulates are the information necessary to build a UsfCasCor class. ...
void TrainingTimeEnd()
Derived classes call this method to stop the clock for 'trainingTime'.
virtual kkint32 MemoryConsumedEstimated() const
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)
ModelUsfCasCor(const KKStr &_name, const ModelParamUsfCasCor &_param, FactoryFVProducerPtr _factoryFVProducer)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
ClassStatisticListPtr GetClassStatistics() const
Returns the number of FeatureVectors per class.
virtual bool NormalizeNominalAttributes() const
virtual void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
ClassProbList(const ClassProbList &pairList)
void NormalizeProbabilitiesWithAMinumum(kkint32 numClasses, double *probabilities, double minProbability)
virtual const KKStr & VarName() const
ClassStatisticList * ClassStatisticListPtr
virtual void ProbabilitiesByClassDual(FeatureVectorPtr example, KKStr &classifier1Desc, KKStr &classifier2Desc, ClassProbListPtr &classifier1Results, ClassProbListPtr &classifier2Results, RunLog &log)
Only applied to ModelDual classifier.
KKStr YYYY_MM_DD_HH_MM_SS() const
Model(const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer)
Construct a instance of 'Model' using the parameters specified in '_param'.
void WriteXML(const KKStr &varName, std::ostream &o) const
void ReduceTrainExamples(RunLog &log)
Reduces the Training Images down to the size dictated by the 'examplesPerClass' parameter.
KKStr operator+(const KKStr &right) const
Will implement the Dual Classifier Model.
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.
double osGetSystemTimeUsed()
Returns the number of CPU seconds used by current process.
double ** crossClassProbTable
virtual KKStr ModelTypeStr() const
KKException(const char *_exceptionStr)
Normalization Parameters; calculation and implementation.
FeatureNumListConst * FeatureNumListConstPtr
Used for logging messages.
void EncodeProblem(const struct svm_paramater ¶m, 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 ...
KKStr StrFromInt16(kkint16 i)
void TrainingTimeStart()
Derived classes call this method to start the clock for 'trainingTime'.
kkint32 MemoryConsumedEstimated() const
const FeatureEncoder2 & Encoder() const
void ReadXMLModelPost(RunLog &log)
virtual ~Model()
Frees any memory allocated by, and owned by the Model.
ModelSvmBase(const KKStr &_name, const ModelParamSvmBase &_param, FactoryFVProducerPtr _factoryFVProducer)
virtual TokenTypes TokenType()=0
Model(const Model &_madel)
Copy Constructor.
kkint32 crossClassProbTableSize
KKException(const KKStr &_exceptionStr)
Maintains a list of MLClass instances.
Will only write the ClassName rather than complete MLClass instances.
FeatureEncoder2Ptr encoder
virtual KKStr Description() const
Abstract Base class for Machine Learning parameters.
virtual FileDescPtr FileDesc() const =0
virtual kkint32 ExamplesPerClass() const
void WriteXML(const KKStr &varName, std::ostream &o) const
virtual const KKStr & VarName() const
FeatureVectorPtr EncodeAExample(FeatureVectorPtr src) const
MLClassIndexListPtr classesIndex
ModelDual(const KKStr &_name, const ModelParamDual &_param, FactoryFVProducerPtr _factoryFVProducer)
FeatureVectorListPtr EncodeAllExamples(const FeatureVectorListPtr srcData)
virtual bool ValidParam() const
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
void AllocatePredictionVariables()
XmlElementMLClassNameList * XmlElementMLClassNameListPtr
MLClassListPtr MLClassesNewInstance() const
virtual FeatureNumListConstPtr SelectedFeatures() const
volatile const bool VolConstBool