| 1 | /********************************************************************** |
| 2 | CostFactorySVM.cc: select features, typical use: feature selection for classification |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include "CostFactorySVM.h" |
| 22 | #include "svm.h" |
| 23 | |
| 24 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) |
| 25 | |
| 26 | CostFactorySVM::CostFactorySVM() |
| 27 | : CostFactory(2,0), m_svm_type("C_SVC"), m_kernel_type("radial"), m_kernel_degree(3), m_gamma(1.0), m_coef0(0), m_ccost(1000), m_nu(0.5), m_epsilon_loss(100), m_cache(100), m_epsilon_tol(0.001), m_shrinking(false), m_prob_est(true){ |
| 28 | } |
| 29 | |
| 30 | CostFactorySVM::~CostFactorySVM(){ |
| 31 | } |
| 32 | |
| 33 | CostFactorySVM::CostFactorySVM(std::string svm_type, std::string kernel_type, unsigned short kernel_degree, float gamma, float coef0, float ccost, float nu, float epsilon_loss, int cache, float epsilon_tol, bool shrinking, bool prob_est, unsigned short cv, short verbose) |
| 34 | : CostFactory(cv,verbose), m_svm_type(svm_type), m_kernel_type(kernel_type), m_kernel_degree(kernel_degree), m_gamma(gamma), m_coef0(coef0), m_ccost(ccost), m_nu(nu), m_epsilon_loss(epsilon_loss), m_cache(cache), m_epsilon_tol(epsilon_tol), m_shrinking(shrinking), m_prob_est(prob_est){}; |
| 35 | |
| 36 | double CostFactorySVM::getCost(const std::vector<Vector2d<float> > &trainingFeatures){ |
| 37 | std::map<std::string, svm::SVM_TYPE> svmMap; |
| 38 | |
| 39 | svmMap["C_SVC"]=svm::C_SVC; |
| 40 | svmMap["nu_SVC"]=svm::nu_SVC; |
| 41 | svmMap["one_class"]=svm::one_class; |
| 42 | svmMap["epsilon_SVR"]=svm::epsilon_SVR; |
| 43 | svmMap["nu_SVR"]=svm::nu_SVR; |
| 44 | |
| 45 | std::map<std::string, svm::KERNEL_TYPE> kernelMap; |
| 46 | |
| 47 | kernelMap["linear"]=svm::linear; |
| 48 | kernelMap["polynomial"]=svm::polynomial; |
| 49 | kernelMap["radial"]=svm::radial; |
| 50 | kernelMap["sigmoid;"]=svm::sigmoid; |
| 51 | |
| 52 | unsigned short nclass=trainingFeatures.size(); |
| 53 | unsigned int ntraining=0; |
| 54 | unsigned int ntest=0; |
| 55 | for(int iclass=0;iclass<nclass;++iclass){ |
| 56 | ntraining+=m_nctraining[iclass]; |
| 57 | ntest+=m_nctest[iclass]; |
| 58 | } |
| 59 | if(ntest) |
| 60 | assert(!m_cv); |
| 61 | if(!m_cv) |
| 62 | assert(ntest); |
| 63 | unsigned short nFeatures=trainingFeatures[0][0].size(); |
| 64 | |
| 65 | struct svm_parameter param; |
| 66 | param.svm_type = svmMap[m_svm_type]; |
| 67 | param.kernel_type = kernelMap[m_kernel_type]; |
| 68 | param.degree = m_kernel_degree; |
| 69 | param.gamma = (m_gamma>0)? m_gamma : 1.0/nFeatures; |
| 70 | param.coef0 = m_coef0; |
| 71 | param.nu = m_nu; |
| 72 | param.cache_size = m_cache; |
| 73 | param.C = m_ccost; |
| 74 | param.eps = m_epsilon_tol; |
| 75 | param.p = m_epsilon_loss; |
| 76 | param.shrinking = (m_shrinking)? 1 : 0; |
| 77 | param.probability = (m_prob_est)? 1 : 0; |
| 78 | param.nr_weight = 0;//not used: I use priors and balancing |
| 79 | param.weight_label = NULL; |
| 80 | param.weight = NULL; |
| 81 | param.verbose=(m_verbose>1)? true:false; |
| 82 | struct svm_model* svm; |
| 83 | struct svm_problem prob; |
| 84 | struct svm_node* x_space; |
| 85 | |
| 86 | prob.l=ntraining; |
| 87 | prob.y = Malloc(double,prob.l); |
| 88 | prob.x = Malloc(struct svm_node *,prob.l); |
| 89 | x_space = Malloc(struct svm_node,(nFeatures+1)*ntraining); |
| 90 | unsigned long int spaceIndex=0; |
| 91 | int lIndex=0; |
| 92 | for(int iclass=0;iclass<nclass;++iclass){ |
| 93 | // for(int isample=0;isample<trainingFeatures[iclass].size();++isample){ |
| 94 | for(int isample=0;isample<m_nctraining[iclass];++isample){ |
| 95 | prob.x[lIndex]=&(x_space[spaceIndex]); |
| 96 | for(int ifeature=0;ifeature<nFeatures;++ifeature){ |
| 97 | x_space[spaceIndex].index=ifeature+1; |
| 98 | x_space[spaceIndex].value=trainingFeatures[iclass][isample][ifeature]; |
| 99 | ++spaceIndex; |
| 100 | } |
| 101 | x_space[spaceIndex++].index=-1; |
| 102 | prob.y[lIndex]=iclass; |
| 103 | ++lIndex; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | assert(lIndex==prob.l); |
| 108 | if(m_verbose>2) |
| 109 | std::cout << "checking parameters" << std::endl; |
| 110 | svm_check_parameter(&prob,¶m); |
| 111 | if(m_verbose>2) |
| 112 | std::cout << "parameters ok, training" << std::endl; |
| 113 | svm=svm_train(&prob,¶m); |
| 114 | if(m_verbose>2) |
| 115 | std::cout << "SVM is now trained" << std::endl; |
| 116 | |
| 117 | m_cm.clearResults(); |
| 118 | if(m_cv>1){ |
| 119 | double *target = Malloc(double,prob.l); |
| 120 | svm_cross_validation(&prob,¶m,m_cv,target); |
| 121 | assert(param.svm_type != EPSILON_SVR&¶m.svm_type != NU_SVR);//only for regression |
| 122 | for(int i=0;i<prob.l;i++){ |
| 123 | std::string refClassName=m_nameVector[prob.y[i]]; |
| 124 | std::string className=m_nameVector[target[i]]; |
| 125 | if(m_classValueMap.size()) |
| 126 | m_cm.incrementResult(type2string<short>(m_classValueMap[refClassName]),type2string<short>(m_classValueMap[className]),1.0); |
| 127 | else |
| 128 | m_cm.incrementResult(m_cm.getClass(prob.y[i]),m_cm.getClass(target[i]),1.0); |
| 129 | } |
| 130 | free(target); |
| 131 | } |
| 132 | else{ |
| 133 | struct svm_node *x_test; |
| 134 | std::vector<double> result(nclass); |
| 135 | x_test = Malloc(struct svm_node,(nFeatures+1)); |
| 136 | for(int iclass=0;iclass<nclass;++iclass){ |
| 137 | for(int isample=0;isample<m_nctest[iclass];++isample){ |
| 138 | for(int ifeature=0;ifeature<nFeatures;++ifeature){ |
| 139 | x_test[ifeature].index=ifeature+1; |
| 140 | x_test[ifeature].value=trainingFeatures[iclass][m_nctraining[iclass]+isample][ifeature]; |
| 141 | } |
| 142 | x_test[nFeatures].index=-1; |
| 143 | double predict_label=0; |
| 144 | assert(svm_check_probability_model(svm)); |
| 145 | predict_label = svm_predict_probability(svm,x_test,&(result[0])); |
| 146 | // predict_label = svm_predict(svm,x_test); |
| 147 | std::string refClassName=m_nameVector[iclass]; |
| 148 | std::string className=m_nameVector[static_cast<short>(predict_label)]; |
| 149 | if(m_classValueMap.size()) |
| 150 | m_cm.incrementResult(type2string<short>(m_classValueMap[refClassName]),type2string<short>(m_classValueMap[className]),1.0); |
| 151 | else |
| 152 | m_cm.incrementResult(refClassName,className,1.0); |
| 153 | } |
| 154 | } |
| 155 | free(x_test); |
| 156 | } |
| 157 | if(m_verbose>1) |
| 158 | std::cout << m_cm << std::endl; |
| 159 | assert(m_cm.nReference()); |
| 160 | // if(m_verbose) |
| 161 | |
| 162 | // std::cout << m_cm << std::endl; |
| 163 | // std::cout << "Kappa: " << m_cm.kappa() << std::endl; |
| 164 | // double se95_oa=0; |
| 165 | // double doa=0; |
| 166 | // doa=m_cm.oa_pct(&se95_oa); |
| 167 | // std::cout << "Overall Accuracy: " << doa << " (" << se95_oa << ")" << std::endl; |
| 168 | |
| 169 | // *NOTE* Because svm_model contains pointers to svm_problem, you can |
| 170 | // not free the memory used by svm_problem if you are still using the |
| 171 | // svm_model produced by svm_train(). |
| 172 | // however, we will re-train the svm later on after the feature selection |
| 173 | free(prob.y); |
| 174 | free(prob.x); |
| 175 | free(x_space); |
| 176 | svm_free_and_destroy_model(&(svm)); |
| 177 | |
| 178 | return(m_cm.kappa()); |
| 179 | } |