test-svm.C

Go to the documentation of this file.
00001 /*!@file Learn/test-svm.C test the svm algo
00002 */
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Learn/test-svm.C $
00036 // $Id: test-svm.C 12062 2009-11-19 15:02:09Z lior $
00037 //
00038 
00039 #include "Component/ModelManager.H"
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <ctype.h>
00044 #include "svm.h"
00045 
00046 void read_problem(const char *filename,
00047     struct svm_problem &prob,
00048     struct svm_parameter &param)
00049 {
00050   int elements, max_index, i, j;
00051   FILE *fp = fopen(filename,"r");
00052 
00053   if(fp == NULL)
00054   {
00055     fprintf(stderr,"can't open input file %s\n",filename);
00056     exit(1);
00057   }
00058 
00059   prob.l = 0;
00060   elements = 0;
00061   while(1)
00062   {
00063     int c = fgetc(fp);
00064     switch(c)
00065     {
00066       case '\n':
00067         ++prob.l;
00068         // fall through,
00069         // count the '-1' element
00070       case ':':
00071         ++elements;
00072         break;
00073       case EOF:
00074         goto out;
00075       default:
00076         ;
00077     }
00078   }
00079 out:
00080   rewind(fp);
00081 
00082   prob.y = new double[prob.l];
00083   prob.x = new struct svm_node *[prob.l];
00084   struct svm_node *x_space = new struct svm_node[elements];
00085 
00086   max_index = 0;
00087   j=0;
00088   for(i=0;i<prob.l;i++)
00089   {
00090     double label;
00091     prob.x[i] = &x_space[j];
00092     fscanf(fp,"%lf",&label);
00093     prob.y[i] = label;
00094 
00095     while(1)
00096     {
00097       int c;
00098       do {
00099         c = getc(fp);
00100         if(c=='\n') goto out2;
00101       } while(isspace(c));
00102       ungetc(c,fp);
00103       if (fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value)) < 2)
00104       {
00105         fprintf(stderr,"Wrong input format at line %d\n", i+1);
00106         exit(1);
00107       }
00108       ++j;
00109     }
00110 out2:
00111     if(j>=1 && x_space[j-1].index > max_index)
00112       max_index = x_space[j-1].index;
00113     x_space[j++].index = -1;
00114   }
00115 
00116   if(param.gamma == 0)
00117     param.gamma = 1.0/max_index;
00118 
00119   if(param.kernel_type == PRECOMPUTED)
00120     for(i=0;i<prob.l;i++)
00121     {
00122       if (prob.x[i][0].index != 0)
00123       {
00124         fprintf(stderr,"Wrong input format: first column must be 0:sample_serial_number\n");
00125         exit(1);
00126       }
00127       if ((int)prob.x[i][0].value <= 0 || (int)prob.x[i][0].value > max_index)
00128       {
00129         fprintf(stderr,"Wrong input format: sample_serial_number out of range\n");
00130         exit(1);
00131       }
00132     }
00133 
00134   fclose(fp);
00135 }
00136 
00137 void predict(const char* filename, struct svm_model *model)
00138 {
00139   int correct = 0;
00140   int total = 0;
00141   double error = 0;
00142   double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
00143   int max_nr_attr = 64;
00144   struct svm_node *x = new struct svm_node[max_nr_attr];
00145 
00146   FILE *input = fopen(filename, "r");
00147   if(input == NULL)
00148   {
00149     fprintf(stderr,"can't open testing file %s\n",filename);
00150     exit(1);
00151   }
00152   while(1)
00153   {
00154     int i = 0;
00155     int c;
00156     double target,v;
00157 
00158     if (fscanf(input,"%lf",&target)==EOF)
00159       break;
00160 
00161     while(1)
00162     {
00163       if(i>=max_nr_attr-1)        // need one more for index = -1
00164       {
00165         max_nr_attr *= 2;
00166         x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
00167       }
00168 
00169       do {
00170         c = getc(input);
00171         if(c=='\n' || c==EOF) goto out2;
00172       } while(isspace(c));
00173       ungetc(c,input);
00174       if (fscanf(input,"%d:%lf",&x[i].index,&x[i].value) < 2)
00175       {
00176         fprintf(stderr,"Wrong input format at line %d\n", total+1);
00177         exit(1);
00178       }
00179       ++i;
00180     }
00181 
00182 out2:
00183     x[i].index = -1;
00184 
00185     v = svm_predict(model,x);
00186 
00187     if(v == target)
00188       ++correct;
00189     error += (v-target)*(v-target);
00190     sumv += v;
00191     sumy += target;
00192     sumvv += v*v;
00193     sumyy += target*target;
00194     sumvy += v*target;
00195     ++total;
00196   }
00197   printf("Accuracy = %g%% (%d/%d) (classification)\n",
00198       (double)correct/total*100,correct,total);
00199 }
00200 
00201 
00202 int main(int argc, char** argv)
00203 {
00204   struct svm_parameter param;                // set by parse_command_line
00205   struct svm_problem prob;                // set by read_problem
00206 
00207   // Instantiate a ModelManager:
00208   ModelManager manager("Test SVM");
00209 
00210   // Parse command-line:
00211   if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false)
00212     return(1);
00213 
00214   manager.start();
00215 
00216   //default paramaters
00217   param.svm_type = C_SVC;
00218   param.kernel_type = RBF;
00219   param.degree = 3;
00220   param.gamma = 0;        // 1/k
00221   param.coef0 = 0;
00222   param.nu = 0.5;
00223   param.cache_size = 100;
00224   param.C = 1;
00225   param.eps = 1e-3;
00226   param.p = 0.1;
00227   param.shrinking = 1;
00228   param.probability = 0;
00229   param.nr_weight = 0;
00230   param.weight_label = NULL;
00231   param.weight = NULL;
00232 
00233 
00234   read_problem("tests/train.1.scale", prob, param);
00235 
00236   struct svm_model *model = svm_train(&prob,&param);
00237   //if((model=svm_load_model(argv[i+1]))==0)
00238   //{
00239   //        fprintf(stderr,"can't open model file %s\n",argv[i+1]);
00240   //        exit(1);
00241   //}
00242 
00243   //svm_save_model(model_file_name,model);
00244   //svm_destroy_model(model);
00245 
00246   //predict
00247 
00248   LINFO("Predicting");
00249   predict("tests/test.1.scale", model);
00250   LINFO("Done");
00251 
00252   svm_destroy_param(&param);
00253   free(prob.y);
00254   free(prob.x);
00255   //free(x_space);
00256 
00257 
00258   // stop all our ModelComponents
00259   manager.stop();
00260 
00261   // all done!
00262   return 0;
00263 }
00264 
00265 // ######################################################################
00266 /* So things look consistent in everyone's emacs... */
00267 /* Local Variables: */
00268 /* indent-tabs-mode: nil */
00269 /* End: */
Generated on Sun May 8 08:40:59 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3