37 using namespace KKMLL;
67 Model (_name
, _param
, _factoryFVProducer
),
105 return memoryConsumedEstimated;
176 KKStr errMsg =
"ModelOldSVM::Param ***ERROR*** param not defined (param == NULL).";
177 cerr << endl << errMsg << endl << endl;
183 KKStr errMsg =
"ModelOldSVM::Param ***ERROR*** param variable of wrong type.";
184 cerr << endl << errMsg << endl << endl;
202 MLClassPtr knownClass,
203 MLClassPtr& predClass1,
204 MLClassPtr& predClass2,
207 double& probOfKnownClass,
208 double& predClass1Prob,
209 double& predClass2Prob,
211 bool& knownClassOneOfTheWinners,
216 bool newExampleCreated =
false;
218 svmModel
->Predict (encodedExample
, knownClass
, predClass1
, predClass2
, 219 predClass1Votes
, predClass2Votes
, 221 predClass1Prob
, predClass2Prob
, 223 knownClassOneOfTheWinners
, 226 if (newExampleCreated)
228 delete encodedExample;
229 encodedExample = NULL;
243 bool newExampleCreated =
false;
247 if (newExampleCreated)
249 delete encodedExample;
250 encodedExample = NULL;
262 MLClassPtr & predClass,
279 KKStr errMsg =
"ModelOldSVM::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
280 log.Level (-1) << endl << errMsg << endl;
284 bool newExampleCreated =
false;
288 if (newExampleCreated)
290 delete encodedExample;
291 encodedExample = NULL;
298 MLClassPtr ic = classes->IdxToPtr (idx);
317 double* _probabilities,
321 bool newExampleCreated =
false;
325 if (newExampleCreated)
327 delete encodedExample;
328 encodedExample = NULL;
340 double* _probabilities,
344 bool newExampleCreated =
false;
347 if (newExampleCreated)
349 delete encodedExample;
350 encodedExample = NULL;
366 return svmModel->SupportVectorNames (c1, c2);
377 return svmModel->SupportVectorNames ();
392 return svmModel->FindWorstSupportVectors (example, numToFind, c1, c2);
406 return svmModel->FindWorstSupportVectors2 (example, numToFind, c1, c2);
422 double** crossProbTable,
434 bool _alreadyNormalized,
440 _log.Level (20) <<
"ModelOldSVM::TrainModel - Constructing From Training Data, Model[" << rootFileName <<
"]" << endl;
451 _trainExamples = NULL;
455 KKStr errMsg =
" ModelOldSVM::TrainModel ***ERROR*** (trainExamples == NULL).";
456 _log.Level (-1) << endl << errMsg << endl << endl;
467 KKStr errMsg =
" ModelOldSVM::TrainModel ***ERROR*** (svmParam == NULL).";
468 _log.Level (-1) << endl << errMsg << endl << endl;
476 numOfClasses = classes->QueueSize ();
489 _log.Level (-1) << endl <<
"ModelOldSVM::TrainModel Exception occurred building training model." << endl << endl;
507 bool& newExampleCreated
511 newExampleCreated =
false;
516 if (newExampleCreated)
521 newExampleCreated =
true;
570 while (t && (!cancelFlag))
580 assignments->ParseToString (*(s->Value ()), log);
583 else if ((t->VarName ().EqualIgnoreCase (
"SvmModel")) && (
typeid(*t) ==
typeid(XmlElementSVMModel)))
586 svmModel =
dynamic_cast<XmlElementSVMModelPtr> (t)->TakeOwnership ();
592 errMsg <<
"ModelOldSVM::ReadXML ***ERROR*** Unexpected Token; Section:" << t->SectionName () <<
" VarName: " << t->VarName ();
593 AddErrorMsg (errMsg, 0);
594 log.Level (-1) << endl << errMsg << endl << endl;
613 errMsg <<
"ModelOldSVM::ReadXML ***ERROR*** Base class 'Model' does not have 'param' defined.";
615 log.Level (-1) << endl << errMsg << endl << endl;
623 log.Level (-1) << endl << errMsg << endl << endl;
629 errMsg <<
"ModelOldSVM::ReadXML ***ERROR*** 'SvmModel' was not defined or is not valid.";
631 log.Level (-1) << endl << errMsg << endl << endl;
656 return new XmlElementModelOldSVM(tag, s, cancelFlag, log);
ModelOldSVM(const KKStr &_name, const ModelParamOldSVM &_param, FactoryFVProducerPtr _factoryFVProducer)
Creates a new svm model from the provided example (example) data.
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for 'size' characters.
XmlTag(const KKStr &_name, TagTypes _tagType)
const KKStr & Name() const
Base class to all Learning Algorithms.
virtual bool NormalizeNominalAttributes() const
kkint32 MemoryConsumedEstimated() const
virtual XmlElementModelOldSVM * ManufatureXmlElement(XmlTagPtr tag, XmlStream &s, VolConstBool &cancelFlag, RunLog &log)
ClassAssignments * ClassAssignmentsPtr
bool EqualIgnoreCase(const char *s2) const
std::vector< ProbNamePair > FindWorstSupportVectors(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the 'numToFind' worst S/V's.
std::vector< KKStr > SupportVectorNames() const
FeatureVector * FeatureVectorPtr
void SupportVectorStatistics(kkint32 &numSVs, kkint32 &totalNumSVs)
ModelParamOldSVMPtr Param() const
ModelOldSVM(FactoryFVProducerPtr _factoryFVProducer)
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 ...
FeatureNumListConstPtr GetFeatureNums() const
virtual void PushOnBack(ClassProbPtr cp)
FeatureNumListConstPtr GetFeatureNums() const
MLClassListPtr ExtractListOfClasses() const
FeatureVectorListPtr trainExamples
SVM_SelectionMethod SelectionMethod() const
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 ...
const FileDescPtr FileDesc() const
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
void SupportVectorStatistics(kkint32 &numSVs, kkint32 &totalNumSVs)
MLClassPtr Predict(FeatureVectorPtr example)
const ClassAssignments & Assignments() const
static void RegisterFactory(XmlFactory *factory)
void WriteModelXMLFields(std::ostream &o) const
The "WriteXML" method in Derived classes call this method to include the parents classes fields in th...
virtual void PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
SVM_MachineType MachineType() const
KKStr operator+(const char *right) const
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
Will get the probabilities assigned to each class.
virtual ModelOldSVMPtr Duplicate() const
FeatureNumListConstPtr GetFeatureNums(FileDescPtr filedesc, MLClassPtr class1, MLClassPtr class2)
XmlFactory(const KKStr &_clasName)
kkint32 NumOfSupportVectors() const
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
FeatureVectorPtr ToNormalized(FeatureVectorPtr example) const
KKStr operator+(const char *left, const KKStr &right)
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
void SortByProbability(bool highToLow=true)
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)
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Used to record probability for a specified class; and a list of classes.
XmlTag const * XmlTagConstPtr
static XmlFactoryModelOldSVM * factoryInstance
virtual ModelParamTypes ModelParamType() const =0
kkint32 MemoryConsumedEstimated() const
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
void TrainingTimeEnd()
Derived classes call this method to stop the clock for 'trainingTime'.
virtual kkint32 MemoryConsumedEstimated() const
Binds MLClass objects to the appropriate number that the Learning Algorithm expects.
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2) const
std::vector< ProbNamePair > FindWorstSupportVectors2(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the 'numToFind' worst S/V's.
KKStr MachineTypeToStr(SVM_MachineType machineType)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
ClassProbList * ClassProbListPtr
virtual KKStr Description() const
This class encapsulates are the information necessary to build a SVMModel class.
virtual void TrainModel(FeatureVectorListPtr _trainExamples, bool _alreadyNormalized, bool _takeOwnership, VolConstBool &_cancelFlag, RunLog &_log)
Use given training data to create a trained Model that can be used for classifying examples...
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
Will return the probabilities for all pairs of the classes listed in 'classes'.
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
SVM_SelectionMethod SelectionMethod() const
static XmlFactoryModelOldSVM * FactoryInstance()
void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &_log)
Will get the probabilities assigned to each class.
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)
Will predict the two most likely classes of 'example'.
void SortByVotes(bool highToLow=true)
ModelParamOldSVM * ModelParamOldSVMPtr
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
const ClassAssignments & Assignments() const
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
SVM_SelectionMethod SelectionMethod() const
ModelOldSVM(const ModelOldSVM &_madel)
virtual MLClassPtr Predict(FeatureVectorPtr image, RunLog &log)
XmlElementModelTemplate< ModelOldSVM > XmlElementModelOldSVM
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
ModelOldSVM Specific 'PrepExampleForPrediction' will only normalize data.
XmlElementKKStr * XmlElementKKStrPtr
void WriteXML(std::ostream &o)
FeatureNumListConst * FeatureNumListConstPtr
KKStr SelectionMethodToStr(SVM_SelectionMethod selectionMethod)
Used for logging messages.
void EncodeProblem(const struct svm_paramater ¶m, struct svm_problem &prob_in, struct svm_problem &prob_out)
ClassAssignments(const MLClassList &classes)
Creates assignment for all classes in 'classes.
SVMModel(const SVMparam &_svmParam, FeatureVectorList &_examples, ClassAssignments &_assignments, FileDescPtr _fileDesc, RunLog &_log)
Constructor that will create a svmlib training model using the features and classes for training purp...
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 PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
Returns the distance from the decision border of the SVM.
void TrainingTimeStart()
Derived classes call this method to start the clock for 'trainingTime'.
void ReadXMLModelPost(RunLog &log)
virtual KKStr ModelParamTypeStr() const
Model(const Model &_madel)
Copy Constructor.
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc, MLClassPtr class1, MLClassPtr class2) const
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
KKException(const KKStr &_exceptionStr)
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Maintains a list of MLClass instances.
FeatureVectorList * FeatureVectorListPtr
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
Abstract Base class for Machine Learning parameters.
virtual bool NormalizeNominalAttributes()
virtual const KKStr & VarName() const
kkint32 NumOfSupportVectors() const
FactoryFVProducer * FactoryFVProducerPtr
virtual SVMparamPtr SvmParameters() const
SVMparam const * SVMParameters() const
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Retrieves probabilities assigned to each class.
virtual kkint32 MemoryConsumedEstimated() const
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2) const
volatile const bool VolConstBool