test-GBC.C

00001 /*!@file HMAX/test-GBC.C Test Gentle Boost Component */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://dparks@isvn.usc.edu/software/invt/trunk/saliency/src/HMAX/test-hmax.C $
00035 // $Id: test-hmax.C 9412 2008-03-10 23:10:15Z farhan $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Learn/GentleBoostComponent.H"
00040 #include "Image/Image.H"
00041 #include "Image/ImageSet.H"
00042 #include "Image/Kernels.H"   // for dogFilterHmax()
00043 #include "Image/MathOps.H"
00044 #include "Image/ShapeOps.H"
00045 #include "Raster/Raster.H"
00046 #include "Util/log.H"
00047 #include "rutz/rand.h"
00048 
00049 #include <cmath>
00050 #include <dirent.h>
00051 #include <fstream>
00052 #include <iostream>
00053 #include <string>
00054 #include <vector>
00055 
00056 // Run this program:
00057 // ./bin/test-GBC --gb-model-names="test" --gb-model-outputfiles="test.dat" --gb-mode="Train"
00058 
00059 
00060 void makeData(const int numCategories, const uint sampleDim, const uint dataDim, std::vector<std::vector<float> >& data, std::vector<int>& labels, bool printData);
00061 
00062 //! Debug Main
00063 int main(int argc, char **argv)
00064 {
00065   MYLOGVERB = LOG_INFO;
00066   ModelManager *mgr = new ModelManager("Test GentleBoost Component");
00067 
00068   nub::ref<GentleBoostComponent> gbc(new GentleBoostComponent(*mgr));
00069   mgr->addSubComponent(gbc);
00070 
00071   mgr->exportOptions(MC_RECURSE);
00072 
00073   if (mgr->parseCommandLine(
00074         (const int)argc, (const char**)argv, "", 0, 0) == false)
00075     return 1;
00076 
00077 
00078   mgr->start();
00079 
00080   // 
00081   // Create log likelihood classifier and local binary patterns objects
00082   uint nDim=4;
00083   int numCategories=3;
00084   int numSamples=100;
00085 
00086   std::vector<std::vector<float> > traindata;
00087   std::vector<int> trainlabels;
00088 
00089   makeData(numCategories,numSamples,nDim,traindata,trainlabels,false);
00090   // Train the classifier on the training set
00091   for(int i=0;i<numSamples;i++)
00092     gbc->addTrainVector(traindata[i],trainlabels[i]);
00093 
00094   // Train the classifiers
00095   gbc->train();
00096 
00097   // Validate on training set
00098   int numCorrect=0;
00099   for(int i=0;i<numSamples;i++)
00100   {
00101     int predId = gbc->predict(traindata[i]);
00102     if(trainlabels[i]==predId) numCorrect++;
00103   }
00104   printf("Training Accuracy:[Correct/Total]=[%d/%Zu]:%f\n",numCorrect,trainlabels.size(),numCorrect/float(trainlabels.size()));
00105 
00106   std::vector<std::vector<float> > testdata;
00107   std::vector<int> testlabels;
00108 
00109   makeData(numCategories,numSamples,nDim,testdata,testlabels,false);
00110   // Test the classifier on the testing set
00111   numCorrect = 0;
00112   for(int i=0;i<numSamples;i++)
00113   {
00114     std::map<int,float> testPDF = gbc->predictPDF(testdata[i]);
00115     int predId = gbc->getMostLikelyClass(testPDF);
00116     if(testlabels[i]==predId) numCorrect++;
00117     printf("Guess %d [",predId);
00118     std::map<int,float>::iterator litr;
00119     for(litr=testPDF.begin();litr!=testPDF.end();litr++)
00120     {
00121       printf("(%d)%f, ",litr->first,litr->second);
00122     }
00123     printf("] *** Ground Truth %d\n",testlabels[i]);
00124   }
00125   printf("Accuracy:[Correct/Total]=[%d/%Zu]:%f\n",numCorrect,testlabels.size(),numCorrect/float(testlabels.size()));
00126 
00127   mgr->stop();
00128 
00129 
00130 }//end of main
00131 
00132 
00133 void makeData(const int numCategories, const uint sampleDim, const uint dataDim, std::vector<std::vector<float> >& data, std::vector<int>& labels, bool printData)
00134 {
00135   // Create uniform random number generator
00136   rutz::urand rgen(time((time_t*)0)+getpid());
00137   // Create data and labels
00138   data = std::vector<std::vector<float> >(sampleDim);
00139 
00140   for(uint i=0;i<data.size();i++)
00141   {
00142     int l=rgen.idraw(numCategories)+1;
00143     if(printData) printf("data[][%u]: l=%d; ",i,l);
00144     for(uint j=0;j<dataDim;j++)
00145         {
00146           data[i].push_back(rgen.fdraw_range(l-0.75,l+0.75));//*dimVarIn[j]+dimMeanIn[j]);
00147       if(printData) printf("%f, ",data[i][j]);
00148         }      
00149     if(printData) printf("\n");
00150     labels.push_back(l);
00151   }
00152 }
00153 
00154 
00155 // ######################################################################
00156 /* So things look consistent in everyone's emacs... */
00157 /* Local Variables: */
00158 /* indent-tabs-mode: nil */
00159 /* End: */
Generated on Sun May 8 08:05:19 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3