Definition at line 2441 of file svm.cpp. References svm_parameter::coef0, svm_parameter::degree, svm_parameter::gamma, svm_parameter::kernel_type, svm_model::l, svm_model::label, Malloc, svm_model::nr_class, svm_model::nSV, svm_model::param, svm_model::probA, svm_model::probB, svm_model::rho, svm_model::SV, svm_model::sv_coef, and svm_parameter::svm_type. Referenced by Impala::Core::Trec::TrecSVM::LoadModel(), and Impala::Core::Training::Svm::LoadModel(). 02442 { 02443 FILE *fp = fopen(model_file_name,"rb"); 02444 if(fp==NULL) return NULL; 02445 02446 // read parameters 02447 02448 svm_model *model = Malloc(svm_model,1); 02449 svm_parameter& param = model->param; 02450 model->rho = NULL; 02451 model->probA = NULL; 02452 model->probB = NULL; 02453 model->label = NULL; 02454 model->nSV = NULL; 02455 02456 char cmd[81]; 02457 while(1) 02458 { 02459 fscanf(fp,"%80s",cmd); 02460 02461 if(strcmp(cmd,"svm_type")==0) 02462 { 02463 fscanf(fp,"%80s",cmd); 02464 int i; 02465 for(i=0;svm_type_table[i];i++) 02466 { 02467 if(strcmp(svm_type_table[i],cmd)==0) 02468 { 02469 param.svm_type=i; 02470 break; 02471 } 02472 } 02473 if(svm_type_table[i] == NULL) 02474 { 02475 fprintf(stderr,"unknown svm type.\n"); 02476 free(model->rho); 02477 free(model->label); 02478 free(model->nSV); 02479 free(model); 02480 return NULL; 02481 } 02482 } 02483 else if(strcmp(cmd,"kernel_type")==0) 02484 { 02485 fscanf(fp,"%80s",cmd); 02486 int i; 02487 for(i=0;kernel_type_table[i];i++) 02488 { 02489 if(strcmp(kernel_type_table[i],cmd)==0) 02490 { 02491 param.kernel_type=i; 02492 break; 02493 } 02494 } 02495 if(kernel_type_table[i] == NULL) 02496 { 02497 fprintf(stderr,"unknown kernel function.\n"); 02498 free(model->rho); 02499 free(model->label); 02500 free(model->nSV); 02501 free(model); 02502 return NULL; 02503 } 02504 } 02505 else if(strcmp(cmd,"degree")==0) 02506 fscanf(fp,"%lf",¶m.degree); 02507 else if(strcmp(cmd,"gamma")==0) 02508 fscanf(fp,"%lf",¶m.gamma); 02509 else if(strcmp(cmd,"coef0")==0) 02510 fscanf(fp,"%lf",¶m.coef0); 02511 else if(strcmp(cmd,"nr_class")==0) 02512 fscanf(fp,"%d",&model->nr_class); 02513 else if(strcmp(cmd,"total_sv")==0) 02514 fscanf(fp,"%d",&model->l); 02515 else if(strcmp(cmd,"rho")==0) 02516 { 02517 int n = model->nr_class * (model->nr_class-1)/2; 02518 model->rho = Malloc(double,n); 02519 for(int i=0;i<n;i++) 02520 fscanf(fp,"%lf",&model->rho[i]); 02521 } 02522 else if(strcmp(cmd,"label")==0) 02523 { 02524 int n = model->nr_class; 02525 model->label = Malloc(int,n); 02526 for(int i=0;i<n;i++) 02527 fscanf(fp,"%d",&model->label[i]); 02528 } 02529 else if(strcmp(cmd,"probA")==0) 02530 { 02531 int n = model->nr_class * (model->nr_class-1)/2; 02532 model->probA = Malloc(double,n); 02533 for(int i=0;i<n;i++) 02534 fscanf(fp,"%lf",&model->probA[i]); 02535 } 02536 else if(strcmp(cmd,"probB")==0) 02537 { 02538 int n = model->nr_class * (model->nr_class-1)/2; 02539 model->probB = Malloc(double,n); 02540 for(int i=0;i<n;i++) 02541 fscanf(fp,"%lf",&model->probB[i]); 02542 } 02543 else if(strcmp(cmd,"nr_sv")==0) 02544 { 02545 int n = model->nr_class; 02546 model->nSV = Malloc(int,n); 02547 for(int i=0;i<n;i++) 02548 fscanf(fp,"%d",&model->nSV[i]); 02549 } 02550 else if(strcmp(cmd,"SV")==0) 02551 { 02552 while(1) 02553 { 02554 int c = getc(fp); 02555 if(c==EOF || c=='\n') break; 02556 } 02557 break; 02558 } 02559 else 02560 { 02561 fprintf(stderr,"unknown text in model file\n"); 02562 free(model->rho); 02563 free(model->label); 02564 free(model->nSV); 02565 free(model); 02566 return NULL; 02567 } 02568 } 02569 02570 // read sv_coef and SV 02571 02572 int elements = 0; 02573 long pos = ftell(fp); 02574 02575 while(1) 02576 { 02577 int c = fgetc(fp); 02578 switch(c) 02579 { 02580 case '\n': 02581 // count the '-1' element 02582 case ':': 02583 ++elements; 02584 break; 02585 case EOF: 02586 goto out; 02587 default: 02588 ; 02589 } 02590 } 02591 out: 02592 fseek(fp,pos,SEEK_SET); 02593 02594 int m = model->nr_class - 1; 02595 int l = model->l; 02596 model->sv_coef = Malloc(double *,m); 02597 int i; 02598 for(i=0;i<m;i++) 02599 model->sv_coef[i] = Malloc(double,l); 02600 model->SV = Malloc(svm_node*,l); 02601 svm_node *x_space = Malloc(svm_node,elements); 02602 02603 int j=0; 02604 for(i=0;i<l;i++) 02605 { 02606 model->SV[i] = &x_space[j]; 02607 for(int k=0;k<m;k++) 02608 fscanf(fp,"%lf",&model->sv_coef[k][i]); 02609 while(1) 02610 { 02611 int c; 02612 do { 02613 c = getc(fp); 02614 if(c=='\n') goto out2; 02615 } while(isspace(c)); 02616 ungetc(c,fp); 02617 fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value)); 02618 ++j; 02619 } 02620 out2: 02621 x_space[j++].index = -1; 02622 } 02623 02624 fclose(fp); 02625 02626 model->free_sv = 1; // XXX 02627 return model; 02628 }
|