lvq_run.c
来自「face recognition test source code」· C语言 代码 · 共 1,242 行 · 第 1/3 页
C
1,242 行
/* check that initialization or previous olvq1 has been done */
sprintf(l,"%s%s",c->cout,train_ext);
if (faccess(l, R_OK)==0) {
input_ext = train_ext;
} else {
sprintf(l,"%s%s",c->cout,init_ext);
if (faccess(l, R_OK)==0) {
input_ext = init_ext;
} else {
fprintf(stdout,"\nERROR: No initialization has been done for the classifier!\n");
exit(-1);
}
}
/* run the olvq1 training */
fprintf(stdout,"\nStarting olvq1 training:\n");
sprintf(l,"%solvq1 -din %s -cin %s%s -cout %s%s -rlen %ld",
prog_dir, c->din, c->cout, input_ext,
c->cout, train_ext, c->rlen);
systemh(l,c);
c->totrlen += c->rlen;
c->rlen = c->totrlen;
c->lvq_status = TRAIN;
fix_train_history(c);
/* check that the trained code file was created */
sprintf( l, "%s%s", c->cout, train_ext);
if ( faccess(l, R_OK) != 0) {
fprintf(stdout,"\nUnsuccesful training!\n");
exit(-1);
}
/* olvq1 created a file containing learning rates
for each of the codevectors. It is stored for further
use in case where you have done retraining by LVQ? (that
destroys the ".lra"-file), and you'd like to continue some
more olvq1-training. */
sprintf(l,"%s %s%s %s%s",copy_cmd, c->cout,
alpha_ext, c->cout, train_alpha_ext);
system(l);
}
}
void retrain_classifier (CLASSIFIER *c)
{
char l[CMD_LEN], *ext;
/* check that olvq1 or previous lvq has been done */
sprintf(l,"%s%s",c->cout,retrain_ext);
if (faccess(l, R_OK)==0) {
ext = retrain_ext;
} else {
sprintf(l,"%s%s",c->cout,train_ext);
if (faccess(l, R_OK)==0) {
ext = train_ext;
} else {
fprintf(stdout,"\nERROR: No training done for the classifier!\n");
exit(-1);
}
}
/* run the lvq-training */
fprintf(stdout,"\nStarting training:\n");
switch(c->rt_lvq_type) {
case 1:
sprintf(l,"%slvq1 -din %s -cin %s%s -cout %s%s -alpha %g -rlen %ld",
prog_dir, c->din, c->cout, ext,
c->cout, retrain_ext, c->rt_alpha, c->rt_rlen);
systemh(l,c);
break;
case 2:
sprintf(l,"%slvq2 -din %s -cin %s%s -cout %s%s -alpha %g -rlen %ld -win %g",
prog_dir, c->din, c->cout, ext,
c->cout, retrain_ext,
c->rt_alpha, c->rt_rlen, c->rt_win);
systemh(l,c);
break;
case 3:
sprintf(l,"%slvq3 -din %s -cin %s%s -cout %s%s -alpha %g -rlen %ld -win %g -epsilon %g",
prog_dir, c->din, c->cout, ext,
c->cout, retrain_ext,
c->rt_alpha, c->rt_rlen, c->rt_win, c->rt_epsilon);
systemh(l,c);
break;
default:
fprintf(stdout,"\nIllegal lvq-type %d\n",c->rt_lvq_type);
exit(-1);
}
/* check that the trained code file was created */
sprintf( l, "%s%s", c->cout, retrain_ext);
if ( faccess(l, R_OK) != 0) {
fprintf(stdout,"\nUnsuccesful training!\n");
exit(-1);
}
c->lvq_status = RETRAIN;
}
void test_classifier (CLASSIFIER *c)
{
char l[CMD_LEN];
char acc_name[FLEN];
FILE *facc;
char *ext;
/* check that olvq1 or previous lvq has been done */
sprintf(l,"%s%s",c->cout,retrain_ext);
if (faccess(l, R_OK)==0) {
ext = retrain_ext;
} else {
sprintf(l,"%s%s",c->cout,train_ext);
if (faccess(l, R_OK)==0) {
ext = train_ext;
} else {
fprintf(stdout,"\nERROR: No training done for the classifier!\n");
exit(-1);
}
}
fprintf(stdout,sep);
fprintf(stdout,"Starting testing:\n");
fflush(stdout);
/* redirect output of "accuracy" to a file */
sprintf(acc_name, "%s%s",c->cout, acc_ext);
remove(acc_name);
sprintf(l,"%saccuracy -din %s -cin %s%s -cfout %s%s > %s",
prog_dir, c->tdin, c->cout, ext, c->cout, class_ext, acc_name);
systemd(l);
/* type the file */
sprintf(l,"%s %s", type_cmd, acc_name);
system(l);
/* read the final recognition accuracy from the file */
facc = fopen(acc_name,"r");
do fscanf(facc,"%s",l); while (strcmp(l,"Total"));
fscanf(facc,"%s",l);
fscanf(facc,"%s",l);
fscanf(facc,"%s",l);
fscanf(facc,"%f", &(c->accuracy) );
fclose(facc);
}
void compare_classifiers (CLASSIFIER *c1, CLASSIFIER *c2)
{
char cif1[FLEN], cif2[FLEN], l[CMD_LEN];
/* check that both classifiers have same test files
and classification information files have been created
for both of them */
if ( strcmp(c1->tdin,c2->tdin) != 0 ) {
fprintf(stdout,"\nClassifiers have been tested with different files %s and %s!\n",
c1->tdin,c2->tdin);
return;
}
sprintf(cif1,"%s%s",c1->cout,class_ext);
if ( faccess(cif1,R_OK) != 0 ) {
fprintf(stdout,"\nCannot read classification information file %s!\n",
cif1);
test_classifier(c1);
}
sprintf(cif2,"%s%s",c2->cout,class_ext);
if ( faccess(cif2,R_OK) != 0 ) {
fprintf(stdout,"\nCannot read classification information file %s!\n",
cif2);
test_classifier(c2);
}
/* run McNemar's test */
fprintf(stdout,sep);
sprintf(l,"%smcnemar %s %s",prog_dir, cif1,cif2);
systemd(l);
}
/*---------------------------------------------------------------------------*/
void main(int argc, char **argv)
{
int i,j, status;
char l[CMD_LEN];
int opt;
int nocl;
static CLASSIFIER c[MAX_NUM_CLASSIFIERS];
char *cpo;
#ifdef MSDOS
prog_dir = ostrdup(argv[0]);
cpo = strrchr(prog_dir, '\\');
if (cpo != NULL) {
*(cpo+1) = (char) NULL;
}
else {
prog_dir = ostrdup(".\\");
}
#else
prog_dir = ostrdup(argv[0]);
cpo = strrchr(prog_dir, '/');
if (cpo != NULL) {
*(cpo+1) = (char) NULL;
}
else {
prog_dir = ostrdup("./");
}
#endif
silent((int) oatoi(extract_parameter(argc, argv, SILENT, OPTION), 0));
fprintf(stdout,introMsg0);
fprintf(stdout,introMsg1);
fprintf(stdout,"\nPress enter to continue.");
getsb(l);
if (argc > 1) {
nocl = retrieve_classifiers(argc,argv,c);
} else {
nocl = 0;
}
do {
fprintf(stdout,sep);
if (nocl==0)
fprintf(stdout,"You don't have any classifiers yet. Start by option 1.");
else if (nocl==1)
fprintf(stdout,"You have now 1 classifier. Do you want to:");
else
fprintf(stdout,"You have now %d classifiers. Do you want to:",nocl);
fprintf(stdout,"\n 0 -> Quit and save current classifiers.");
fprintf(stdout,"\n 1 -> Create a completely new classifier from scratch. ");
fprintf(stdout,"\n 2 -> Create a new classifier by copying the parameters of an old one.");
fprintf(stdout,"\n Use option 3 thereafter to modify the new classifier.");
fprintf(stdout,"\n 3 -> Modify the parameters of a classifier and train it. You can modify as");
fprintf(stdout,"\n many or as few parameters as you wish. However, if you have done repeated");
fprintf(stdout,"\n fine-tuning, only the parameters of the latest one are in memory and");
fprintf(stdout,"\n modifiable. All previous repeated cycles of fine-tuning are then replaced");
fprintf(stdout,"\n by this new cycle with modified parameters.");
fprintf(stdout,"\n 4 -> Fine-tune a classifier by using LVQ1, LVQ2.1, or LVQ3.");
fprintf(stdout,"\n You can repeat this step as many times as you wish.");
fprintf(stdout,"\n 5 -> Delete a classifier.");
fprintf(stdout,"\n 6 -> View the parameters of a classifier.");
fprintf(stdout,"\n 7 -> Compare whether two classifiers tested with the same data have any");
fprintf(stdout,"\n statistically significant difference.");
fprintf(stdout,"\n Enter your choice --> ");
opt=0; getsb(l); sscanf(l, "%d", &opt);
switch (opt) {
case 1:
default_classifier(&c[nocl]);
read_classifier_parameters(&c[nocl]);
read_classifier_file(&c[nocl]);
init_classifier(&c[nocl]);
train_classifier(&c[nocl]);
test_classifier(&c[nocl]);
nocl++;
break;
case 2:
if (nocl<1) {
fprintf(stdout,"\nNo classifiers to be copied.");
break;
} else if (nocl==1)
i=1;
else {
fprintf(stdout,"\nEnter the classifier to be copied [1..%d]: ",nocl);
getsb(l); sscanf(l, "%d", &i);
}
memcpy(&c[nocl],&c[i-1],sizeof(CLASSIFIER));
read_classifier_file(&c[nocl]);
copy_history(&c[nocl],&c[i-1]);
/* duplicate all the associated files, too */
copy_classifier_files( c[i-1].cout, c[nocl].cout );
nocl++;
break;
case 3:
if (nocl<1) {
fprintf(stdout,"\nNo classifiers to be replaced.");
break;
} else if (nocl==1)
i=1;
else {
fprintf(stdout,"\nEnter the classifier to be replaced [1..%d]: ",nocl);
getsb(l); sscanf(l, "%d", &i);
}
status = c[i-1].lvq_status;
read_classifier_parameters(&c[i-1]);
init_classifier(&c[i-1]);
train_classifier(&c[i-1]);
if (status==RETRAIN) {
sprintf(l,"%s%s",c[i-1].cout,retrain_ext);
remove(l);
decrease_lvq_status(&c[i-1], TRAIN);
fprintf(stdout,"\nThe previous classifier was fine-tuned.");
fprintf(stdout,"\nFine-tune this one, too? [y/n] (default=n) ");
getsb(l);
if (*l == 'y') {
read_retrain_parameters(&c[i-1], 0);
retrain_classifier(&c[i-1]);
}
}
test_classifier(&c[i-1]);
break;
case 4:
if (nocl<1) {
fprintf(stdout,"\nNo classifiers to be retrained.");
break;
} else if (nocl==1)
i=1;
else {
fprintf(stdout,"\nEnter the classifier [1..%d]: ",nocl);
getsb(l); sscanf(l, "%d", &i);
}
read_retrain_parameters(&c[i-1], 1);
retrain_classifier(&c[i-1]);
test_classifier(&c[i-1]);
break;
case 5:
if (nocl<1) {
fprintf(stdout,"\nNo classifiers to be deleted.");
break;
} else if (nocl==1)
i=1;
else {
fprintf(stdout,"\nEnter the classifier to be deleted [1..%d]: ",nocl);
getsb(l); sscanf(l, "%d", &i);
}
remove_classifier_files( c[i-1].cout );
for (j=i; j<nocl; j++) memcpy(&c[j-1],&c[j],sizeof(CLASSIFIER));
nocl--;
break;
case 6:
if (nocl<1) {
fprintf(stdout,"\nNo classifiers to be viewed.");
break;
} else if (nocl==1)
i=1;
else {
fprintf(stdout,"\nEnter the classifier [1..%d]: ",nocl);
getsb(l); sscanf(l, "%d", &i);
}
print_classifier(stdout,&c[i-1]);
break;
case 7:
if (nocl<1) {
fprintf(stdout,"\nCannot compare less than two classifiers!\n");
break;
} else if (nocl==2) {
i=1; j=2;
} else {
fprintf(stdout,"\nEnter the 1st classifier [1..%d]: ",nocl);
getsb(l); sscanf(l, "%d", &i);
fprintf(stdout,"Enter the 2nd classifier [1..%d]: ",nocl);
getsb(l); sscanf(l, "%d", &j);
}
compare_classifiers( &c[i-1], &c[j-1] );
break;
case 0:
if (nocl) {
FILE *f;
fprintf(stdout,"\nAs the result of this session of lvq_run,");
fprintf(stdout,"\nthe following classifiers remain on disk:");
for (i=0; i<nocl; i++) {
sprintf(l,"%s%s",c[i].cout,log_ext);
f=fopen(l,"w");
if (f!=NULL) {
fprintf(stdout,"\n %s",c[i].cout);
print_classifier(f,&c[i]);
fclose(f);
} else {
fprintf(stdout,"\nERROR: Couldn't write to %s.\n",l);
}
}
fprintf(stdout,"\n\nYou can read in the stored classifiers by starting lvq_run as follows:");
fprintf(stdout,"\n>> lvq_run classifier1 classifier2 ... classifier10");
fprintf(stdout,"\nDo not enter any extensions to classifier filenames, just the baseforms.");
}
default:
break;
}
} while (opt);
fprintf(stdout,"\n\n");
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?