bool SvmClassifier::Save(const string &model_name) const { // Save the normalization parameters and penalty coefficient vector<float> c(1, c_); if (!SaveMat(model_name + "_normA", normA_, c)) { return false; } if (!SaveMat(model_name + "_normB", normB_)) { return false; } // Save the SVM model if (svm_model_ == NULL) { return false; } if (svm_save_model((model_name + FILE_EXT).c_str(), svm_model_) != 0) { return false; } return true; }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"Error: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(); } else { model = svm_train(&prob,¶m); svm_save_model(model_file_name,model); svm_destroy_model(model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); return 0; }
void svm_train_and_test(const std::vector<feature_t> &feats, const std::vector<int> &labels, const char * model_file, std::ofstream &ofs) { svm_model * model; if(_access(model_file, 0) == -1) { auto param = svm_fill_parameter(); auto prob = svm_fill_problem(feats, labels); model = svm_train(prob, param); svm_save_model(model_file, model); auto acc = svm_test_acc(prob, model); svm_destroy_param(param); svm_free_problem(prob); std::cout<<model_file<<" acc: "<<acc*100<<std::endl; ofs<<model_file<<" acc: "<<acc*100<<std::endl; } else { model = svm_load_model(model_file); auto acc = svm_test_acc(feats, labels, model); std::cout<<model_file<<" acc: "<<acc*100<<std::endl; ofs<<model_file<<" acc: "<<acc*100<<std::endl; } //free svm_free_and_destroy_model(&model); }
static void cmd_train(const char *modelfile, double cParam, double gParam, WindowFile &trainA, WindowFile &trainB) { SVMProblem problem(trainA, trainB); SVMParam param(cParam, gParam, true); svm_model *model = svm_train(&problem, ¶m); svm_save_model(modelfile, model); svm_free_and_destroy_model(&model); }
int jpcnn_save_predictor(const char* filename, void* predictorHandle) { SPredictorInfo* predictorInfo = (SPredictorInfo*)(predictorHandle); struct svm_model* model = predictorInfo->model; const int saveResult = svm_save_model(filename, model); if (saveResult != 0) { fprintf(stderr, "Couldn't save libsvm model file to '%s'\n", filename); return 0; } return 1; }
double svm_train_and_test(const std::vector<feature_t> &train_feats, const std::vector<int> &train_labels, const std::vector<feature_t> &test_feats, const std::vector<int> &test_labels, const char * model_file) { svm_model * model; auto param = svm_fill_parameter(); auto prob = svm_fill_problem(train_feats, train_labels); model = svm_train(prob, param); svm_save_model(model_file, model); auto acc = svm_test_acc(test_feats, test_labels, model); svm_destroy_param(param); svm_free_problem(prob); //free svm_free_and_destroy_model(&model); return acc; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { struct svm_model *model; char *filename; const char *error_msg; int status; /* check input */ if(nrhs != 2) { mexPrintf("Usage: svm_savemodel(model, 'filename');\n"); fake_answer(plhs); return; } if(!mxIsStruct(prhs[0])) { mexPrintf("model file should be a struct array\n"); fake_answer(plhs); return; } if(!mxIsChar(prhs[1]) || mxGetM(prhs[1])!=1) { mexPrintf("filename should be given as char(s)\n"); fake_answer(plhs); return; } /* convert MATLAB struct to C struct */ model = matlab_matrix_to_model(prhs[0], &error_msg); if(model == NULL) { mexPrintf("Error: can't read model: %s\n", error_msg); fake_answer(plhs); return; } /* get filename */ filename = mxArrayToString(prhs[1]); /* save model to file */ status = svm_save_model(filename,model); if (status != 0) { mexWarnMsgTxt("Error occured while writing to file."); } /* destroy model */ svm_free_and_destroy_model(&model); mxFree(filename); /* return status value (0: success, -1: failure) */ plhs[0] = mxCreateDoubleScalar(status); return; }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); #ifdef CUSTOM_SOLVER if (param.svm_type == ONE_CLASS) { param.strong_footlier_indexes = filter_strong_footliers(input_file_name); } #endif error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"Error: %s\n",error_msg); exit(1); } if(cross_validation) { if(param.svm_type == R2 || param.svm_type == R2q) fprintf(stderr, "\"R^2\" cannot do cross validation.\n"); else do_cross_validation(); } else { model = svm_train(&prob,¶m); if(param.svm_type == R2 || param.svm_type == R2q) fprintf(stderr, "\"R^2\" does not generate model.\n"); else if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
static VALUE cModel_save(VALUE obj, VALUE filename) { const struct svm_model *model; const char *path; int rc; Data_Get_Struct(obj, struct svm_model, model); path = StringValueCStr(filename); if(rc = svm_save_model(path, model)) { rb_raise(rb_eStandardError, "Error on saving model, code: %i", rc); } return Qnil; }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { if (nr_fold <= 10) { do_cross_validation(); } else { double cv; nr_fold = nr_fold - 10; cv = binary_class_cross_validation(&prob, ¶m, nr_fold); printf("Cross Validation = %g%%\n",100.0*cv); } } else { model = svm_train(&prob,¶m); if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
bool CmySvmArth::Train( char* path) { const char *error_msg; error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); free(prob.y); free(prob.x); free(x_space); free(line); line = NULL; x_space = NULL; return false; } if(cross_validation) { do_cross_validation(); } else { model = svm_train(&prob,¶m); if(path!=NULL&&svm_save_model(path,model)) { fprintf(stderr, "can't save model to file %s\n", path); return false; } if(path!=NULL) { svm_free_and_destroy_model(&model); } } svm_destroy_param(¶m); free(prob.y); free(prob.x); if(path!=NULL) { free(x_space); free(line); line = NULL; x_space = NULL; } return true; }
int SVMTrainModel::train(double &RecRate, std::vector<int> &ConfusionTable) { if((!have_input_file_name) || (!have_model_file_name)) { fprintf(stderr,"ERROR: Set Input and Model files first!\n"); exit(1); } const char *error_msg; readProblem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(RecRate,ConfusionTable); } else { model = svm_train(&prob,¶m); if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(); } else { model = svm_train(&prob,¶m); if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); #ifdef _DENSE_REP for (int i = 0; i < prob.l; ++i) free((prob.x+i)->values); #else free(x_space); #endif free(prob.x); free(line); return 0; }
int savemodel(const char *filename, const mxArray *matlab_struct) { const char *error_msg; struct svm_model* model; int result; model = matlab_matrix_to_model(matlab_struct, &error_msg); if (model == NULL) { mexPrintf("Error: can't read model: %s\n", error_msg); } result = svm_save_model(filename, model); if( result != 0 ) { mexPrintf("Error: can't write model to file!\n"); } return result; }
void SVM::train(char* pInputSampleFileName, char* OutputModelFilename, double &dRetTrainError, double &dRetCrossValError) { struct svm_parameter strSvmParameters; struct svm_problem strSvmProblem; struct svm_model *pstrSvmModel; const char *error_msg; double dCrossValError = -1; double dTrainError = -1; //set parameters this->setParameters(strSvmParameters); //read sample file this->read_problem(pInputSampleFileName, strSvmProblem, strSvmParameters); //check parameters error_msg = svm_check_parameter(&strSvmProblem, &strSvmParameters); //train model pstrSvmModel = svm_train(&strSvmProblem, &strSvmParameters); //do cross validation check dCrossValError = this->crossValidationSamples(strSvmProblem, strSvmParameters, 5); //save trained model svm_save_model(OutputModelFilename, pstrSvmModel); //test trained model with training set -> train error cout << "test model " << OutputModelFilename << " with the training set " << pInputSampleFileName << std::endl; this->test(pInputSampleFileName, OutputModelFilename, dTrainError); //clean up svm_destroy_model(pstrSvmModel); svm_destroy_param(&strSvmParameters); free(strSvmProblem.y); free(strSvmProblem.x); dRetTrainError = dTrainError; dRetCrossValError = dCrossValError; }
bool Problem::CProblem::trainAndSaveModel(const FilePath& path, const Paramter& param) const { if (!m_hasData) { return false; } svm_model* model = svm_train(&m_problem, ¶m); const FilePath parentFilePath = FileSystem::ParentPath(path); if (!FileSystem::Exists(parentFilePath) && !FileSystem::CreateDirectories(parentFilePath)) { return false; } const int32 result = svm_save_model(path.narrow().c_str(), model); svm_free_and_destroy_model(&model); return (result == 0); }
int svmtrain(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { LOGD("ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(); } else { modelt = svm_train(&prob,¶m); if(svm_save_model(model_file_name,modelt)) { LOGD("can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&modelt); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
bool CmySvmArth::SaveTrainModel(const char* pPath) { if(pPath==NULL||model==NULL) return false; if(svm_save_model(pPath,model)) { fprintf(stderr, "can't save model to file %s\n", pPath); return false; } svm_free_and_destroy_model(&model); if(x_space!=NULL) { free(x_space); x_space = NULL; } if(line!=NULL) { free(line); line = NULL; } return true; }
void svmwrite (double *v, int *r, int *c, int *rowindex, int *colindex, double *coefs, double *rho, int *compprob, double *probA, double *probB, int *nclasses, int *totnSV, int *labels, int *nSV, int *sparsemodel, int *svm_type, int *kernel_type, int *degree, double *gamma, double *coef0, char **filename) { struct svm_model m; int i; char *fname = *filename; /* set up model */ m.l = *totnSV; m.nr_class = *nclasses; m.sv_coef = (double **) malloc (m.nr_class * sizeof(double*)); for (i = 0; i < m.nr_class - 1; i++) { m.sv_coef[i] = (double *) malloc (m.l * sizeof (double)); memcpy (m.sv_coef[i], coefs + i*m.l, m.l * sizeof (double)); } if (*sparsemodel > 0) m.SV = transsparse(v, *r, rowindex, colindex); else m.SV = sparsify(v, *r, *c); m.rho = rho; m.label = labels; m.nSV = nSV; if (*compprob) { m.probA = probA; m.probB = probB; } else { m.probA = NULL; m.probB = NULL; } /* set up parameter */ m.param.svm_type = *svm_type; m.param.kernel_type = *kernel_type; m.param.degree = *degree; m.param.gamma = *gamma; m.param.coef0 = *coef0; m.free_sv = 1; /* write svm model */ svm_save_model(fname, &m); for (i = 0; i < m.nr_class - 1; i++) free(m.sv_coef[i]); free(m.sv_coef); for (i = 0; i < *r; i++) free (m.SV[i]); free (m.SV); }
int main(int argc, char**argv) { if (argc < 3) { printf("\nUSAGE: %s <ASCII model file> <binary model file>\n",argv[0]); printf(" Convert an ASCII LibSVM model file into a binary LibSVM model file.\n\n"); printf("USAGE: %s - <binary model file> <ASCII model file>\n",argv[0]); printf(" Convert a binary LibSVM model file into an ASCII LibSVM model file.\n\n"); return -1; } if (argv[1][0] == '-') { // bin -> ASCII if (argc < 4) { printf("USAGE: %s - <binary model file> <ASCII model file>\n",argv[0]); printf(" Convert a binary LibSVM model file into an ASCII LibSVM model file.\n\n"); return -1; } printf("Loading binary model '%s'... ",argv[2]); svm_model * m = svm_load_binary_model(argv[2]); if (m==NULL) { printf("\nERROR: failed loading model '%s'!\n",argv[2]); return -2; } printf ("OK\n"); printf("Saving ASCII model '%s'... ",argv[3]); int r = svm_save_model(argv[3],m); if (!r) printf ("OK\n"); else { printf("ERROR: failed saving ASCII model '%s' (code=%i)\n",argv[3],r); return -3; } svm_destroy_model(m); } else { // ASCII -> bin printf("Loading ASCII model '%s'... ",argv[1]); svm_model * m = svm_load_model(argv[1]); if (m==NULL) { printf("\nERROR: failed loading model '%s'!\n",argv[1]); return -2; } printf ("OK\n"); printf("Saving binary model '%s'... ",argv[2]); int r = svm_save_binary_model(argv[2],m); if (!r) printf ("OK\n"); else { printf("ERROR: failed saving binary model '%s' (code=%i)\n",argv[2],r); return -3; } svm_destroy_model(m); } return 0; }
void SVM::save_model(string model_name) { svm_save_model(model_name.c_str(), model); }
Transformation SVMTrain::analyze(const DataSet* dataset) const { G_INFO("Doing SVMTrain analysis..."); QStringList sdescs = selectDescriptors(dataset->layout(), StringType, _descriptorNames, _exclude, false); if (!sdescs.isEmpty()) { throw GaiaException("SVMTrain: if you want to use string descriptors for training your SVM, " "you first need to enumerate them using the 'enumerate' transform on them. " "String descriptors: ", sdescs.join(", ")); } QStringList descs = selectDescriptors(dataset->layout(), UndefinedType, _descriptorNames, _exclude); // sort descriptors in the order in which they are taken inside the libsvm dataset sort(descs.begin(), descs.end(), DescCompare(dataset->layout())); Region region = dataset->layout().descriptorLocation(descs); QStringList classMapping = svm::createClassMapping(dataset, _className); // first, convert the training data into an SVM problem structure // NB: all checks about fixed-length and type of descriptors are done in this function struct svm_problem prob = svm::dataSetToLibsvmProblem(dataset, _className, region, classMapping); // also get dimension (this trick works because a vector in there is the number // of dimensions + 1 for the sentinel, and we're not in a sparse representation) int dimension = prob.x[1] - prob.x[0] - 1; // default values struct svm_parameter param; //param.svm_type = C_SVC; //param.kernel_type = RBF; //param.degree = 3; //param.gamma = 0; // 1/k param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; //param.C = 1; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; //param.probability = 0; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; // get parameters QString svmType = _params.value("type", "C-SVC").toString().toLower(); param.svm_type = _svmTypeMap.value(svmType); QString kernelType = _params.value("kernel", "RBF").toString().toLower(); param.kernel_type = _kernelTypeMap.value(kernelType); param.degree = _params.value("degree", 3).toInt(); param.C = _params.value("c", 1).toDouble(); param.gamma = _params.value("gamma", 1.0/dimension).toDouble(); param.probability = _params.value("probability", false).toBool() ? 1 : 0; const char* error_msg = svm_check_parameter(&prob, ¶m); if (error_msg) { throw GaiaException(error_msg); } // do it! struct svm_model* model; const bool crossValidation = false; if (crossValidation) { int nr_fold = 10; int total_correct = 0; double total_error = 0; double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; double* target = new double[prob.l]; svm_cross_validation(&prob, ¶m, nr_fold, target); if (param.svm_type == EPSILON_SVR || param.svm_type == NU_SVR) { for (int i=0; i<prob.l; i++) { double y = prob.y[i]; double v = target[i]; total_error += (v-y)*(v-y); sumv += v; sumy += y; sumvv += v*v; sumyy += y*y; sumvy += v*y; } G_INFO("Cross Validation Mean squared error =" << total_error/prob.l); G_INFO("Cross Validation Squared correlation coefficient =" << ((prob.l*sumvy - sumv*sumy) * (prob.l*sumvy - sumv*sumy)) / ((prob.l*sumvv - sumv*sumv) * (prob.l*sumyy - sumy*sumy)) ); } else { for (int i=0; i<prob.l; i++) if (target[i] == prob.y[i]) ++total_correct; G_INFO("Cross Validation Accuracy =" << 100.0*total_correct/prob.l << "%"); } } else { // !crossValidation model = svm_train(&prob, ¶m); } // save model to a temporary file (only method available from libsvm...), // reload it and put it into a gaia2::Parameter QTemporaryFile modelFile; modelFile.open(); QString modelFilename = modelFile.fileName(); modelFile.close(); if (svm_save_model(modelFilename.toAscii().constData(), model) == -1) { throw GaiaException("SVMTrain: error while saving SVM model to temp file"); } modelFile.open(); QByteArray modelData = modelFile.readAll(); modelFile.close(); // if we asked for the model to be output specifically, also do it if (_params.value("modelFilename", "").toString() != "") { QString filename = _params.value("modelFilename").toString(); svm_save_model(filename.toAscii().constData(), model); } // destroy the model allocated by libsvm svm_destroy_model(model); Transformation result(dataset->layout()); result.analyzerName = "svmtrain"; result.analyzerParams = _params; result.applierName = "svmpredict"; result.params.insert("modelData", modelData); result.params.insert("className", _params.value("className")); result.params.insert("descriptorNames", descs); result.params.insert("classMapping", classMapping); result.params.insert("probability", (param.probability == 1 && (param.svm_type == C_SVC || param.svm_type == NU_SVC))); return result; }
int main(int argc, char *argv[]) { float labels[4] = {1.0, 1.0, -1.0, -1.0}; cv::Mat labelsMat(4, 1, CV_32FC1, labels); float trainingData[4][2] = {{501, 10}, {255, 10}, {501, 255}, {10, 501}}; cv::Mat trainingDataMat(4, 2, CV_32FC1, trainingData); svm_parameter param; param.svm_type = C_SVC; param.kernel_type = LINEAR; param.degree = 3; param.gamma = 0; param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; param.C = 1; param.eps = 1e-6; param.p = 0.1; param.shrinking = 1; param.probability = 1; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; svm_problem svm_prob_vector = libSVMWrapper( trainingDataMat, labelsMat, param); struct svm_model *model = new svm_model; if (svm_check_parameter(&svm_prob_vector, ¶m)) { std::cout << "ERROR" << std::endl; } else { model = svm_train(&svm_prob_vector, ¶m); } bool is_compute_probability = true; std::string model_file_name = "svm"; bool save_model = true; if (save_model) { try { svm_save_model(model_file_name.c_str(), model); std::cout << "Model file Saved Successfully..." << std::endl; } catch(std::exception& e) { std::cout << e.what() << std::endl; } } bool is_probability_model = svm_check_probability_model(model); int svm_type = svm_get_svm_type(model); int nr_class = svm_get_nr_class(model); // number of classes double *prob_estimates = new double[nr_class]; cv::Vec3b green(0, 255, 0); cv::Vec3b blue(255, 0, 0); int width = 512, height = 512; cv::Mat image = cv::Mat::zeros(height, width, CV_8UC3); for (int i = 0; i < image.rows; ++i) { for (int j = 0; j < image.cols; ++j) { cv::Mat sampleMat = (cv::Mat_<float>(1, 2) << j, i); int dims = sampleMat.cols; svm_node* test_pt = new svm_node[dims]; for (int k = 0; k < dims; k++) { test_pt[k].index = k + 1; test_pt[k].value = static_cast<double>(sampleMat.at<float>(0, k)); } test_pt[dims].index = -1; float response = 0.0f; if (is_probability_model && is_compute_probability) { response = svm_predict_probability(model, test_pt, prob_estimates); } else { response = svm_predict(model, test_pt); } /* std::cout << "Predict: " << prob << std::endl; for (int y = 0; y < nr_class; y++) { std::cout << prob_estimates[y] << " "; }std::cout << std::endl; */ if (prob_estimates[0] > 0.5 || response == 1) { image.at<cv::Vec3b>(i, j) = green; } else if (prob_estimates[1] >= 0.5 || response == -1) { image.at<cv::Vec3b>(i, j) = blue; } } } cv::imshow("image", image); cv::waitKey(0); return 0; }
int svmTrain (double gamma, std::vector<std::vector<svm_node> > data, std::vector<int> labels) { struct svm_model* model; struct svm_problem prob; struct svm_node* x_space; struct svm_parameter param; // Make sure we have data if (data.empty ()) { CORE_ERROR ("No training data\n"); return (-1); } prob.l = data.size (); prob.y = Malloc (double, prob.l); prob.x = Malloc (struct svm_node* , prob.l); x_space = Malloc (struct svm_node, data.size () * data[0].size ()); int j = 0; for (int i = 0; i < prob.l; ++i) { prob.y[i] = *(labels.begin () + i); prob.x[i] = &x_space[j]; for (std::vector<svm_node>::iterator it = data[i].begin (); it != data[i].end (); ++it) { x_space[j].index = it->index; x_space[j].value = it->value; ++j; } } param.svm_type = C_SVC; param.kernel_type = RBF; param.gamma = gamma; param.degree = 3; param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; param.C = 1; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; // Check whether the parameters are within a feasible range of the problem const char* error_msg = svm_check_parameter (&prob, ¶m); if (error_msg) { CORE_ERROR ("Error checking SVM parameters: %s\n", error_msg); return (-1); } model = svm_train (&prob, ¶m); if (svm_save_model ("model.txt", model)) { CORE_ERROR ("Save SVM model failed\n"); return (-1); } svm_free_and_destroy_model (&model); svm_destroy_param (¶m); free (prob.y); free (prob.x); free (x_space); return (0); }
void LibsvmSvm::save(std::string file) { svm_save_model(file.c_str(), _model); Util::zip(file); }
int Solver_Core::Solve(int num_basis, double cvm_eps) { timeusedForModelSaving = 0; modelsWritten = 0; algorithmStartTime = pnow(); nextSaveTime = param->saveFactor * (modelsWritten+1) + floor (pow (param->saveExponential, 1)); saveExponential = param->saveExponential; this->maxNumBasis = num_basis; // The convergence of CVM does not require the exact MEB on the core-set. // With the recent advance of core-set approximation, the (1+epsilon/2)-MEB approximation on the core-set // can guarantee the quality and the convergence of the (1+epsilon)-MEB approximation for the whole data set. // See [Kumar, Mitchell, and Yildirim, 2003] // // iterate on epsilons double epsilonFactor = EPS_SCALING; for(double currentEpsilon = INITIAL_EPS; currentEpsilon/epsilonFactor > cvm_eps; currentEpsilon *= epsilonFactor) { // check epsilon currentEpsilon = (currentEpsilon < cvm_eps ? cvm_eps : currentEpsilon); // solve problem with current epsilon (warm start from the previous solution) double maxDistance2 = 0.0; int maxDistance2Idx = 0; double factor = 1.0 + currentEpsilon; factor *= factor; // The convergence of CVM does not require the exact most violating point. // A violating point is enough for the convergence of CVM. // Probabilistic speedup lead to a good tradeoff between convergence and complexity at each iteration // while (maxDistance2Idx != -1) { // get a probabilistic sample maxDistance2 = r2 * factor; maxDistance2Idx = -1; for(int sampleIter = 0; (sampleIter < 7) && (maxDistance2Idx == -1); sampleIter++) maxDistance2 = _maxDistFromSampling(maxDistance2, maxDistance2Idx); // check maximal distance if (maxDistance2Idx != -1) { _UpdateCoreSet(maxDistance2Idx); #ifndef RELEASE_VER printf("#%d eps: %g |c|: %.10f R: %.10f |c-x|: %.10f r: %.10f\n",coreNum, currentEpsilon, coreNorm2, r2, maxDistance2, sqrt(maxDistance2/r2)-1.0); #endif solver->Solve(coreIdx,coreNum,tempD); outAlpha = solver->getAlpha(); ComputeRadius2(); // if (coreNum%20 < 1) info("."); } double currentTime = pnow (); if ((param->saveExponential >= 0.0) && (currentTime - algorithmStartTime >= nextSaveTime + timeusedForModelSaving )) { modelsWritten++; printf( "&%d..", modelsWritten); nextSaveTime = param->saveFactor * (modelsWritten+1) + pow (param->saveExponential, modelsWritten + 1); // std::cout << "Algorithm time is now " << currentTime - algorithmStartTime - timeusedForModelSaving << "\n"; // std::cout << " Saving model now at " << currentTime - startTime << "\n"; double before = pnow(); svm_model *model = Malloc(svm_model,1); model->param = *param; model->free_sv = 0; // XXX model->nr_class = param->nr_classes; model->label = Malloc(int,model->nr_class); for(int i=0;i<model->nr_class;i++) model->label[i] = param->label[i]; model->rho = Malloc(double,1); double *alpha = Malloc(double,param->prob->l); double THRESHOLD = 1e-5/coreNum; for(int i=0;i<1;i++) model->rho[i] = -ComputeSolution(alpha, THRESHOLD); //overwrite alphas! model->nSV = Malloc(int, model->nr_class); // assume all the nonzeros are false at the beginning, // we have binary classification, they will not change in the // outer loop in svm.cpp int total_sv = 0; for(int i=0;i<model->nr_class;i++) { int nSV = 0; for(int j=0;j<param->count[i];j++) { if(fabs(alpha[param->start[i]+j])) { ++nSV; ++total_sv; } } model->nSV[i] = nSV; } model->l = total_sv; //anzahl der sv im model // ASSUME PERMUTATION IS IDENTITY svm_node **x = Malloc(svm_node *,param->prob->l); int i; for(int i=0;i<param->prob->l;i++) x[i] = param->prob->x[i]; // x[i] = param->prob->x[perm[i]]; model->SV = Malloc(svm_node *,total_sv); int p = 0; for(int i=0;i<param->prob->l;i++) { if(fabs(alpha[i]) > 0.0) { model->SV[p++] = x[i]; } } model->sv_coef = Malloc(double *,model->nr_class-1); for(int i=0;i<model->nr_class-1;i++) { model->sv_coef[i] = Malloc(double,total_sv); } // printf ("%d SV class 0, %d SV class 1\n", model->nSV[0], model->nSV[1]); int q = 0; for(int i=0;i<model->nr_class;i++) { for(int j=0;j < param->count[i];j++) { if(fabs(alpha[param->start[i]+j]) > 0.0) { model->sv_coef[0][q++] = alpha[param->start[i]+j];// * param->prob->y[param->start[i]+j]; // printf ("%d -- %f\n", q, alpha[param->start[i]+j]); } } } p = 0; char tmp[1000]; if (strlen(param->modelPath) < 2) { sprintf(tmp,"%s_%d",param->modelFile, (int)floor( before - algorithmStartTime - timeusedForModelSaving) ); } else { sprintf(tmp,"%s/%d.cvm.model",param->modelPath, (int)floor( before - algorithmStartTime - timeusedForModelSaving) ) ; } model->probB = NULL; model->probA = NULL; svm_save_model(tmp, model); // just to keep compability FILE* fModel = fopen(tmp, "a+t"); // append mode fprintf(fModel, "CPU Time = %f second\n", before - algorithmStartTime - timeusedForModelSaving); fclose(fModel); // svm_destroy_model(model); //free(x); double after = pnow(); timeusedForModelSaving += after - before; } // check if we are over the walltime if (pnow() - param->startTime > param->wallTime + timeusedForModelSaving) { printf ("Hit the walltime, exiting now..\n"); return coreNum; } if (IsExitOnMaxIter()) { return coreNum; break; } } }
//train the svm using the parameters defined inside this method void ML2::trainData() { //file to store the svm model structure string model_file_name1 = flowstatistics_train_name+"_model.csv"; const char *model_file_name = model_file_name1.c_str(); //file to read the data from char input_file_name[1024] = "velocityArray.csv"; //char input_file_name2[1024] = "data/flowstatistics_train_mu.csv"; const char *error_msg; //parameters of the svm /* "-s svm_type : set type of SVM (default 0)\n" " 0 -- C-SVC (multi-class classification)\n" " 1 -- nu-SVC (multi-class classification)\n" " 2 -- one-class SVM\n" " 3 -- epsilon-SVR (regression)\n" " 4 -- nu-SVR (regression)\n" "-t kernel_type : set type of kernel function (default 2)\n" " 0 -- linear: u'*v\n" " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" " 4 -- precomputed kernel (kernel values in training_set_file)\n" "-d degree : set degree in kernel function (default 3)\n" "-g gamma : set gamma in kernel function (default 1/num_features)\n" "-r coef0 : set coef0 in kernel function (default 0)\n" "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" "-m cachesize : set cache memory size in MB (default 100)\n" "-e epsilon : set tolerance of termination criterion (default 0.001)\n" "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" "-v n: n-fold cross validation mode\n" "-q : quiet mode (no outputs)\n" */ //set the parameters to be used for svm param.svm_type = 4; param.kernel_type = 1;//RBF; param.degree = 2; param.gamma = 0.125; // 1/num_features param.coef0 = 0; param.nu = 0.4; param.cache_size = 100; param.C = 0.125; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; //param.v = 10; nr_fold =10; //read from the data file read_problem_data( input_file_name, flowcol); //checking the parameters, if they are set correctly error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { cout<<"ERROR: "<<error_msg<<endl<<flush; exit(1); } //do_cross_validation(); // first do grid search do find optimal parameters //paramSelection(); // then do training with optimal parameters //param.gamma = best_gamma; //param.C = best_C; cout<< "start training\n"<<endl<<flush; model = svm_train(&prob,¶m); cout<< "end training\n"<<endl<<flush; // then do cross fold validation cout<< "start with cross validation" <<endl<<flush; do_cross_validation(); cout<< "end cross validation" <<endl<<flush; //save model if(svm_save_model(model_file_name,model)) { cout<< "can't save model to file "<< model_file_name <<endl<<flush; exit(1); } //free all the pointers used, except the model which is required for prediction // svm_destroy_param(¶m); // free(prob.y); // free(prob.x); // free(x_space); // free(line); return; }
int main(int argc, char **argv) { #ifdef WIN32 // Send all reports to STDOUT _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT ); _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT ); _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT ); // enable the options SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF ); SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF ); #endif printf("int %d, short int %d, char %d, double %d, float %d, node %d\n",sizeof(int),sizeof(short int), sizeof(char), sizeof(double), sizeof(float), sizeof(svm_node)); char input_file_name[FILENAME_LEN]; char model_file_name[FILENAME_LEN]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); param.modelFile = model_file_name; printf ("Finish reading input files!\n"); error_msg = svm_check_parameter(&prob,¶m); #ifdef WIN32 assert(_CrtCheckMemory()); #endif if(error_msg) { fprintf(stderr,"Error: %s\n",error_msg); exit(1); } double duration; double start = getRunTime(); if(cross_validation) { do_cross_validation(); } else { printf("kernel: %d\n",param.kernel_type); model = svm_train(&prob,¶m); double finish = getRunTime(); duration = (double)(finish - start); #ifdef WIN32 assert(_CrtCheckMemory()); #endif svm_save_model(model_file_name,model); svm_destroy_model(model); } printf("CPU Time = %f second\n", duration); FILE* fModel = fopen(model_file_name, "a+t"); // append mode fprintf(fModel, "CPU Time = %f second\n", duration); fclose(fModel); svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); #ifdef WIN32 assert(_CrtCheckMemory()); #endif return 0; }
/* @return -> cross validation error * -1 -> could not open statistic_filename * */ int SVM::crossValidationParameters(char* pSampleFilename, char* pFixedTestFilename, char* pStatisticsFilename) { struct svm_parameter strSvmParameters; struct svm_problem strSvmProblem; struct svm_model* pstrSvmModel; const char *error_msg; double dCrossValError = -1; double dFixTestSetError = -1; double dTrainError = -1; ofstream outputStatisticFile; char *pTempModelFilename = "tempSvmModel.tmp"; double dGamma = -1; double dC = -1; char tempText[1000]; //open statistic file outputStatisticFile.open(pStatisticsFilename, std::ios::trunc); if(!outputStatisticFile.is_open()) { cout << "can not open statistic file " << pStatisticsFilename << std::endl; return -1; } //initial parameters this->setParameters(strSvmParameters); this->read_problem(pSampleFilename, strSvmProblem, strSvmParameters); error_msg = svm_check_parameter(&strSvmProblem, &strSvmParameters); outputStatisticFile << "\% " << pSampleFilename << std::endl; outputStatisticFile << "\% Gamma \t C \t TrainError \t CrossValError \t FixedTestError" << std::endl; cout << "\% Gamma \t C \t TrainError \t CrossValError \t FixedTestError" << std::endl; for(int iExponentGamma = -25; iExponentGamma <= -2; ++iExponentGamma) { dGamma = pow(2.0, iExponentGamma); strSvmParameters.gamma = dGamma; for(int iExponentC = -1; iExponentC <= 15; ++iExponentC) { dC = pow(2.0, iExponentC); strSvmParameters.C = dC; //train pstrSvmModel = svm_train(&strSvmProblem, &strSvmParameters); svm_save_model(pTempModelFilename, pstrSvmModel); svm_destroy_model(pstrSvmModel); //test with train-set this->test(pSampleFilename, pTempModelFilename, dTrainError); //crossval dCrossValError = this->crossValidationSamples(strSvmProblem, strSvmParameters, 5); //test with fixed test-set this->test(pFixedTestFilename, pTempModelFilename, dFixTestSetError); remove(pTempModelFilename); sprintf(tempText, "%.15lf\t%10lf\t%10lf\t%10lf\t%10lf", dGamma, dC, dTrainError, dCrossValError, dFixTestSetError); outputStatisticFile << tempText << std::endl; cout << "\n#######################################################################" << std::endl; cout << tempText << std::endl; cout << "#######################################################################\n" << std::endl; } } //clean up outputStatisticFile.close(); svm_destroy_param(&strSvmParameters); free(strSvmProblem.y); free(strSvmProblem.x); //free(pTempModelFilename); return 0; }
double im_svm_train(IMbdd& bdd, const std::vector<std::string>& trainingPeople, MatrixC& trainMC, const std::vector<std::string>& testingPeople, MatrixC& testMC){ std::string path2bdd(bdd.getFolder()); int k = bdd.getK(); // Computing Bag Of Words for each people std::cout << "Computing BOWs..." << std::endl; std::map<std::string, struct svm_problem> peopleBOW; std::map<std::string, int> activitiesLabel; std::cout << "Computing the SVM problem (ie. BOW) of all the people..." << std::endl; im_compute_bdd_bow(bdd,peopleBOW); // all the BOW are saved in peopleBOW // Normalization: do not forget to change the normalization before this step //bdd.changeNormalizationSettings(normalization, // "means.txt", //"stand_devia.txt"); for(std::vector<std::string>::const_iterator trainingPerson = trainingPeople.begin(); trainingPerson != trainingPeople.end(); ++ trainingPerson){ for(int i=0 ; i<peopleBOW[*trainingPerson].l ; i++) std::cout << peopleBOW[*trainingPerson].y[i] << std::endl; } std::cout << "Normalizing the BOW..." << std::endl; bdd.changeNormalizationSettings("simple", "means.txt", "stand_devia.txt"); im_normalize_bdd_bow(bdd,trainingPeople,peopleBOW); std::cout << "Not exporting the problem..." << std::endl; // Export ?? OR NOT export ???????? THAT IS THE QUESTION // Exporting problem //exportProblem(svmTrainProblem, path2bdd + "/concatenate.bow.train"); //if(testingPeople != {""}) //exportProblem(svmTestProblem, path2bdd + "/concatenate.bow.test"); // * SVM * // (One Versus the Rest (OVR)) // SVM parameters struct svm_parameter svmParameter; get_svm_parameter(k,svmParameter); std::cout << "Searching the best C and the best Gamma..." << std::endl; // We have to do a leave one out with the training data // For the moment we use the same training centers to find Gamma and C int minC = -5, maxC = 5; int minG = -10, maxG = 3; double crossValidationAccuracy = im_training_leave_one_out(bdd, trainingPeople, peopleBOW, minC, maxC, // ln(min/max C) minG, maxG, // ln(min/max G) svmParameter); // N.B: when we export the model to the robot, peopleBOW index = trainingPeople // Generating the SVM model std::cout << "Generating the SVM model..." << std::endl; struct svm_problem trainingProblem; trainingProblem.l = 0; trainingProblem.x = NULL; trainingProblem.y = NULL; for(std::vector<std::string>::const_iterator trainingPerson = trainingPeople.begin(); trainingPerson != trainingPeople.end(); ++ trainingPerson){ for(int i=0 ; i < peopleBOW.at(*trainingPerson).l ; i++){ struct svm_node* bow = peopleBOW.at(*trainingPerson).x[i]; addBOW(bow, peopleBOW.at(*trainingPerson).y[i],trainingProblem); } } struct svm_model** svmModels = svm_train_ovr(&trainingProblem,&svmParameter); // Exporting models std::cout << "Saving the SVM model..." << std::endl; std::vector <std::string> modelFiles; int nrActivities = bdd.getActivities().size(); for(int i=0 ; i< nrActivities ; i++){ std::string fileToSaveModel = path2bdd; std::stringstream ss; ss << i; fileToSaveModel = fileToSaveModel + "/svm_ovr_" + ss.str() + ".model"; svm_save_model(fileToSaveModel.c_str(),svmModels[i]); modelFiles.push_back(fileToSaveModel); } bdd.changeSVMSettings(nrActivities, modelFiles); // Calculate the confusion matrix and the probability estimation std::cout << "Filling the training confusion matrix..." << std::endl; im_fill_confusion_matrix(bdd,trainingProblem,svmModels, trainMC); destroy_svm_problem(trainingProblem); if(testingPeople.size() > 0){ std::cerr << "Entering in Hell..." << std::endl; struct svm_problem testingProblem; trainingProblem.l = 0; trainingProblem.x = NULL; trainingProblem.y = NULL; for(std::vector<std::string>::const_iterator testingPerson = testingPeople.begin(); testingPerson != testingPeople.end(); ++ testingPerson){ for(int i=0 ; i < peopleBOW.at(*testingPerson).l ; i++){ std::cerr << "HELlllll" << std::endl; struct svm_node* bow = peopleBOW.at(*testingPerson).x[i]; std::cerr << peopleBOW.at(*testingPerson).y[i] << std::endl; std::cerr << "YOOOO man" << std::endl; addBOW(bow, peopleBOW.at(*testingPerson).y[i],testingProblem); std::cerr << "Why ?!!!" << std::endl; } } std::cout << "Filling the testing confusion matrix..." << std::endl; im_fill_confusion_matrix(bdd,testingProblem,svmModels, testMC); destroy_svm_problem(testingProblem); } std::cout << "Releasing peopleBOW" << std::endl; // Releasing peopleBOW std::vector<std::string> people = bdd.getPeople(); for(std::vector<std::string>::iterator person = people.begin(); person != people.end(); ++ person){ destroy_svm_problem(peopleBOW[*person]); } // Releasing OVR models for(int i=0;i<nrActivities;i++){ svm_free_and_destroy_model(&svmModels[i]);} delete [] svmModels; svmModels = NULL; return crossValidationAccuracy; }