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 >
Wrap
C/C++ Source or Header
|
2002-06-25
|
5KB
|
214 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "patt_rec.h"
#include "nrutil.h"
int dim;
int degree;
double normalization, sigma;
int delta_g, delta_r, delta_c;
int dim_r, dim_c;
help_message()
{
fprintf(stderr, "usage: patt_rec -i sv_file \n");
fprintf(stderr, " -t test_file\n");
fprintf(stderr, " -o out_file\n");
fprintf(stderr, " -c class (e-->esterni; i-->interni)\n");
fprintf(stderr, " -l bias (b+ls) (default l=0)\n");
exit(-1);
}
void ReadParameters(char *filename) {
FILE *fp;
fp = fopen(filename,"r");
fscanf(fp,"%d %d ",&dim_r, &dim_c);
fscanf(fp,"%d %d %d ",&delta_g, &delta_r, &delta_c);
}
main(int argc, char *argv[])
{
int contatot,contauno,contamenouno,knl, i, j, k, nsv, isv, np, ntest, *class, *test_val;
int chiudifp3;
char *knl_t, sv_file[256], test_file[256], cla, out_file[256];
double *x_vec, *sv, bee, *alpha, c_const, kernel(), aux,percent;
float ls;
FILE *fp1, *fp2, *fp3, *fpo;
strcpy(sv_file, "NO_SV");
strcpy(test_file, "NO_TEST");
strcpy(&cla, "NO_CLASS");
strcpy(out_file, "NO_OUT");
ls=0.;
for (k = 1; k < argc; k++)
if (*argv[k] == '-')
switch (*(argv[k] + 1)) {
case 'i':
if (sscanf(argv[++k], "%s", sv_file) == NULL)
exit(-1);
break;
case 't':
if (sscanf(argv[++k], "%s", test_file) == NULL)
exit(-1);
break;
case 'c':
if (sscanf(argv[++k], "%c", &cla) == NULL)
exit(-1);
break;
case 'o':
if (sscanf(argv[++k], "%s", out_file) == NULL)
exit(-1);
break;
/*********************MIA AGGIUNTA*******************************/
case 'l':
if (sscanf(argv[++k], "%f", &ls) == NULL)
exit(-1);
break;
/****************FINE MIA AGGIUNTA*******************************/
case 'h':
help_message();
break;
default:
help_message();
exit(-1);
}
if (!strcmp(sv_file, "NO_SV"))
{
fprintf(stderr, "missing sv file\n");
help_message();
exit(-1);
}
if (!strcmp(test_file, "NO_TEST"))
{
fprintf(stderr, "missing sv file\n");
help_message();
exit(-1);
}
if (!strcmp(&cla, "NO_CLASS"))
{
fprintf(stderr, "missing sv file\n");
help_message();
exit(-1);
}
if (!strcmp(test_file, "NO_OUT"))
{
fprintf(stderr, "missing out file\n");
help_message();
exit(-1);
}
contauno = contamenouno = 0;
fp1 = fopen(sv_file, "r"); /* SV file */
fp2 = fopen(test_file, "r"); /* Test file (with reference values) */
/* reading support vector file */
fscanf (fp1, "%d %d", &nsv, &dim);
alpha = (double *)malloc (nsv*sizeof(double));
class = (int *)malloc (nsv*sizeof(double));
sv = (double*)malloc (nsv*dim* sizeof(double));
for (isv = 0; isv < nsv; isv++) {
for (i = 0; i < dim; i++)
fscanf(fp1, "%lf", &sv[isv*dim+i]);
fscanf(fp1, "%d %lf", &class[isv], &alpha[isv]);
}
fscanf(fp1, "%lf", &bee);
fscanf(fp1, "%lf", &c_const);
fscanf(fp1, "%d", &knl);
/*********************MIA MODIFICA*******************************/
printf("knl = %d\tls = %f\n", knl,ls);
/****************FINE MIA MODIFICA*******************************/
/* printf("knl = %d\n", knl);*/
switch (knl) {
case LINEAR_KERNEL:
fscanf(fp1, "%lf", &normalization);
break;
case POLY_KERNEL:
fscanf(fp1, "%lf %d", &normalization, °ree);
break;
case RBF_KERNEL:
fscanf(fp1, "%lf", &sigma);
break;
/*********************MIA AGGIUNTA*******************************/
case MV_KERNEL:
fscanf(fp1, "%lf", &sigma);
break;
/****************FINE MIA AGGIUNTA*******************************/
default:
help_message();
exit(UNKNOWN_KERNEL);
}
fclose(fp1);
/* reading test file */
fscanf(fp2, "%d %d", &ntest, &dim) ;
x_vec = (double*)malloc(ntest*dim*sizeof(double));
test_val = (int*)malloc(ntest*sizeof(double));
for (i = 0; i < ntest; i++) {
for (j = 0; j < dim; j++)
fscanf(fp2, "%lf", &x_vec[i * dim + j]);
/* fscanf(fp2, "%d", &test_val[i]);*/
}
fclose(fp2);
fpo = fopen(out_file, "w");
/* pattern classification */
fprintf(fpo, "%d\n",ntest);
for(i = 0; i < ntest; i++) {
aux = bee;
for (isv = 0; isv < nsv; isv++)
aux += (class[isv] * alpha[isv] * kernel(knl, &sv[isv*dim], &x_vec[i*dim]));
/*********************MIA MODIFICA*******************************/
if (aux > ls)
/****************FINE MIA MODIFICA*******************************/
/* if (aux > 0)*/
{
fprintf(stdout, "1 %lf\n", aux);
fprintf(fpo, "%lf\n", aux);
contauno++;
}
else
{
fprintf(stdout, "-1 %lf\n", aux);
fprintf(fpo, "%lf\n", aux);
contamenouno++;
}
}
contatot = contauno + contamenouno;
percent = (contauno * 100.) / contatot;
if(cla=='i')
{
fprintf(stdout,"%lf\n",100-percent);
}
else
{
fprintf(stdout,"%lf\n",(percent));
}
printf("uno = %d\tmenouno = %d\npercentouno = %lf\tpercentomenouno = %lf\n",contauno,contamenouno,percent,(100-percent));
printf("contatot= %d\n", contatot);
fclose(fpo);
}