cjlin1 / libsvm

LIBSVM -- A Library for Support Vector Machines
https://www.csie.ntu.edu.tw/~cjlin/libsvm/
BSD 3-Clause "New" or "Revised" License
4.54k stars 1.64k forks source link

The best parameter for prob outputs may be different from the setting #153

Open pango99 opened 4 years ago

pango99 commented 4 years ago

The best parameter for prob outputs may be different from the setting without.. See also the following faq

Q: Why using svm-predict -b 0 and -b 1 gives different accuracy values?

Let's just consider two-class classification here. After probability information is obtained in training, we do not have

prob > = 0.5 if and only if decision value >= 0.

So predictions may be different with -b 0 and 1.

On 2019-09-27 01:22, Zheng Li wrote:

Hi, I use libSVM to develop my face recognition system,I use the libSVM to find the closet face feature and identify target person,but I found when I call svm_predict_probability() to do the prediction,the accuracy is very low,and when I call svm_predict(), the accuracy is high,because I need the probability param to check similarity , so I want to know why svm_predict_probability()'s accuracy is so low?

below is my code to create the svm_model:

inline void fillLibSvmNodes(std::vector &fea, std::vector &line_x_space) { size_t featuresNb = fea.size(); assert((fea.size() + 1) == line_x_space.size()); for (int i = 0; i < featuresNb; i++) { line_x_space[i].index = i + 1; line_x_space[i].value = (double)fea[i]; } line_x_space[featuresNb].index = -1; }

void testLibSVM() { // dbFaceFeatures is a global variable to store the load face features, it is vector<pair<string,vector>> type,the face feature is 512D; assert(dbFaceFeatures.size() > 0); int featuresNb = (int)dbFaceFeatures[0].second.size(); // featuresNb=512

memset(&param, 0, sizeof(svm_parameter)); param.svm_type = C_SVC; param.kernel_type = LINEAR; param.cache_size = 512; param.eps = cv::TermCriteria(cv::TermCriteria::MAX_ITER, 20000, 1e-06).epsilon; param.degree = 0; param.gamma = 1.0; param.coef0 = 0; param.nu = 0; param.C = 1.0; param.p = 0; param.probability = 1; // do probability estimate param.shrinking = 0; param.nr_weight = 0; param.weight = nullptr; param.weight_label = nullptr;

memset(&prob, 0, sizeof(svm_problem)); prob.l = (int)dbFaceFeatures.size();

y_space.resize(dbFaceFeatures.size()); // y_space is vector type prob.y = y_space.data(); for (size_t i = 0; i < y_space.size(); i++) y_space.at(i) = (double)(i + 1);

prob.x = (svm_node *)malloc(sizeof(svm_node )*dbFaceFeatures.size()); x_space.resize(dbFaceFeatures.size()); // x_space is vector<vector> type for(int n=0;n<(int)dbFaceFeatures.size();n++) { std::vector &line_x_space = x_space.at(n); line_x_space.resize(featuresNb + 1); prob.x[n] = line_x_space.data(); fillLibSvmNodes(dbFaceFeatures.at(n).second, line_x_space); }

const char * err_msg = svm_check_parameter(&prob, &param); if (err_msg) { std::cerr << "svm_check_parameter() fail " << err_msg << std::endl; assert(false); return; } lib_svm = svm_train(&prob, &param); assert(lib_svm); int chkRet = svm_check_probability_model(lib_svm); if(chkRet == 0) { std::cerr << "svm_check_probability_model() fail" << std::endl; assert(false); return; }

}

-- You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub [1], or mute the thread [2]. [ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/cjlin1/libsvm/issues/152?email_source=notifications\u0026email_token=ABI3BHWM3YKIZAE6BTHHJCTQLW7DTA5CNFSM4I3DKNV2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HOC2G3A", "url": "https://github.com/cjlin1/libsvm/issues/152?email_source=notifications\u0026email_token=ABI3BHWM3YKIZAE6BTHHJCTQLW7DTA5CNFSM4I3DKNV2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HOC2G3A", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

Links:

[1] https://github.com/cjlin1/libsvm/issues/152?email_source=notifications&amp;email_token=ABI3BHWM3YKIZAE6BTHHJCTQLW7DTA5CNFSM4I3DKNV2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HOC2G3A [2] https://github.com/notifications/unsubscribe-auth/ABI3BHUFUBG3VCMXEIEHU2DQLW7DTANCNFSM4I3DKNVQ

Originally posted by @cjlin1 in https://github.com/cjlin1/libsvm/issues/152#issuecomment-535949617