Exemplo n.º 1
0
mx_real_t svm_class_prob(svm_classifier_t *svm, mx_real_t *instance, int class_ind) {
    int i, svm_type=svm_get_svm_type(svm->model);
    double *prob_estimates=NULL;
    struct svm_node *x;
	mx_real_t class_prob;

    if (svm_type!=C_SVC && svm_type != NU_SVC)
	rs_error("Cannot give class probability for 1-class or regression SVM!");

    x = (struct svm_node *) rs_malloc((svm->feature_dim+1)*sizeof(struct svm_node),"Feature vector representation for svm");
    for (i=0;i<svm->feature_dim;i++) {
		x[i].index=i+1;
		x[i].value=instance[i];
    }
    x[i].index=-1;

    _scale_instance(&x,svm->feature_dim,svm->max,svm->min);
    
    prob_estimates = (double *) rs_malloc(svm->n_classes*sizeof(double),"SVM probability estimates");
    
    svm_predict_probability(svm->model,x,prob_estimates);
    
	class_prob = prob_estimates[class_ind];
	rs_free (x);
	rs_free (prob_estimates);
	return class_prob;
}
Exemplo n.º 2
0
/**
 * @brief Predict with SVM for a given vector.
 * @param type Type of feature vector: Type of svm-model (1, 2, ...)
 * @param vec Feature vector for the SVM.
 * @param prob Probability of correct prediction for each class.
 * @return Returns the prediction label
 */
bool SVMPredictorSingle::process(int type, std::vector<double> &vec, std::vector<double> &prob)
{
  int svm_type = svm_get_svm_type(model);
  int nr_class = svm_get_nr_class(model);
  double *prob_estimates = NULL;
  int j;

  if(predict_probability) {
    if (svm_type == NU_SVR || svm_type == EPSILON_SVR)
      printf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n", svm_get_svr_probability(model));
    else
      prob_estimates = (double *) malloc(nr_class*sizeof(double));
  }

  // we copy now the feature vector
  double predict_label;
  for(unsigned idx = 0; idx < vec.size(); idx++) {
    node[idx].index = idx+1;
    node[idx].value = vec[idx];
    node[idx+1].index = -1;
  }

  if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC)) {
    predict_label = svm_predict_probability(model, node, prob_estimates);
    for(j=0;j<nr_class;j++)
      prob.push_back(prob_estimates[j]);
  }
  else
    predict_label = svm_predict(model, node);

  if(predict_probability)
    free(prob_estimates);
  
  return predict_label;
}
Exemplo n.º 3
0
	int PredictProb( const std::vector<T>& x, std::map<int, double>& prob ) {
		int dimension = scale_info_.dimension();
		assert( static_cast<int>( x.size() ) == dimension );
		assert( prob.empty() );

		if( (libsvm_model_->param.svm_type == C_SVC || libsvm_model_->param.svm_type == NU_SVC)
			&& libsvm_model_->probA!=NULL && libsvm_model_->probB!=NULL ) {
			std::vector<double> scaled_x;
			scale_info_.Scale( x, scaled_x );
			struct svm_node* nodes;
			nodes = (struct svm_node *) malloc( ( dimension + 1 ) * sizeof(struct svm_node) );
			GetSVMNodes( scaled_x, nodes );
			int class_num = libsvm_model_->nr_class;
			double probs[class_num];
			double predict_label = svm_predict_probability( libsvm_model_, nodes, probs );
			std::vector<int> class_labels;
			GetClassLabels( class_labels );
			for( int i = 0; i < class_num; ++i ) {
				prob[ class_labels[i] ] = probs[i];
			}
			free(nodes);
			return static_cast<int>( predict_label );
		} else {
			return Predict( x );
		}
	}
Exemplo n.º 4
0
static void cmd_test_list(svm_model *model, WindowFile &testFile)
{
    const QString fmt("%1: prob { %2 , %3 } @ %4 ch %5\n");

    const qint32 samples = getNumSamples(testFile);
    float *buf = new float[samples];
    SVMNodeList nodelist(samples);
    double probEstim[2];

    while(testFile.nextChannel()) {
        assert(testFile.getEventSamples() == samples);
        testFile.read((char*)buf, samples*sizeof(float));
        nodelist.fill(buf);

        const char subj =
                (svm_predict_probability(model, nodelist, probEstim) > 0)
                ? 'A' : 'B';

        const double t = (testFile.getEventOffset() / BytesPerSample)/
                (double)SamplingRate;

        fputs(fmt.arg(subj)
                .arg(probEstim[0], 0, 'f', 4)
                .arg(probEstim[1], 0, 'f', 4)
                .arg(t, 0, 'f', 6)
                .arg(testFile.getChannelId())
                .toAscii(), stdout);
    }

    delete[] buf;
}
Exemplo n.º 5
0
bool SvmClassifier::Predict(const Mat &feats, vector<int> *labels,
        vector<float> *probs) const
{
    if (labels == nullptr)
    {
        return false;
    }

    int m = feats.cols;
    int n = feats.rows;
    labels->clear();
    if (probs != nullptr)
    {
        probs->clear();
    }

    // Normalize the features
    Mat feats_norm;
    Normalize(feats, &feats_norm);

    // Predict using SVM
    svm_node *x = static_cast<svm_node*>(malloc((m + 1) * sizeof(svm_node)));
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            x[j].index = j + 1;
            x[j].value = feats_norm.at<float>(i, j);
        }
        x[m].index = -1;

        if (probs == nullptr)
        {
            labels->push_back(svm_predict(svm_model_, x));
        }
        else
        {
            double *prob = static_cast<double*>(
                    malloc(svm_model_->nr_class * sizeof(double)));
            int label = svm_predict_probability(svm_model_, x, prob);
            labels->push_back(label);
            int idx = 0;
            for (int k = 0; k < svm_model_->nr_class; ++k)
            {
                if (label == svm_model_->label[k])
                {
                    idx = k;
                    break;
                }
            }
            probs->push_back(prob[idx]);
            delete prob;
        }
    }
    free(x);

    return true;
}
Exemplo n.º 6
0
float jpcnn_predict(void* predictorHandle, float* predictions, int predictionsLength) {
  SPredictorInfo* predictorInfo = (SPredictorInfo*)(predictorHandle);
  struct svm_model* model = predictorInfo->model;
  struct svm_node* nodes = create_node_list(predictions, predictionsLength);
  double probabilityEstimates[2];
  svm_predict_probability(model, nodes, probabilityEstimates);
  const double predictionValue = probabilityEstimates[0];
  destroy_node_list(nodes);
  return predictionValue;
}
Exemplo n.º 7
0
int gcm::patchRun(vector<SLR_ST_Skeleton> vSkeletonData, vector<Mat> vDepthData, vector<IplImage*> vColorData, 
				  int *rankIndex, double *rankScore)
{
	int kernelFeatureDim = NClass*NTrainSample;
	//clock_t startT=clock();
	oriData2Feature(vSkeletonData, vDepthData, vColorData);
	//cout<<"=======Time========="<<clock()-startT<<endl;
 	gcmSubspace();

	x[0].index = 0;
	for (int j=0; j<kernelFeatureDim; j++)
	{
		subMatrix(subFeaAll_model, subFea1, 0, featureDim, j*subSpaceDim, subSpaceDim);
		x[j+1].value = myGcmKernel.Frobenius(subFea1, gcm_subspace, featureDim, subSpaceDim);
		x[j+1].index=j+1;
	}
	x[kernelFeatureDim+1].index=-1;

	//int testID = svm_predict_probability(myModel, x, prob_estimates);
	int testID_noPro = svm_predict(myModel, x);

	int testID = svm_predict_probability(myModel_candi, x, prob_estimates);

	//Sort and get the former 5 ranks. 
	vector<scoreAndIndex> rank;
	for (int i=0; i<myModel->nr_class; i++)
	{
		scoreAndIndex temp;
		temp.index = myModel->label[i];
		temp.score = prob_estimates[i];
		rank.push_back(temp);
	}
	sort(rank.begin(),rank.end(),comp);

	
	rankIndex[0] = testID_noPro;
	rankScore[0] = 1.0;
	int candiN = 0;
	//for (int i=1; i<5; i++)
	int seqCandiN = 1;
	while(seqCandiN<5)
	{
		if (rank[candiN].index == testID_noPro)
		{
			candiN++;
			continue;
		}
		rankIndex[seqCandiN] = rank[candiN].index;
		rankScore[seqCandiN] = rank[candiN].score;
		candiN++;
		seqCandiN++;
	}
	releaseResource();
	return rankIndex[0];
}
bool SVM::predictSVM(VectorDouble &inputVector,double &maxProbability, VectorDouble &probabilites){

		if( !trained || param.probability == 0 || inputVector.size() != numInputDimensions ) return false;

		double *prob_estimates = NULL;
		svm_node *x = NULL;

		//Setup the memory for the probability estimates
		prob_estimates = new double[ model->nr_class ];

		//Copy the input data into the SVM format
		x = new svm_node[numInputDimensions+1];
		for(UINT j=0; j<numInputDimensions; j++){
			x[j].index = (int)j+1;
			x[j].value = inputVector[j];
		}
		//The last value in the input vector must be set to -1
		x[numInputDimensions].index = -1;
		x[numInputDimensions].value = 0;

		//Scale the input data if required
		if( useScaling ){
			for(UINT j=0; j<numInputDimensions; j++)
				x[j].value = scale(x[j].value,ranges[j].minValue,ranges[j].maxValue,SVM_MIN_SCALE_RANGE,SVM_MAX_SCALE_RANGE);
		}

		//Perform the SVM prediction
		double predict_label = svm_predict_probability(model,x,prob_estimates);

		predictedClassLabel = 0;
		maxProbability = 0;
		probabilites.resize(model->nr_class);
		for(int k=0; k<model->nr_class; k++){
			if( maxProbability < prob_estimates[k] ){
				maxProbability = prob_estimates[k];
                predictedClassLabel = k+1;
                maxLikelihood = maxProbability;
            }
			probabilites[k] = prob_estimates[k];
		}

        if( !useNullRejection ) predictedClassLabel = (UINT)predict_label;
        else{
            if( maxProbability >= classificationThreshold ){
                predictedClassLabel = (UINT)predict_label;
            }else predictedClassLabel = GRT_DEFAULT_NULL_CLASS_LABEL;
        }

		//Clean up the memory
		delete[] prob_estimates;
		delete[] x;

		return true;
}
Exemplo n.º 9
0
void SVMPredict::predict_prob(const struct svm_model* m,
                              const struct svm_node* x,
                              double* label,
                              double* prob_estimate) {
  //v8::HandleScope scope(GetIsolate());
#if defined(PROCESS_DEBUG)
  log("is called");
  //CW_ASSERT(m==NULL);
  //CW_ASSERT(x==NULL);
#endif
  *label = svm_predict_probability(m,x,prob_estimate);
}
int predict(double **values, int **indices, int rowNum, int *arrayNcol, int *labels, double **prob_estimates, int isProb)
{
    int svm_type=svm_get_svm_type(model);
    int nr_class=svm_get_nr_class(model);
    int j;
    LOGD("isProb:%d", isProb);
    if(isProb)
    {
        if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
            LOGD("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model));
        else
        {
            int *labels=(int *) malloc(nr_class*sizeof(int));
            svm_get_labels(model,labels);
            //	fprintf(output,"labels");
            //	for(j=0;j<nr_class;j++)
            //		fprintf(output," %d",labels[j]);
            //	fprintf(output,"\n");
            //	free(labels);
        }
    }

    // each record will receive
    // a predicted label and a [nClass]-D probability estimate array
    for (int i = 0; i < rowNum; i++)
    {
        int nCol = arrayNcol[i];
        double target_label = 0;
        int predict_label=0;
        x = (struct svm_node *) realloc(x,(nCol+1)*sizeof(struct svm_node));

        for (int j = 0; j < nCol; j++)
        {
            x[j].index = indices[i][j];
            x[j].value = values[i][j];
        }
        x[nCol].index = -1;

        // Probability prediction
        if (isProb && (svm_type==C_SVC || svm_type==NU_SVC))
        {
            // prob_estimate[rowNum][nClass]
            labels[i] = svm_predict_probability(model,x,prob_estimates[i]);
        }
        // without probability
        else
        {
            labels[i] = svm_predict(model,x);
        }
    } // For

    return 0;
}
Exemplo n.º 11
0
void SvmClassiUser(float* feature,int &lable)
{
	//double out;	
	InitData(feature);
	int feature_dimension=5;
	double* outPro=NULL;
	//ReScaleFeatures(float* features,int feature_dimension,float* feature_min,float* feature_max);
	svm_node*  nodes=(svm_node*)malloc((feature_dimension+1)*sizeof(svm_node));
	GenerateSvmNode(feature,feature_dimension,nodes);
	lable=svm_predict_probability(svmModel_userFeel,nodes,outPro);
	//printf("%d\n",lable);
	free(nodes);
}
Exemplo n.º 12
0
double SVM::classify(struct svm_node *data, char* filename_model)
{
	double predict_label;
	double *prob_estimates = NULL;
	int svm_type;
	int nr_class;

	// load feature file only once
	if(!this->_bFeatureFileLoaded)
	{
		if((this->_svmModel = svm_load_model(filename_model))==0)
		{
			printf("can't open model file %s\n",filename_model);
			return -99;
		}

		this->_bFeatureFileLoaded = true;
	}

	svm_type = svm_get_svm_type(this->_svmModel);
	nr_class = svm_get_nr_class(this->_svmModel);

	prob_estimates = (double *) malloc(nr_class*sizeof(double));

	if (svm_type==C_SVC || svm_type==NU_SVC)
	{
		predict_label = svm_predict_probability(this->_svmModel,data,prob_estimates);

	//	printf("%g",predict_label);

		//for(int k=0; k < nr_class; k++)
			//printf(" %g",prob_estimates[k]);

		//printf("\n");
	}
	else
	{
		predict_label = svm_predict(this->_svmModel, data);

		printf("%g\n",predict_label);
	}

	free(data);
	free(prob_estimates);

	return predict_label;
}
Exemplo n.º 13
0
int predict(float **values, int **indices, int rowNum, int colNum, int *labels, double *prob_estimates, int isProb)
{
	int svm_type=svm_get_svm_type(model);
	int nr_class=svm_get_nr_class(model);
	int j;
	
	if(isProb)
	{
		if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
			LOGD("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model));
		else
		{
			int *labels=(int *) malloc(nr_class*sizeof(int));
			svm_get_labels(model,labels);
		//	fprintf(output,"labels");		
		//	for(j=0;j<nr_class;j++)
		//		fprintf(output," %d",labels[j]);
		//	fprintf(output,"\n");
		//	free(labels);
		}
	}

        for (int i = 0; i < rowNum; i++)
        {
            double target_label, predict_label=0;
            x = (struct svm_node *) realloc(x,(colNum+1)*sizeof(struct svm_node));

            for (int j = 0; j < colNum; j++)
            {
                x[j].index = indices[i][j];
                x[j].value = values[i][j];
            }
            x[colNum].index = -1;

            // Probability prediction 
            if (isProb && (svm_type==C_SVC || svm_type==NU_SVC))
            {
                    predict_label = svm_predict_probability(model,x,prob_estimates);
					labels[0]=predict_label;

            }
            else { labels[i] = svm_predict(model,x); }
	} // For

        return 0;
}
int copy_predict_proba(char *predict, struct svm_model *model, npy_intp *predict_dims,
                 char *dec_values)
{
    npy_intp i, n, m;
    struct svm_node *predict_nodes;
    n = predict_dims[0];
    m = (npy_intp) model->nr_class;
    predict_nodes = dense_to_libsvm((double *) predict, predict_dims);
    if (predict_nodes == NULL)
        return -1;
    for(i=0; i<n; ++i) {
        svm_predict_probability(model, &predict_nodes[i],
                                ((double *) dec_values) + i*m);
    }
    free(predict_nodes);
    return 0;
}
Exemplo n.º 15
0
int main(){
	auto test_data = read_data("1_3");
	svm_model* model = svm_load_model("1_3_b_model");
	int nr_class = svm_get_nr_class(model);

	for (auto svmData : test_data){
		svm_node* nodes = AssignSVMNode(svmData);
		double* probEstimates = new double[nr_class];
		svm_predict_probability(model, nodes, probEstimates);
		for (int i = 0; i < nr_class; ++i){
			std::cout<<i<<":"<<probEstimates[i]<<"  ";
		}
		std::cout<<std::endl;
	}

	getchar();

	return 0;
}
Exemplo n.º 16
0
bool CmySvmArth::Sim(double* res , int& len)
{
	if(model==NULL||res==NULL)
		return false;
	int svm_type=svm_get_svm_type(model);
	int nr_class=svm_get_nr_class(model);
	double *prob_estimates=NULL;
	len = m_nSimDataLen;
	if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
	{
		prob_estimates = new double[nr_class];
		*res = svm_predict_probability(model,m_pTestdata,prob_estimates);
		delete prob_estimates;
	}
	else
	{
		*res = svm_predict(model,m_pTestdata);
	}
	return true;
}
Exemplo n.º 17
0
void predict_strand_MEANMFE_CONSMFE(double *prob, double *decValue, double deltaMeanMFE, double deltaConsMFE, int n_seq, double id)
{
    struct svm_node node[5];
    double value=0;
    
    node[0].index = 1; node[0].value = deltaMeanMFE;
    node[1].index = 2; node[1].value = deltaConsMFE;
    node[2].index = 3; node[2].value = (double)n_seq;
    node[3].index = 4; node[3].value = id;
    node[4].index = -1;
    
    scale_strand_decision_node((struct svm_node*)&node);
    
    svm_predict_values(strand_model,node,&value);
    *decValue=value;
    svm_predict_probability(strand_model,node,&value);  // ATTENTION: If model parameters are not set correctly, then value is not set, type of SVM need to 
                                                        // to be C_SVC or nu_SVC!
    *prob=value;
    
    return;
}
Exemplo n.º 18
0
int SVMPredict2(struct svm_model* model, float *descript_vector, double *prob_est, int size)// prob_est is a pointer to array
{
	int correct = 0;
	int total = 0;
	double error = 0;
	double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;

	int svm_type = svm_get_svm_type(model);
	int nr_class = svm_get_nr_class(model);
	double *prob_estimates = NULL;
	int j;

	int i = 0;
	double target_label, predict_label;
	char *idx, *val, *label, *endptr;
	int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0

	int n = 1;
	while (n < size)
		n *= 2;
	struct svm_node* x = (struct svm_node *) malloc(n*sizeof(struct svm_node));

	for (int i = 0; i < size; i++)
	{
		x[i].index = i + 1;
		x[i].value = descript_vector[i];
	}
	x[size].index = -1;
	if (prob_est != NULL)
		predict_label = svm_predict_probability(model, x, prob_est);
	else
		predict_label = svm_predict(model, x);

	if (prob_est != NULL)
		*prob_est = -1;
	free(x);
	return predict_label;
}
double HandTranslating::predictTestImage() {

    double predict_label;

    printf("labels");
    for(int j = 0;j < nr_class;j++) {
        printf(" %d",labels[j]);
    }
    printf("\n");
    if (svm_type==C_SVC || svm_type==NU_SVC)
    {

        predict_label = svm_predict_probability(model,nodeTest,prob_estimates);
        printf("%g",predict_label);
        for(int j = 0;j < nr_class;j++)
        {
            printf(" %g",prob_estimates[j]);
        }
        printf("\n");
    }

    return predict_label;
}
Exemplo n.º 20
0
		Label PredictModel::CPredictModel::predictProbability(const Array<std::pair<int32, double>>& vector, Array<double>& probabilities) const
		{
			if (!m_model)
			{
				probabilities.clear();

				return Math::NaN;
			}

			Array<svm_node> node(vector.size() + 1);

			for (int32 i = 0; i < static_cast<int32>(vector.size()); ++i)
			{
				node[i].index = vector[i].first;

				node[i].value = vector[i].second;
			}

			node.back().index = -1;

			probabilities.resize(m_model->nr_class, 3);

			return svm_predict_probability(m_model, node.data(), probabilities.data());
		}
Exemplo n.º 21
0
int cLibsvmLiveSink::myTick(long long t)
{
  if (model == NULL) return 0;

  SMILE_DBG(4,"tick # %i, classifiy value vector using LibSVM (lag=%i):",t,lag);
  cVector *vec= reader->getFrameRel(lag);  //new cVector(nValues+1);
  if (vec == NULL) return 0;
//  else reader->nextFrame();

  struct svm_node *x = NULL;
  int i = 0;
  double v;
                                          // need one more for index = -1
  long Nft = Nsel;
  if (Nft <= 0) Nft = vec->N;

  if (fselType) {
    if ((outputSelIdx.enabled == NULL)&&(outputSelStr.names != NULL)) {
      buildEnabledSelFromNames(vec->N, vec->fmeta);
    } else if (outputSelIdx.enabled == NULL) Nft = vec->N;
  }
    
  // TODO: fselection by names... 
  // TODO: compute Nsel in loadSelection

  x = (struct svm_node *) malloc( (Nft + 1) * sizeof(struct svm_node));
  int j = 0;
  for (i=0; i<vec->N; i++) {
    if ((outputSelIdx.enabled == NULL)||(Nsel<=0)) {
      x[i].index = i+1; // FIXME!!! +1 is ok??? (no!?)
      x[i].value = vec->dataF[i];
    } else {
      if (outputSelIdx.enabled[i]) {
        x[j].index = j+1; // FIXME!!! +1 is ok??? (no!?)
        x[j].value = vec->dataF[i];
        j++;
      }
    }
  }
  if ((outputSelIdx.enabled == NULL)||(Nsel<=0)) {
    x[i].index = -1;
    x[i].value = 0.0;
  } else {
    x[j].index = -1;
    x[j].value = 0.0;
  }

  svm_apply_scale(scale,x);

     /*
        for (i=0; i<vec->n; i++) {
          printf("%i:%f ",i,x[i].value);
        }
        printf("\n");
     */
  long vi = vec->tmeta->vIdx;
  double tm = vec->tmeta->smileTime;
  double dur = vec->tmeta->lengthSec;

  if ( (predictProbability) && (svmType==C_SVC || svmType==NU_SVC) ) {
    v = svm_predict_probability(model,x,probEstimates);
    processResult(t, vi, tm, v, probEstimates, nClasses, dur);
//    printf("%g",v);
//    for(j=0;j<nr_class;j++)
//      printf(" %g",prob_estimates[j]);
//    printf("\n");
  } else {
    v = svm_predict(model,x);
    processResult(t, vi, tm, v, NULL, nClasses, dur);
//    result = v;
//    printf("%g\n",v);
  }
  free(x);

/*
	if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
	{
		printf("Mean squared error = %g (regression)\n",error/total);
		printf("Squared correlation coefficient = %g (regression)\n",
		       ((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
		       ((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))
		       );
	}
	else
		printf("Accuracy = %g%% (%d/%d) (classification)\n",
		       (double)correct/total*100,correct,total);
  */
  
  


  // tick success
  return 1;
}
Exemplo n.º 22
0
JNIEXPORT jint JNICALL Java_LibSVMTest_doClassificationNative
        (JNIEnv *env, jobject obj, jobjectArray valuesArr,
         jobjectArray indicesArr, jint isProb, jintArray labelsArr, jdoubleArray probsArr) {
    int *labels = env->GetIntArrayElements(labelsArr, NULL);
    // initiate the arrays
    int nRow = env->GetArrayLength(valuesArr);
    double **probs = (double **) calloc(nRow, sizeof(double *));
    jfloatArray vrows = (jfloatArray) env->GetObjectArrayElement(valuesArr, 0);
    jintArray irows = (jintArray) env->GetObjectArrayElement(indicesArr, 0);
    jfloat *velement = env->GetFloatArrayElements(vrows, NULL);
    jint *ielement = env->GetIntArrayElements(irows, NULL);
    int colNum = env->GetArrayLength(vrows);
    probs[0] = env->GetDoubleArrayElements(probsArr, NULL);
    
    if(isProb)
    {
        if(svm_check_probability_model(model_F)==0)
        {
            fprintf(stderr, "Model does not support probabiliy estimates\n");
            exit(1);
        }
    }
    else
    {
        if(svm_check_probability_model(model_F)!=0)
            fprintf(stderr, "Model supports probability estimates, but disabled in prediction.\n");
    }

    if(isProb)
    {
        if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
            fprintf(stderr, "Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model_F));
        else
        {
            int *labels=(int *) malloc(nr_class*sizeof(int));
            svm_get_labels(model_F,labels);
        }
    }

    double predict_label=0;
    struct svm_node *x = (struct svm_node *) malloc(64*sizeof(struct svm_node));
    x = (struct svm_node *) realloc(x, (colNum+1)*sizeof(struct svm_node));

    for (int j = 0; j < colNum; j++)
    {
        x[j].index = velement[j];
        x[j].value = ielement[j];
    }
    x[colNum].index = -1;


    // Probability prediction
    if (isProb && (svm_type==C_SVC || svm_type==NU_SVC))
    {
        predict_label = svm_predict_probability(model_F,x,*probs);
        labels[0]=predict_label;

    }
    else { labels[0] = svm_predict(model_F,x);}

    env->ReleaseFloatArrayElements(vrows, velement, JNI_ABORT);
    env->ReleaseIntArrayElements(irows, ielement, JNI_ABORT);
    //added for fixing : JNI ERROR (app bug): local reference table overflow (max=512)
    env->DeleteLocalRef(vrows);
    env->DeleteLocalRef(irows);
    // release the one-D arrays and strings
    env->ReleaseIntArrayElements(labelsArr, labels, 0);
    //added for fixing : JNI ERROR (app bug): local reference table overflow (max=512)
    env->DeleteLocalRef(labelsArr);
    free(x);

    return 0;
}
Exemplo n.º 23
0
    vector<double> classifyObjects(vector<MatDict > features)
    {
        
        // Load the SVM
        svm_model *model = nullptr;
        Mat train_max;
        Mat train_min;
        if (DEBUG) {
            *model = *svm_load_model(MODEL_PATH);
            train_max = loadCSV(TRAIN_MAX_PATH);
            train_min = loadCSV(TRAIN_MIN_PATH);
        } else {
#if !DEBUG
            CFURLRef model_url = CFBundleCopyResourceURL(CFBundleGetMainBundle(),
                                                            CFSTR("model_out"), CFSTR("txt"),
                                                            NULL);
            CFURLRef max_url = CFBundleCopyResourceURL(CFBundleGetMainBundle(),
                                                         CFSTR("train_max"), CFSTR("csv"),
                                                         NULL);
            CFURLRef min_url = CFBundleCopyResourceURL(CFBundleGetMainBundle(),
                                                         CFSTR("train_min"), CFSTR("csv"),
                                                         NULL);
            
            char model_path[1024];
            char max_path[1024];
            char min_path[1024];
            
            CFURLGetFileSystemRepresentation(model_url, true,
                                             model_path, sizeof(model_path));
            CFURLGetFileSystemRepresentation(max_url, true,
                                             max_path, sizeof(max_path));
            CFURLGetFileSystemRepresentation(min_url, true,
                                             min_path, sizeof(min_path));
            CFRelease(model_url);
            CFRelease(max_url);
            CFRelease(min_url);
            
            *model = *svm_load_model(model_path);
            train_max = loadCSV(max_path);
            train_min = loadCSV(min_path);
#endif            
        }

        // Combine the features
        cv::Mat featuresMatrix = cv::Mat((int)features.size(), 22, CV_64F);
        
        vector<MatDict >::const_iterator it = features.begin();
        int row = 0;
        for (; it != features.end(); it++)
        {
            MatDict patch = *it;
            cv::Mat geom = patch.find("geom")->second;
            cv::Mat phi = patch.find("phi")->second;
            int index = 0;
            for (int i = 0; i < phi.rows; i++)
            {
				double val = phi.at<double>(i, 0);
                featuresMatrix.at<double>(row, index) = val;
				index++;
            }
            for (int i = 0; i < geom.rows; i++)
            {
				cv::Mat rowMat = patch.find("row")->second;
				cv::Mat colMat = patch.find("col")->second;
				float geom_val = geom.at<float>(i, 0);
                featuresMatrix.at<double>(row, index) = (double) geom_val;
				index++;
            }
            row++;
        }
        
        Debug::print(featuresMatrix, "xtest.txt");
    
        // minmax normalization of features
        cv::Mat maxMatrix = repMat(train_max, featuresMatrix.rows);
        cv::Mat minMatrix = repMat(train_min, featuresMatrix.rows);
                
        cv::Mat testMatrix = cv::Mat(featuresMatrix.rows, featuresMatrix.cols, featuresMatrix.type());
        cv::Mat numerator = cv::Mat(featuresMatrix.rows, featuresMatrix.cols, featuresMatrix.type());
        cv::Mat denominator = cv::Mat(featuresMatrix.rows, featuresMatrix.cols, featuresMatrix.type());
        cv::subtract(featuresMatrix, minMatrix, numerator);
        cv::subtract(maxMatrix, minMatrix, denominator);
        cv::divide(numerator, denominator, testMatrix);
        
		Debug::print(numerator, "numerator.txt");
        Debug::print(testMatrix, "xtest_final.txt");

		//testMatrix = Debug::loadMatrix("xtest_final.txt", 120, 22, CV_64F);
        
        // Classify objects and get probabilities
        vector<double> prob_results;
        
        for (int i = 0; i < testMatrix.rows; i++) {

            svm_node *node = new svm_node[testMatrix.cols + 2];
            for (int j = 0; j < testMatrix.cols; j++) {
                double d = testMatrix.at<double>(i, j);
                node[j].index = j+1;
                node[j].value = d;
            }
            node[testMatrix.cols].index = -1;
            node[testMatrix.cols].value = 0;
            
            double *probabilities = new double[2];
            svm_predict_probability(model, node, probabilities);
            prob_results.push_back(probabilities[0]);
        }
        
        cout << "Finished classifying features" << endl;
		Debug::printVector(prob_results, "dvtest.txt");
        return prob_results;
    }
Exemplo n.º 24
0
void svmpredict  (int    *decisionvalues,
		  int    *probability,

		  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,

		  double *x, int *xr,
		  int    *xrowindex,
		  int    *xcolindex,
		  int    *sparsex,
		  
		  double *ret,
		  double *dec,
		  double *prob)
{
    struct svm_model m;
    struct svm_node ** train;
    int i;
    
    /* 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.probA    = probA;
    m.probB    = probB;
    m.label    = labels;
    m.nSV      = nSV;

    /* 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.param.probability = *compprob;      

    m.free_sv           = 1;

    /* create sparse training matrix */
    if (*sparsex > 0)
	train = transsparse(x, *xr, xrowindex, xcolindex);
    else
	train = sparsify(x, *xr, *c);

    /* call svm-predict-function for each x-row, possibly using probability 
       estimator, if requested */
    if (*probability && svm_check_probability_model(&m)) {
      for (i = 0; i < *xr; i++)
	ret[i] = svm_predict_probability(&m, train[i], prob + i * *nclasses);
    } else {
      for (i = 0; i < *xr; i++)
	ret[i] = svm_predict(&m, train[i]);
    }

    /* optionally, compute decision values */
    if (*decisionvalues)
      for (i = 0; i < *xr; i++)
	svm_predict_values(&m, train[i], dec + i * *nclasses * (*nclasses - 1) / 2);

    /* clean up memory */
    for (i = 0; i < *xr; i++)
	free (train[i]);
    free (train);

    for (i = 0; i < *r; i++)
	free (m.SV[i]);
    free (m.SV);
    
    for (i = 0; i < m.nr_class - 1; i++)
      free(m.sv_coef[i]);
    free(m.sv_coef);
}	     
Exemplo n.º 25
0
void predict(FILE *input, FILE *output)
{
	int correct = 0;
	int total = 0;
	double error = 0;
	double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;

	int svm_type=svm_get_svm_type(model);
	int nr_class=svm_get_nr_class(model);
	double *prob_estimates=NULL;
	int j;

	if(predict_probability)
	{
		if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
			printf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model));
		else
		{
			int *labels=(int *) malloc(nr_class*sizeof(int));
			svm_get_labels(model,labels);
			prob_estimates = (double *) malloc(nr_class*sizeof(double));
			fprintf(output,"labels");		
			for(j=0;j<nr_class;j++)
				fprintf(output," %d",labels[j]);
			fprintf(output,"\n");
			free(labels);
		}
	}

	max_line_len = 1024;
	line = (char *)malloc(max_line_len*sizeof(char));
	while(readline(input) != NULL)
	{
		int i = 0;
		double target_label, predict_label;
		char *idx, *val, *label, *endptr;
		int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0

		label = strtok(line," \t");
		target_label = strtod(label,&endptr);
		if(endptr == label)
			exit_input_error(total+1);

		while(1)
		{
			if(i>=max_nr_attr-1)	// need one more for index = -1
			{
				max_nr_attr *= 2;
				x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
			}

			idx = strtok(NULL,":");
			val = strtok(NULL," \t");

			if(val == NULL)
				break;
			errno = 0;
			x[i].index = (int) strtol(idx,&endptr,10);
			if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index)
				exit_input_error(total+1);
			else
				inst_max_index = x[i].index;

			errno = 0;
			x[i].value = strtod(val,&endptr);
			if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr)))
				exit_input_error(total+1);

			++i;
		}
		x[i].index = -1;

		if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
		{
			predict_label = svm_predict_probability(model,x,prob_estimates);
			fprintf(output,"%g",predict_label);
			for(j=0;j<nr_class;j++)
				fprintf(output," %g",prob_estimates[j]);
			fprintf(output,"\n");
		}
		else
		{
			predict_label = svm_predict(model,x);
			fprintf(output,"%g\n",predict_label);
		}

		if(predict_label == target_label)
			++correct;
		error += (predict_label-target_label)*(predict_label-target_label);
		sump += predict_label;
		sumt += target_label;
		sumpp += predict_label*predict_label;
		sumtt += target_label*target_label;
		sumpt += predict_label*target_label;
		++total;
	}
	if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
	{
		printf("Mean squared error = %g (regression)\n",error/total);
		printf("Squared correlation coefficient = %g (regression)\n",
		       ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
		       ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt))
		       );
	}
	else
		printf("Accuracy = %g%% (%d/%d) (classification)\n",
		       (double)correct/total*100,correct,total);
	if(predict_probability)
		free(prob_estimates);
}
Exemplo n.º 26
0
void predict(FILE *input, FILE *output, FILE *output_w2, FILE *output_prob,int predict_probability, FILE *addr, int Ncol, int Nlig, long Npolar)
{
	int svm_type=svm_get_svm_type(model);
	int nr_class=svm_get_nr_class(model);
	double *prob_estimates=NULL;
	int i,j,k,l,num_block,p,n,count;
	int Nband[Npolar];
	long n_addr = 0,m=0, n_total_band, offset=0;
	long max_num_pixel, num_rest_pixel, n_img_pixel = (long)Ncol * (long)Nlig;
	float buff_float, zero = 0;
	float **V_pol_rest, **V_pol_block, *v_pol_buff;
	long int *tab_addr, buff_long_int;
	float *output_label;
	double *w2_predict;
	float **w2_predict_out, *mean_dist, *max_prob, **prob_estimates_out;
	
	
	max_num_pixel = (long)floor(memory_pixel /((long)4 * Npolar));
	
	for(i=0; i< n_img_pixel; i++){// We initiaite the output classification file with '0' value
		fwrite(&zero, sizeof(float), 1, output);
	}
	rewind(output);
	
/*	for(i=0; i< n_img_pixel*(nr_class*(nr_class-1)/2 +1) ; i++){// We initiaite the output classification file with '0' value
		fwrite(&zero, sizeof(float), 1, output_w2);
	}
	rewind(output_w2);
*/
	if(predict_probability)
	{
		printf("Predict prob!!!!\n");
		for(i=0; i< n_img_pixel*(nr_class + 1) ; i++){// We initiaite the output classification file with '0' value
			fwrite(&zero, sizeof(float), 1, output_prob);
		}
		rewind(output_prob);
		
		if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
			printf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model));
		else
		{
			int *labels=(int *) malloc(nr_class*sizeof(int));
			svm_get_labels(model,labels);
			prob_estimates = (double *) malloc(nr_class*sizeof(double));
			//fprintf(output,"labels");		
			//for(j=0;j<nr_class;j++)
			//	printf(" %d\n",labels[j]);
				//fprintf(output," %d",labels[j]);
			//fprintf(output,"\n");
			free(labels);
		}
	}

	while(fread(&buff_long_int, sizeof(long int), 1, addr)!=0){ // We read the addr file to know the number of pixel to be classified
		n_addr++;
	}
	n_addr = n_addr - Npolar;
	rewind(addr);
	tab_addr = (long int *) malloc((long int) (n_addr) * sizeof(long int));

	for(i=0; i<Npolar;i++){
		fread(&buff_long_int, sizeof(long int), 1, addr);
		Nband[i] = (int)buff_long_int;
	}
	i=0;


	while(fread(&tab_addr[i], sizeof(long int), 1, addr)!=0){ // We read the addr of each classified pixel
//hde
//printf("tab_addr[%d]:%d\n",i,tab_addr[i]);

		i++;

	}
//hde
int min,max;
min=tab_addr[0];
max=min;
for (i=0;i<n_addr;i++)
{
	if (tab_addr[i]>max) {max=tab_addr[i];}
	if (tab_addr[i]<min) {min=tab_addr[i];}
}
printf("min:%d; max:%d\n",min,max);
// fin HDE

	// We compute the necessary number of block and remaining pixel
	num_block = (int)floor(n_addr /max_num_pixel);
	num_rest_pixel = n_addr - num_block * max_num_pixel;
	printf("num_block: %i\n",num_block);

	/////////////////////////////////////////////////Ajouter boucle de lecture des float input pour ensuite calculer le
	//nb de bande total initial pour faire une lecutre par bloc de bande puis extraire celle qui sont interessante
	while(fread(&buff_float, sizeof(float), 1, input)!=0){ 
		m++;
	}
	rewind(input);
	
	if(num_block > 0){//Loop on the block
		V_pol_block = matrix_float((int)max_num_pixel,(int)Npolar);
		output_label = vector_float((int)max_num_pixel);
	//	mean_dist = vector_float((int)max_num_pixel);
	//	w2_predict_out = matrix_float((int)max_num_pixel,nr_class*(nr_class-1)/2);
		prob_estimates_out = matrix_float((int)max_num_pixel,nr_class);
		max_prob = vector_float((int)max_num_pixel);
		for (i=0; i<num_block; i++){	// blocs pour l'optimisation accès disque
			for (j=0; j< max_num_pixel; j++){
				for (k=0; k<Npolar; k++){	
					// pointe sur le pixel col,lig, k(bande), selon la bande
					offset = (long)(sizeof(float)) * (Nband[k] * n_img_pixel + tab_addr[i*max_num_pixel + j]);
					fseek(input, offset, SEEK_SET); //place le pointeur à l'offset calculé avant qui s'appelle offset ! 
					fread(&V_pol_block[j][k], sizeof(float), 1, input);

				}
			}
			for (j=0; j< max_num_pixel; j++){
				for (k=0; k<Npolar; k++){
					if(k>=max_nr_attr-1)	// need one more for index = -1
					{
						max_nr_attr *= 2;
						x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
					}
					x[k].index = k+1;
					x[k].value = scale(k, (double)V_pol_block[j][k]);
				}
				x[k].index = -1;
				double predict_label;
				
				if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
				{
					//printf("Predict prob if block!!!!\n");
					predict_label = svm_predict_probability(model,x,prob_estimates);
					
					w2_predict = svm_w2(model,x);				
					
					output_label[j] = (float)predict_label;
					
	//				mean_dist[j] = 0.;
					count = 0;
					l=0;
					max_prob[j] = 0.;
					for(p=0;p<nr_class;p++){
						for(n=p+1;n<nr_class;n++){
		//					w2_predict_out[j][l] =(float)w2_predict[l];
						//	printf("w2_predict[%i] : %g\n",w2_predict[l]);
							if(p+1 == output_label[j] || n+1 == output_label[j]){
			//					mean_dist[j] = mean_dist[j] + w2_predict_out[j][l];
								count = count + 1;
							}
							l++;
						}
						prob_estimates_out[j][p] = (float)prob_estimates[p];
						if(max_prob[j] < prob_estimates_out[j][p] ){
							max_prob[j] = prob_estimates_out[j][p];
						}
					}
		//			mean_dist[j] = mean_dist[j] / (float)count;
				}
				else
				{
					predict_label = svm_predict(model,x);
					
					w2_predict = svm_w2(model,x);
					
					
					
					output_label[j] = (float)predict_label;
					
		//			mean_dist[j] = 0.;
					count = 0;
					l=0;
					for(p=0;p<nr_class;p++){
						for(n=p+1;n<nr_class;n++){
			//				w2_predict_out[j][l] =(float)w2_predict[l];
							
							if(p+1 == output_label[j] || n+1 == output_label[j]){
				//			mean_dist[j] = mean_dist[j] + w2_predict_out[j][l];
								count = count + 1;
							}
							l++;
						}
					}
		//			mean_dist[j] = mean_dist[j] / (float)count;
						
				
						
				}
			}/* pixels block */

			for (j=0; j< max_num_pixel; j++){ // On ecrit le fichier de classif
				offset = (long)(sizeof(float)) * tab_addr[i*max_num_pixel + j];
					fseek (output, offset, SEEK_SET);
					fwrite(&output_label[j], sizeof(float), 1, output);
					
					
			}
/*			for (l=0; l< nr_class*(nr_class-1)/2; l++){ // On ecrit les cartes de distances
				for (j=0; j< max_num_pixel; j++){
					offset = (long)(sizeof(float)) * tab_addr[i*max_num_pixel + j];
					fseek(output_w2, offset + n_img_pixel * sizeof(float) * l, SEEK_SET);
					fwrite(&w2_predict_out[j][l],  sizeof(float), 1, output_w2);		
				}			
			}
			
			for (j=0; j< max_num_pixel; j++){ // On ecrit la distance moyenne
				offset = (long)(sizeof(float)) * tab_addr[i*max_num_pixel + j];
				fseek(output_w2, offset + n_img_pixel * sizeof(float) * (nr_class*(nr_class-1)/2), SEEK_SET);// on ecrit la distance moyenne parmi les classif bianaire renvoyant le label final
				fwrite(&mean_dist[j],  sizeof(float), 1, output_w2);
			}
*/			
			if(predict_probability){
				//printf("Predict prob fin if block ecriture!!!!\n");
				for (l=0; l< nr_class; l++){ // On ecrit les cartes de probabilité
					for (j=0; j< max_num_pixel; j++){
						offset = (long)(sizeof(float)) * tab_addr[i*max_num_pixel + j];
						fseek(output_prob, offset + n_img_pixel * sizeof(float) * l, SEEK_SET);
						fwrite(&prob_estimates_out[j][l],  sizeof(float), 1, output_prob);		
					}			
				}
				
				for (j=0; j< max_num_pixel; j++){
						offset = (long)(sizeof(float)) * tab_addr[i*max_num_pixel + j];
						fseek(output_prob, offset + n_img_pixel * sizeof(float) * nr_class, SEEK_SET);
						fwrite(&max_prob[j],  sizeof(float), 1, output_prob);		
					}	
			}

		}/* Loop over the blocks*/
		free_matrix_float(V_pol_block, max_num_pixel);
	//	free_matrix_float(w2_predict_out, max_num_pixel);
		free_matrix_float(prob_estimates_out, max_num_pixel);
		free_vector_float(output_label);
	//	free_vector_float(mean_dist);
		free(w2_predict);
		free_vector_float(max_prob);
		printf("FREE block: %i\n",num_block);
	}else{
		printf("PAS DE BLOCK\n");
	}
	if(num_rest_pixel > 0){// Loop on the remaining pixel
		V_pol_rest = matrix_float((int)num_rest_pixel,(int)Npolar);/* Vector of polarimetrics indicators  for the remaining pixel */
		output_label = vector_float((int)num_rest_pixel);
	//	w2_predict_out = matrix_float((int)num_rest_pixel,nr_class*(nr_class-1)/2);
	//	mean_dist = vector_float((int)num_rest_pixel);
		prob_estimates_out = matrix_float((int)num_rest_pixel,nr_class);
		max_prob = vector_float((int)max_num_pixel);
		for (j=0; j< num_rest_pixel;j++){
			for (k=0; k<Npolar; k++){	
				offset = (long)(sizeof(float)) * (Nband[k] * n_img_pixel + tab_addr[num_block*max_num_pixel + j]);
				fseek (input, offset , SEEK_SET);
				fread(&V_pol_rest[j][k], sizeof(float), 1, input);
				
//hde
//printf("valeur pixel a adresse %d et pour bande %d: %d\n", tab_addr[num_block*max_num_pixel + j],k,V_pol_rest[j][k]);
			}
		}
		for (j=0; j< num_rest_pixel;j++){
			for (k=0; k<Npolar; k++){
				if(k>=max_nr_attr-1)	// need one more for index = -1
				{
					max_nr_attr *= 2;
					x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
				}
				x[k].index = k+1;
				x[k].value = scale(k, (double)V_pol_rest[j][k]);
			}
			x[k].index = -1;
			double predict_label;
			if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
			{
				//printf("Predict prob if fin apres block!!!!\n");
				predict_label = svm_predict_probability(model,x,prob_estimates);
				 w2_predict = svm_w2(model,x);				
					
				output_label[j] = (float)predict_label;
					
		//		mean_dist[j] = 0.;
				count = 0;
				l=0;
				max_prob[j] = 0.;
				for(p=0;p<nr_class;p++){
					for(n=p+1;n<nr_class;n++){
				//		w2_predict_out[j][l] =(float)w2_predict[l];
					//	printf("w2_predict[%i] : %g\n",l,w2_predict[l]);
					//	printf("w2_predict_out[%i][%i] : %g\n",j,l,w2_predict_out[j][l]);
						if(p+1 == output_label[j] || n+1 == output_label[j]){
			//				mean_dist[j] = mean_dist[j] + w2_predict_out[j][l];
							count = count + 1;
						}
						l++;
					}
					prob_estimates_out[j][p] = (float)prob_estimates[p];
					if(max_prob[j] < prob_estimates_out[j][p] ){
							max_prob[j] = prob_estimates_out[j][p];
						}
				}
		//		mean_dist[j] = mean_dist[j] / (float)count;
				//printf("mean_dist[%i],%f\n",j,mean_dist[j]);
			}
			else
			{
				predict_label = svm_predict(model,x);
				
				w2_predict = svm_w2(model,x);
					
				
				
				output_label[j] = (float)predict_label;
					
		//		mean_dist[j] = 0.;
				count = 0;
				l=0;
				for(p=0;p<nr_class;p++){
					for(n=p+1;n<nr_class;n++){
			//			w2_predict_out[j][l] =(float)w2_predict[l];
						
						if(p+1 == output_label[j] || n+1 == output_label[j]){
		//					mean_dist[j] = mean_dist[j] + w2_predict_out[j][l];
							count = count + 1;
						}
						l++;
					}
				}
		//		mean_dist[j] = mean_dist[j] / (float)count;
			}
		}
		for (j=0; j< num_rest_pixel; j++){
				offset = (long)(sizeof(float)) * (tab_addr[num_block*max_num_pixel + j]);
					fseek (output, offset, SEEK_SET);
					fwrite(&output_label[j], sizeof(float), 1, output);
					
					
			}
/*			for (l=0; l< nr_class*(nr_class-1)/2; l++){
				for (j=0; j< num_rest_pixel; j++){
					offset = (long)(sizeof(float)) * (tab_addr[num_block*max_num_pixel + j]);
					fseek(output_w2, offset + n_img_pixel * sizeof(float) * l, SEEK_SET);
					fwrite(&w2_predict_out[j][l],  sizeof(float), 1, output_w2);		
				}			
			}
			
			for (j=0; j< num_rest_pixel; j++){
				offset = (long)(sizeof(float)) * (tab_addr[num_block*max_num_pixel + j]);
				fseek(output_w2, offset + n_img_pixel * sizeof(float) * (nr_class*(nr_class-1)/2), SEEK_SET);// on ecrit la distance moyenne parmi les classif bianaire renvoyant le label final
				fwrite(&mean_dist[j],  sizeof(float), 1, output_w2);
			}
*/			
			
			if(predict_probability){
				//printf("Predict prob if fin apres blockeCRITUR!!!!!!!\n");
				for (l=0; l< nr_class; l++){ // On ecrit les cartes de probabilité
					for (j=0; j< num_rest_pixel; j++){
						offset = (long)(sizeof(float)) * (tab_addr[num_block*max_num_pixel + j]);
						fseek(output_prob, offset + n_img_pixel * sizeof(float) * l, SEEK_SET);
						fwrite(&prob_estimates_out[j][l],  sizeof(float), 1, output_prob);	
						//printf("prob_estimates_out[%i][%i] : %f\n", j, l, prob_estimates_out[j][l]);	
					}			
				}
				for (j=0; j< num_rest_pixel; j++){
						offset = (long)(sizeof(float)) * (tab_addr[num_block*max_num_pixel + j]);
						fseek(output_prob, offset + n_img_pixel * sizeof(float) * nr_class, SEEK_SET);
						fwrite(&max_prob[j],  sizeof(float), 1, output_prob);	
					}	
				
			}

		free_matrix_float(V_pol_rest, num_rest_pixel);
	//	free_matrix_float(w2_predict_out, num_rest_pixel);
		free_matrix_float(prob_estimates_out, num_rest_pixel);
		free_vector_float(output_label);
		free(w2_predict);
	//	free_vector_float(mean_dist);
		free_vector_float(max_prob);
	}/* remaning pixel */
	free(tab_addr);	
	
	if(predict_probability)
		free(prob_estimates);
}
Exemplo n.º 27
0
Arquivo: RNAz.c Projeto: wash/rnaz
PRIVATE void classify(double* prob, double* decValue,
		      struct svm_model* decision_model,
		      double id,int n_seq, double z,double sci,
		      double entropy, int decision_model_type){

  FILE *out=stdout; /* Output file */

  /************************************/
  /* normal model as used in RNAz 1.0 */
  /************************************/
  if (decision_model_type == 1) {
    struct svm_node node[5];
    
    double* value;
    value=(double*)space(sizeof(double)*2);
    
    node[0].index = 1; node[0].value = z;
    node[1].index = 2; node[1].value = sci;
    node[2].index = 3; node[2].value = id;
    node[3].index = 4; node[3].value = n_seq;
    node[4].index =-1;
    
    scale_decision_node((struct svm_node*)&node,decision_model_type);
    
    svm_predict_values(decision_model,node,value);
    *decValue=value[0];
    
    svm_predict_probability(decision_model,node,value);
    *prob=value[0];

    free(value);
  }

  /****************************************************/
  /* dinucleotide model for sequence based alignments */
  /****************************************************/
  if (decision_model_type == 2) {

    /* For training we used the z-score and the SCI rounded to tow 
       decimal places. To make results comparable we do it also here. */

    char tmp[10];
    sprintf(tmp, "%.2f", (double) z);
    double z_tmp = (double) atof(tmp);

    sprintf(tmp, "%.2f", (double) sci);
    double sci_tmp = (double) atof(tmp);

    /* In some rare cases it might happen that z-score and SCI are
       out of the training range. In these cases they are just set to the 
       maximum or minimum. */
 
    if (z_tmp > 2.01) z_tmp = 2.01;
    if (z_tmp < -8.15) z_tmp = -8.15;
    if (sci_tmp > 1.29) sci_tmp = 1.29;

    /* construct SVM node and scale parameters */

    struct svm_node node[4];
    double* value;
    value=(double*)space(sizeof(double)*2);
    
    node[0].index = 1; node[0].value = z_tmp;
    node[1].index = 2; node[1].value = sci_tmp;
    node[2].index = 3; node[2].value = entropy;
    node[3].index =-1; 
    
    scale_decision_node((struct svm_node*)&node,decision_model_type);

    /* For training we used scaled variables rounded to five
       decimal places. To make results comparable we do it also here. */

    sprintf(tmp, "%.5f", (double) node[0].value);
    node[0].value = (double) atof(tmp);
    sprintf(tmp, "%.5f", (double) node[1].value);
    node[1].value = (double) atof(tmp);
    sprintf(tmp, "%.5f", (double) node[2].value);
    node[2].value = (double) atof(tmp);

    /* Now predict decision value and probability */

    svm_predict_values(decision_model,node,value);
    *decValue=value[0];
    
    svm_predict_probability(decision_model,node,value);
    *prob=value[0];

    free(value);
  }


  /***********************************************************************/
  /* dinucleotide model for structural alignments generated by locarnate */
  /***********************************************************************/
  if (decision_model_type == 3) {

    /* For training we used the z-score and the SCI rounded to tow 
       decimal places. To make results comparable we do it also here. */

    char tmp[10];
    sprintf(tmp, "%.2f", (double) z);
    double z_tmp = (double) atof(tmp);

    sprintf(tmp, "%.2f", (double) sci);
    double sci_tmp = (double) atof(tmp);

    /* In some rare cases it might happen that z-score and SCI are
       out of the training range. In these cases they are just set to the 
       maximum or minimum. */
 
    if (z_tmp > 2.01) z_tmp = 2.01;
    if (z_tmp < -8.13) z_tmp = -8.13;
    if (sci_tmp > 1.31) sci_tmp = 1.31;

    /*fprintf(out," z: %f, sci: %f, entropy:%f\n",z_tmp,sci_tmp,entropy);*/

    /* construct SVM node and scale parameters */

    struct svm_node node[4];
    double* value;
    value=(double*)space(sizeof(double)*2);
    
    node[0].index = 1; node[0].value = z_tmp;
    node[1].index = 2; node[1].value = sci_tmp;
    node[2].index = 3; node[2].value = entropy;
    node[3].index =-1; 
    
    scale_decision_node((struct svm_node*)&node,decision_model_type);

    /* For training we used scaled variables rounded to five
       decimal places. To make results comparable we do it also here. */

    sprintf(tmp, "%.5f", (double) node[0].value);
    node[0].value = (double) atof(tmp);
    sprintf(tmp, "%.5f", (double) node[1].value);
    node[1].value = (double) atof(tmp);
    sprintf(tmp, "%.5f", (double) node[2].value);
    node[2].value = (double) atof(tmp);

    /*fprintf(out," z: %f, sci: %f, entropy:%f\n",node[0].value,node[1].value,node[2].value);*/

    /* Now predict decision value and probability */

    svm_predict_values(decision_model,node,value);
    *decValue=value[0];
    
    svm_predict_probability(decision_model,node,value);
    *prob=value[0];

    free(value);
  }
  
  
}
Exemplo n.º 28
0
//This function is used to recognize continuous SLR in a off-line way. 
//Since the data should be read in all at once, the class gcmCont is not necessary used. 
int gcm::patchRun_continuous_PQ(vector<SLR_ST_Skeleton> vSkeletonData, vector<Mat> vDepthData, vector<IplImage*> vColorData, 
								int *rankIndex, double *rankScore)
{
	int window = 40;
	int kernelFeatureDim = NClass*NTrainSample;
	//Computing features
	cout<<"Computing features..."<<endl;
	oriData2Feature(vSkeletonData, vDepthData, vColorData);

	//////////////////////////////////////////////////////////////////////////
	//Compute P and Q all at once.
	vector<double*> P;
	vector<double**> Q;
	cout<<"Computing P and Q..."<<endl;

	//computePQ(P, Q, vColorData.size());
	//white
	for (int d=0; d<nDimension; d++)
	{
		double dimAve = 0.0;
		for (int f=0; f<nFrames; f++)
		{
			dimAve += feature_ori[f][d];
		}
		dimAve /= nFrames;
		for (int f=0; f<nFrames; f++)
		{
			feature_ori[f][d] -= dimAve;
		}
	}

	//Compute P and Q.
	int nFrames_PQ = vColorData.size();
	for (int i=0; i<nFrames_PQ; i++)
	{
		cout<<"Current "<<i<<"/"<<nFrames_PQ<<endl;
		//Compute P
		double* tempP = new double[featureDim];
		for (int j=0; j<featureDim; j++)
		{
			tempP[j] = 0;
			for (int k=0; k<i; k++)
			{
				tempP[j] += feature_ori[k][j];
			}
		}
		P.push_back(tempP);    //To release it when reaching the end of a sentence 

		//Compute Q
		double** tempQ;
		tempQ = newMatrix(featureDim, featureDim);
		for (int f1=0; f1<featureDim; f1++)
		{
			for (int f2=0; f2<featureDim; f2++) 
			{
				tempQ[f1][f2] = 0;
				for (int l=0; l<i; l++)
				{
					tempQ[f1][f2] += feature_ori[f1][l]*feature_ori[f2][l];
				}
			}
		}
		Q.push_back(tempQ);      //To release it when reaching the end of a sentence 
	}
	//////////////////////////////////////////////////////////////////////////	
	
	double** C = newMatrix(featureDim, featureDim);
	double* p_temp = new double[featureDim];  //The deltaP
	double** Pm = newMatrix(featureDim, featureDim);
	for (int i=window; i<vColorData.size()-window; i++)
	{
		int begin = i-window/2;
		int end = i+window/2;

		//The matrix from p
		for (int pf=0; pf<featureDim; pf++)
		{
			p_temp[pf] = P[end][pf]-P[begin][pf];
		}
		vector2matrix(p_temp, p_temp, Pm,featureDim);

		//Compute the covariance matrix
		for (int l=0; l<featureDim; l++)
		{
			for (int m=0; m<featureDim; m++)
			{
				C[l][m] = ((Q[end][l][m]-Q[begin][l][m])-Pm[l][m]/(end-begin+1))/(end-begin);
			}
		}

		//Regularization term added
		for (int d=0; d<nDimension; d++)
		{
			for (int d2=0; d2<nDimension; d2++)
			{
				//C[d][d2] /= nFrames;
				if (d == d2)
				{
					C[d][d2] += 0.001;
				}
			}
		}

		//The subspace matrix
		PQsubspace(C, gcm_subspace);

		//For debug
		ofstream foutDebug;
		foutDebug.open("..\\output\\debug.txt");
		for (int i=0; i<featureDim; i++)
		{
			for (int j=0; j<subSpaceDim; j++)
			{
				foutDebug<<gcm_subspace[i][j]<<"\t";
			}
			foutDebug<<"\n";
		}
		foutDebug << flush;
		foutDebug.close();

		//The SVM classification
		x[0].index = 0;
		for (int j=0; j<kernelFeatureDim; j++)
		{
			subMatrix(subFeaAll_model, subFea1, 0, featureDim, j*subSpaceDim, subSpaceDim);
			x[j+1].value = myGcmKernel.Frobenius(subFea1, gcm_subspace, featureDim, subSpaceDim);
			x[j+1].index=j+1;
		}
		x[kernelFeatureDim+1].index=-1;

		int testID = svm_predict_probability(myModel, x, prob_estimates);
		cout<<"Frame: "<<i<<"/"<<vColorData.size()-window<<" Result: "<<testID<<endl;
	}
	delete[] p_temp;
	deleteMatrix(Pm,featureDim);
	deleteMatrix(C, featureDim);

	//To delete P and Q


	return 1;

// 	gcmSubspace();
// 
// 	x[0].index = 0;
// 	for (int j=0; j<kernelFeatureDim; j++)
// 	{
// 		subMatrix(subFeaAll_model, subFea1, 0, featureDim, j*subSpaceDim, subSpaceDim);
// 		x[j+1].value = myGcmKernel.Frobenius(subFea1, gcm_subspace, featureDim, subSpaceDim);
// 		x[j+1].index=j+1;
// 	}
// 	x[kernelFeatureDim+1].index=-1;
// 
// 	int testID = svm_predict_probability(myModel, x, prob_estimates);
// 
// 	//Sort and get the former 5 ranks. 
// 	vector<scoreAndIndex> rank;
// 	for (int i=0; i<myModel->nr_class; i++)
// 	{
// 		scoreAndIndex temp;
// 		temp.index = myModel->label[i];
// 		temp.score = prob_estimates[i];
// 		rank.push_back(temp);
// 	}
// 	sort(rank.begin(),rank.end(),comp);
// 
// 	for (int i=0; i<5; i++)
// 	{
// 		rankIndex[i] = rank[i].index;
// 		rankScore[i] = rank[i].score;
// 	}
// 
// 
// 	//Release
// 	nFrames = 0;
// 
// 	return testID;
}
Exemplo n.º 29
0
void predict(mxArray *plhs[], const mxArray *prhs[], struct svm_model *model, const int predict_probability)
{
	int label_vector_row_num, label_vector_col_num;
	int feature_number, testing_instance_number;
	int instance_index;
	double *ptr_instance, *ptr_label, *ptr_predict_label; 
	double *ptr_prob_estimates, *ptr_dec_values, *ptr;
	struct svm_node *x;
	mxArray *pplhs[1]; // transposed instance sparse matrix

	int correct = 0;
	int total = 0;
	double error = 0;
	double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;

	int svm_type=svm_get_svm_type(model);
	int nr_class=svm_get_nr_class(model);
	double *prob_estimates=NULL;

	// prhs[1] = testing instance matrix
	feature_number = (int)mxGetN(prhs[1]);
	testing_instance_number = (int)mxGetM(prhs[1]);
	label_vector_row_num = (int)mxGetM(prhs[0]);
	label_vector_col_num = (int)mxGetN(prhs[0]);

	if(label_vector_row_num!=testing_instance_number)
	{
		mexPrintf("Length of label vector does not match # of instances.\n");
		fake_answer(plhs);
		return;
	}
	if(label_vector_col_num!=1)
	{
		mexPrintf("label (1st argument) should be a vector (# of column is 1).\n");
		fake_answer(plhs);
		return;
	}

	ptr_instance = mxGetPr(prhs[1]);
	ptr_label    = mxGetPr(prhs[0]);

	// transpose instance matrix
	if(mxIsSparse(prhs[1]))
	{
		if(model->param.kernel_type == PRECOMPUTED)
		{
			// precomputed kernel requires dense matrix, so we make one
			mxArray *rhs[1], *lhs[1];
			rhs[0] = mxDuplicateArray(prhs[1]);
			if(mexCallMATLAB(1, lhs, 1, rhs, "full"))
			{
				mexPrintf("Error: cannot full testing instance matrix\n");
				fake_answer(plhs);
				return;
			}
			ptr_instance = mxGetPr(lhs[0]);
			mxDestroyArray(rhs[0]);
		}
		else
		{
			mxArray *pprhs[1];
			pprhs[0] = mxDuplicateArray(prhs[1]);
			if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose"))
			{
				mexPrintf("Error: cannot transpose testing instance matrix\n");
				fake_answer(plhs);
				return;
			}
		}
	}

	if(predict_probability)
	{
		if(svm_type==NU_SVR || svm_type==EPSILON_SVR)
			mexPrintf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model));
		else
			prob_estimates = (double *) malloc(nr_class*sizeof(double));
	}

	plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL);
	if(predict_probability)
	{
		// prob estimates are in plhs[2]
		if(svm_type==C_SVC || svm_type==NU_SVC)
			plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL);
		else
			plhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL);
	}
	else
	{
		// decision values are in plhs[2]
		if(svm_type == ONE_CLASS ||
		   svm_type == EPSILON_SVR ||
		   svm_type == NU_SVR)
			plhs[2] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL);
		else
			plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class*(nr_class-1)/2, mxREAL);
	}

	ptr_predict_label = mxGetPr(plhs[0]);
	ptr_prob_estimates = mxGetPr(plhs[2]);
	ptr_dec_values = mxGetPr(plhs[2]);
	x = (struct svm_node*)malloc((feature_number+1)*sizeof(struct svm_node) );
	for(instance_index=0;instance_index<testing_instance_number;instance_index++)
	{
		int i;
		double target_label, predict_label;

		target_label = ptr_label[instance_index];

		if(mxIsSparse(prhs[1]) && model->param.kernel_type != PRECOMPUTED) // prhs[1]^T is still sparse
			read_sparse_instance(pplhs[0], instance_index, x);
		else
		{
			for(i=0;i<feature_number;i++)
			{
				x[i].index = i+1;
				x[i].value = ptr_instance[testing_instance_number*i+instance_index];
			}
			x[feature_number].index = -1;
		}

		if(predict_probability)
		{
			if(svm_type==C_SVC || svm_type==NU_SVC)
			{
				predict_label = svm_predict_probability(model, x, prob_estimates);
				ptr_predict_label[instance_index] = predict_label;
				for(i=0;i<nr_class;i++)
					ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i];
			} else {
				predict_label = svm_predict(model,x);
				ptr_predict_label[instance_index] = predict_label;
			}
		}
		else
		{
			predict_label = svm_predict(model,x);
			ptr_predict_label[instance_index] = predict_label;

			if(svm_type == ONE_CLASS ||
			   svm_type == EPSILON_SVR ||
			   svm_type == NU_SVR)
			{
				double res;
				svm_predict_values(model, x, &res);
				ptr_dec_values[instance_index] = res;
			}
			else
			{
				double *dec_values = (double *) malloc(sizeof(double) * nr_class*(nr_class-1)/2);
				svm_predict_values(model, x, dec_values);
				for(i=0;i<(nr_class*(nr_class-1))/2;i++)
					ptr_dec_values[instance_index + i * testing_instance_number] = dec_values[i];
				free(dec_values);
			}
		}

		if(predict_label == target_label)
			++correct;
		error += (predict_label-target_label)*(predict_label-target_label);
		sump += predict_label;
		sumt += target_label;
		sumpp += predict_label*predict_label;
		sumtt += target_label*target_label;
		sumpt += predict_label*target_label;
		++total;
	}
	if(svm_type==NU_SVR || svm_type==EPSILON_SVR)
	{
		mexPrintf("Mean squared error = %g (regression)\n",error/total);
		mexPrintf("Squared correlation coefficient = %g (regression)\n",
			((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
			((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt))
			);
	}
	/* else */
	/*   mexPrintf("Accuracy = %g%% (%d/%d) (classification)\n", */
	/* 	    (double)correct/total*100,correct,total); */

	// return accuracy, mean squared error, squared correlation coefficient
	plhs[1] = mxCreateDoubleMatrix(3, 1, mxREAL);
	ptr = mxGetPr(plhs[1]);
	ptr[0] = (double)correct/total*100;
	ptr[1] = error/total;
	ptr[2] = ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
				((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt));

	free(x);
	if(prob_estimates != NULL)
		free(prob_estimates);
}
Exemplo n.º 30
0
void predict(int nlhs, mxArray *plhs[], const mxArray *prhs[], struct svm_model *model)
{
	int feature_number, testing_instance_number;
	int instance_index;
	double *ptr_instance; 
	double *ptr_prob_estimates;
	
	mxArray *tplhs; // temporary storage for plhs[]

	int svm_type=svm_get_svm_type(model);
	int nr_class=svm_get_nr_class(model);

	// prhs[0] = testing instance matrix
	feature_number = (int)mxGetM(prhs[0]);
	testing_instance_number = (int)mxGetN(prhs[0]);

	ptr_instance = mxGetPr(prhs[0]);

	tplhs = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL);

	ptr_prob_estimates = mxGetPr(tplhs);    
    
    int freeInstance = 0;
    
	#pragma omp parallel
    {
    struct svm_node *x = (struct svm_node*)malloc((feature_number+1)*sizeof(struct svm_node) );
    double *prob_estimates = (double *) malloc(nr_class*sizeof(double));
    int i,base;
    double predict_label; 
    
    int instance_index;
    while (true) {
        #pragma omp critical 
        {
          instance_index = freeInstance;
          freeInstance++;
        }

        if (instance_index >= testing_instance_number)
            break;    
        
        base = feature_number*instance_index;
        for(i=0;i<feature_number;i++)
        {
            x[i].index = i+1;
            x[i].value = ptr_instance[base + i];
        }
        x[feature_number].index = -1;
        
        predict_label = svm_predict_probability(model, x, prob_estimates);

        for(i=0;i<nr_class;i++)
            ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i];
    }
    free(x);
    free(prob_estimates);
    }
		
    plhs[0] = tplhs;

}