20 #include "CostFactorySVM.h" 23 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 25 CostFactorySVM::CostFactorySVM()
26 :
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){
29 CostFactorySVM::~CostFactorySVM(){
32 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)
33 :
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 double CostFactorySVM::getCost(
const std::vector<
Vector2d<float> > &trainingFeatures){
36 std::map<std::string, svm::SVM_TYPE> svmMap;
38 svmMap[
"C_SVC"]=svm::C_SVC;
39 svmMap[
"nu_SVC"]=svm::nu_SVC;
40 svmMap[
"one_class"]=svm::one_class;
41 svmMap[
"epsilon_SVR"]=svm::epsilon_SVR;
42 svmMap[
"nu_SVR"]=svm::nu_SVR;
44 std::map<std::string, svm::KERNEL_TYPE> kernelMap;
46 kernelMap[
"linear"]=svm::linear;
47 kernelMap[
"polynomial"]=svm::polynomial;
48 kernelMap[
"radial"]=svm::radial;
49 kernelMap[
"sigmoid;"]=svm::sigmoid;
51 unsigned short nclass=trainingFeatures.size();
52 unsigned int ntraining=0;
54 for(
int iclass=0;iclass<nclass;++iclass){
55 ntraining+=m_nctraining[iclass];
56 ntest+=m_nctest[iclass];
62 unsigned short nFeatures=trainingFeatures[0][0].size();
65 param.svm_type = svmMap[m_svm_type];
66 param.kernel_type = kernelMap[m_kernel_type];
67 param.degree = m_kernel_degree;
68 param.gamma = (m_gamma>0)? m_gamma : 1.0/nFeatures;
69 param.coef0 = m_coef0;
71 param.cache_size = m_cache;
73 param.eps = m_epsilon_tol;
74 param.p = m_epsilon_loss;
75 param.shrinking = (m_shrinking)? 1 : 0;
76 param.probability = (m_prob_est)? 1 : 0;
78 param.weight_label = NULL;
80 param.verbose=(m_verbose>1)?
true:
false;
86 prob.y = Malloc(
double,prob.l);
87 prob.x = Malloc(
struct svm_node *,prob.l);
88 x_space = Malloc(
struct svm_node,(nFeatures+1)*ntraining);
89 unsigned long int spaceIndex=0;
91 for(
int iclass=0;iclass<nclass;++iclass){
93 for(
int isample=0;isample<m_nctraining[iclass];++isample){
94 prob.x[lIndex]=&(x_space[spaceIndex]);
95 for(
int ifeature=0;ifeature<nFeatures;++ifeature){
96 x_space[spaceIndex].index=ifeature+1;
97 x_space[spaceIndex].value=trainingFeatures[iclass][isample][ifeature];
100 x_space[spaceIndex++].index=-1;
101 prob.y[lIndex]=iclass;
106 assert(lIndex==prob.l);
108 std::cout <<
"checking parameters" << std::endl;
109 svm_check_parameter(&prob,¶m);
111 std::cout <<
"parameters ok, training" << std::endl;
112 svm=svm_train(&prob,¶m);
114 std::cout <<
"SVM is now trained" << std::endl;
118 double *target = Malloc(
double,prob.l);
119 svm_cross_validation(&prob,¶m,m_cv,target);
120 assert(param.svm_type != EPSILON_SVR&¶m.svm_type != NU_SVR);
121 for(
int i=0;i<prob.l;i++){
122 std::string refClassName=m_nameVector[prob.y[i]];
123 std::string className=m_nameVector[target[i]];
124 if(m_classValueMap.size())
125 m_cm.incrementResult(type2string<short>(m_classValueMap[refClassName]),type2string<short>(m_classValueMap[className]),1.0);
127 m_cm.incrementResult(m_cm.getClass(prob.y[i]),m_cm.getClass(target[i]),1.0);
133 std::vector<double> result(nclass);
134 x_test = Malloc(
struct svm_node,(nFeatures+1));
135 for(
int iclass=0;iclass<nclass;++iclass){
136 for(
int isample=0;isample<m_nctest[iclass];++isample){
137 for(
int ifeature=0;ifeature<nFeatures;++ifeature){
138 x_test[ifeature].index=ifeature+1;
139 x_test[ifeature].value=trainingFeatures[iclass][m_nctraining[iclass]+isample][ifeature];
141 x_test[nFeatures].index=-1;
142 double predict_label=0;
143 assert(svm_check_probability_model(svm));
144 predict_label = svm_predict_probability(svm,x_test,&(result[0]));
146 std::string refClassName=m_nameVector[iclass];
147 std::string className=m_nameVector[
static_cast<short>(predict_label)];
148 if(m_classValueMap.size())
149 m_cm.incrementResult(type2string<short>(m_classValueMap[refClassName]),type2string<short>(m_classValueMap[className]),1.0);
151 m_cm.incrementResult(refClassName,className,1.0);
157 std::cout << m_cm << std::endl;
158 assert(m_cm.nReference());
175 svm_free_and_destroy_model(&(svm));
177 return(m_cm.kappa());