home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / BarlaA / sw / OLD / Simo / SVM / patt_rec.c < prev    next >
C/C++ Source or Header  |  2002-06-25  |  5KB  |  214 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "patt_rec.h"
  6. #include "nrutil.h"
  7.  
  8. int    dim;
  9. int    degree;
  10. double    normalization, sigma;
  11. int     delta_g, delta_r, delta_c;
  12. int     dim_r, dim_c;
  13.  
  14. help_message()
  15. {
  16.   fprintf(stderr, "usage: patt_rec -i sv_file \n");
  17.   fprintf(stderr, "                -t test_file\n");
  18.   fprintf(stderr, "                -o out_file\n");
  19.   fprintf(stderr, "                -c class (e-->esterni; i-->interni)\n");
  20.   fprintf(stderr, "                -l bias  (b+ls) (default l=0)\n");
  21.   exit(-1);
  22. }
  23.  
  24. void ReadParameters(char *filename) {
  25.   FILE *fp;
  26.  
  27.   fp = fopen(filename,"r");
  28.   fscanf(fp,"%d %d ",&dim_r, &dim_c);
  29.   fscanf(fp,"%d %d %d ",&delta_g, &delta_r, &delta_c);
  30. }
  31.  
  32.  
  33. main(int argc, char *argv[])
  34. {
  35.     int          contatot,contauno,contamenouno,knl, i, j, k, nsv, isv, np, ntest, *class, *test_val; 
  36.     int       chiudifp3;
  37.     char        *knl_t, sv_file[256], test_file[256], cla, out_file[256];
  38.     double        *x_vec, *sv, bee, *alpha, c_const, kernel(), aux,percent; 
  39.      float     ls;
  40.     FILE        *fp1, *fp2, *fp3, *fpo;
  41.  
  42.     strcpy(sv_file, "NO_SV");
  43.     strcpy(test_file, "NO_TEST");
  44.     strcpy(&cla, "NO_CLASS");
  45.     strcpy(out_file, "NO_OUT");
  46.     ls=0.;
  47.     
  48.     for (k = 1; k < argc; k++)
  49.         if (*argv[k] == '-')
  50.             switch (*(argv[k] + 1)) {
  51.             case 'i':
  52.               if (sscanf(argv[++k], "%s", sv_file) == NULL)
  53.                 exit(-1);
  54.               break;
  55.             case 't':
  56.               if (sscanf(argv[++k], "%s", test_file) == NULL)
  57.                 exit(-1);
  58.               break; 
  59.             case 'c': 
  60.               if (sscanf(argv[++k], "%c", &cla) == NULL) 
  61.                 exit(-1); 
  62.               break; 
  63.             case 'o':
  64.               if (sscanf(argv[++k], "%s", out_file) == NULL)
  65.                 exit(-1);
  66.               break;
  67.               /*********************MIA AGGIUNTA*******************************/ 
  68.             case 'l': 
  69.               if (sscanf(argv[++k], "%f", &ls) == NULL) 
  70.                 exit(-1); 
  71.               break; 
  72.               /****************FINE MIA AGGIUNTA*******************************/
  73.             case 'h':    
  74.               help_message();
  75.               break;
  76.             default:
  77.               help_message();
  78.               exit(-1);
  79.             }
  80.     if (!strcmp(sv_file, "NO_SV")) 
  81.       {
  82.         fprintf(stderr, "missing sv file\n");
  83.         help_message();
  84.         exit(-1);
  85.       }
  86.     if (!strcmp(test_file, "NO_TEST")) 
  87.       {
  88.         fprintf(stderr, "missing sv file\n");
  89.         help_message();
  90.         exit(-1);
  91.       }
  92.     if (!strcmp(&cla, "NO_CLASS")) 
  93.       {
  94.         fprintf(stderr, "missing sv file\n");
  95.         help_message();
  96.         exit(-1);
  97.       }
  98.     if (!strcmp(test_file, "NO_OUT")) 
  99.       {
  100.         fprintf(stderr, "missing out file\n");
  101.         help_message();
  102.         exit(-1);
  103.       }
  104.     contauno = contamenouno = 0; 
  105.     fp1 = fopen(sv_file, "r");    /* SV file */
  106.     fp2 = fopen(test_file, "r");      /* Test file (with reference values) */
  107.     
  108.     /* reading support vector file */
  109.     
  110.     fscanf (fp1, "%d %d", &nsv, &dim);
  111.     alpha = (double *)malloc (nsv*sizeof(double));
  112.     class = (int *)malloc (nsv*sizeof(double));
  113.     sv = (double*)malloc (nsv*dim* sizeof(double));
  114.     
  115.     for (isv = 0; isv < nsv; isv++) {
  116.         for (i = 0; i < dim; i++)
  117.           fscanf(fp1, "%lf", &sv[isv*dim+i]);
  118.         fscanf(fp1, "%d %lf", &class[isv], &alpha[isv]);
  119.           }
  120.     fscanf(fp1, "%lf", &bee);
  121.     fscanf(fp1, "%lf", &c_const);
  122.     fscanf(fp1, "%d", &knl); 
  123.     /*********************MIA MODIFICA*******************************/ 
  124.     printf("knl = %d\tls = %f\n", knl,ls); 
  125.     /****************FINE MIA MODIFICA*******************************/ 
  126.     /*    printf("knl = %d\n", knl);*/
  127.     switch (knl) {
  128.         case LINEAR_KERNEL:
  129.       fscanf(fp1, "%lf", &normalization);    
  130.       break;
  131.     case POLY_KERNEL:
  132.       fscanf(fp1, "%lf %d", &normalization, °ree);    
  133.       break;
  134.         case RBF_KERNEL:
  135.       fscanf(fp1, "%lf", &sigma);
  136.       break; 
  137.       /*********************MIA AGGIUNTA*******************************/ 
  138.     case MV_KERNEL: 
  139.       fscanf(fp1, "%lf", &sigma); 
  140.       break; 
  141.       /****************FINE MIA AGGIUNTA*******************************/
  142.         default:
  143.         help_message();
  144.         exit(UNKNOWN_KERNEL);
  145.       }
  146.     fclose(fp1); 
  147.     
  148.     /* reading test file */
  149.     
  150.     fscanf(fp2, "%d %d", &ntest, &dim) ;
  151.     x_vec = (double*)malloc(ntest*dim*sizeof(double));
  152.     test_val = (int*)malloc(ntest*sizeof(double));
  153.     
  154.     for (i = 0; i < ntest; i++) {
  155.       for (j = 0; j < dim; j++)
  156.         fscanf(fp2, "%lf", &x_vec[i * dim + j]);
  157.       /*    fscanf(fp2, "%d", &test_val[i]);*/
  158.     }
  159.     fclose(fp2);
  160.     
  161.  
  162.     fpo = fopen(out_file, "w");
  163.     /* pattern classification */ 
  164.     fprintf(fpo, "%d\n",ntest);
  165.     for(i = 0; i < ntest; i++) {
  166.       aux = bee;
  167.       for (isv = 0; isv < nsv; isv++)
  168.         aux += (class[isv] * alpha[isv] * kernel(knl, &sv[isv*dim], &x_vec[i*dim])); 
  169.       /*********************MIA MODIFICA*******************************/ 
  170.       if (aux > ls) 
  171.         /****************FINE MIA MODIFICA*******************************/
  172.         /*    if (aux > 0)*/ 
  173.         {
  174.           fprintf(stdout, "1 %lf\n", aux);  
  175.           fprintf(fpo, "%lf\n", aux);  
  176.           contauno++; 
  177.         }
  178.       else 
  179.         {
  180.           fprintf(stdout, "-1 %lf\n", aux);
  181.           fprintf(fpo, "%lf\n", aux);
  182.           contamenouno++; 
  183.         }
  184.     } 
  185.     contatot = contauno + contamenouno; 
  186.     percent = (contauno * 100.) / contatot; 
  187.     if(cla=='i') 
  188.       { 
  189.         fprintf(stdout,"%lf\n",100-percent); 
  190.         
  191.       } 
  192.        else 
  193.          { 
  194.            fprintf(stdout,"%lf\n",(percent)); 
  195.          }    
  196.     printf("uno = %d\tmenouno = %d\npercentouno = %lf\tpercentomenouno = %lf\n",contauno,contamenouno,percent,(100-percent));
  197.     printf("contatot= %d\n", contatot);
  198.     fclose(fpo);
  199.       }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.