示例#1
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 = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}
	if( flag_find_C && flag_warm_start)
	{
		fprintf(stderr,"ERROR: Option -C and -i can't both exist\n");
		exit(1);
	}
	if (flag_find_C)
	{
		do_find_parameter_C();
	}
	else if(flag_cross_validation)
	{
		do_cross_validation();
	}
	else
	{
		if(flag_warm_start)
		{
			if(prob.n != initial_model->nr_feature)
				fprintf(stderr,"WARNING: The number of features in the input file does not match that in the initial model\n");
			model_=warm_start_train(&prob, &param, initial_model);
			free_and_destroy_model(&initial_model);
		}
		else
			model_=train(&prob, &param);
		if(save_model(model_file_name, model_))
		{
			fprintf(stderr,"can't save model to file %s\n",model_file_name);
			exit(1);
		}
		free_and_destroy_model(&model_);
	}
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	free(line);

	return 0;
}
QPredictLinearLearner::~QPredictLinearLearner()
{
    if (m_model) {
        free_and_destroy_model(&m_model);
    }

}
示例#3
0
int main(int argc, char **argv)
{
	FILE *input, *output;
	int i;

	// parse options
	for(i=1;i<argc;i++)
	{
		if(argv[i][0] != '-') break;
		++i;
		switch(argv[i-1][1])
		{
//			case 'b':
//				flag_predict_probability = atoi(argv[i]);
//				break;
			case 'o':
				output_option = atoi(argv[i]);
				break;
			case 'q':
				info = &print_null;
				i--;
				break;
			default:
				fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]);
				exit_with_help();
				break;
		}
	}
	if(i>=argc)
		exit_with_help();

	input = fopen(argv[i],"r");
	if(input == NULL)
	{
		fprintf(stderr,"can't open input file %s\n",argv[i]);
		exit(1);
	}

	output = fopen(argv[i+2],"w");
	if(output == NULL)
	{
		fprintf(stderr,"can't open output file %s\n",argv[i+2]);
		exit(1);
	}

	if((model_=load_model(argv[i+1]))==0)
	{
		fprintf(stderr,"can't open model file %s\n",argv[i+1]);
		exit(1);
	}

	x = (struct feature_node *) malloc(max_nr_attr*sizeof(struct feature_node));
	do_predict(input, output);
	free_and_destroy_model(&model_);
	free(line);
	free(x);
	fclose(input);
	fclose(output);
	return 0;
}
// Training SVM with feature vector X and label Y. 
// Each row of X is a feature vector, with corresponding label in Y.
// Return a CV_32F weight Mat
Mat Objectness::trainSVM(CMat &X1f, const vecI &Y, int sT, double C, double bias, double eps)
{
	// Set SVM parameters
	parameter param; {
		param.solver_type = sT; // L2R_L2LOSS_SVC_DUAL;
		param.C = C;
		param.eps = eps; // see setting below
		param.p = 0.1;
		param.nr_weight = 0;
		param.weight_label = NULL;
		param.weight = NULL;
		set_print_string_function(print_null);
		CV_Assert(X1f.rows == Y.size() && X1f.type() == CV_32F);
	}

	// Initialize a problem
	feature_node *x_space = NULL;
	problem prob;{
		prob.l = X1f.rows;
		prob.bias = bias;
		prob.y = Malloc(double, prob.l);
		prob.x = Malloc(feature_node*, prob.l);
		const int DIM_FEA = X1f.cols;
		prob.n = DIM_FEA + (bias >= 0 ? 1 : 0);
		x_space = Malloc(feature_node, (prob.n + 1) * prob.l);
		int j = 0;
		for (int i = 0; i < prob.l; i++){
			prob.y[i] = Y[i];
			prob.x[i] = &x_space[j];
			const float* xData = X1f.ptr<float>(i);
			for (int k = 0; k < DIM_FEA; k++){
				x_space[j].index = k + 1;
				x_space[j++].value = xData[k];
			}
			if (bias >= 0){
				x_space[j].index = prob.n;
				x_space[j++].value = bias;
			}
			x_space[j++].index = -1;
		}
		CV_Assert(j == (prob.n + 1) * prob.l);
	}

	// Training SVM for current problem
	const char*  error_msg = check_parameter(&prob, &param);
	if(error_msg){
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}
	model *svmModel = train(&prob, &param);
	Mat wMat(1, prob.n, CV_64F, svmModel->w);
	wMat.convertTo(wMat, CV_32F);
	free_and_destroy_model(&svmModel);
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	return wMat;
}
示例#5
0
void Classifier::unload_predict_resources(){
	delete _vocabulary;
	delete _expander;
	if(_model != NULL) {
		free_and_destroy_model(&_model);
		_model = NULL;
	}
}
示例#6
0
文件: train.c 项目: Joelone/MLEA
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 = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"Error: %s\n",error_msg);
		exit(1);
	}

	if(flag_cross_validation)
	{
		if (nr_fold <= 10)
		{
			do_cross_validation();
		}
		else
		{
			double cv;
			nr_fold = nr_fold - 10;
			cv =  binary_class_cross_validation(&prob, &param, nr_fold);
			printf("Cross Validation = %g%%\n",100.0*cv);
		}
	}
	else
	{
		model_=train(&prob, &param);
		if(save_model(model_file_name, model_))
		{
			fprintf(stderr,"can't save model to file %s\n",model_file_name);
			exit(1);
		}
		free_and_destroy_model(&model_);
	}
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(prob.W);
	free(x_space);
	free(line);

	return 0;
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  struct model *linearmodel = (struct model*)malloc( 1*sizeof(struct model) );
  //struct model *linearmodel;
  char *pFileName;
  const char *pErrorMsg;
  int status;

  if( nrhs != 2 ){
    mexPrintf("mat2liblinear(model, 'output_name');\n");
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }
  
  if( !mxIsStruct(prhs[0]) ){
    mexPrintf("model is not structure array\n");
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }

  if( !mxIsChar(prhs[1]) || mxGetM(prhs[1]) != 1 ){
    mexPrintf("FileName is not char\n");
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }
  
  //convert matlab structure to c structure
  pErrorMsg = matlab_matrix_to_model(linearmodel, prhs[0]);
  if( linearmodel == NULL ){
    mexPrintf("Can't read model: %s\n", pErrorMsg);
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }
  
  //save model
  pFileName = mxArrayToString(prhs[1]);
  status = save_model(pFileName, linearmodel);
  if( status != 0 ){
    mexWarnMsgTxt("While writing to file, error occured");
  }
  
  free_and_destroy_model(&linearmodel);
  mxFree(pFileName);
  
  //return 0 or 1. 0:success, 1:failure
  plhs[0] = mxCreateDoubleScalar(status);
  return;
}
示例#8
0
文件: train.c 项目: qmmlib/QmmLibrary
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);
	param.train_file = Malloc(char,1024);
	strcpy(param.train_file, input_file_name);
	error_msg = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}

	if(flag_cross_validation)
	{
		do_cross_validation();
	}
	else
	{
		clock_t start_cpu, end_cpu;
		double cpu_time_used;
     	start_cpu = clock();
		model_=train(&prob, &param);
		end_cpu = clock();
     	cpu_time_used = ((double) (end_cpu - start_cpu)) / CLOCKS_PER_SEC;
		if(save_model(model_file_name, model_))
		{
			fprintf(stderr,"can't save model to file %s\n",model_file_name);
			exit(1);
		}
		free_and_destroy_model(&model_);
	}
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	free(line);

	return 0;
}
示例#9
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 = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}

	if (flag_find_C)
	{
		do_find_parameter_C();
	}
	else if(flag_cross_validation)
	{
		do_cross_validation();
	}
	else
	{
		model_=train(&prob, &param);
		if(save_model(model_file_name, model_))
		{
			fprintf(stderr,"can't save model to file %s\n",model_file_name);
			exit(1);
		}
		free_and_destroy_model(&model_);
	}
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	free(line);

	return 0;
}
cv::Mat_<double> cRegression::__train_regressor(const cv::Mat_<double>& label_vec, const cv::Mat_<int>& instance_mat)
{
	void(*print_func)(const char*) = &print_null;
	const char *error_msg;

	struct parameter param;
	struct problem   problem;
	struct feature_node *x_space = NULL;

	srand(1);
	// std::cout << "initialize liblinear parameter." << std::endl;
	param.solver_type = L2R_L2LOSS_SVR_DUAL;
	param.C = 1.0 / (double)label_vec.rows;
	param.eps = 0.1;
	param.p = 0;
	param.nr_weight = 0;
	param.weight_label = NULL;
	param.weight = NULL;
	// std::cout << "initialize liblinear parameter finished." << std::endl;
	set_print_string_function(print_func);

	std::vector<int>*  prob_x = NULL;
	prob_x = new std::vector<int>[label_vec.rows]; // number of samples = label_vec.rows

	size_t nzcount = 0;
	// std::cout << "copy feature." << std::endl;
	for (int i = 0; i < instance_mat.rows; ++i) {
	    for (int j = 0; j < instance_mat.cols; ++j) {
		    int elem = instance_mat(i, j);
			if (elem != 0) {
				prob_x[i].push_back(j);
				++nzcount;
			}
		}
	}
	// std::cout << "copy feature finished." << std::endl;

	//sort the vector
	for (int i = 0; i < label_vec.rows; i++){
		std::sort(prob_x[i].begin(), prob_x[i].end());
	}

	problem.l = label_vec.rows;
	problem.n = instance_mat.cols;
	problem.bias = -1;

	int elements = (int)(nzcount + problem.l);

	problem.y = Malloc(double, problem.l);
	problem.x = Malloc(struct feature_node *, problem.l);
	x_space = Malloc(struct feature_node, elements);

	int j = 0;
	for (int i = 0; i < problem.l; i++){
		problem.y[i] = label_vec(i, 0);
		problem.x[i] = &x_space[j];

		for (int k = 0; k < prob_x[i].size(); k++){
			x_space[j].index = prob_x[i][k] + 1;
			x_space[j].value = 1;
			j++;
		}
		x_space[j++].index = -1;
	}

	delete[] prob_x;

	error_msg = check_parameter(&problem, &param);
	if (error_msg){
		fprintf(stderr, "ERROR: %s\n", error_msg);
	}

	// std::cout << "train model." << std::endl;
	struct model *model = NULL;
	model = train(&problem, &param);
	// std::cout << "train model finished." << std::endl;

	cv::Mat_<double> weight = cv::Mat::zeros(model->nr_feature, 1, CV_64FC1);
	for (int i = 0; i < model->nr_feature; i++){
		weight(i, 0) = model->w[i];
		// std::cout << weight(i, 0) << " "; // std::endl;
	}


	free_and_destroy_model(&model);
	destroy_param(&param);

	free((void*)(problem.y));
	free((void*)(problem.x));
	free((void*)(x_space));
	return weight;
}
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	const char *error_msg;
	// fix random seed to have same results for each run
	// (for cross validation)
	srand(1);

	// Transform the input Matrix to libsvm format
	if(nrhs > 1 && nrhs < 5)
	{
		int err=0;

		if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
			mexPrintf("Error: label vector and instance matrix must be double\n");
			fake_answer(plhs);
			return;
		}

		if(parse_command_line(nrhs, prhs, NULL))
		{
			exit_with_help();
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}

		if(mxIsSparse(prhs[1]))
			err = read_problem_sparse(prhs[0], prhs[1]);
		else
		{
			mexPrintf("Training_instance_matrix must be sparse; "
				"use sparse(Training_instance_matrix) first\n");
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}

		// train's original code
		error_msg = check_parameter(&prob, &param);

		if(err || error_msg)
		{
			if (error_msg != NULL)
				mexPrintf("Error: %s\n", error_msg);
			destroy_param(&param);
			free(prob.y);
			free(prob.x);
			free(x_space);
			fake_answer(plhs);
			return;
		}

		if(cross_validation_flag)
		{
			double *ptr;
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			ptr = mxGetPr(plhs[0]);
			ptr[0] = do_cross_validation();
		}
		else
		{
			const char *error_msg;

			model_ = train(&prob, &param);
			error_msg = model_to_matlab_structure(plhs, model_);
			if(error_msg)
				mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
			free_and_destroy_model(&model_);
		}
		destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(x_space);
	}
	else
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
}
示例#12
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	const char *error_msg;
	// fix random seed to have same results for each run
	// (for cross validation)
	srand(1);

	// Transform the input Matrix to libsvm format
	if(nrhs > 2 && nrhs <= 6)
	{
		int err=0;

		if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]) ) {
			mexPrintf("Error: weight vector, label vector matrix must be double\n");
			fake_answer(plhs);
			return;
		}
		if(!mxIsSingle(prhs[2])) {
			mexPrintf("Error: instance matrix must be single\n");
			fake_answer(plhs);
			return;
		}

		if(parse_command_line(nrhs, prhs, NULL))
		{
			exit_with_help();
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}

#ifdef _DENSE_REP
		if(!mxIsSparse(prhs[2]))
			err = read_problem_sparse(prhs[0], prhs[1],prhs[2]);
		else
		{
			mexPrintf("Training_instance_matrix must be dense\n");
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}
#else
		if(mxIsSparse(prhs[2]))
			err = read_problem_sparse(prhs[0], prhs[1],prhs[2]);
		else
		{
			mexPrintf("Training_instance_matrix must be sparse\n");
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}
#endif

                // xren: delete the input instance matrix to free up space
                if (nrhs==6) {
                        mxArray* var=(mxArray*)prhs[5];
                        int status=mexCallMATLAB(0,NULL,1, &var, "clear");
                        if (status!=0) mexPrintf("Failed to delete variable %s\n",mxArrayToString(prhs[5]));
                        //mxDestroyArray( (mxArray*)prhs[1] );
                }

		// train's original code
		error_msg = check_parameter(&prob, &param);

		if(err || error_msg)
		{
			if (error_msg != NULL)
				mexPrintf("Error: %s\n", error_msg);
			destroy_param(&param);
			free(prob.y);
			free(prob.x);
                        if (!use_existing_space)
			free(x_space);
			fake_answer(plhs);
			return;
		}

		if(cross_validation_flag)
		{
			double *ptr;
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			ptr = mxGetPr(plhs[0]);
			ptr[0] = do_cross_validation();
		}
		else
		{
			const char *error_msg;

			model_ = train(&prob, &param);
			error_msg = model_to_matlab_structure(plhs, model_);
			if(error_msg)
				mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
			free_and_destroy_model(&model_);
		}
		destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(prob.W);
                if (!use_existing_space)
		free(x_space);
	}
	else
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
}
示例#13
0
int main(int argc, char **argv)
{
	FILE *input, *output;
	int i;
	char model_file[1024];

	// parse options
	for(i=1;i<argc;i++)
	{
		if(argv[i][0] != '-') break;
		++i;
		switch(argv[i-1][1])
		{
			case 'b':
				flag_predict_probability = atoi(argv[i]);
				break;
			case 'q':
				info = &print_null;
				i--;
				break;
			default:
				fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]);
				exit_with_help();
				break;
		}
	}
	if(i>=argc)
		exit_with_help();

	input = fopen(argv[i],"r");
	if(input == NULL)
	{
		fprintf(stderr,"can't open input file %s\n",argv[i]);
		exit(1);
	}

	output = fopen(argv[i+2],"w");
	if(output == NULL)
	{
		fprintf(stderr,"can't open output file %s\n",argv[i+2]);
		exit(1);
	}

	// if((model_=load_model(argv[i+1]))==0)
	// {
	// 	fprintf(stderr,"can't open model file %s\n",argv[i+1]);
	// 	exit(1);
	// }

	for ( int pid = 0; pid < sum_pro; pid++) {
		sprintf(model_file,"L%d%s%d%s",pid/(BLOCK * N) + 1, "_R", pid%(BLOCK * N) + 1,".model");
		printf("%s\n", model_file);
		if ((model_[pid] = load_model(model_file)) == 0) {
			fprintf(stderr,"can't open model file %s\n",argv[i+1]);
			exit(1);
		}
	}

	x = (struct feature_node *) malloc(max_nr_attr*sizeof(struct feature_node));
	do_predict(input, output);
	for ( int pid = 0; pid < sum_pro; pid++) {
		free_and_destroy_model(&model_[pid]);
	}
	free(line);
	free(x);
	fclose(input);
	fclose(output);
	return 0;
}
void *predictModelWholeGenome(void *arg) {
  thread_data_t *data = (thread_data_t *) arg;

  printf("data->trainedModel is %s\n", data->trainedModel);
  printf("data->coverageFileList is %s\n", data->coverageFileList);
  printf("data->trainFile %s\n", data->trainFile);
  printf("data->paramFile %s\n", data->paramFile);
  printf("data->chr is %d\n", data->chr);

  char *trainedModel = data->trainedModel;
  char *coverageFileList = data->coverageFileList;
  // char *trainFile = data->trainFile;
  char *paramFile = data->paramFile;
  int chr = data->chr;

  // utility var
  int i,j,k;
  
  // trainedModel
  struct model *mymodel;
  if( (mymodel = load_model(trainedModel)) == 0) {
    printf("cannot load model from file %s\n", trainedModel);
    return EXIT_SUCCESS;
  }

  // coverageFileList
  int totalCoverageFiles;
  FILE *coverageFileListFp = NULL;
  if( (coverageFileListFp = fopen(coverageFileList, "r") ) == NULL) {
    printf("Cannot open file %s\n", coverageFileList);
    return EXIT_SUCCESS;
  }
  char **coverageFiles = (char **)calloc(MAX_BAM_FILES,sizeof(char *));
  for(i = 0; i < MAX_BAM_FILES; i++) {
    coverageFiles[i] = (char *)calloc(MAX_DIR_LEN, sizeof(char));
  }
  
  i = 0;
  while (!feof(coverageFileListFp)) {
    if (i >= MAX_BAM_FILES) {
      printf("Error: the number of input coverages files exceeds the limit %d\n", i);
      return EXIT_SUCCESS;
    }
    if( ( fscanf(coverageFileListFp, "%s\n", coverageFiles[i]) ) != 1) {
      printf("Error: reading %dth from %s\n", i, coverageFileList);
      return EXIT_SUCCESS;
    }
    i++;
  }
  totalCoverageFiles = i;
  fclose(coverageFileListFp);

  // open coverage Files
  FILE *coverageFps[totalCoverageFiles];
  for(i = 0; i < totalCoverageFiles; i++) {
    if( (coverageFps[i] = fopen(coverageFiles[i], "rb")) == NULL ) {
      printf("Error opening coverage file %s\n", coverageFiles[i]);
      return EXIT_SUCCESS;
    }
  }

  // paramFile
  struct extractFeatureParam *param = (struct extractFeatureParam *)calloc(1, sizeof(struct extractFeatureParam));
  parseParam(paramFile, param);

  // predict model: by default: predict probability
  int nr_class = get_nr_class(mymodel);
  double *prob_estimates = (double *)calloc(nr_class, sizeof(double));

  // predResult for storing results
  int totalBins = 0;
  int cumBins[NUM_SEQ];
  for (i = 0; i < NUM_SEQ; i++) {
    totalBins += (int)(chrlen[i] / param->resolution) + 1;
    cumBins[i] = totalBins;
  }

  // allocate memory for result based on thread data chr
  // as we are using one thread for each chr
  float *predResult = (float *)calloc( (int)(chrlen[chr] / param->resolution) + 1, sizeof(float));

  // read in feature for each bin and do prediction
  for(j = 0; j < (int)(chrlen[chr] / param->resolution) + 1; j++) {
    if(j % 100000 == 0) {
      printf("Predicting chr%d:%dth bin\n", chr,j);
      fflush(stdout);
    }
    int max_nr_feature = 100;
    struct feature_node *myX = (struct feature_node *)calloc(max_nr_feature, sizeof(struct feature_node));
    int idx = 0;
    for(k = 0; k < totalCoverageFiles; k++) {
      float *buffer = (float *)calloc( param->windowSize/param->resolution,sizeof(float));
      int offset = j;
      offset += -(int)((float)(param->windowSize / 2) / (float)param->resolution + 0.5);
      if(offset < 0 || offset + (int)((float)(param->windowSize) / (float)param->resolution + 0.5) > (int)(chrlen[i] / param->resolution) + 1) {
        // printf("offset is %d\n", offset);
        free(buffer);
        continue;
      }
      if(chr != 0) offset += cumBins[chr-1];
      // printf("offset is %d\n", offset);
      fseek(coverageFps[k], offset*sizeof(float), SEEK_SET);
      fread(buffer, sizeof(float), param->windowSize/param->resolution, coverageFps[k]);
      int l;
      // printf("buffer[%d] is:",l);
      for(l = 0; l < param->windowSize/param->resolution; l++) {
        // if(j == 289540) printf("%f,",buffer[l]);
        if(buffer[l] != 0) {
          myX[idx].index = k*(param->windowSize/param->resolution) + l + 1;
          myX[idx].value = buffer[l];
          idx++;
        }
        if(idx >= max_nr_feature -2) { // feature_node is not long enough
          max_nr_feature *= 2;
          myX = (struct feature_node *)realloc(myX, max_nr_feature*sizeof(struct feature_node));
        }
      }
      free(buffer);
    } // end of loop through coverageFiles
    // printf("\n");
    myX[idx].index = -1; // a flag for end of features
    if(idx == 0) {
      // printf("idx is %d\n",idx);
      predResult[j] = 0.0;
      free(myX);
      continue;
    }
    // printf("nr_feature is %d\n", idx);
    predict_probability(mymodel, myX, prob_estimates);
    // printf("num of feature is %d\n", get_nr_feature(mymodel));
    // printf("num of class is %d\n", get_nr_class(mymodel));
    int *mylabel = (int *)calloc(10, sizeof(int));
    // added, in order to get the correct label
    get_labels(mymodel, mylabel);
    if(mylabel[0] == 1) {
      predResult[j] = prob_estimates[0];
    } else {
      predResult[j] = prob_estimates[1];
    }
 
    free(myX);
    free(mylabel);
  }


  for(i = 0; i < totalCoverageFiles; i++) {
    fclose(coverageFps[i]);
  }
  // free pointers
  for(i = 0; i < MAX_BAM_FILES; i++) {
    free(coverageFiles[i]);
  }
  free(coverageFiles);
  free(param);
  free(prob_estimates);
  // give address of pointer to this function, so that the function can free the pointer.
  free_and_destroy_model(&mymodel); 
  pthread_exit((void *) predResult);
}
示例#15
0
LinearSVM::~LinearSVM()
{
	if (linearmodel!=NULL)
		free_and_destroy_model(&linearmodel);
}
int main(int argc, char **argv)
{
  const char *error_msg;
  parse_command_line(argc, argv); // also load data
  error_msg = check_parameter(prob,&param);
  
  if(error_msg)
    {
      fprintf(stderr,"Error: %s\n",error_msg);
      exit(1);
    }
	

  std::vector< std::pair<double, double> > test_errors(nb_runs);
  std::vector< std::pair<double, double> > train_errors(nb_runs);
  double trn_mean=0;
  double tst_mean=0;
  double mse_trn_mean=0;
  double mse_tst_mean=0;
  int *start = NULL;

  // perform runs
  for (int run=0; run<nb_runs; run++)
    {

      if ((trnsz>=prob->l) || (trnsz<=0))
	{
	  fprintf(stderr,"\nRun %d (from 0 to %d)\n", run, prob->l-1);

	  //train
	  model_=train(prob, &param);
	  
	  // test
	  test_errors[run]=do_predict(tprob, model_);
	  train_errors[run]=do_predict(prob, model_);
	}
      else
	{
          // select all the splits before optimizing
          if(run == 0)
            {
              start = Malloc(int,nb_runs); 
              for (int run2=0; run2<nb_runs; run2++)
                start[run2] = (rand() % (prob->l-trnsz));
            }
	  // select examples
	  fprintf(stderr,"\nRun %d (from %d to %d)\n", run, start[run], start[run]+trnsz-1);
	  struct problem* subprob=extract_subprob(prob, start[run], trnsz);
	  
	  //train
	  model_=train(subprob, &param);
	  
	  // test
	  test_errors[run]=do_predict(tprob, model_);
	  train_errors[run]=do_predict(subprob, model_);
	  free(subprob->y);
	  free(subprob->x);
	}

      tst_mean+=test_errors[run].first;
      printf("Test  classification ERROR = %g\n",test_errors[run].first);
      trn_mean+=train_errors[run].first;
      printf("Train classification ERROR = %g\n",train_errors[run].first);

      mse_tst_mean+=test_errors[run].second;
      printf("Test  normalized ACCURACY (ET requirement) = %g\n",test_errors[run].second);
      mse_trn_mean+=train_errors[run].second;
      printf("Train normalized ACCURACY (ET requirement) = %g\n",train_errors[run].second);

      //destroy model
      free_and_destroy_model(&model_);
      destroy_param(&param);
      
    }
示例#17
0
int main(int argc, char **argv)
{
#ifdef GPU
    int dev = findCudaDevice(argc, (const char **) argv);
    if (dev == -1)
        return 0;

    if (cublasCreate(&handle) != CUBLAS_STATUS_SUCCESS)
    {
        fprintf(stdout, "CUBLAS initialization failed!\n");
        cudaDeviceReset();
        exit(EXIT_FAILURE);
    }
#endif // GPU

	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);
  time_t t1 = clock();
	read_problem(input_file_name);
  time_t t2 = clock();
  printf("reading the input file took %f seconds.\n", float(t2-t1)/CLOCKS_PER_SEC);
	error_msg = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}

	if(flag_cross_validation)
	{
		do_cross_validation();
	}
	else
	{
		model_=train(&prob, &param);
		if(save_model(model_file_name, model_))
		{
			fprintf(stderr,"can't save model to file %s\n",model_file_name);
			exit(1);
		}
		free_and_destroy_model(&model_);
	}
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	free(line);


#ifdef GPU
    cublasDestroy(handle);
    cudaDeviceReset();
#endif // GPU
  printf("reading the input file took %f seconds.\n", float(t2-t1)/CLOCKS_PER_SEC);

	return 0;
}
// Train an SVM classifier for the current node given the current feature
//   and evaluate the obtained classifier using information gain.
float TrainCurrentRegion(float ftrX, float ftrY, float ftrWid, float ftrHgt,
    int &ftrDim, float *ftrWeight, vector<SINGLEIM> imVec, vector<int> trainClassID,
    vector<int> trainID, vector<int> valClassID, vector<int> valID, PARAMETER param, 
    vector<int> &leftTrainID, vector<int> &rightTrainID, vector<int> &leftValID, 
    vector<int> &rightValID, vector<PMREGION> &pmRegions)
{
//clock_t t1, t2;
//t1 = clock();
  struct feature_node *x_space;
  struct parameter svm_param;
  struct problem prob;
  struct model* model_;

  // setup the problem
  pmRegions.clear();
  GetPmRegionConfig(ftrX, ftrY, ftrWid, ftrHgt, pmRegions);
  prob.l = trainID.size();  // number of training data for the current node
  // number of features for each data, note the bg feature dim is added
  int fgFtrDim = pmRegions.size() * param.siftCodebookSize;
  prob.n = fgFtrDim+1;// + imVec[0].bgFtrDim;//changing so background features are not considered!!!!!!!!!!!!!!
  prob.bias = 0.1;
  prob.y = (int*)malloc(sizeof(int) * prob.l);
  prob.x = (struct feature_node**)malloc(sizeof(struct feature_node*) * prob.l);
  x_space = (struct feature_node*)malloc(sizeof(struct feature_node) * prob.l * prob.n);

  // do the max-pooling and get the features for the images
  float *tmpFeature = (float*)malloc(sizeof(float) * prob.n);
  int x_count = 0;
  for (int i = 0; i < prob.l; i++)
  {
    // max pooling to obtain foreground features
    MaxPooling(imVec[trainID[i]], tmpFeature, pmRegions, param, "fg");
    // pad the foreground feature with background information
    //memcpy(tmpFeature + fgFtrDim, imVec[trainID[i]].bgHist,
      //  imVec[trainID[i]].bgFtrDim * sizeof(float));
    *(tmpFeature + prob.n - 1) = 0.1f;  // the bias term
    prob.y[i] = trainClassID[i];
    prob.x[i] = &x_space[x_count];

    float *featurePTR = tmpFeature;
    for (int j = 1; j <= prob.n; j++)
    {
      if (*featurePTR > 0)
      {
        x_space[x_count].index = j;
        x_space[x_count].value = *featurePTR;
        x_count++;
      }
      featurePTR++;
    }
    x_space[x_count].index = -1;
    x_count++;
  }

  // setup the training parameters
  svm_param.solver_type = L2R_L2LOSS_SVC_DUAL;
  svm_param.C = 1;
  if ((svm_param.solver_type == L2R_LR) || (svm_param.solver_type == L2R_L2LOSS_SVC))
    svm_param.eps = 0.2;
  else if ((svm_param.solver_type == L2R_L2LOSS_SVC_DUAL) || (svm_param.solver_type == MCSVM_CS)
      || (svm_param.solver_type == L2R_L1LOSS_SVC_DUAL) || (svm_param.solver_type == L2R_LR_DUAL))
    svm_param.eps = 2.0;
  else if ((svm_param.solver_type == L1R_L2LOSS_SVC) || (svm_param.solver_type == L1R_LR))
    svm_param.eps = 0.2;
  svm_param.nr_weight = 0;
  svm_param.weight_label = NULL;
  svm_param.weight = NULL;
//t2 = clock();
//mexPrintf("    Time before SVM: %f\n", (float)(t2 - t1) / CLOCKS_PER_SEC);

  // train the model
//t1 = clock();
  model_ = train(&prob, &svm_param);
//t2 = clock();
//mexPrintf("    Time for SVM: %f\n", (float)(t2 - t1) / CLOCKS_PER_SEC);

  // copy the model to return values
//t1 = clock();
  ftrDim = prob.n;
  float *ftrWeightPTR = ftrWeight;
  double *modelWeightPTR = model_->w;
  for (int i = 0; i < prob.n; i++)
  {
    *ftrWeightPTR = *modelWeightPTR;
    ftrWeightPTR++;
    modelWeightPTR++;
  }

  float leftDist[2], rightDist[2];
  memset(leftDist, 0, sizeof(float) * 2);
  memset(rightDist, 0, sizeof(float) * 2);
  EvalOnTrain(ftrWeight, prob, leftDist, rightDist, trainID, leftTrainID, rightTrainID);
//  mexPrintf("      Left: %d, %d; Right: %d, %d.    ", (int)(leftDist[0]), 
//      (int)(leftDist[1]), (int)(rightDist[0]), (int)(rightDist[1]));
  EvalOnVal(ftrWeight, ftrDim, imVec, pmRegions, param, leftDist, 
      rightDist, valID, valClassID, leftValID, rightValID);
//  mexPrintf("Left: %d, %d; Right: %d, %d.\n", (int)(leftDist[0]), 
//      (int)(leftDist[1]), (int)(rightDist[0]), (int)(rightDist[1]));
  float infoGain = ComputeInfoGain(leftDist, rightDist);

  // destroy and free the memory
  free_and_destroy_model(&model_);
  free(tmpFeature);
  destroy_param(&svm_param);
  free(prob.y);
  free(prob.x);
  free(x_space);
//t2 = clock();
//mexPrintf("    Time after SVM: %f\n", (float)(t2 - t1) / CLOCKS_PER_SEC);

  //mexPrintf("End of TrainCurrentRegion.\n");
  return infoGain;
}
示例#19
0
int main(int argc, char **argv)
{
    char input_file_name[1024];
    char model_file_name[1024];
    const char *error_msg;
    /*
     * Some bookkeeping variables for MPI. The 'rank' of a process is its numeric id
     * in the process pool. For example, if we run a program via `mpirun -np 4 foo', then
     * the process ranks are 0 through 3. Here, N and size are the total number of processes 
     * running (in this example, 4).
    */

    
    start_t = time(NULL);     
    MPI_Init(&argc, &argv);               // Initialize the MPI execution environment
    MPI_Comm_rank(MPI_COMM_WORLD, &param.rank); // Determine current running process
    MPI_Comm_size(MPI_COMM_WORLD, &param.size); // Total number of processes
    //double N = (double) size;             // Number of subsystems/slaves for ADMM
    if (param.rank==param.root)
        printf ("Number of subsystems: %d \n", param.size);
    
    parse_command_line(argc, argv, input_file_name, model_file_name);
    // Read the meta data
    bprob.read_metadata(input_file_name);
    bprob.set_bias(bias);
    error_msg = block_check_parameter(&bprob,&param);
    
    if(error_msg)
    {
        fprintf(stderr,"Error: %s\n",error_msg);
        exit(1);
    }
    
    if (param.rank==param.root)
    {    
        if (param.solver_type == L2R_L2LOSS_SVC)
            printf("ADMM + Primal trust region Newton's method for L2 loss SVM:\n");
        else if (param.solver_type == L2R_L2LOSS_SVC_DUAL)
            printf("ADMM + Dual coordinate descent for L2 loss SVM: \n");
        else if (param.solver_type ==  L2R_L1LOSS_SVC_DUAL)
            printf("ADMM + Dual coordinate descent for L1 loss SVM:\n");
        else
            printf("Not supported. \n"); 
    }
    
    srand(1);
    // Now read the local data 
    problem  * prob = read_problem(&bprob, &param);
    
    
    if(flag_cross_validation)
        do_cross_validation(prob);
    else
    {
        model_=block_train(prob, &param);   
        save_model(model_file_name, model_);  
        free_and_destroy_model(&model_);
    }
    destroy_param(&param);
    MPI_Finalize(); 
    return 0;
}
示例#20
0
//---------------------------- global variables -------------------------------
int main(int argc, char **argv)
{
	char input_file_name[1024];
	char model_file_name[1024];
	const char *error_msg;
	
#ifdef FIGURE56
	char test_file_name[1024];
	parse_command_line(argc, argv, input_file_name, test_file_name);
#else
	parse_command_line(argc, argv, input_file_name, model_file_name);//initialize global struct param, according to commond line 
	//_parse_command_line(argc, argv, input_file_name, model_file_name);//initialize global struct param, according to commond line 
#endif
	read_problem(input_file_name);//get all possible information about the train file into global struct prob
#ifdef FIGURE56
	read_problem_test(test_file_name);
#endif
	error_msg = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}
	//	struct model
//{
//	struct parameter param;
//	int nr_class;		/* number of classes */
//	int nr_feature;
//	double *w;
//	int *label;		/* label of each class */
//};
//	model_=train(&prob, &param);
//--------apply memory for V matrix--------------
	int i=0;
	double * p = Malloc(double,param.col_size * prob.l);
	//srand( (unsigned)time( NULL ) );  //种子函数
	for (i=0;i<param.col_size * prob.l;i++)
	{		
		p[i]=rand()/(RAND_MAX+1.0);  //产生随机数的函数
		//p[i]=rand();
	}
	double ** v_pp = Malloc(double* ,prob.l);
	param.v_pp = v_pp;
	
	for (i=0;i<prob.l;i++)
		param.v_pp[i] = &p[param.col_size * i];
	model_=_train(&prob, &param);

#ifdef FIGURE56
#else
	if(save_model(model_file_name, model_))
	{
		fprintf(stderr,"can't save model to file %s\n",model_file_name);
		exit(1);
	}
#endif
	free_and_destroy_model(&model_);
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(prob.query);
	free(x_space);
	////////free the variable
	free(v_pp);
	free(p);
#ifdef FIGURE56
	free(probtest.y);
	free(probtest.x);
	free(x_spacetest);
#endif
	free(line);
	return 0;
}
示例#21
0
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	int prob_estimate_flag = 0;
	struct model *model_;
	char cmd[CMD_LEN];
	col_format_flag = 0;

	if(nrhs > 5 || nrhs < 3)
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
	if(nrhs == 5)
	{
		mxGetString(prhs[4], cmd, mxGetN(prhs[4])+1);
		if(strcmp(cmd, "col") == 0)
		{			
			col_format_flag = 1;
		}
	}

	if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
		mexPrintf("Error: label vector and instance matrix must be double\n");
		fake_answer(plhs);
		return;
	}

	if(mxIsStruct(prhs[2]))
	{
		const char *error_msg;

		// parse options
		if(nrhs>=4)
		{
			int i, argc = 1;
			char *argv[CMD_LEN/2];

			// put options in argv[]
			mxGetString(prhs[3], cmd,  mxGetN(prhs[3]) + 1);
			if((argv[argc] = strtok(cmd, " ")) != NULL)
				while((argv[++argc] = strtok(NULL, " ")) != NULL)
					;

			for(i=1;i<argc;i++)
			{
				if(argv[i][0] != '-') break;
				if(++i>=argc)
				{
					exit_with_help();
					fake_answer(plhs);
					return;
				}
				switch(argv[i-1][1])
				{
					case 'b':
						prob_estimate_flag = atoi(argv[i]);
						break;
					default:
						mexPrintf("unknown option\n");
						exit_with_help();
						fake_answer(plhs);
						return;
				}
			}
		}

		model_ = Malloc(struct model, 1);
		error_msg = matlab_matrix_to_model(model_, prhs[2]);
		if(error_msg)
		{
			mexPrintf("Error: can't read model: %s\n", error_msg);
			free_and_destroy_model(&model_);
			fake_answer(plhs);
			return;
		}

		if(prob_estimate_flag)
		{
			if(!check_probability_model(model_))
			{
				mexPrintf("probability output is only supported for logistic regression\n");
				prob_estimate_flag=0;
			}
		}

		if(mxIsSparse(prhs[1]))
			do_predict(plhs, prhs, model_, prob_estimate_flag);
		else
		{
			mexPrintf("Testing_instance_matrix must be sparse; "
				"use sparse(Testing_instance_matrix) first\n");
			fake_answer(plhs);
		}

		// destroy model_
		free_and_destroy_model(&model_);
	}
	else
	{
示例#22
0
文件: eval.cpp 项目: Joelone/MLEA
double binary_class_cross_validation(const problem *prob, const parameter *param, int nr_fold)
{
	dvec_t dec_values;
	ivec_t ty;
	int *labels;

	if (nr_fold > 1)
	{
		int i;
		int *fold_start = Malloc(int,nr_fold+1);
		int l = prob->l;
		int *perm = Malloc(int,l);

		for(i=0;i<l;i++) perm[i]=i;
		for(i=0;i<l;i++)
		{
			int j = i+rand()%(l-i);
			std::swap(perm[i],perm[j]);
		}
		for(i=0;i<=nr_fold;i++)
			fold_start[i]=i*l/nr_fold;

		for(i=0;i<nr_fold;i++)
		{
			int                begin   = fold_start[i];
			int                end     = fold_start[i+1];
			int                j,k;
			struct problem subprob;

			subprob.l = l-(end-begin);
			subprob.x = Malloc(struct feature_node*,subprob.l);
			subprob.y = Malloc(int,subprob.l);

			k=0;
			for(j=0;j<begin;j++)
			{
				subprob.x[k] = prob->x[perm[j]];
				subprob.y[k] = prob->y[perm[j]];
				++k;
			}
			for(j=end;j<l;j++)
			{
				subprob.x[k] = prob->x[perm[j]];
				subprob.y[k] = prob->y[perm[j]];
				++k;
			}
			struct model *submodel = train(&subprob,param);
			//int svm_type = get_svm_type(submodel);
	
			//if(svm_type == NU_SVR || svm_type == EPSILON_SVR){
			//	fprintf(stderr, "wrong svm type");
			//	exit(1);
			//}

			labels = Malloc(int, get_nr_class(submodel));
			get_labels(submodel, labels);

			if(get_nr_class(submodel) > 2) 
			{
				fprintf(stderr,"Error: the number of class is not equal to 2\n");
				exit(-1);
			}

			dec_values.resize(end);
			ty.resize(end);

			for(j=begin;j<end;j++) {
				predict_values(submodel,prob->x[perm[j]], &dec_values[j]);
				ty[j] = (prob->y[perm[j]] > 0)? 1: -1;
			}


			if(labels[0] <= 0) {
				for(j=begin;j<end;j++)
					dec_values[j] *= -1;
			}
	
			free_and_destroy_model(&submodel);
			free(subprob.x);
			free(subprob.y);
			free(labels);
		}

		free(perm);
		free(fold_start);
	}