test-FFN.C

Go to the documentation of this file.
00001 /*! @file Gist/test-FFN.C -- testing the FFN class                      */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: Christian Siagian <siagian@usc.edu>
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Gist/test-FFN.C $
00034 // $Id: test-FFN.C 13712 2010-07-28 21:00:40Z itti $
00035 
00036 #include "rutz/shared_ptr.h"
00037 #include "Gist/FFN.H"
00038 #include "Raster/Raster.H"
00039 #include "Image/MatrixOps.H"
00040 #include "Gist/trainUtils.H"
00041 #include <string>
00042 #include <vector>
00043 
00044 int main(const int argc, const char **argv)
00045 {
00046   LINFO("Testing with small Neural Network");
00047 
00048   // create a network with 2->3->2 architecture
00049   rutz::shared_ptr<FeedForwardNetwork> ffn(new FeedForwardNetwork());
00050 
00051   Image<double> wh(10,7, NO_INIT);
00052   wh.setVal(0,0, 1.0); wh.setVal(0,1, 1.0); wh.setVal(0,2, 1.0);
00053   wh.setVal(1,0, 1.0); wh.setVal(1,1, 1.0); wh.setVal(1,2, 1.0);
00054   wh.setVal(2,0, 1.0); wh.setVal(2,1, 1.0); wh.setVal(2,2, 1.0);
00055 
00056   Image<double> wh2(8,5, NO_INIT);
00057   wh2.setVal(0,0, 1.0); wh2.setVal(0,1, 1.0); wh2.setVal(0,2, 1.0);
00058   wh2.setVal(1,0, 1.0); wh2.setVal(1,1, 1.0); wh2.setVal(1,2, 1.0);
00059   wh2.setVal(2,0, 1.0); wh2.setVal(2,1, 1.0); wh2.setVal(2,2, 1.0);
00060 
00061   Image<double> wo(6,3, NO_INIT);
00062   wo.setVal(0,0, 1.0);  wo.setVal(0,1, 1.0);
00063   wo.setVal(1,0, 1.0);  wo.setVal(1,1, 1.0);
00064   wo.setVal(2,0, 1.0);  wo.setVal(2,1, 1.0);
00065   wo.setVal(3,0, 1.0);  wo.setVal(3,1, 1.0);
00066 
00067   ffn->init3L(wh, wh2, wo, 0.1, 0.0);
00068 
00069   // set the input
00070   Image<double> in(1,9, NO_INIT);
00071   in.setVal(0, 0, 1.0);
00072   in.setVal(0, 1, 2.0);
00073 
00074   // run it
00075   Image<double> out = ffn->run3L(in);
00076   for(int i = 0; i < out.getSize(); i++)
00077     LINFO("%d: %f", i, out.getVal(0,i));
00078 
00079   // train it
00080   Image<double> target(1,3, NO_INIT);
00081   target.setVal(0, 0, 0.5);
00082   target.setVal(0, 1, 0.2);
00083   target.setVal(0, 2, 0.7);
00084   ffn->backprop3L(target);
00085 
00086   // run it again
00087   out = ffn->run3L(in);
00088   for(int i = 0; i < out.getHeight(); i++)
00089     LINFO("%d: %f", i, out.getVal(i));
00090 
00091   // ===============================================
00092   // ===============================================
00093   Raster::waitForKey();
00094   LINFO("Now testing with provided files");
00095 
00096   rutz::shared_ptr<FeedForwardNetwork> ffn2(new FeedForwardNetwork());
00097   std::string infoFName("../data/PAMI07data/ACB_GIST_train.txt");
00098   FFNtrainInfo pcInfo(infoFName);
00099 
00100   ffn2->init3L(pcInfo.h1Name, pcInfo.h2Name, pcInfo.oName,
00101                pcInfo.redFeatSize, pcInfo.h1size, pcInfo.h2size,
00102                pcInfo.nOutput, 0.0, 0.0);
00103 
00104   // setup the PCA eigenvector
00105   Image<double> pcaVec =
00106     setupPcaIcaMatrix(pcInfo.trainFolder+pcInfo.evecFname,
00107                       pcInfo.oriFeatSize, pcInfo.redFeatSize);
00108 
00109   // open an input
00110   Image<double> oriin(1,pcInfo.oriFeatSize,NO_INIT);
00111   Image<double>::iterator aptr = oriin.beginw();
00112   FILE *gfp;
00113   std::string gfname = pcInfo.trainFolder + std::string("ACB1B_000.gist");
00114   if((gfp = fopen(gfname.c_str(),"rb")) != NULL)
00115     {
00116       LINFO("gist file found: %s", gfname.c_str());
00117       for(uint i = 0; i < pcInfo.oriFeatSize; i++)
00118         {
00119           double tval;  if (fread(&tval, sizeof(double), 1, gfp) != 1) LFATAL("fread failed");
00120           *aptr++ = tval;
00121         }
00122       fclose(gfp);
00123     }
00124   else LFATAL("gist file NOT found: %s", gfname.c_str());
00125 
00126   // run the ffn
00127   out = ffn2->run3L(matrixMult(pcaVec,oriin));
00128   for(int i = 0; i < out.getHeight(); i++)
00129     LINFO("%d: %f", i, out.getVal(i));
00130 }
00131 
00132 // ######################################################################
00133 /* So things look consistent in everyone's emacs... */
00134 /* Local Variables: */
00135 /* indent-tabs-mode: nil */
00136 /* End: */
Generated on Sun May 8 08:04:48 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3