int cLibsvmLiveSink::myFinaliseInstance() { int ap=0; int ret = cDataSink::myFinaliseInstance(); if (ret==0) return 0; // TODO: binary model files... // load model SMILE_MSG(2,"loading LibSVM model for instance '%s' ...",getInstName()); if((model=svm_load_model(modelfile))==0) { COMP_ERR("can't open libSVM model file '%s'",modelfile); } nClasses = svm_get_nr_class(model); svmType = svm_get_svm_type(model); if(predictProbability) { if ((svmType==NU_SVR) || (svmType==EPSILON_SVR)) { nClasses = 0; SMILE_MSG(2,"LibSVM prob. model (regression) for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g",svm_get_svr_probability(model)); } else { labels=(int *) malloc(nClasses*sizeof(int)); svm_get_labels(model,labels); SMILE_MSG(3,"LibSVM %i labels in model '%s':",nClasses,modelfile); int j; for(j=0;j<nClasses;j++) SMILE_MSG(3," Label[%i] : '%d'",j,labels[j]); } } //?? move this in front of above if() block ? if ((predictProbability)&&(nClasses>0)) probEstimates = (double *) malloc(nClasses*sizeof(double)); // load scale if((scale=svm_load_scale(scalefile))==0) { COMP_ERR("can't open libSVM scale file '%s'",scalefile); } // load selection loadSelection(fselection); //TODO: check compatibility of getLevelN() (possibly after selection), number of features in model, and scale if (nClasses>0) { // load class mapping loadClasses(classes); } else { if (classes != NULL) SMILE_IWRN(2,"not loading given class mapping file for regression SVR model (there are no classes...)!"); } return ret; }
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; }
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; }
HandTranslating::HandTranslating(QObject *parent) : QThread(parent) { databaseFolderPath = "../Database/Main"; modelFile = "databaseMain.model"; string model_file_name = databaseFolderPath + "/" + modelFile; if((model=svm_load_model(model_file_name.c_str()))==0) { STOP = true; cout << "ko mo duoc model\n"; } else { svm_type = svm_get_svm_type(model); nr_class = svm_get_nr_class(model); labels=(int *) malloc(nr_class*sizeof(int)); svm_get_labels(model,labels); prob_estimates = (double *) malloc(nr_class * sizeof(double)); STOP = false; enableToTranslate = false; } }
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); }
double binary_class_cross_validation(const svm_problem *prob, const svm_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 svm_problem subprob; subprob.l = l-(end-begin); subprob.x = Malloc(struct svm_node*,subprob.l); subprob.y = Malloc(double,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 svm_model *submodel = svm_train(&subprob,param); int svm_type = svm_get_svm_type(submodel); if(svm_type == NU_SVR || svm_type == EPSILON_SVR){ fprintf(stderr, "wrong svm type"); exit(1); } labels = Malloc(int, svm_get_nr_class(submodel)); svm_get_labels(submodel, labels); if(svm_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++) { svm_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; } svm_free_and_destroy_model(&submodel); free(subprob.x); free(subprob.y); free(labels); } free(perm); free(fold_start); }
void binary_class_predict(FILE *input, FILE *output){ int total = 0; int *labels; int max_nr_attr = 64; struct svm_node *x = Malloc(struct svm_node, max_nr_attr); dvec_t dec_values; ivec_t true_labels; int svm_type=svm_get_svm_type(model); if (svm_type==NU_SVR || svm_type==EPSILON_SVR){ fprintf(stderr, "wrong svm type."); exit(1); } labels = Malloc(int, svm_get_nr_class(model)); svm_get_labels(model, 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 - 2) // 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; predict_label = svm_predict(model,x); fprintf(output,"%g\n",predict_label); double dec_value; svm_predict_values(model, x, &dec_value); true_labels.push_back((target_label > 0)? 1: -1); if(labels[0] <= 0) dec_value *= -1; dec_values.push_back(dec_value); } // validation_function(dec_values, true_labels); accuracy(dec_values, true_labels); bac(dec_values, true_labels); free(labels); free(x); }
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); }
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; }
void predict(FILE *input, FILE *output) { int correct = 0; int total = 0; double error = 0, brier_score = 0, log_likelihood = 0; double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; int svm_type=svm_get_svm_type(model); int nr_class=svm_get_nr_class(model); int *labels=(int *) malloc(nr_class*sizeof(int)); 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 { 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"); } } while(1) { int i = 0; int c; double target,v; if (fscanf(input,"%lf",&target)==EOF) break; 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)); } do { c = getc(input); if(c=='\n' || c==EOF) goto out2; } while(isspace(c)); ungetc(c,input); fscanf(input,"%d:%lf",&x[i].index,&x[i].value); ++i; } out2: x[i++].index = -1; if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC)) { v = svm_predict_probability(model,x,prob_estimates, predict_probability); fprintf(output,"%g ",v); for(j=0;j<nr_class;j++) fprintf(output,"%g ",prob_estimates[j]); fprintf(output,"\n"); } else { v = svm_predict(model,x); fprintf(output,"%g\n",v); } if(v == target) ++correct; error += (v-target)*(v-target); sumv += v; sumy += target; sumvv += v*v; sumyy += target*target; sumvy += v*target; ++total; //Brier Score and log likelihood if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC)) { int j; double bscore=0; for( j=0; j<nr_class && target != labels[j]; j++) ; if(j < nr_class) { for(int i=0; i<nr_class; i++) bscore += prob_estimates[i]*prob_estimates[i]; bscore = (bscore - 2 * prob_estimates[j] + 1)/nr_class; brier_score += bscore; log_likelihood += log(prob_estimates[j]); } } } printf("Accuracy = %g%% (%d/%d) (classification)\n", (double)correct/total*100,correct,total); 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)) ); if(predict_probability) { printf("Brier score = %g\n", brier_score/total); printf("Normalized log likelihood = %g\n", log_likelihood/total); free(prob_estimates); free(labels); } }
int SVM::test(char* data_filename, char* filename_model, double &dTestError) { int correct = 0; int total = 0; double error = 0; double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0; struct svm_model* svmModel; int svm_type; int nr_class; double *prob_estimates=NULL; FILE *fInputTestData = NULL; int max_line_len = 0; char *line = NULL; int max_nr_attr = 64; struct svm_node *x = NULL; fInputTestData = fopen(data_filename,"r"); if(fInputTestData == NULL) { fprintf(stderr,"can't open input file %s\n",data_filename); return -1; } if((svmModel=svm_load_model(filename_model))==0) { fprintf(stderr,"can't open model file %s\n",filename_model); return -3; } svm_type=svm_get_svm_type(svmModel); nr_class=svm_get_nr_class(svmModel); 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(svmModel)); else { int *labels=(int *) malloc(nr_class*sizeof(int)); svm_get_labels(svmModel,labels); prob_estimates = (double *) malloc(nr_class*sizeof(double)); //printf("labels"); //for(j=0;j<nr_class;j++) //printf(" %d",labels[j]); //printf("\n"); free(labels); } max_line_len = 1024; while((line = this->read_line(fInputTestData, max_line_len)) != 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) { fprintf(stderr,"Wrong input format at line %d\n", total+1); return -2; } x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node)); 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) { fprintf(stderr,"Wrong input format at line %d\n", total+1); return -2; } else inst_max_index = x[i].index; errno = 0; x[i].value = strtod(val,&endptr); if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) { fprintf(stderr,"Wrong input format at line %d\n", total+1); return -2; } ++i; } x[i].index = -1; if (svm_type==C_SVC || svm_type==NU_SVC) { predict_label = svm_predict_probability(svmModel,x,prob_estimates); //printf("%g",predict_label); //for(j=0;j<nr_class;j++) // printf(" %g",prob_estimates[j]); //printf("\n"); } else { predict_label = svm_predict(svmModel,x); // printf("%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; free(line); 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*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)) ); } else { printf("Classification Accuracy = %g%% (%d/%d)\n", (double)correct/total*100,correct,total); } dTestError = 1-((double)correct/(double)total); fclose(fInputTestData); free(prob_estimates); svm_destroy_model(svmModel); return 0; }
v8::Local<v8::Object> SVMPredict::predict_single ( const struct svm_model* m, const struct svm_node* p, v8::Local<v8::Value> id, int nr) { #if defined(PROCESS_DEBUG) log("is called."); #endif if (m==NULL) { std::cerr<<"The model variable is NULL\n"; } if (p == NULL) { std::cerr<<"The query vector variable is NULL\n"; } // v8::Isolate* isolate = GetIsolate();//v8::Isolate::GetCurrent(); // v8::HandleScope scope(isolate); // v8::Local<v8::Context> context = // v8::Local<v8::Context>::New(isolate,context_); // v8::Context::Scope context_scop(context); v8::Local<v8::Object> Result = v8::Object::New(isolate); double dec_values = std::numeric_limits<double>::infinity(); double pred_result = std::numeric_limits<double>::infinity(); double *probab_estimate = (double *) malloc(nr*sizeof(double)); int *labels=(int *) malloc(nr*sizeof(int)); svm_get_labels(this->model,labels); #if defined(PROCESS_DEBUG) // CW_ASSERT(m==NULL); // CW_ASSERT(p==NULL); log(" Ready to call local function predict"); #endif //this->predict(m,p,&dec_values,&pred_result); //this->print_node(p); pred_result = svm_predict_values(m, p, &dec_values); Result->Set(v8::String::NewFromUtf8(isolate, "id"),id); Result->Set(v8::String::NewFromUtf8(isolate, "dec_val"),ToV8<double>(dec_values)); Result->Set(v8::String::NewFromUtf8(isolate, "predict"),ToV8<double>(pred_result)); if (this->predict_probability) { #if defined(PROCESS_DEBUG) //CW_ASSERT(m==NULL); //CW_ASSERT(p==NULL); log(" Ready to call local function predict_prob"); #endif this->predict_prob(m, p, &pred_result, probab_estimate); //pred_result = svm_predict_probability(this->model,p,probab_estimate); v8::Local<v8::Object> prob = v8::Object::New(isolate); for(int i=0;i<nr;i++){ std::string cl = _toString<int>(labels[i],std::dec); prob->Set(v8::String::NewFromUtf8(isolate, cl.c_str()),ToV8<double>(probab_estimate[i])); } Result->Set(v8::String::NewFromUtf8(isolate, "probability"),prob); } return Result; }
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; // This block by Jianxin Wu, for average accuracy computation int ii,label_index; // number of correct predictions in each category int* correct_sub = (int *)malloc(nr_class*sizeof(int)); for(ii=0;ii<nr_class;ii++) correct_sub[ii] = 0; // number of testing examples in each category int* total_sub = (int *)malloc(nr_class*sizeof(int)); for(ii=0;ii<nr_class;ii++) total_sub[ii] = 0; int* labels_avg = (int*)malloc(nr_class*sizeof(int)); svm_get_labels(model,labels_avg); 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); } // This block by Jianxin Wu, for average accuracy label_index = FindLabel((int)target_label,labels_avg); total_sub[label_index]++; if(predict_label == target_label) correct_sub[label_index]++; 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); // This block (till endo of function) by Jianxin WU // Print per-category accuracy and average accuracy of categories double sub_score = 0; int nonempty_category = 0; for(ii=0;ii<nr_class;ii++) { if(total_sub[ii]>0) { sub_score += (correct_sub[ii]*1.0/total_sub[ii]); nonempty_category++; } } printf("-----------\n"); for(ii=0;ii<nr_class;ii++) { printf("%d / %d (Category %d)\n",correct_sub[ii],total_sub[ii],labels_avg[ii]); } printf("-----------\n"); printf("Mean Accuray across classes = %g%%\n",sub_score*100.0/nonempty_category); free(correct_sub); free(total_sub); free(labels_avg); }
int predict(FILE *input , FILE *output) { int correct =0; int total = 0; int error = 0; double sump = 0, sumt = 0, sumpt = 0, sumpp = 0, sumtt = 0; int svm_type=svm_get_svm_type(model); int nr_class=svm_get_nr_class(model); double *prob_estimate = NULL; int j; if(predict_probability) { 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); } max_line_len = 1024; line = (char *)malloc(max_line_len*sizeof(char)); while(readline(input) != NULL) { int i =0; double target_label, predict_label,idVal; char *idx, *val, *label, *endptr; int inst_max_index = -1; label = strtok(line, " \t\n"); if(label == NULL) { exit_input_error_predict(total+1); } target_label = strtod(label, &endptr); //if(*endptr =='\0') if(endptr == label|| *endptr !='\0') { exit_input_error_predict(total +1); } while(1) { if(i >= max_nr_attr-1) { max_nr_attr *=2; x =(struct svm_node *) realloc(x, max_nr_attr*sizeof(struct svm_node)); } //LOGD("line = %s ",line); //idVal = strtod(line,&endptr); //LOGD("endptr = %s", endptr); 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) if(endptr ==idx ||errno !=0 || *endptr !='\0'||x[i].index <=inst_max_index) { exit_input_error_predict(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))) if(endptr == val || errno !=0||(*endptr !='\0'&& !isspace(*endptr))) { exit_input_error_predict(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_estimate); fprintf(output,"%g", predict_label); for (j = 0; j < nr_class; j++) { fprintf(output,"%g",prob_estimate[j]); } fprintf(output,"\n"); } else { //LOGD(" CAI DECK"); 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(predict_probability) free(prob_estimate); }