21 #define GRT_DLL_EXPORTS 29 const std::string SVM::id =
"SVM";
35 SVM::SVM(KernelType kernelType,SVMType svmType,
bool useScaling,
bool useNullRejection,
bool useAutoGamma,Float gamma,UINT degree,Float coef0,Float nu,Float C,
bool useCrossValidation,UINT kFoldValue) :
Classifier(
SVM::getId() )
40 param.weight_label = NULL;
47 param.svm_type = C_SVC;
48 param.kernel_type = LINEAR_KERNEL;
53 param.cache_size = 100;
58 param.probability = 1;
60 param.weight_label = NULL;
62 this->useScaling =
false;
63 this->useCrossValidation =
false;
64 this->useNullRejection =
false;
65 this->useAutoGamma =
true;
66 classificationThreshold = 0.5;
67 crossValidationResult = 0;
69 classifierMode = STANDARD_CLASSIFIER_MODE;
71 init(kernelType,svmType,useScaling,useNullRejection,useAutoGamma,gamma,degree,coef0,nu,C,useCrossValidation,kFoldValue);
77 param.weight_label = NULL;
82 classifierMode = STANDARD_CLASSIFIER_MODE;
97 this->problemSet =
false;
98 this->model = rhs.deepCopyModel();
99 this->deepCopyParam( rhs.param, this->param );
100 this->numInputDimensions = rhs.numInputDimensions;
101 this->kFoldValue = rhs.kFoldValue;
102 this->classificationThreshold = rhs.classificationThreshold;
103 this->crossValidationResult = rhs.crossValidationResult;
104 this->useAutoGamma = rhs.useAutoGamma;
105 this->useCrossValidation = rhs.useCrossValidation;
115 if( classifier == NULL )
return false;
118 const SVM *ptr =
dynamic_cast<const SVM*
>(classifier);
123 this->problemSet =
false;
124 this->model = ptr->deepCopyModel();
125 this->deepCopyParam( ptr->param, this->param );
126 this->numInputDimensions = ptr->numInputDimensions;
127 this->kFoldValue = ptr->kFoldValue;
128 this->classificationThreshold = ptr->classificationThreshold;
129 this->crossValidationResult = ptr->crossValidationResult;
130 this->useAutoGamma = ptr->useAutoGamma;
131 this->useCrossValidation = ptr->useCrossValidation;
146 errorLog << __GRT_LOG__ <<
" Training data has zero samples!" << std::endl;
159 if( useValidationSet ){
160 validationData = trainingData.
split( 100-validationSetSize );
164 if( !convertClassificationDataToLIBSVMFormat(trainingData) ){
165 errorLog << __GRT_LOG__ <<
" Failed To Convert Classification Data To LIBSVM Format!" << std::endl;
169 if( useAutoGamma ) param.gamma = 1.0/numInputDimensions;
172 bool trainingResult = trainSVM();
174 if(! trainingResult ){
175 errorLog << __GRT_LOG__ <<
" Failed To Train SVM Model!" << std::endl;
184 trainingSetAccuracy = 0;
185 validationSetAccuracy = 0;
188 bool scalingState = useScaling;
192 errorLog << __GRT_LOG__ <<
" Failed to compute training set accuracy! Failed to fully train model!" << std::endl;
196 if( useValidationSet ){
199 errorLog << __GRT_LOG__ <<
" Failed to compute validation set accuracy! Failed to fully train model!" << std::endl;
204 trainingLog <<
"Training set accuracy: " << trainingSetAccuracy << std::endl;
206 if( useValidationSet ){
207 trainingLog <<
"Validation set accuracy: " << validationSetAccuracy << std::endl;
211 useScaling = scalingState;
219 errorLog << __GRT_LOG__ <<
" The SVM model has not been trained!" << std::endl;
223 if( inputVector.
getSize() != numInputDimensions ){
224 errorLog << __GRT_LOG__ <<
" The size of the input vector (" << inputVector.
getSize() <<
") does not match the number of features of the model (" << numInputDimensions <<
")" << std::endl;
228 if( param.probability == 1 ){
229 if( !predictSVM( inputVector, maxLikelihood, classLikelihoods ) ){
230 errorLog << __GRT_LOG__ <<
" Prediction Failed!" << std::endl;
234 if( !predictSVM( inputVector ) ){
235 errorLog << __GRT_LOG__ <<
" Prediction Failed!" << std::endl;
243 bool SVM::init(KernelType kernelType,SVMType svmType,
bool useScaling,
bool useNullRejection,
bool useAutoGamma,Float gamma,UINT degree,Float coef0,Float nu,Float C,
bool useCrossValidation,UINT kFoldValue){
249 if( !validateKernelType(kernelType) ){
250 errorLog << __GRT_LOG__ <<
" Unknown kernelType!\n";
254 if( !validateSVMType(svmType) ){
255 errorLog << __GRT_LOG__ <<
" Unknown kernelType!\n";
259 param.svm_type = (int)svmType;
260 param.kernel_type = (int)kernelType;
261 param.degree = (int)degree;
265 param.cache_size = 100;
270 param.probability = 1;
272 param.weight_label = NULL;
274 this->useScaling = useScaling;
275 this->useCrossValidation = useCrossValidation;
276 this->useNullRejection = useNullRejection;
277 this->useAutoGamma = useAutoGamma;
278 classificationThreshold = 0.5;
279 crossValidationResult = 0;
284 void SVM::deleteProblemSet(){
286 for(
int i=0; i<prob.l; i++){
305 param.svm_type = C_SVC;
306 param.kernel_type = LINEAR_KERNEL;
311 param.cache_size = 100;
316 param.probability = 1;
318 param.weight_label = NULL;
320 useCrossValidation =
false;
325 bool SVM::validateProblemAndParameters(){
327 const char *errorMsg = svm_check_parameter(&prob,¶m);
330 errorLog << __GRT_LOG__ <<
" Parameters do not match problem! error: " << errorMsg << std::endl;
337 bool SVM::trainSVM(){
339 crossValidationResult = 0;
343 svm_free_and_destroy_model(&model);
349 errorLog << __GRT_LOG__ <<
" Problem not set!" << std::endl;
354 if( !validateProblemAndParameters() )
return false;
356 if( useCrossValidation ){
358 Float total_correct = 0;
359 Float total_error = 0;
360 Float sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
361 Float *target =
new Float[prob.l];
363 svm_cross_validation(&prob,¶m,kFoldValue,target);
364 if( param.svm_type == EPSILON_SVR || param.svm_type == NU_SVR )
366 for(i=0;i<prob.l;i++)
370 total_error += (v-y)*(v-y);
377 crossValidationResult = total_error/prob.l;
381 for(i=0;i<prob.l;i++){
382 if(target[i] == prob.y[i]){
386 crossValidationResult = total_correct/prob.l*100.0;
392 model = svm_train(&prob,¶m);
395 errorLog << __GRT_LOG__ <<
" Failed to train SVM Model!" << std::endl;
404 classLabels[k] = model->label[k];
407 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
415 if( !trained || inputVector.size() != numInputDimensions )
return false;
420 x =
new svm_node[numInputDimensions+1];
421 for(UINT j=0; j<numInputDimensions; j++){
422 x[j].index = (int)j+1;
423 x[j].value = inputVector[j];
426 x[numInputDimensions].index = -1;
427 x[numInputDimensions].value = 0;
431 for(UINT i=0; i<numInputDimensions; i++)
432 x[i].value = grt_scale(x[i].value,ranges[i].minValue,ranges[i].maxValue,
SVM_MIN_SCALE_RANGE,SVM_MAX_SCALE_RANGE);
436 Float predict_label = svm_predict(model,x);
439 predictedClassLabel = (UINT)predict_label;
449 if( !trained || param.probability == 0 || inputVector.size() != numInputDimensions )
return false;
451 Float *prob_estimates = NULL;
455 prob_estimates =
new Float[ model->nr_class ];
458 x =
new svm_node[numInputDimensions+1];
459 for(UINT j=0; j<numInputDimensions; j++){
460 x[j].index = (int)j+1;
461 x[j].value = inputVector[j];
464 x[numInputDimensions].index = -1;
465 x[numInputDimensions].value = 0;
469 for(UINT j=0; j<numInputDimensions; j++)
470 x[j].value = grt_scale(x[j].value,ranges[j].minValue,ranges[j].maxValue,
SVM_MIN_SCALE_RANGE,SVM_MAX_SCALE_RANGE);
474 Float predict_label = svm_predict_probability(model,x,prob_estimates);
476 predictedClassLabel = 0;
478 probabilites.
resize(model->nr_class);
479 for(
int k=0; k<model->nr_class; k++){
480 if( maxProbability < prob_estimates[k] ){
481 maxProbability = prob_estimates[k];
482 predictedClassLabel = k+1;
483 maxLikelihood = maxProbability;
485 probabilites[k] = prob_estimates[k];
488 if( !useNullRejection ) predictedClassLabel = (UINT)predict_label;
490 if( maxProbability >= classificationThreshold ){
491 predictedClassLabel = (UINT)predict_label;
492 }
else predictedClassLabel = GRT_DEFAULT_NULL_CLASS_LABEL;
496 delete[] prob_estimates;
507 const UINT numTrainingExamples = trainingData.
getNumSamples();
512 prob.l = numTrainingExamples;
513 prob.x =
new svm_node*[numTrainingExamples];
514 prob.y =
new Float[numTrainingExamples];
517 for(UINT i=0; i<numTrainingExamples; i++){
519 prob.y[i] = trainingData[i].getClassLabel();
522 prob.x[i] =
new svm_node[numInputDimensions+1];
523 for(UINT j=0; j<numInputDimensions; j++){
524 prob.x[i][j].index = j+1;
525 prob.x[i][j].value = trainingData[i].getSample()[j];
527 prob.x[i][numInputDimensions].index = -1;
528 prob.x[i][numInputDimensions].value = 0;
536 if( !file.is_open() ){
540 file <<
"SVM_MODEL_FILE_V2.0\n";
544 errorLog << __GRT_LOG__ <<
" Failed to save classifier base settings to file!" << std::endl;
548 const svm_parameter& param = trained ? model->param : this->param;
550 file <<
"ModelType: ";
551 switch( param.svm_type ){
562 file <<
"EPSILON_SVR";
568 errorLog << __GRT_LOG__ <<
" Invalid model type: " << param.svm_type << std::endl;
574 file <<
"KernelType: ";
575 switch(param.kernel_type){
580 file <<
"POLYNOMIAL";
589 file <<
"PRECOMPUTED";
592 errorLog << __GRT_LOG__ <<
" Invalid kernel type: " << param.kernel_type << std::endl;
597 file <<
"Degree: " << param.degree << std::endl;
598 file <<
"Gamma: " << param.gamma << std::endl;
599 file <<
"Coef0: " << param.coef0 << std::endl;
600 file <<
"NumberOfFeatures: " << numInputDimensions << std::endl;
601 file <<
"UseShrinking: " << param.shrinking << std::endl;
602 file <<
"UseProbability: " << param.probability << std::endl;
605 UINT numClasses = (UINT)model->nr_class;
606 UINT numSV = (UINT)model->l;
607 file <<
"NumberOfSupportVectors: " << numSV << std::endl;
610 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file << model->rho[i] <<
"\t";
615 for(UINT i=0;i<numClasses;i++) file << model->label[i] <<
"\t";
621 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file << model->probA[i] <<
"\t";
627 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file << model->probB[i] <<
"\t";
632 file <<
"NumSupportVectorsPerClass: \n";
633 for(UINT i=0;i<numClasses;i++) file << model->nSV[i] <<
"\t";
637 file <<
"SupportVectors: \n";
639 const Float *
const *sv_coef = model->sv_coef;
640 const svm_node *
const *SV = model->SV;
642 for(UINT i=0;i<numSV;i++){
643 for(UINT j=0;j<numClasses-1;j++)
644 file << sv_coef[j][i] <<
"\t";
648 if(param.kernel_type == PRECOMPUTED) file << (int) p->value <<
"\t";
650 while(p->index != -1){
651 file << p->index <<
"\t" << p->value <<
"\t";
666 UINT halfNumClasses = 0;
671 if( !file.is_open() ){
672 errorLog << __GRT_LOG__ <<
" The file is not open!" << std::endl;
680 if( word ==
"SVM_MODEL_FILE_V1.0" ){
685 if( word !=
"SVM_MODEL_FILE_V2.0" ){
686 errorLog << __GRT_LOG__ <<
" Invalid file format!" << std::endl;
693 errorLog << __GRT_LOG__ <<
" Failed to load base settings from file!" << std::endl;
702 model->sv_coef = NULL;
713 model->param.svm_type = 0;
714 model->param.kernel_type = 0;
715 model->param.degree = 0;
716 model->param.gamma = 0;
717 model->param.coef0 = 0;
718 model->param.cache_size = 0;
719 model->param.eps = 0;
721 model->param.nr_weight = 0;
722 model->param.weight_label = NULL;
723 model->param.weight = NULL;
726 model->param.shrinking = 0;
727 model->param.probability = 1;
731 if(word !=
"ModelType:"){
732 errorLog << __GRT_LOG__ <<
" Failed to find ModelType header!" << std::endl;
737 if( word ==
"C_SVC" ){
738 model->param.svm_type = C_SVC;
740 if( word ==
"NU_SVC" ){
741 model->param.svm_type = NU_SVC;
743 if( word ==
"ONE_CLASS" ){
744 model->param.svm_type = ONE_CLASS;
746 if( word ==
"EPSILON_SVR" ){
747 model->param.svm_type = EPSILON_SVR;
749 if( word ==
"NU_SVR" ){
750 model->param.svm_type = NU_SVR;
752 errorLog << __GRT_LOG__ <<
" Failed to find SVM type!" << std::endl;
763 if(word !=
"KernelType:"){
764 errorLog << __GRT_LOG__ <<
" Failed to find kernel type!" << std::endl;
769 if( word ==
"LINEAR" ){
770 model->param.kernel_type = LINEAR;
772 if( word ==
"POLYNOMIAL" ){
773 model->param.kernel_type = POLY;
776 model->param.kernel_type = RBF;
778 if( word ==
"SIGMOID" ){
779 model->param.kernel_type = SIGMOID;
781 if( word ==
"PRECOMPUTED" ){
782 model->param.kernel_type = PRECOMPUTED;
784 errorLog << __GRT_LOG__ <<
" Failed to find kernel type!" << std::endl;
795 if(word !=
"Degree:"){
796 errorLog << __GRT_LOG__ <<
" Failed to find Degree header!" << std::endl;
800 file >> model->param.degree;
804 if(word !=
"Gamma:"){
805 errorLog << __GRT_LOG__ <<
" Failed to find Gamma header!" << std::endl;
809 file >> model->param.gamma;
813 if(word !=
"Coef0:"){
814 errorLog << __GRT_LOG__ <<
" Failed to find Coef0 header!" << std::endl;
818 file >> model->param.coef0;
822 if(word !=
"NumberOfFeatures:"){
823 errorLog << __GRT_LOG__ <<
" Failed to find NumberOfFeatures header!" << std::endl;
827 file >> numInputDimensions;
831 if(word !=
"UseShrinking:"){
832 errorLog << __GRT_LOG__ <<
" Failed to find UseShrinking header!" << std::endl;
836 file >> model->param.shrinking;
840 if(word !=
"UseProbability:"){
841 errorLog << __GRT_LOG__ <<
" Failed to find UseProbability header!" << std::endl;
845 file >> model->param.probability;
850 if(word !=
"NumberOfSupportVectors:"){
851 errorLog << __GRT_LOG__ <<
" Failed to find NumberOfSupportVectors header!" << std::endl;
858 halfNumClasses = numClasses*(numClasses-1)/2;
859 model->nr_class = numClasses;
865 errorLog << __GRT_LOG__ <<
" Failed to find RHO header!" << std::endl;
869 model->rho =
new Float[ halfNumClasses ];
870 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->rho[i];
874 if(word !=
"Label:"){
877 model->label =
new int[ numClasses ];
878 for(UINT i=0;i<numClasses;i++) file >> model->label[i];
885 if(word !=
"ProbA:"){
888 model->probA =
new Float[ halfNumClasses ];
889 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probA[i];
896 if(word !=
"ProbB:"){
899 model->probB =
new Float[ halfNumClasses ];
900 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probB[i];
907 if( word ==
"NumSupportVectorsPerClass:" ){
908 model->nSV =
new int[ numClasses ];
909 for(UINT i=0; i<numClasses; i++) file >> model->nSV[i];
918 if(word !=
"SupportVectors:"){
919 errorLog << __GRT_LOG__ <<
" Failed to find SupportVectors header!" << std::endl;
925 model->sv_coef =
new Float*[numClasses-1];
926 for(UINT j=0;j<numClasses-1;j++) model->sv_coef[j] =
new Float[numSV];
929 for(UINT i=0; i<numSV; i++){
930 for(UINT j=0; j<numClasses-1; j++){
931 file >> model->sv_coef[j][i];
934 model->SV[i] =
new svm_node[numInputDimensions+1];
936 if(model->param.kernel_type == PRECOMPUTED) file >> model->SV[i][0].value;
938 for(UINT j=0; j<numInputDimensions; j++){
939 file >> model->SV[i][j].index;
940 file >> model->SV[i][j].value;
942 model->SV[i][numInputDimensions].index = -1;
943 model->SV[i][numInputDimensions].value = 0;
951 classLabels[k] = model->label[k];
959 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
961 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
972 crossValidationResult = 0;
974 svm_free_and_destroy_model(&model);
975 svm_destroy_param(¶m);
982 return useCrossValidation;
992 std::string modelName =
"UNKNOWN";
994 paramPtr = &model->param;
995 }
else paramPtr = ¶m;
997 switch(paramPtr->svm_type){
1002 modelName =
"NU_SVC";
1005 modelName =
"ONE_CLASS";
1008 modelName =
"EPSILON_SVR";
1011 modelName =
"NU_SVR";
1022 std::string modelName =
"UNKNOWN";
1024 paramPtr = &model->param;
1025 }
else paramPtr = ¶m;
1027 switch(paramPtr->kernel_type){
1028 case(LINEAR_KERNEL):
1029 modelName =
"LINEAR_KERNEL";
1032 modelName =
"POLY_KERNEL";
1035 modelName =
"RBF_KERNEL";
1037 case(SIGMOID_KERNEL):
1038 modelName =
"SIGMOID_KERNEL";
1040 case(PRECOMPUTED_KERNEL):
1041 modelName =
"PRECOMPUTED_KERNEL";
1050 if( !trained )
return 0;
1051 return (UINT) model->nr_class;
1056 return (UINT)model->param.degree;
1058 return (UINT)param.gamma;
1063 return model->param.gamma;
1070 return model->param.nu;
1077 return model->param.coef0;
1084 return model->param.C;
1094 if( validateSVMType(svmType) ){
1095 param.svm_type = (int)svmType;
1103 if( validateKernelType(kernelType) ){
1104 param.kernel_type = (int)kernelType;
1107 warningLog << __GRT_LOG__ <<
" Failed to set kernel type, unknown kernelType!" << std::endl;
1112 if( !useAutoGamma ){
1113 this->param.gamma = gamma;
1116 warningLog << __GRT_LOG__ <<
" Failed to set gamma, useAutoGamma is enabled, setUseAutoGamma to false first!" << std::endl;
1121 this->param.degree = (int)degree;
1126 this->param.nu = nu;
1131 this->param.coef0 = coef0;
1141 if( kFoldValue > 0 ){
1142 this->kFoldValue = kFoldValue;
1145 warningLog << __GRT_LOG__ <<
" Failed to set kFoldValue, the kFoldValue must be greater than 0!" << std::endl;
1150 this->useAutoGamma = useAutoGamma;
1155 this->useCrossValidation = useCrossValidation;
1159 bool SVM::validateSVMType(
const SVMType svmType){
1160 if( svmType == C_SVC ){
1163 if( svmType == NU_SVC ){
1166 if( svmType == ONE_CLASS ){
1169 if( svmType == EPSILON_SVR ){
1172 if( svmType == NU_SVR ){
1178 bool SVM::validateKernelType(
const KernelType kernelType){
1179 if( kernelType == LINEAR_KERNEL ){
1182 if( kernelType == POLY_KERNEL ){
1185 if( kernelType == RBF_KERNEL ){
1188 if( kernelType == SIGMOID_KERNEL ){
1191 if( kernelType == PRECOMPUTED_KERNEL ){
1197 struct svm_model* SVM::deepCopyModel()
const{
1199 if( model == NULL )
return NULL;
1201 UINT halfNumClasses = 0;
1219 m->param.svm_type = 0;
1220 m->param.kernel_type = 0;
1221 m->param.degree = 0;
1224 m->param.cache_size = 0;
1227 m->param.nr_weight = 0;
1228 m->param.weight_label = NULL;
1229 m->param.weight = NULL;
1232 m->param.shrinking = 0;
1233 m->param.probability = 1;
1236 m->param.svm_type = model->param.svm_type;
1237 m->param.kernel_type = model->param.kernel_type ;
1238 m->param.degree = model->param.degree;
1239 m->param.gamma = model->param.gamma;
1240 m->param.coef0 = model->param.coef0;
1241 m->nr_class = model->nr_class;
1243 m->param.shrinking = model->param.shrinking;
1244 m->param.probability = model->param.probability;
1247 halfNumClasses = model->nr_class*(model->nr_class-1)/2;
1249 m->rho =
new Float[ halfNumClasses ];
1250 for(
int i=0;i <model->nr_class*(model->nr_class-1)/2; i++) m->rho[i] = model->rho[i];
1252 if( model->label != NULL ){
1253 m->label =
new int[ model->nr_class ];
1254 for(
int i=0;i<model->nr_class;i++) m->label[i] = model->label[i];
1257 if( model->probA != NULL ){
1258 m->probA =
new Float[ halfNumClasses ];
1259 for(UINT i=0;i<halfNumClasses; i++) m->probA[i] = model->probA[i];
1262 if( model->probB != NULL ){
1263 m->probB =
new Float[ halfNumClasses ];
1264 for(UINT i=0; i<halfNumClasses; i++) m->probB[i] = model->probB[i];
1267 if( model->nSV != NULL ){
1268 m->nSV =
new int[ model->nr_class ];
1269 for(
int i=0; i<model->nr_class; i++) m->nSV[i] = model->nSV[i];
1273 m->sv_coef =
new Float*[numClasses-1];
1274 for(UINT j=0;j<numClasses-1;j++) m->sv_coef[j] =
new Float[model->l];
1277 for(
int i=0; i<model->l; i++){
1278 for(
int j=0; j<model->nr_class-1; j++){
1279 m->sv_coef[j][i] = model->sv_coef[j][i];
1282 m->SV[i] =
new svm_node[numInputDimensions+1];
1284 if(model->param.kernel_type == PRECOMPUTED) m->SV[i][0].value = model->SV[i][0].value;
1286 for(UINT j=0; j<numInputDimensions; j++){
1287 m->SV[i][j].index = model->SV[i][j].index;
1288 m->SV[i][j].value = model->SV[i][j].value;
1290 m->SV[i][numInputDimensions].index = -1;
1291 m->SV[i][numInputDimensions].value = 0;
1301 bool SVM::deepCopyProblem(
const struct svm_problem &source,
struct svm_problem &target,
const unsigned int numInputDimensions )
const{
1304 if( target.y != NULL ){
1308 if( target.x != NULL ){
1309 for(
int i=0; i<target.l; i++){
1310 delete[] target.x[i];
1316 target.l = source.l;
1318 if( source.x != NULL ){
1319 target.x =
new svm_node*[ target.l ];
1320 for(
int i=0; i<target.l; i++){
1321 target.x[i] =
new svm_node[ numInputDimensions+1 ];
1322 for(
unsigned int j=0; j<numInputDimensions+1; j++){
1323 target.x[i][j] = source.x[i][j];
1328 if( source.y != NULL ){
1329 target.y =
new Float[ target.l ];
1330 for(
int i=0; i<target.l; i++){
1331 target.y[i] = source.y[i];
1341 if( target_param.weight_label != NULL ){
1342 delete[] target_param.weight_label;
1343 target_param.weight_label = NULL;
1345 if( target_param.weight != NULL ){
1346 delete[] target_param.weight;
1347 target_param.weight = NULL;
1351 target_param.svm_type = source_param.svm_type;
1352 target_param.kernel_type = source_param.kernel_type;
1353 target_param.degree = source_param.degree;
1354 target_param.gamma = source_param.gamma;
1355 target_param.coef0 = source_param.coef0;
1356 target_param.cache_size = source_param.cache_size;
1357 target_param.eps = source_param.eps;
1358 target_param.C = source_param.C;
1359 target_param.nr_weight = source_param.nr_weight;
1360 target_param.nu = source_param.nu;
1361 target_param.p = source_param.p;
1362 target_param.shrinking = source_param.shrinking;
1363 target_param.probability = source_param.probability;
1366 if( source_param.weight_label != NULL ){
1369 if( source_param.weight != NULL ){
1381 UINT halfNumClasses = 0;
1382 numInputDimensions = 0;
1386 model->nr_class = 0;
1389 model->sv_coef = NULL;
1391 model->probA = NULL;
1392 model->probB = NULL;
1393 model->label = NULL;
1395 model->label = NULL;
1400 model->param.svm_type = 0;
1401 model->param.kernel_type = 0;
1402 model->param.degree = 0;
1403 model->param.gamma = 0;
1404 model->param.coef0 = 0;
1405 model->param.cache_size = 0;
1406 model->param.eps = 0;
1408 model->param.nr_weight = 0;
1409 model->param.weight_label = NULL;
1410 model->param.weight = NULL;
1411 model->param.nu = 0;
1413 model->param.shrinking = 0;
1414 model->param.probability = 1;
1418 if(word !=
"ModelType:"){
1419 errorLog << __GRT_LOG__ <<
" Failed to find ModelType header!" << std::endl;
1424 if( word ==
"C_SVC" ){
1425 model->param.svm_type = C_SVC;
1427 if( word ==
"NU_SVC" ){
1428 model->param.svm_type = NU_SVC;
1430 if( word ==
"ONE_CLASS" ){
1431 model->param.svm_type = ONE_CLASS;
1433 if( word ==
"EPSILON_SVR" ){
1434 model->param.svm_type = EPSILON_SVR;
1436 if( word ==
"NU_SVR" ){
1437 model->param.svm_type = NU_SVR;
1439 errorLog << __GRT_LOG__ <<
" Failed to find SVM type!" << std::endl;
1450 if(word !=
"KernelType:"){
1451 errorLog << __GRT_LOG__ <<
" Failed to find kernel type!" << std::endl;
1456 if( word ==
"LINEAR" ){
1457 model->param.kernel_type = LINEAR;
1459 if( word ==
"POLYNOMIAL" ){
1460 model->param.kernel_type = POLY;
1462 if( word ==
"RBF" ){
1463 model->param.kernel_type = RBF;
1465 if( word ==
"SIGMOID" ){
1466 model->param.kernel_type = SIGMOID;
1468 if( word ==
"PRECOMPUTED" ){
1469 model->param.kernel_type = PRECOMPUTED;
1471 errorLog << __GRT_LOG__ <<
" Failed to find kernel type!" << std::endl;
1482 if(word !=
"Degree:"){
1483 errorLog << __GRT_LOG__ <<
" Failed to find Degree header!" << std::endl;
1487 file >> model->param.degree;
1491 if(word !=
"Gamma:"){
1492 errorLog << __GRT_LOG__ <<
" Failed to find Gamma header!" << std::endl;
1496 file >> model->param.gamma;
1500 if(word !=
"Coef0:"){
1501 errorLog << __GRT_LOG__ <<
" Failed to find Coef0 header!" << std::endl;
1505 file >> model->param.coef0;
1509 if(word !=
"NumberOfClasses:"){
1510 errorLog << __GRT_LOG__ <<
" Failed to find NumberOfClasses header!" << std::endl;
1518 if(word !=
"NumberOfSupportVectors:"){
1519 errorLog << __GRT_LOG__ <<
" Failed to find NumberOfSupportVectors header!" << std::endl;
1527 if(word !=
"NumberOfFeatures:"){
1528 errorLog << __GRT_LOG__ <<
" Failed to find NumberOfFeatures header!" << std::endl;
1532 file >> numInputDimensions;
1536 if(word !=
"UseShrinking:"){
1537 errorLog << __GRT_LOG__ <<
" Failed to find UseShrinking header!" << std::endl;
1541 file >> model->param.shrinking;
1545 if(word !=
"UseProbability:"){
1546 errorLog << __GRT_LOG__ <<
" Failed to find UseProbability header!" << std::endl;
1550 file >> model->param.probability;
1554 if(word !=
"UseScaling:"){
1555 errorLog << __GRT_LOG__ <<
" Failed to find UseScaling header!" << std::endl;
1563 if(word !=
"Ranges:"){
1564 errorLog << __GRT_LOG__ <<
" Failed to find Ranges header!" << std::endl;
1570 ranges.
resize(numInputDimensions);
1573 for(UINT i=0; i<ranges.size(); i++){
1574 file >> ranges[i].minValue;
1575 file >> ranges[i].maxValue;
1579 halfNumClasses = numClasses*(numClasses-1)/2;
1580 model->nr_class = numClasses;
1586 errorLog << __GRT_LOG__ <<
" Failed to find RHO header!" << std::endl;
1590 model->rho =
new Float[ halfNumClasses ];
1591 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->rho[i];
1595 if(word !=
"Label:"){
1596 model->label = NULL;
1598 model->label =
new int[ numClasses ];
1599 for(UINT i=0;i<numClasses;i++) file >> model->label[i];
1606 if(word !=
"ProbA:"){
1607 model->probA = NULL;
1609 model->probA =
new Float[ halfNumClasses ];
1610 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probA[i];
1617 if(word !=
"ProbB:"){
1618 model->probB = NULL;
1620 model->probB =
new Float[ halfNumClasses ];
1621 for(UINT i=0;i<numClasses*(numClasses-1)/2;i++) file >> model->probB[i];
1628 if(word !=
"NumSupportVectorsPerClass:"){
1631 model->nSV =
new int[ numClasses ];
1632 for(UINT i=0;i<numClasses;i++) file >> model->nSV[i];
1639 if(word !=
"SupportVectors:"){
1640 errorLog << __GRT_LOG__ <<
" Failed to find SupportVectors header!" << std::endl;
1646 model->sv_coef =
new Float*[numClasses-1];
1647 for(UINT j=0;j<numClasses-1;j++) model->sv_coef[j] =
new Float[numSV];
1650 for(UINT i=0; i<numSV; i++){
1651 for(UINT j=0; j<numClasses-1; j++){
1652 file >> model->sv_coef[j][i];
1655 model->SV[i] =
new svm_node[numInputDimensions+1];
1657 if(model->param.kernel_type == PRECOMPUTED) file >> model->SV[i][0].value;
1659 for(UINT j=0; j<numInputDimensions; j++){
1660 file >> model->SV[i][j].index;
1661 file >> model->SV[i][j].value;
1663 model->SV[i][numInputDimensions].index = -1;
1664 model->SV[i][numInputDimensions].value = 0;
1672 classLabels[k] = model->label[k];
bool saveBaseSettingsToFile(std::fstream &file) const
std::string getKernelType() const
std::string getId() const
bool setCoef0(const Float coef0)
#define DEFAULT_NULL_LIKELIHOOD_VALUE
Float getCrossValidationResult() const
bool enableCrossValidationTraining(const bool useCrossValidation)
bool loadLegacyModelFromFile(std::fstream &file)
const struct LIBSVM::svm_model * getLIBSVMModel() const
bool init(KernelType kernelType, SVMType svmType, bool useScaling, bool useNullRejection, bool useAutoGamma, Float gamma, UINT degree, Float coef0, Float nu, Float C, bool useCrossValidation, UINT kFoldValue)
bool enableAutoGamma(const bool useAutoGamma)
bool getIsCrossValidationTrainingEnabled() const
#define SVM_MIN_SCALE_RANGE
std::string getSVMType() const
bool getIsAutoGammaEnabled() const
virtual UINT getNumClasses() const
SVM(KernelType kernelType=LINEAR_KERNEL, SVMType 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)
virtual bool resize(const unsigned int size)
virtual bool load(std::fstream &file)
void initDefaultSVMSettings()
virtual bool save(std::fstream &file) const
virtual bool computeAccuracy(const ClassificationData &data, Float &accuracy)
UINT getNumSamples() const
bool setDegree(const UINT degree)
static std::string getId()
bool copyBaseVariables(const Classifier *classifier)
bool loadBaseSettingsFromFile(std::fstream &file)
UINT getNumDimensions() const
UINT getNumClasses() const
virtual bool train_(ClassificationData &trainingData)
bool setKFoldCrossValidationValue(const UINT kFoldValue)
Vector< MinMax > getRanges() const
ClassificationData split(const UINT splitPercentage, const bool useStratifiedSampling=false)
bool setNu(const Float nu)
bool setGamma(const Float gamma)
virtual bool deepCopyFrom(const Classifier *classifier)
bool scale(const Float minTarget, const Float maxTarget)
bool setKernelType(const KernelType kernelType)
SVM & operator=(const SVM &rhs)
virtual bool predict_(VectorFloat &inputVector)
bool setSVMType(const SVMType svmType)
This is the main base class that all GRT Classification algorithms should inherit from...