30 using namespace KKMLL;
56 Model (_name
, _param
, _factoryFVProducer
),
103 sizeof (ModelParamSvmBasePtr);
107 return memoryConsumedEstimated;
145 bool _alreadyNormalized,
151 _log.Level (10) <<
"ModelSvmBase::TrainModel[" << param->FileName () <<
"]." << endl;
156 KKStr errMsg =
"ModelSvmBase::TrainModel (param == NULL)";
157 _log.Level (-1) << endl << endl << errMsg << endl << endl;
175 KKStr errMsg =
"ModelSvmBase::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
176 _log.Level (-1) << endl << errMsg << endl << e.ToString () << endl << endl;
179 catch (
const exception& e2)
182 KKStr errMsg =
"ModelSvmBase::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
183 _log.Level (-1) << endl << errMsg << endl << e2.what () << endl << endl;
189 KKStr errMsg =
"ModelSvmBase::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
190 _log.Level (-1) << endl << errMsg << endl << endl;
203 float* y =
new float[trainExamples->QueueSize ()];
205 for (
kkint32 labelIndex = 0; labelIndex < trainExamples->QueueSize (); labelIndex++)
207 kkint16 label = classesIndex->GetClassIndex (trainExamples->IdxToPtr (labelIndex)->MLClass ());
210 _log.Level (-1) << endl <<
" ModelSvmBase::TrainModel ***ERROR*** Label computed to -1; should not be able to happen." << endl << endl;
212 y[labelIndex] = (
float)label;
217 delete[] y; y = NULL;
225 catch (
const std::exception& e)
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;
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;
244 KKStr errMsg =
"ModelSvmBase::TrainModel ***ERROR*** Building 'LibSVM' training model[" + rootFileName + "].";
245 _log.Level (-1) << endl << endl << errMsg << endl << endl;
258 log.Level (-1) << endl << endl <<
"ModelSvmBase::Predict ***ERROR*** (svmModel == NULL)" << endl << endl;
264 log.Level (-1) << endl << endl <<
"ModelSvmBase::Predict ***ERROR*** (classesIndex == NULL)" << endl << endl;
268 bool newExampleCreated =
false;
271 if (newExampleCreated)
273 delete encodedExample;
274 encodedExample = NULL;
281 log.Level (-1) << endl << endl <<
"ModelSvmBase::Predict ***ERROR*** Label[" << y <<
"] Returned from the SVM was not in the ClassAssignments list." << endl << endl;
290 MLClassPtr knownClass,
291 MLClassPtr& predClass1,
292 MLClassPtr& predClass2,
295 double& probOfKnownClass,
296 double& predClass1Prob,
297 double& predClass2Prob,
299 bool& knownClassOneOfTheWinners,
306 KKStr errMsg =
"ModelSvmBase::Predict ***ERROR*** (svmModel == NULL)";
307 log.Level (-1) << endl << endl << errMsg << endl << endl;
313 KKStr errMsg =
"ModelSvmBase::Predict ***ERROR*** (classesIndex == NULL)";
314 log.Level (-1) << endl << endl << errMsg << endl << endl;
320 bool newExampleCreated =
false;
325 if (newExampleCreated)
327 delete encodedExample;
328 encodedExample = NULL;
334 double maxProb1 = -1.0;
335 double maxProb2 = -1.0;
345 maxIndex2 = maxIndex1;
355 if (
votes[idx] > winnerNumVotes)
358 knownClassOneOfTheWinners =
false;
360 winnerNumVotes =
votes[idx];
361 if (knownClassIdx == idx)
362 knownClassOneOfTheWinners =
true;
364 else if (
votes[idx] == winnerNumVotes)
367 if (knownClassIdx == idx)
368 knownClassOneOfTheWinners =
true;
374 predClass1Votes =
votes[maxIndex1];
375 predClass2Votes =
votes[maxIndex2];
376 predClass1Prob = maxProb1;
377 predClass2Prob = maxProb2;
379 breakTie = predClass1Prob - predClass2Prob;
381 if ((knownClassIdx < 0) || (knownClassIdx >=
numOfClasses))
383 probOfKnownClass = 0.0;
384 knownClassOneOfTheWinners =
false;
388 probOfKnownClass = (
float)
classProbs[knownClassIdx];
404 KKStr errMsg =
"ModelSvmBase::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
405 log.Level (-1) << endl << endl << errMsg << endl << endl;
411 KKStr errMsg =
"ModelSvmBase::ProbabilitiesByClass ***ERROR*** (classesIndex == NULL)";
412 log.Level (-1) << endl << endl << errMsg << endl << endl;
416 bool newExampleCreated =
false;
421 if (newExampleCreated)
423 delete encodedExample;
424 encodedExample = NULL;
446 double* _probabilities,
452 KKStr errMsg =
"ModelSvmBase::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
453 _log.Level (-1) << endl << errMsg << endl << endl;
459 KKStr errMsg =
"ModelSvmBase::ProbabilitiesByClass ***ERROR*** (classesIndex == NULL)";
460 _log.Level (-1) << endl << errMsg << endl << endl;
464 bool newExampleCreated =
false;
469 if (newExampleCreated)
471 delete encodedExample;
472 encodedExample = NULL;
476 for (idx = 0; idx < _mlClasses.size (); idx++)
478 MLClassPtr ic = _mlClasses.IdxToPtr (idx);
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;
486 _probabilities [idx] = 0.0f;
490 _votes [idx] =
votes [classIndex];
491 _probabilities [idx] =
classProbs [classIndex];
504 double* _probabilities,
510 KKStr errMsg =
"ModelSvmBase::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
511 _log.Level (-1) << endl << endl << errMsg << endl << endl;
517 KKStr errMsg =
"ModelSvmBase::ProbabilitiesByClass ***ERROR*** (classesIndex == NULL)";
518 _log.Level (-1) << endl << endl << errMsg << endl << endl;
522 bool newExampleCreated =
false;
527 if (newExampleCreated)
529 delete encodedExample;
530 encodedExample = NULL;
534 for (idx = 0; idx < _mlClasses.size (); idx++)
536 MLClassPtr ic = _mlClasses.IdxToPtr (idx);
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;
547 _probabilities [idx] =
classProbs [classIndex];
558 double** _crossProbTable,
563 VectorInt pairWiseIndexes (_classes.size (), 0);
564 for (idx1 = 0; idx1 < _classes.size (); idx1++)
566 MLClassPtr ic = _classes.IdxToPtr (idx1);
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;
577 pairWiseIndexes[idx1] = pairWiseIndex;
580 for (idx2 = 0; idx2 < _classes.size (); idx2++)
581 _crossProbTable[idx1][idx2] = 0.0;
588 for (idx1 = 0; idx1 < (_classes.size () - 1); idx1++)
590 kkint32 pairWiseIndex1 = pairWiseIndexes [idx1];
593 for (idx2 = idx1 + 1; idx2 < _classes.size (); idx2++)
595 kkint32 pairWiseIndex2 = pairWiseIndexes [idx2];
598 _crossProbTable[idx1][idx2] = pairWiseProb[pairWiseIndex1][pairWiseIndex2];
599 _crossProbTable[idx2][idx1] = pairWiseProb[pairWiseIndex2][pairWiseIndex1];
610 kkint32 numOfSupportVectors = 0;
662 if ((t->VarName ().EqualIgnoreCase (
"SvmModel")) && (
typeid(*t) ==
typeid(XmlElementSvm_Model)))
665 svmModel =
dynamic_cast<XmlElementSvm_ModelPtr> (t)->TakeOwnership ();
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;
690 errMsg <<
"ModelSvmBase::ReadXML ***ERROR*** Base class 'Model' does not have 'param' defined.";
692 log.Level (-1) << endl << errMsg << endl << endl;
700 log.Level (-1) << endl << errMsg << endl << endl;
double svm_predict_probability(Svm_Model *model, const FeatureVector &x, double *prob_estimates, kkint32 *votes)
__int16 kkint16
16 bit signed integer.
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.
Svm_Model * svm_train(const svm_problem &prob, const svm_parameter ¶m, RunLog &log)
FeatureVector * FeatureVectorPtr
virtual void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
virtual ModelSvmBasePtr Duplicate() const
KKException(const KKStr &_exceptionStr, const KKException &_innerException)
kkint16 GetClassIndex(MLClassPtr c)
Returns the corresponding index to the class 'c'; if not in list will return -1.
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 void PushOnBack(ClassProbPtr cp)
std::vector< int > VectorInt
FeatureVectorListPtr trainExamples
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &_log)
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
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)
void WriteModelXMLFields(std::ostream &o) const
The "WriteXML" method in Derived classes call this method to include the parents classes fields in th...
const SVM289_MFS::svm_parameter & SvmParam()
ModelSvmBase(FactoryFVProducerPtr _factoryFVProducer)
Svm_Model(const Svm_Model &_model, FileDescPtr _fileDesc)
KKStr operator+(const char *right) const
unsigned __int32 kkuint32
KKException(const KKStr &_exceptionStr, const std::exception &_innerException)
MLClassPtr GetMLClass(kkint16 classIndex)
Locates the MLClass that was assigned classIndex.
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
Every prediction method in every class that is inherited from this class should call this method befo...
virtual KKStr Description() const
virtual void WriteXML(const KKStr &varName, ostream &o) const
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)
double svm_predict(const struct Svm_Model *model, const FeatureVector &x)
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Used to record probability for a specified class; and a list of classes.
virtual FeatureNumListConstPtr SelectedFeatures() const
XmlTag const * XmlTagConstPtr
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
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 'trainingTime'.
virtual kkint32 MemoryConsumedEstimated() const
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
ClassProbList * ClassProbListPtr
void svm_destroy_model(struct Svm_Model *&model)
ModelParamSvmBasePtr param
void SortByVotes(bool highToLow=true)
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Derives predicted probabilities by class.
const KKStr & Name() const
Model(const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer)
Construct a instance of 'Model' using the parameters specified in '_param'.
virtual kkint32 MemoryConsumedEstimated() const
ModelSvmBase(const ModelSvmBase &_model)
svm_problem(const FeatureVectorList &_x, const float *_y, const FeatureNumList &_selFeatures)
kkint32 MemoryConsumedEstimated() const
void WriteXML(std::ostream &o)
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 ...
void TrainingTimeStart()
Derived classes call this method to start the clock for 'trainingTime'.
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
void ReadXMLModelPost(RunLog &log)
virtual KKStr ModelParamTypeStr() const
ModelSvmBase(const KKStr &_name, const ModelParamSvmBase &_param, FactoryFVProducerPtr _factoryFVProducer)
Model(const Model &_madel)
Copy Constructor.
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
KKException(const KKStr &_exceptionStr)
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Maintains a list of MLClass instances.
virtual void WriteXML(const KKStr &varName, ostream &o) const
FeatureVectorList * FeatureVectorListPtr
#define XmlFactoryMacro(NameOfClass)
MLClassIndexListPtr classesIndex
virtual kkint32 NumOfSupportVectors() const
FactoryFVProducer * FactoryFVProducerPtr
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
SVM289_MFS::Svm_Model * svmModel
volatile const bool VolConstBool