int _svm_struct_learn (int argc, char* argv[]) { char trainfile[200]; /* file with training examples */ char modelfile[200]; /* file for resulting classifier */ SAMPLE sample; /* training sample */ LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type; HIDEO_ENV *hideo_env=create_env(); svm_struct_learn_api_init(argc,argv); svm_struct_main_read_input_parameters(argc,argv,trainfile,modelfile,&verbosity, &struct_verbosity,&struct_parm,&learn_parm, &kernel_parm,&alg_type); if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } /* read the training examples */ sample=read_struct_examples(trainfile,&struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,hideo_env); else if(alg_type == 2) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,PRIMAL_ALG,hideo_env); else if(alg_type == 3) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_ALG,hideo_env); else if(alg_type == 4) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_CACHE_ALG,hideo_env); else exit(1); /* Warning: The model contains references to the original data 'docs'. If you want to free the original data, and only keep the model, you have to make a deep copy of 'model'. */ if(struct_verbosity>=1) { printf("Writing learned model...");fflush(stdout); } write_struct_model(modelfile,&structmodel,&struct_parm); if(struct_verbosity>=1) { printf("done\n");fflush(stdout); } free_struct_sample(sample); free_struct_model(structmodel); svm_struct_learn_api_exit(); return 0; }
int _svm_struct_learn (int argc, char* argv[]) { SAMPLE sample; /* training sample */ LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type; svm_struct_learn_api_init(argc,argv); _svm_struct_learn_read_input_parameters(argc,argv,trainfile,modelfile,&verbosity, &struct_verbosity,&struct_parm,&learn_parm, &kernel_parm,&alg_type); if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } /* read the training examples */ sample=read_struct_examples(trainfile,&struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ if(alg_type == 0) svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_ALG); else if(alg_type == 1) svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_SHRINK_ALG); else if(alg_type == 2) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_CACHE_ALG); else if(alg_type == 9) svm_learn_struct_joint_custom(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel); else exit(1); /* Warning: The model contains references to the original data 'docs'. If you want to free the original data, and only keep the model, you have to make a deep copy of 'model'. */ if(struct_verbosity>=1) { printf("Writing learned model...");fflush(stdout); } write_struct_model(modelfile,&structmodel,&struct_parm); if(struct_verbosity>=1) { printf("done\n");fflush(stdout); } free_struct_sample(sample); free_struct_model(structmodel); svm_struct_learn_api_exit(); return 0; }
int main (int argc, char* argv[]) { SAMPLE sample; /* training sample */ LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type; /* Allow the API to perform whatever initialization is required. */ api_initialize(argv[0]); read_input_parameters(argc,argv,trainfile,modelfile,&verbosity, &struct_verbosity,&struct_parm,&learn_parm, &kernel_parm,&alg_type); if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } /* read the training examples */ sample=read_struct_examples(trainfile,&struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_CACHE_ALG); else exit(1); /* Warning: The model contains references to the original data 'docs'. If you want to free the original data, and only keep the model, you have to make a deep copy of 'model'. */ if(struct_verbosity>=1) { printf("Writing learned model...");fflush(stdout); } write_struct_model(modelfile,&structmodel,&struct_parm); if(struct_verbosity>=1) { printf("done\n");fflush(stdout); } free_struct_sample(sample); free_struct_model(structmodel); /* Allow the API to perform whatever cleanup is required. */ api_finalize(); return 0; }
int main (int argc, char* argv[]) { SAMPLE sample; /* training sample */ LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type; svm_struct_learn_api_init(argc,argv); read_input_parameters(argc,argv,trainfile,modelfile,&verbosity, &struct_verbosity,&struct_parm,&learn_parm, &kernel_parm,&alg_type); if(struct_verbosity>=1) { //verbose = true; printf("Reading training examples..."); fflush(stdout); } /* read the training examples */ sample=read_struct_examples(trainfile,&struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } string config_tmp; bool update_loss_function = false; if(Config::Instance()->getParameter("update_loss_function", config_tmp)) { update_loss_function = config_tmp.c_str()[0] == '1'; } printf("[Main] update_loss_function=%d\n", (int)update_loss_function); if(!update_loss_function) { printf("update_loss_function should be true\n"); exit(-1); } eUpdateType updateType = UPDATE_NODE_EDGE; if(Config::Instance()->getParameter("update_type", config_tmp)) { updateType = (eUpdateType)atoi(config_tmp.c_str()); } printf("[Main] update_type=%d\n", (int)updateType); mkdir(loss_dir, 0777); // check if parameter vector files exist const char* parameter_vector_dir = "parameter_vector"; int idx = 0; string parameter_vector_dir_last = findLastFile(parameter_vector_dir, "", &idx); string parameter_vector_file_pattern = parameter_vector_dir_last + "/iteration_"; int idx_1 = 1; string parameter_vector_file_last = findLastFile(parameter_vector_file_pattern, ".txt", &idx_1); printf("[Main] Checking parameter vector file %s\n", parameter_vector_file_last.c_str()); // vectors used to store RF weights vector<double>* alphas = new vector<double>[sample.n]; vector<double>* alphas_edge = 0; if(updateType == UPDATE_NODE_EDGE) { alphas_edge = new vector<double>[sample.n]; } int alphas_idx = 0; if(fileExists("alphas.txt") && fileExists(parameter_vector_file_last)) { // Loading alpha coefficients ifstream ifs("alphas.txt"); string line; int lineIdx = 0; while(lineIdx < sample.n && getline(ifs, line)) { vector<string> tokens; splitString(line, tokens); for(vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it) { alphas[lineIdx].push_back(atoi(it->c_str())); } ++lineIdx; } ifs.close(); if(lineIdx > 0) { alphas_idx = alphas[0].size(); } // Loading parameters printf("[Main] Found parameter vector file %s\n", parameter_vector_file_last.c_str()); struct_parm.ssvm_iteration = idx + 1; update_output_dir(struct_parm.ssvm_iteration); //EnergyParam param(parameter_vector_file_last.c_str()); //updateCoeffs(sample, param, struct_parm, updateType, alphas, alphas_idx); //alphas_idx = 1; } else { struct_parm.ssvm_iteration = 0; // insert alpha coefficients for first iteration for(int i = 0; i < sample.n; ++i) { alphas[i].push_back(1); } ofstream ofs("alphas.txt", ios::app); int i = 0; for(; i < sample.n - 1; ++i) { ofs << alphas[i][alphas_idx] << " "; } if(i < sample.n) { ofs << alphas[i][alphas_idx]; } ofs << endl; ofs.close(); // edges for(int i = 0; i < sample.n; ++i) { alphas_edge[i].push_back(1); } ofstream ofse("alphas_edge.txt", ios::app); i = 0; for(; i < sample.n - 1; ++i) { ofse << alphas_edge[i][alphas_idx] << " "; } if(i < sample.n) { ofse << alphas_edge[i][alphas_idx]; } ofse << endl; ofse.close(); ++alphas_idx; } const int nMaxIterations = 5; bool bIterate = true; do { printf("-----------------------------------------SSVM-ITERATION-%d-START\n", struct_parm.ssvm_iteration); struct_parm.iterationId = 1; /* Do the learning and return structmodel. */ if(alg_type == 0) svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_ALG); else if(alg_type == 1) svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_SHRINK_ALG); else if(alg_type == 2) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_CACHE_ALG); else if(alg_type == 9) svm_learn_struct_joint_custom(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel); else exit(1); char _modelfile[BUFFER_SIZE]; //sprintf(_modelfile, "%s_%d", modelfile, struct_parm.ssvm_iteration); sprintf(_modelfile, "%s_%d", modelfile, struct_parm.ssvm_iteration); printf("[Main] Writing learned model to %s\n", _modelfile); write_struct_model(_modelfile, &structmodel, &struct_parm); // Run inference on training data and increase loss for misclassified points printf("[Main] Loading learned model to %s\n", _modelfile); EnergyParam param(_modelfile); updateCoeffs(sample, param, struct_parm, updateType, alphas, alphas_edge, struct_parm.ssvm_iteration + 1); ofstream ofs("alphas.txt", ios::app); int i = 0; for(; i < sample.n - 1; ++i) { ofs << alphas[i][alphas_idx] << " "; } if(i < sample.n) { ofs << alphas[i][alphas_idx]; } ofs << endl; ofs.close(); ofstream ofse("alphas_edge.txt", ios::app); i = 0; for(; i < sample.n - 1; ++i) { ofse << alphas_edge[i][alphas_idx] << " "; } if(i < sample.n) { ofse << alphas_edge[i][alphas_idx]; } ofse << endl; ofse.close(); ++alphas_idx; printf("-----------------------------------------SSVM-ITERATION-%d-END\n", struct_parm.ssvm_iteration); ++struct_parm.ssvm_iteration; bIterate = (nMaxIterations == -1 || struct_parm.ssvm_iteration < nMaxIterations); } while(bIterate); // Output final segmentation for all examples long nExamples = sample.n; int nRFs = struct_parm.ssvm_iteration; double* lossPerLabel = 0; labelType* groundTruthLabels = 0; for(int i = 0; i < nExamples; i++) { /*** example loop ***/ Slice_P* slice = sample.examples[i].x.slice; Feature* feature = sample.examples[i].x.feature; //map<sidType, nodeCoeffType>* nodeCoeffs = sample.examples[i].x.nodeCoeffs; //map<sidType, edgeCoeffType>* edgeCoeffs = sample.examples[i].x.edgeCoeffs; map<sidType, nodeCoeffType>* nodeCoeffs = 0; map<sidType, edgeCoeffType>* edgeCoeffs = 0; int nNodes = slice->getNbSupernodes(); stringstream soutPb; soutPb << loss_dir; soutPb << "pb_"; soutPb << getNameFromPathWithoutExtension(slice->getName()); soutPb << "_"; soutPb << "combined"; //soutPb << setw(5) << setfill('0') << ssvm_iteration; soutPb << ".tif"; printf("[Main] Exporting %s\n", soutPb.str().c_str()); labelType* inferredLabels = computeCombinedLabels(slice, feature, groundTruthLabels, lossPerLabel, nRFs, alphas, i, nodeCoeffs, edgeCoeffs, soutPb.str().c_str()); stringstream sout; sout << loss_dir; sout << getNameFromPathWithoutExtension(slice->getName()); sout << "_"; sout << "combined"; //sout << setw(5) << setfill('0') << ssvm_iteration; sout << ".tif"; printf("[Main] Exporting %s\n", sout.str().c_str()); slice->exportOverlay(sout.str().c_str(), inferredLabels); stringstream soutBW; soutBW << loss_dir; soutBW << getNameFromPathWithoutExtension(slice->getName()); soutBW << "_"; soutBW << "combined"; //soutBW << setw(5) << setfill('0') << ssvm_iteration; soutBW << "_BW.tif"; printf("[Main] Exporting %s\n", soutBW.str().c_str()); slice->exportSupernodeLabels(soutBW.str().c_str(), struct_parm.nClasses, inferredLabels, nNodes, &(struct_parm.labelToClassIdx)); delete[] inferredLabels; } free_struct_sample(sample); free_struct_model(structmodel); svm_struct_learn_api_exit(); return 0; }
int main (int argc, char* argv[]) { int o, i, j, cad_num; char filename[256]; CAD **cads, *cad; SAMPLE sample, sample_part; /* training sample */ LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type; int rank; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* select GPU */ select_gpu(rank); svm_struct_learn_api_init(argc,argv); read_input_parameters(argc, argv, trainfile, cadfile, testfile, &verbosity, &struct_verbosity, &struct_parm, &learn_parm, &kernel_parm, &alg_type); /* read cad models */ cads = read_cad_model(cadfile, &cad_num, 0, &struct_parm); /* if cad_num == 1, train the final model */ if(cad_num == 1) { printf("Train the hierarchical model\n"); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; printf("Read training samples\n"); sample = read_struct_examples(trainfile, &struct_parm, &structmodel); printf("Read training samples done\n"); if(struct_parm.is_root == 1 || struct_parm.is_aspectlet == 1) { /* first train weights for root parts */ printf("Train root models\n"); cad = cads[0]; struct_parm.cad_index = 0; /* for each part */ for(i = 0; i < cad->part_num; i++) { /* choose what parts to train */ if((cad->roots[i] == -1 && struct_parm.is_root == 1) || (cad->roots[i] == 1 && struct_parm.is_aspectlet == 1)) { printf("Train part %d\n", i); /* training iteration index */ struct_parm.iter = 0; struct_parm.part_index = i; /* select training samples for part */ if(rank == 0) select_examples_part(trainfile, sample, cad, 0, i); MPI_Barrier(MPI_COMM_WORLD); sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel); /* train the root template */ struct_parm.deep = 0; /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); /* if aspectlet, train the subtree */ if(cad->roots[i] == 1 && struct_parm.is_aspectlet == 1) { printf("Train subtree for part %d\n", i); struct_parm.deep = 1; struct_parm.iter++; /* select training samples for part */ if(rank == 0) select_examples_part(trainfile, sample, cad, 0, i); MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample_part); sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel); free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); } /* data mining hard examples */ for(j = 0; j < struct_parm.hard_negative; j++) { /* increase iteration number */ struct_parm.iter++; data_mining_hard_examples("temp_part.dat", testfile, &struct_parm, &structmodel); /* read the training examples */ if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } free_struct_sample(sample_part); sample_part = read_struct_examples("temp.dat", &struct_parm, &structmodel); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); } /* save constraints for training the full model */ if(rank == 0) save_constraints(&struct_parm, &structmodel); MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample_part); free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; } } /* end for each part */ if(rank == 0) { write_constraints(struct_parm.cset, &struct_parm); free(struct_parm.cset.rhs); for(i = 0; i < struct_parm.cset.m; i++) free_example(struct_parm.cset.lhs[i], 1); free(struct_parm.cset.lhs); } MPI_Barrier(MPI_COMM_WORLD); } /* end if is_root == 1 */ /* train the full model */ printf("Train the full model\n"); struct_parm.iter = 0; struct_parm.cad_index = -1; struct_parm.part_index = -1; struct_parm.deep = 1; /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); /* data mining hard examples */ while(struct_parm.hard_negative > 0) { /* increase iteration number */ struct_parm.iter++; data_mining_hard_examples(trainfile, testfile, &struct_parm, &structmodel); /* read the training examples */ if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } free_struct_sample(sample); sample = read_struct_examples("temp.dat", &struct_parm, &structmodel); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; if(alg_type == 1) svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); struct_parm.hard_negative--; } if(rank == 0) { if(struct_verbosity >= 1) { printf("Writing learned model..."); fflush(stdout); } sprintf(modelfile, "%s.mod", struct_parm.cls); write_struct_model(modelfile, &structmodel, &struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } } MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample); free_struct_model(structmodel); } /* train weights for aspectlets */ /* start with the second aspectlet, the first one is the whole object */ for(o = 1; o < cad_num; o++) { printf("Train aspectlet %d\n", o); cad = cads[o]; /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = &cad; /* training iteration index */ struct_parm.iter = 0; struct_parm.cad_index = -1; struct_parm.part_index = -1; struct_parm.deep = 1; /* select training samples for the aspectlet */ if(rank == 0) select_examples_aspectlet(trainfile, cads, o); MPI_Barrier(MPI_COMM_WORLD); printf("Read training samples\n"); sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel); printf("Read training samples done\n"); /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); /* data mining hard examples */ for(i = 0; i < struct_parm.hard_negative; i++) { /* increase iteration number */ struct_parm.iter++; data_mining_hard_examples("temp_part.dat", testfile, &struct_parm, &structmodel); /* read the training examples */ if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } free_struct_sample(sample_part); sample_part = read_struct_examples("temp.dat", &struct_parm, &structmodel); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = &cad; if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); } if(rank == 0) { if(struct_verbosity>=1) { printf("Writing learned model..."); fflush(stdout); } sprintf(modelfile, "%s_cad%03d.mod", struct_parm.cls, o); write_struct_model(modelfile, &structmodel, &struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } } MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample_part); free_struct_model(structmodel); } /* end for each cad model */ for(i = 0; i < cad_num; i++) destroy_cad(cads[i]); free(cads); svm_struct_learn_api_exit(); MPI_Finalize(); return 0; }
void MedSTC::svmStructSolver(char *dataFileName, Params *param, double *res) { LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type = 2; /* set the parameters. */ set_init_param(&struct_parm, &learn_parm, &kernel_parm, &alg_type); struct_parm.C = m_dC; /* read the training examples */ SAMPLE sample = read_struct_examples(dataFileName, &struct_parm); if(param->SVM_ALGTYPE == 0) svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, NSLACK_ALG); //else if(alg_type == 1) // svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, NSLACK_SHRINK_ALG); else if(param->SVM_ALGTYPE == 2) { struct_parm.C = m_dC * sample.n; // Note: in n-slack formulation, C is not divided by N. svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, ONESLACK_PRIMAL_ALG); } else if ( param->SVM_ALGTYPE == 3 ) { struct_parm.C = m_dC * sample.n; // Note: in n-slack formulation, C is not divided by N. int nEtaNum = m_nLabelNum * m_nK; for ( int i=1; i<=nEtaNum; i++ ) { initEta_[i] = m_dEta[i-1]; } svm_learn_struct_joint( sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, ONESLACK_PRIMAL_ALG, initEta_, nEtaNum ); } /* get the optimal lagrangian multipliers. * Note: for 1-slack formulation: the "marginalization" is * needed for fast computation. */ int nVar = sample.n * m_nLabelNum; for ( int k=0; k<nVar; k++ ) m_dMu[k] = 0; if ( param->SVM_ALGTYPE == 0 ) { for ( int k=1; k<structmodel.svm_model->sv_num; k++ ) { int docnum = structmodel.svm_model->supvec[k]->orgDocNum; m_dMu[docnum] = structmodel.svm_model->alpha[k]; } } else if ( param->SVM_ALGTYPE == 2 ) { for ( int k=1; k<structmodel.svm_model->sv_num; k++ ) { int *vecLabel = structmodel.svm_model->supvec[k]->lvec; double dval = structmodel.svm_model->alpha[k] / sample.n; for ( int d=0; d<sample.n; d++ ) { int label = vecLabel[d]; m_dMu[d*m_nLabelNum + label] += dval; } } } else ; //FILE *fileptr = fopen("SVMLightSolution.txt", "a"); // set the SVM parameters. m_dB = structmodel.svm_model->b; for ( int y=0; y<m_nLabelNum; y++ ) { for ( int k=0; k<m_nK; k++ ){ int etaIx = y * m_nK + k; m_dEta[etaIx] = structmodel.w[etaIx+1]; } } m_dsvm_primalobj = structmodel.primalobj; // free the memory free_struct_sample(sample); free_struct_model(structmodel); }
void mexFunction (int nout, mxArray ** out, int nin, mxArray const ** in) { SAMPLE sample; /* training sample */ LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type; enum {IN_ARGS=0, IN_SPARM} ; enum {OUT_W=0} ; /* SVM-light is not fully reentrant, so we need to run this patch first */ init_qp_solver() ; verbosity = 0 ; kernel_cache_statistic = 0 ; if (nin != 2) { mexErrMsgTxt("Two arguments required") ; } /* Parse ARGS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ char arg [1024 + 1] ; int argc ; char ** argv ; if (! uIsString(in[IN_ARGS], -1)) { mexErrMsgTxt("ARGS must be a string") ; } mxGetString(in[IN_ARGS], arg, sizeof(arg) / sizeof(char)) ; arg_split (arg, &argc, &argv) ; svm_struct_learn_api_init(argc+1, argv-1) ; read_input_parameters (argc+1,argv-1, &verbosity, &struct_verbosity, &struct_parm, &learn_parm, &kernel_parm, &alg_type ) ; if (kernel_parm.kernel_type != LINEAR && kernel_parm.kernel_type != CUSTOM) { mexErrMsgTxt ("Only LINEAR or CUSTOM kerneles are supported") ; } /* Parse SPARM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ mxArray const * sparm_array = in [IN_SPARM] ; mxArray const * patterns_array ; mxArray const * labels_array ; mxArray const * kernelFn_array ; int numExamples, ei ; if (! sparm_array) { mexErrMsgTxt("SPARM must be a structure") ; } struct_parm.mex = sparm_array ; patterns_array = mxGetField(sparm_array, 0, "patterns") ; if (! patterns_array || ! mxIsCell(patterns_array)) { mexErrMsgTxt("SPARM.PATTERNS must be a cell array") ; } numExamples = mxGetNumberOfElements(patterns_array) ; labels_array = mxGetField(sparm_array, 0, "labels") ; if (! labels_array || ! mxIsCell(labels_array) || ! mxGetNumberOfElements(labels_array) == numExamples) { mexErrMsgTxt("SPARM.LABELS must be a cell array " "with the same number of elements of " "SPARM.PATTERNS") ; } sample.n = numExamples ; sample.examples = (EXAMPLE *) my_malloc (sizeof(EXAMPLE) * numExamples) ; for (ei = 0 ; ei < numExamples ; ++ ei) { sample.examples[ei].x.mex = mxGetCell(patterns_array, ei) ; sample.examples[ei].y.mex = mxGetCell(labels_array, ei) ; sample.examples[ei].y.isOwner = 0 ; } if (struct_verbosity >= 1) { mexPrintf("There are %d training examples\n", numExamples) ; } kernelFn_array = mxGetField(sparm_array, 0, "kernelFn") ; if (! kernelFn_array && kernel_parm.kernel_type == CUSTOM) { mexErrMsgTxt("SPARM.KERNELFN must be define for CUSTOM kernels") ; } if (kernelFn_array) { MexKernelInfo * info ; if (mxGetClassID(kernelFn_array) != mxFUNCTION_CLASS) { mexErrMsgTxt("SPARM.KERNELFN must be a valid function handle") ; } info = (MexKernelInfo*) kernel_parm.custom ; info -> structParm = sparm_array ; info -> kernelFn = kernelFn_array ; } /* Learning ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ switch (alg_type) { case 0: svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_ALG) ; break ; case 1: svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_SHRINK_ALG); break ; case 2: svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_PRIMAL_ALG); break ; case 3: svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_ALG); break ; case 4: svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_CACHE_ALG); break ; case 9: svm_learn_struct_joint_custom(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel); break ; default: mexErrMsgTxt("Unknown algorithm type") ; } /* Write output ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* Warning: The model contains references to the original data 'docs'. If you want to free the original data, and only keep the model, you have to make a deep copy of 'model'. */ mxArray * model_array = newMxArrayEncapsulatingSmodel (&structmodel) ; out[OUT_W] = mxDuplicateArray (model_array) ; destroyMxArrayEncapsulatingSmodel (model_array) ; free_struct_sample (sample) ; free_struct_model (structmodel) ; svm_struct_learn_api_exit () ; free_qp_solver () ; }
int svm_struct_learn (int argc, char* argv[], SAMPLE *sample, STRUCTMODEL *model, STRUCT_LEARN_PARM *struct_parm) { char trainfile[200]; /* file with training examples */ char modelfile[200]; /* file for resulting classifier */ STRUCTMODEL structmodel; LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; int alg_type; //SAMPLE _sample; //int i, j; HIDEO_ENV *hideo_env=create_env(); svm_struct_learn_api_init(argc,argv); svm_struct_read_input_parameters(argc,argv,trainfile,modelfile,&verbosity, &struct_verbosity,struct_parm,&learn_parm, &kernel_parm,&alg_type); // _sample=read_struct_examples("C:\\NewWork\\SvmLightLib\\SvmLightLibDemo\\Examples\\Multiclass\\train.dat",struct_parm); // // compare _sample to *sample // assert(_sample.n == sample->n); // for (i = 0; i < _sample.n; i++) // { // SVECTOR *vec_1 = _sample.examples[i].x.doc->fvec; // SVECTOR *vec_2 = sample->examples[i].x.doc->fvec; // assert(vec_1->factor == vec_2->factor); // assert(vec_1->kernel_id == vec_2->kernel_id); // assert(vec_1->next == vec_2->next); // assert(vec_1->twonorm_sq == vec_2->twonorm_sq); // assert(strcmp(vec_1->userdefined, vec_2->userdefined) == 0); // j = 0; // while (vec_1->words[j].wnum > 0 || vec_2->words[j].wnum > 0) // { // //printf("%d %d\n", vec_1->words[j].wnum, vec_2->words[j].wnum); // //printf("%f %f\n", vec_1->words[j].weight, vec_2->words[j].weight); // assert(vec_1->words[j].wnum == vec_2->words[j].wnum); // assert(vec_1->words[j].weight == vec_2->words[j].weight); // j++; // } // } // //sample=&_sample; /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,hideo_env); else if(alg_type == 2) svm_learn_struct_joint(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,PRIMAL_ALG,hideo_env); else if(alg_type == 3) svm_learn_struct_joint(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_ALG,hideo_env); else if(alg_type == 4) svm_learn_struct_joint(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_CACHE_ALG,hideo_env); else exit(1); /* Warning: The model contains references to the original data 'docs'. If you want to free the original data, and only keep the model, you have to make a deep copy of 'model'. */ model->sizePsi=structmodel.sizePsi; model->svm_model=copy_model(structmodel.svm_model); model->w=model->svm_model->lin_weights; free_struct_model(structmodel); svm_struct_learn_api_exit(); return 0; }