21 #define GRT_DLL_EXPORTS
29 SVM::SVM(UINT kernelType,UINT svmType,
bool useScaling,
bool useNullRejection,
bool useAutoGamma,Float gamma,UINT degree,Float coef0,Float nu,Float C,
bool useCrossValidation,UINT kFoldValue){
33 param.weight_label = NULL;
40 param.svm_type = C_SVC;
41 param.kernel_type = LINEAR_KERNEL;
46 param.cache_size = 100;
51 param.probability = 1;
53 param.weight_label = NULL;
55 this->useScaling =
false;
56 this->useCrossValidation =
false;
57 this->useNullRejection =
false;
58 this->useAutoGamma =
true;
59 classificationThreshold = 0.5;
60 crossValidationResult = 0;
63 classifierType = classType;
64 classifierMode = STANDARD_CLASSIFIER_MODE;
65 debugLog.setProceedingText(
"[DEBUG SVM]");
66 errorLog.setProceedingText(
"[ERROR SVM]");
67 trainingLog.setProceedingText(
"[TRAINING SVM]");
68 warningLog.setProceedingText(
"[WARNING SVM]");
70 init(kernelType,svmType,useScaling,useNullRejection,useAutoGamma,gamma,degree,coef0,nu,C,useCrossValidation,kFoldValue);
75 param.weight_label = NULL;
81 classifierType = classType;
82 classifierMode = STANDARD_CLASSIFIER_MODE;
83 debugLog.setProceedingText(
"[DEBUG SVM]");
84 errorLog.setProceedingText(
"[ERROR SVM]");
85 trainingLog.setProceedingText(
"[TRAINING SVM]");
86 warningLog.setProceedingText(
"[WARNING SVM]");
101 this->problemSet =
false;
102 this->model = rhs.deepCopyModel();
103 this->deepCopyParam( rhs.param, this->param );
104 this->numInputDimensions = rhs.numInputDimensions;
105 this->kFoldValue = rhs.kFoldValue;
106 this->classificationThreshold = rhs.classificationThreshold;
107 this->crossValidationResult = rhs.crossValidationResult;
108 this->useAutoGamma = rhs.useAutoGamma;
109 this->useCrossValidation = rhs.useCrossValidation;
119 if( classifier == NULL )
return false;
122 SVM *ptr = (
SVM*)classifier;
127 this->problemSet =
false;
128 this->model = ptr->deepCopyModel();
129 this->deepCopyParam( ptr->param, this->param );
130 this->numInputDimensions = ptr->numInputDimensions;
131 this->kFoldValue = ptr->kFoldValue;
132 this->classificationThreshold = ptr->classificationThreshold;
133 this->crossValidationResult = ptr->crossValidationResult;
134 this->useAutoGamma = ptr->useAutoGamma;
135 this->useCrossValidation = ptr->useCrossValidation;
150 errorLog <<
"train_(ClassificationData &trainingData) - Training data has zero samples!" << std::endl;
155 if( !convertClassificationDataToLIBSVMFormat(trainingData) ){
156 errorLog <<
"train_(ClassificationData &trainingData) - Failed To Convert Labelled Classification Data To LIBSVM Format!" << std::endl;
160 if( useAutoGamma ) param.gamma = 1.0/numInputDimensions;
163 bool trainingResult = trainSVM();
165 if(! trainingResult ){
166 errorLog <<
"train_(ClassificationData &trainingData) - Failed To Train SVM Model!" << std::endl;
176 errorLog <<
"predict_(VectorFloat &inputVector) - The SVM model has not been trained!" << std::endl;
180 if( inputVector.size() != numInputDimensions ){
181 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input vector (" << inputVector.size() <<
") does not match the number of features of the model (" << numInputDimensions <<
")" << std::endl;
185 if( param.probability == 1 ){
186 if( !predictSVM( inputVector, maxLikelihood, classLikelihoods ) ){
187 errorLog <<
"predict(VectorFloat inputVector) - Prediction Failed!" << std::endl;
191 if( !predictSVM( inputVector ) ){
192 errorLog <<
"predict(VectorFloat inputVector) - Prediction Failed!" << std::endl;
200 bool SVM::init(UINT kernelType,UINT svmType,
bool useScaling,
bool useNullRejection,
bool useAutoGamma,Float gamma,UINT degree,Float coef0,Float nu,Float C,
bool useCrossValidation,UINT kFoldValue){
206 if( !validateKernelType(kernelType) ){
207 errorLog <<
"init(...) - Unknown kernelType!\n";
211 if( !validateSVMType(svmType) ){
212 errorLog <<
"init(...) - Unknown kernelType!\n";
216 param.svm_type = (int)svmType;
217 param.kernel_type = (int)kernelType;
218 param.degree = (int)degree;
222 param.cache_size = 100;
227 param.probability = 1;
229 param.weight_label = NULL;
231 this->useScaling = useScaling;
232 this->useCrossValidation = useCrossValidation;
233 this->useNullRejection = useNullRejection;
234 this->useAutoGamma = useAutoGamma;
235 classificationThreshold = 0.5;
236 crossValidationResult = 0;
241 void SVM::deleteProblemSet(){
243 for(
int i=0; i<prob.l; i++){
262 param.svm_type = C_SVC;
263 param.kernel_type = LINEAR_KERNEL;
268 param.cache_size = 100;
273 param.probability = 1;
275 param.weight_label = NULL;
277 useCrossValidation =
false;
282 bool SVM::validateProblemAndParameters(){
284 const char *errorMsg = svm_check_parameter(&prob,¶m);
287 errorLog <<
"validateProblemAndParameters() - Parameters do not match problem!" << std::endl;
294 bool SVM::trainSVM(){
296 crossValidationResult = 0;
300 svm_free_and_destroy_model(&model);
306 errorLog <<
"trainSVM() - Problem not set!" << std::endl;
311 if( !validateProblemAndParameters() )
return false;
315 for(
int i=0; i<prob.l; i++)
316 for(UINT j=0; j<numInputDimensions; j++)
317 prob.x[i][j].value = grt_scale(prob.x[i][j].value,ranges[j].minValue,ranges[j].maxValue,SVM_MIN_SCALE_RANGE,SVM_MAX_SCALE_RANGE);
320 if( useCrossValidation ){
322 Float total_correct = 0;
323 Float total_error = 0;
324 Float sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
325 Float *target =
new Float[prob.l];
327 svm_cross_validation(&prob,¶m,kFoldValue,target);
328 if( param.svm_type == EPSILON_SVR || param.svm_type == NU_SVR )
330 for(i=0;i<prob.l;i++)
334 total_error += (v-y)*(v-y);
341 crossValidationResult = total_error/prob.l;
345 for(i=0;i<prob.l;i++){
346 if(target[i] == prob.y[i]){
350 crossValidationResult = total_correct/prob.l*100.0;
356 model = svm_train(&prob,¶m);
359 errorLog <<
"trainSVM() - Failed to train SVM Model!" << std::endl;
368 classLabels[k] = model->label[k];
371 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
379 if( !trained || inputVector.size() != numInputDimensions )
return false;
384 x =
new svm_node[numInputDimensions+1];
385 for(UINT j=0; j<numInputDimensions; j++){
386 x[j].index = (int)j+1;
387 x[j].value = inputVector[j];
390 x[numInputDimensions].index = -1;
391 x[numInputDimensions].value = 0;
395 for(UINT i=0; i<numInputDimensions; i++)
396 x[i].value = grt_scale(x[i].value,ranges[i].minValue,ranges[i].maxValue,SVM_MIN_SCALE_RANGE,SVM_MAX_SCALE_RANGE);
400 Float predict_label = svm_predict(model,x);
403 predictedClassLabel = (UINT)predict_label;
413 if( !trained || param.probability == 0 || inputVector.size() != numInputDimensions )
return false;
415 Float *prob_estimates = NULL;
419 prob_estimates =
new Float[ model->nr_class ];
422 x =
new svm_node[numInputDimensions+1];
423 for(UINT j=0; j<numInputDimensions; j++){
424 x[j].index = (int)j+1;
425 x[j].value = inputVector[j];
428 x[numInputDimensions].index = -1;
429 x[numInputDimensions].value = 0;
433 for(UINT j=0; j<numInputDimensions; j++)
434 x[j].value = grt_scale(x[j].value,ranges[j].minValue,ranges[j].maxValue,SVM_MIN_SCALE_RANGE,SVM_MAX_SCALE_RANGE);
438 Float predict_label = svm_predict_probability(model,x,prob_estimates);
440 predictedClassLabel = 0;
442 probabilites.
resize(model->nr_class);
443 for(
int k=0; k<model->nr_class; k++){
444 if( maxProbability < prob_estimates[k] ){
445 maxProbability = prob_estimates[k];
446 predictedClassLabel = k+1;
447 maxLikelihood = maxProbability;
449 probabilites[k] = prob_estimates[k];
452 if( !useNullRejection ) predictedClassLabel = (UINT)predict_label;
454 if( maxProbability >= classificationThreshold ){
455 predictedClassLabel = (UINT)predict_label;
456 }
else predictedClassLabel = GRT_DEFAULT_NULL_CLASS_LABEL;
460 delete[] prob_estimates;
471 const UINT numTrainingExamples = trainingData.
getNumSamples();
478 prob.l = numTrainingExamples;
479 prob.x =
new svm_node*[numTrainingExamples];
480 prob.y =
new Float[numTrainingExamples];
483 for(UINT i=0; i<numTrainingExamples; i++){
485 prob.y[i] = trainingData[i].getClassLabel();
488 prob.x[i] =
new svm_node[numInputDimensions+1];
489 for(UINT j=0; j<numInputDimensions; j++){
490 prob.x[i][j].index = j+1;
491 prob.x[i][j].value = trainingData[i].getSample()[j];
493 prob.x[i][numInputDimensions].index = -1;
494 prob.x[i][numInputDimensions].value = 0;
502 if( !file.is_open() ){
506 file <<
"SVM_MODEL_FILE_V2.0\n";
510 errorLog <<
"save(fstream &file) - Failed to save classifier base settings to file!" << std::endl;
514 const svm_parameter& param = trained ? model->param : this->param;
516 file <<
"ModelType: ";
517 switch( param.svm_type ){
528 file <<
"EPSILON_SVR";
534 errorLog <<
"save(fstream &file) - Invalid model type: " << param.svm_type << std::endl;
540 file <<
"KernelType: ";
541 switch(param.kernel_type){
546 file <<
"POLYNOMIAL";
555 file <<
"PRECOMPUTED";
558 errorLog <<
"save(fstream &file) - Invalid kernel type: " << param.kernel_type << std::endl;
563 file <<
"Degree: " << param.degree << std::endl;
564 file <<
"Gamma: " << param.gamma << std::endl;
565 file <<
"Coef0: " << param.coef0 << std::endl;
566 file <<
"NumberOfFeatures: " << numInputDimensions << std::endl;
567 file <<
"UseShrinking: " << param.shrinking << std::endl;
568 file <<
"UseProbability: " << param.probability << std::endl;
571 UINT numClasses = (UINT)model->nr_class;
572 UINT numSV = (UINT)model->l;
573 file <<
"NumberOfSupportVectors: " << numSV << std::endl;
576 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file << model->rho[i] <<
"\t";
581 for(UINT i=0;i<numClasses;i++) file << model->label[i] <<
"\t";
587 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file << model->probA[i] <<
"\t";
593 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file << model->probB[i] <<
"\t";
598 file <<
"NumSupportVectorsPerClass: \n";
599 for(UINT i=0;i<numClasses;i++) file << model->nSV[i] <<
"\t";
603 file <<
"SupportVectors: \n";
605 const Float *
const *sv_coef = model->sv_coef;
606 const svm_node *
const *SV = model->SV;
608 for(UINT i=0;i<numSV;i++){
609 for(UINT j=0;j<numClasses-1;j++)
610 file << sv_coef[j][i] <<
"\t";
614 if(param.kernel_type == PRECOMPUTED) file << (int) p->value <<
"\t";
616 while(p->index != -1){
617 file << p->index <<
"\t" << p->value <<
"\t";
632 UINT halfNumClasses = 0;
637 if( !file.is_open() ){
638 errorLog <<
"load(fstream &file) - The file is not open!" << std::endl;
646 if( word ==
"SVM_MODEL_FILE_V1.0" ){
651 if( word !=
"SVM_MODEL_FILE_V2.0" ){
652 errorLog <<
"load(fstream &file) - Invalid file format!" << std::endl;
659 errorLog <<
"load(string filename) - Failed to load base settings from file!" << std::endl;
668 model->sv_coef = NULL;
679 model->param.svm_type = 0;
680 model->param.kernel_type = 0;
681 model->param.degree = 0;
682 model->param.gamma = 0;
683 model->param.coef0 = 0;
684 model->param.cache_size = 0;
685 model->param.eps = 0;
687 model->param.nr_weight = 0;
688 model->param.weight_label = NULL;
689 model->param.weight = NULL;
692 model->param.shrinking = 0;
693 model->param.probability = 1;
697 if(word !=
"ModelType:"){
698 errorLog <<
"load(fstream &file) - Failed to find ModelType header!" << std::endl;
703 if( word ==
"C_SVC" ){
704 model->param.svm_type = C_SVC;
706 if( word ==
"NU_SVC" ){
707 model->param.svm_type = NU_SVC;
709 if( word ==
"ONE_CLASS" ){
710 model->param.svm_type = ONE_CLASS;
712 if( word ==
"EPSILON_SVR" ){
713 model->param.svm_type = EPSILON_SVR;
715 if( word ==
"NU_SVR" ){
716 model->param.svm_type = NU_SVR;
718 errorLog <<
"load(fstream &file) - Failed to find SVM type!" << std::endl;
729 if(word !=
"KernelType:"){
730 errorLog <<
"load(fstream &file) - Failed to find kernel type!" << std::endl;
735 if( word ==
"LINEAR" ){
736 model->param.kernel_type = LINEAR;
738 if( word ==
"POLYNOMIAL" ){
739 model->param.kernel_type = POLY;
742 model->param.kernel_type = RBF;
744 if( word ==
"SIGMOID" ){
745 model->param.kernel_type = SIGMOID;
747 if( word ==
"PRECOMPUTED" ){
748 model->param.kernel_type = PRECOMPUTED;
750 errorLog <<
"load(fstream &file) - Failed to find kernel type!" << std::endl;
761 if(word !=
"Degree:"){
762 errorLog <<
"load(fstream &file) - Failed to find Degree header!" << std::endl;
766 file >> model->param.degree;
770 if(word !=
"Gamma:"){
771 errorLog <<
"load(fstream &file) - Failed to find Gamma header!" << std::endl;
775 file >> model->param.gamma;
779 if(word !=
"Coef0:"){
780 errorLog <<
"load(fstream &file) - Failed to find Coef0 header!" << std::endl;
784 file >> model->param.coef0;
788 if(word !=
"NumberOfFeatures:"){
789 errorLog <<
"load(fstream &file) - Failed to find NumberOfFeatures header!" << std::endl;
793 file >> numInputDimensions;
797 if(word !=
"UseShrinking:"){
798 errorLog <<
"load(fstream &file) - Failed to find UseShrinking header!" << std::endl;
802 file >> model->param.shrinking;
806 if(word !=
"UseProbability:"){
807 errorLog <<
"load(fstream &file) - Failed to find UseProbability header!" << std::endl;
811 file >> model->param.probability;
816 if(word !=
"NumberOfSupportVectors:"){
817 errorLog <<
"load(fstream &file) - Failed to find NumberOfSupportVectors header!" << std::endl;
824 halfNumClasses = numClasses*(numClasses-1)/2;
825 model->nr_class = numClasses;
831 errorLog <<
"load(fstream &file) - Failed to find RHO header!" << std::endl;
835 model->rho =
new Float[ halfNumClasses ];
836 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->rho[i];
840 if(word !=
"Label:"){
843 model->label =
new int[ numClasses ];
844 for(UINT i=0;i<numClasses;i++) file >> model->label[i];
851 if(word !=
"ProbA:"){
854 model->probA =
new Float[ halfNumClasses ];
855 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probA[i];
862 if(word !=
"ProbB:"){
865 model->probB =
new Float[ halfNumClasses ];
866 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probB[i];
873 if( word ==
"NumSupportVectorsPerClass:" ){
874 model->nSV =
new int[ numClasses ];
875 for(UINT i=0; i<numClasses; i++) file >> model->nSV[i];
884 if(word !=
"SupportVectors:"){
885 errorLog <<
"load(fstream &file) - Failed to find SupportVectors header!" << std::endl;
891 model->sv_coef =
new Float*[numClasses-1];
892 for(UINT j=0;j<numClasses-1;j++) model->sv_coef[j] =
new Float[numSV];
895 for(UINT i=0; i<numSV; i++){
896 for(UINT j=0; j<numClasses-1; j++){
897 file >> model->sv_coef[j][i];
900 model->SV[i] =
new svm_node[numInputDimensions+1];
902 if(model->param.kernel_type == PRECOMPUTED) file >> model->SV[i][0].value;
904 for(UINT j=0; j<numInputDimensions; j++){
905 file >> model->SV[i][j].index;
906 file >> model->SV[i][j].value;
908 model->SV[i][numInputDimensions].index = -1;
909 model->SV[i][numInputDimensions].value = 0;
917 classLabels[k] = model->label[k];
925 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
927 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
938 crossValidationResult = 0;
940 svm_free_and_destroy_model(&model);
941 svm_destroy_param(¶m);
948 return useCrossValidation;
958 std::string modelName =
"UNKNOWN";
960 paramPtr = &model->param;
961 }
else paramPtr = ¶m;
963 switch(paramPtr->svm_type){
968 modelName =
"NU_SVC";
971 modelName =
"ONE_CLASS";
974 modelName =
"EPSILON_SVR";
977 modelName =
"NU_SVR";
988 std::string modelName =
"UNKNOWN";
990 paramPtr = &model->param;
991 }
else paramPtr = ¶m;
993 switch(paramPtr->kernel_type){
995 modelName =
"LINEAR_KERNEL";
998 modelName =
"POLY_KERNEL";
1001 modelName =
"RBF_KERNEL";
1003 case(SIGMOID_KERNEL):
1004 modelName =
"SIGMOID_KERNEL";
1006 case(PRECOMPUTED_KERNEL):
1007 modelName =
"PRECOMPUTED_KERNEL";
1016 if( !trained )
return 0;
1017 return (UINT) model->nr_class;
1022 return (UINT)model->param.degree;
1024 return (UINT)param.gamma;
1029 return model->param.gamma;
1036 return model->param.nu;
1043 return model->param.coef0;
1050 return model->param.C;
1058 if( validateSVMType(svmType) ){
1059 param.svm_type = (int)svmType;
1066 if( validateKernelType(kernelType) ){
1067 param.kernel_type = (int)kernelType;
1070 warningLog <<
"setKernelType(UINT kernelType) - Failed to set kernel type, unknown kernelType!" << std::endl;
1075 if( !useAutoGamma ){
1076 this->param.gamma = gamma;
1079 warningLog <<
"setGamma(Float gamma) - Failed to set gamma, useAutoGamma is enabled, setUseAutoGamma to false first!" << std::endl;
1084 this->param.degree = (int)degree;
1089 this->param.nu = nu;
1094 this->param.coef0 = coef0;
1104 if( kFoldValue > 0 ){
1105 this->kFoldValue = kFoldValue;
1108 warningLog <<
"setKFoldCrossValidationValue(const UINT kFoldValue) - Failed to set kFoldValue, the kFoldValue must be greater than 0!" << std::endl;
1113 this->useAutoGamma = useAutoGamma;
1118 this->useCrossValidation = useCrossValidation;
1122 bool SVM::validateSVMType(
const UINT svmType){
1123 if( svmType == C_SVC ){
1126 if( svmType == NU_SVC ){
1129 if( svmType == ONE_CLASS ){
1132 if( svmType == EPSILON_SVR ){
1135 if( svmType == NU_SVR ){
1141 bool SVM::validateKernelType(
const UINT kernelType){
1142 if( kernelType == LINEAR_KERNEL ){
1145 if( kernelType == POLY_KERNEL ){
1148 if( kernelType == RBF_KERNEL ){
1151 if( kernelType == SIGMOID_KERNEL ){
1154 if( kernelType == PRECOMPUTED_KERNEL ){
1160 struct svm_model* SVM::deepCopyModel()
const{
1162 if( model == NULL )
return NULL;
1164 UINT halfNumClasses = 0;
1182 m->param.svm_type = 0;
1183 m->param.kernel_type = 0;
1184 m->param.degree = 0;
1187 m->param.cache_size = 0;
1190 m->param.nr_weight = 0;
1191 m->param.weight_label = NULL;
1192 m->param.weight = NULL;
1195 m->param.shrinking = 0;
1196 m->param.probability = 1;
1199 m->param.svm_type = model->param.svm_type;
1200 m->param.kernel_type = model->param.kernel_type ;
1201 m->param.degree = model->param.degree;
1202 m->param.gamma = model->param.gamma;
1203 m->param.coef0 = model->param.coef0;
1204 m->nr_class = model->nr_class;
1206 m->param.shrinking = model->param.shrinking;
1207 m->param.probability = model->param.probability;
1210 halfNumClasses = model->nr_class*(model->nr_class-1)/2;
1212 m->rho =
new Float[ halfNumClasses ];
1213 for(
int i=0;i <model->nr_class*(model->nr_class-1)/2; i++) m->rho[i] = model->rho[i];
1215 if( model->label != NULL ){
1216 m->label =
new int[ model->nr_class ];
1217 for(
int i=0;i<model->nr_class;i++) m->label[i] = model->label[i];
1220 if( model->probA != NULL ){
1221 m->probA =
new Float[ halfNumClasses ];
1222 for(UINT i=0;i<halfNumClasses; i++) m->probA[i] = model->probA[i];
1225 if( model->probB != NULL ){
1226 m->probB =
new Float[ halfNumClasses ];
1227 for(UINT i=0; i<halfNumClasses; i++) m->probB[i] = model->probB[i];
1230 if( model->nSV != NULL ){
1231 m->nSV =
new int[ model->nr_class ];
1232 for(
int i=0; i<model->nr_class; i++) m->nSV[i] = model->nSV[i];
1236 m->sv_coef =
new Float*[numClasses-1];
1237 for(UINT j=0;j<numClasses-1;j++) m->sv_coef[j] =
new Float[model->l];
1240 for(
int i=0; i<model->l; i++){
1241 for(
int j=0; j<model->nr_class-1; j++){
1242 m->sv_coef[j][i] = model->sv_coef[j][i];
1245 m->SV[i] =
new svm_node[numInputDimensions+1];
1247 if(model->param.kernel_type == PRECOMPUTED) m->SV[i][0].value = model->SV[i][0].value;
1249 for(UINT j=0; j<numInputDimensions; j++){
1250 m->SV[i][j].index = model->SV[i][j].index;
1251 m->SV[i][j].value = model->SV[i][j].value;
1253 m->SV[i][numInputDimensions].index = -1;
1254 m->SV[i][numInputDimensions].value = 0;
1264 bool SVM::deepCopyProblem(
const struct svm_problem &source,
struct svm_problem &target,
const unsigned int numInputDimensions )
const{
1267 if( target.y != NULL ){
1271 if( target.x != NULL ){
1272 for(
int i=0; i<target.l; i++){
1273 delete[] target.x[i];
1279 target.l = source.l;
1281 if( source.x != NULL ){
1282 target.x =
new svm_node*[ target.l ];
1283 for(
int i=0; i<target.l; i++){
1284 target.x[i] =
new svm_node[ numInputDimensions+1 ];
1285 for(
unsigned int j=0; j<numInputDimensions+1; j++){
1286 target.x[i][j] = source.x[i][j];
1291 if( source.y != NULL ){
1292 target.y =
new Float[ target.l ];
1293 for(
int i=0; i<target.l; i++){
1294 target.y[i] = source.y[i];
1304 if( target_param.weight_label != NULL ){
1305 delete[] target_param.weight_label;
1306 target_param.weight_label = NULL;
1308 if( target_param.weight != NULL ){
1309 delete[] target_param.weight;
1310 target_param.weight = NULL;
1314 target_param.svm_type = source_param.svm_type;
1315 target_param.kernel_type = source_param.kernel_type;
1316 target_param.degree = source_param.degree;
1317 target_param.gamma = source_param.gamma;
1318 target_param.coef0 = source_param.coef0;
1319 target_param.cache_size = source_param.cache_size;
1320 target_param.eps = source_param.eps;
1321 target_param.C = source_param.C;
1322 target_param.nr_weight = source_param.nr_weight;
1323 target_param.nu = source_param.nu;
1324 target_param.p = source_param.p;
1325 target_param.shrinking = source_param.shrinking;
1326 target_param.probability = source_param.probability;
1329 if( source_param.weight_label != NULL ){
1332 if( source_param.weight != NULL ){
1344 UINT halfNumClasses = 0;
1345 numInputDimensions = 0;
1349 model->nr_class = 0;
1352 model->sv_coef = NULL;
1354 model->probA = NULL;
1355 model->probB = NULL;
1356 model->label = NULL;
1358 model->label = NULL;
1363 model->param.svm_type = 0;
1364 model->param.kernel_type = 0;
1365 model->param.degree = 0;
1366 model->param.gamma = 0;
1367 model->param.coef0 = 0;
1368 model->param.cache_size = 0;
1369 model->param.eps = 0;
1371 model->param.nr_weight = 0;
1372 model->param.weight_label = NULL;
1373 model->param.weight = NULL;
1374 model->param.nu = 0;
1376 model->param.shrinking = 0;
1377 model->param.probability = 1;
1381 if(word !=
"ModelType:"){
1382 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find ModelType header!" << std::endl;
1387 if( word ==
"C_SVC" ){
1388 model->param.svm_type = C_SVC;
1390 if( word ==
"NU_SVC" ){
1391 model->param.svm_type = NU_SVC;
1393 if( word ==
"ONE_CLASS" ){
1394 model->param.svm_type = ONE_CLASS;
1396 if( word ==
"EPSILON_SVR" ){
1397 model->param.svm_type = EPSILON_SVR;
1399 if( word ==
"NU_SVR" ){
1400 model->param.svm_type = NU_SVR;
1402 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find SVM type!" << std::endl;
1413 if(word !=
"KernelType:"){
1414 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find kernel type!" << std::endl;
1419 if( word ==
"LINEAR" ){
1420 model->param.kernel_type = LINEAR;
1422 if( word ==
"POLYNOMIAL" ){
1423 model->param.kernel_type = POLY;
1425 if( word ==
"RBF" ){
1426 model->param.kernel_type = RBF;
1428 if( word ==
"SIGMOID" ){
1429 model->param.kernel_type = SIGMOID;
1431 if( word ==
"PRECOMPUTED" ){
1432 model->param.kernel_type = PRECOMPUTED;
1434 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find kernel type!" << std::endl;
1445 if(word !=
"Degree:"){
1446 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find Degree header!" << std::endl;
1450 file >> model->param.degree;
1454 if(word !=
"Gamma:"){
1455 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find Gamma header!" << std::endl;
1459 file >> model->param.gamma;
1463 if(word !=
"Coef0:"){
1464 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find Coef0 header!" << std::endl;
1468 file >> model->param.coef0;
1472 if(word !=
"NumberOfClasses:"){
1473 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find NumberOfClasses header!" << std::endl;
1481 if(word !=
"NumberOfSupportVectors:"){
1482 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find NumberOfSupportVectors header!" << std::endl;
1490 if(word !=
"NumberOfFeatures:"){
1491 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find NumberOfFeatures header!" << std::endl;
1495 file >> numInputDimensions;
1499 if(word !=
"UseShrinking:"){
1500 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find UseShrinking header!" << std::endl;
1504 file >> model->param.shrinking;
1508 if(word !=
"UseProbability:"){
1509 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find UseProbability header!" << std::endl;
1513 file >> model->param.probability;
1517 if(word !=
"UseScaling:"){
1518 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find UseScaling header!" << std::endl;
1526 if(word !=
"Ranges:"){
1527 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find Ranges header!" << std::endl;
1533 ranges.
resize(numInputDimensions);
1536 for(UINT i=0; i<ranges.size(); i++){
1537 file >> ranges[i].minValue;
1538 file >> ranges[i].maxValue;
1542 halfNumClasses = numClasses*(numClasses-1)/2;
1543 model->nr_class = numClasses;
1549 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find RHO header!" << std::endl;
1553 model->rho =
new Float[ halfNumClasses ];
1554 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->rho[i];
1558 if(word !=
"Label:"){
1559 model->label = NULL;
1561 model->label =
new int[ numClasses ];
1562 for(UINT i=0;i<numClasses;i++) file >> model->label[i];
1569 if(word !=
"ProbA:"){
1570 model->probA = NULL;
1572 model->probA =
new Float[ halfNumClasses ];
1573 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probA[i];
1580 if(word !=
"ProbB:"){
1581 model->probB = NULL;
1583 model->probB =
new Float[ halfNumClasses ];
1584 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probB[i];
1591 if(word !=
"NumSupportVectorsPerClass:"){
1594 model->nSV =
new int[ numClasses ];
1595 for(UINT i=0;i<numClasses;i++) file >> model->nSV[i];
1602 if(word !=
"SupportVectors:"){
1603 errorLog <<
"loadLegacyModelFromFile(fstream &file) - Failed to find SupportVectors header!" << std::endl;
1609 model->sv_coef =
new Float*[numClasses-1];
1610 for(UINT j=0;j<numClasses-1;j++) model->sv_coef[j] =
new Float[numSV];
1613 for(UINT i=0; i<numSV; i++){
1614 for(UINT j=0; j<numClasses-1; j++){
1615 file >> model->sv_coef[j][i];
1618 model->SV[i] =
new svm_node[numInputDimensions+1];
1620 if(model->param.kernel_type == PRECOMPUTED) file >> model->SV[i][0].value;
1622 for(UINT j=0; j<numInputDimensions; j++){
1623 file >> model->SV[i][j].index;
1624 file >> model->SV[i][j].value;
1626 model->SV[i][numInputDimensions].index = -1;
1627 model->SV[i][numInputDimensions].value = 0;
1635 classLabels[k] = model->label[k];
bool saveBaseSettingsToFile(std::fstream &file) const
std::string getKernelType() const
bool setCoef0(const Float coef0)
#define DEFAULT_NULL_LIKELIHOOD_VALUE
Float getCrossValidationResult() const
bool enableCrossValidationTraining(const bool useCrossValidation)
bool loadLegacyModelFromFile(std::fstream &file)
bool enableAutoGamma(const bool useAutoGamma)
bool getIsCrossValidationTrainingEnabled() const
bool setSVMType(const UINT svmType)
std::string getClassifierType() const
SVM(UINT kernelType=LINEAR_KERNEL, UINT svmType=C_SVC, bool useScaling=true, bool useNullRejection=false, bool useAutoGamma=true, Float gamma=0.1, UINT degree=3, Float coef0=0, Float nu=0.5, Float C=1, bool useCrossValidation=false, UINT kFoldValue=10)
std::string getSVMType() const
bool getIsAutoGammaEnabled() const
virtual UINT getNumClasses() const
virtual bool resize(const unsigned int size)
virtual bool load(std::fstream &file)
This class acts as a front end for the LIBSVM library (http://www.csie.ntu.edu.tw/~cjlin/libsvm/). It implements a Support Vector Machine (SVM) classifier, a powerful classifier that works well on a wide range of classification problems, particularly on more complex problems that other classifiers (such as the KNN, GMM or ANBC algorithms) might not be able to solve.
void initDefaultSVMSettings()
virtual bool save(std::fstream &file) const
UINT getNumSamples() const
bool init(UINT kernelType, UINT svmType, bool useScaling, bool useNullRejection, bool useAutoGamma, Float gamma, UINT degree, Float coef0, Float nu, Float C, bool useCrossValidation, UINT kFoldValue)
bool setDegree(const UINT degree)
bool setKernelType(const UINT kernelType)
bool copyBaseVariables(const Classifier *classifier)
bool loadBaseSettingsFromFile(std::fstream &file)
UINT getNumDimensions() const
virtual bool train_(ClassificationData &trainingData)
bool setKFoldCrossValidationValue(const UINT kFoldValue)
Vector< MinMax > getRanges() const
bool setNu(const Float nu)
bool setGamma(const Float gamma)
virtual bool deepCopyFrom(const Classifier *classifier)
SVM & operator=(const SVM &rhs)
virtual bool predict_(VectorFloat &inputVector)