test-hmaxFL.C

00001 /*!@file HMAX/test-hmax5.C Test Hmax class and compare to original code */
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: Lior Elazary
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/HMAX/test-hmaxFL.C $
00035 // $Id: test-hmaxFL.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "GUI/XWindow.H"
00040 #include "HMAX/HmaxFL.H"
00041 #include "HMAX/Hmax.H"
00042 #include "Image/Image.H"
00043 #include "Image/ColorOps.H"
00044 #include "Image/CutPaste.H"
00045 #include "Image/ShapeOps.H"
00046 #include "Image/Rectangle.H"
00047 #include "Image/MathOps.H"
00048 #include "Image/MatrixOps.H"
00049 #include "Image/Transforms.H"
00050 #include "Image/Convolutions.H"
00051 #include "Learn/SVMClassifier.H"
00052 #include "Media/FrameSeries.H"
00053 #include "Media/TestImages.H"
00054 #include "nub/ref.h"
00055 #include "Raster/GenericFrame.H"
00056 #include "Transport/FrameInfo.H"
00057 #include "Raster/Raster.H"
00058 #include "Util/Types.H"
00059 #include "Util/log.H"
00060 
00061 #include <fstream>
00062 #include <iostream>
00063 #include <iomanip>
00064 #include <string>
00065 #include <unistd.h>
00066 #include <cstdlib>
00067 
00068 
00069 // number of orientations to use in HmaxFL
00070 #define NORI 4
00071 #define NUM_PATCHES_PER_SIZE 250
00072 
00073 int main(const int argc, const char **argv)
00074 {
00075 
00076   MYLOGVERB = LOG_INFO;
00077   ModelManager *mgr = new ModelManager("Test Hmax with Feature Learning");
00078 
00079   nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*mgr));
00080   mgr->addSubComponent(ifs);
00081 
00082   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00083   mgr->addSubComponent(ofs);
00084 
00085 
00086 
00087   mgr->exportOptions(MC_RECURSE);
00088 
00089   // required arguments
00090   // <c1patchesDir> <dir|list> <id> <outputfile>
00091   //
00092   // <id> is the given id for the given set of images
00093   // --in only needs to happen if we are loading the patches
00094 
00095   if (mgr->parseCommandLine(
00096         (const int)argc, (const char**)argv, "<c1patchesDir> <modelFile> <objectName> train=1/classify=0", 4, 4) == false)
00097     return 1;
00098 
00099   // get an HmaxFL object:
00100   std::vector<int> scss(9);
00101   scss[0] = 1; scss[1] = 3; scss[2] = 5; scss[3] = 7; scss[4] = 9;
00102   scss[5] = 11; scss[6] = 13; scss[7] = 15; scss[8] = 17;
00103   std::vector<int> spss(8);
00104   spss[0] = 8; spss[1] = 10; spss[2] = 12; spss[3] = 14;
00105   spss[4] = 16; spss[5] = 18; spss[6] = 20; spss[7] = 22;
00106   HmaxFL hmax(NORI, spss, scss);
00107 
00108   std::string c1PatchesBaseDir = mgr->getExtraArg(0);
00109   std::string modelFile = mgr->getExtraArg(1);
00110   std::string objName = mgr->getExtraArg(2);
00111   bool training = atoi(mgr->getExtraArg(3).c_str());
00112 
00113   hmax.readInC1Patches(c1PatchesBaseDir);
00114 
00115   mgr->start();
00116 
00117   ifs->startStream();
00118 
00119   SVMClassifier classifier;
00120 
00121   //Load the svm File
00122   if (!training)
00123     classifier.readModel(modelFile);
00124 
00125   while(1)
00126   {
00127     Image< PixRGB<byte> > inputImg;
00128     const FrameState is = ifs->updateNext();
00129     LINFO("Frame %i\n", ifs->frame());
00130     if (is == FRAME_COMPLETE)
00131       break;
00132 
00133     //grab the images
00134     GenericFrame input = ifs->readFrame();
00135     if (!input.initialized())
00136       break;
00137     inputImg = input.asRgb();
00138 
00139     Image<float> inputf = luminance(inputImg);
00140 
00141     inputf = rescale(inputf, 128, 128);
00142 
00143     std::vector<int> patchSizes = hmax.getC1PatchSizes();
00144 
00145     int objId = 0;
00146     //Get the metadata and find if we have the object name in the scene
00147     rutz::shared_ptr<GenericFrame::MetaData>
00148       metaData = input.getMetaData(std::string("SceneData"));
00149     if (metaData.get() != 0) {
00150       rutz::shared_ptr<TestImages::SceneData> sceneData;
00151       sceneData.dyn_cast_from(metaData);
00152       for (uint i = 0; i < sceneData->objects.size(); i++) {
00153         TestImages::ObjData objData = sceneData->objects[i];
00154         if (objData.name == objName)
00155           objId = 1;
00156       }
00157     }
00158 
00159 
00160     //Compute C2 features
00161     float **c2Res = new float*[patchSizes.size()];
00162     for(unsigned int i=0;i<patchSizes.size();i++) {
00163       c2Res[i] = new float[NUM_PATCHES_PER_SIZE];
00164     }
00165     hmax.getC2(inputf,c2Res);
00166 
00167     LINFO("C2 Processing Complete.");
00168 
00169     //printf("%i ", objId);
00170     //for(unsigned int i=0;i<patchSizes.size();i++)
00171     //  for(int j=0;j<NUM_PATCHES_PER_SIZE;j++)
00172     //    printf("%i:%f ", (i*NUM_PATCHES_PER_SIZE+j+1),  c2Res[i][j]);
00173     //printf("\n");
00174 
00175 
00176     if (training)
00177     {
00178       classifier.train(modelFile,objId,c2Res,patchSizes.size(),NUM_PATCHES_PER_SIZE);
00179     } else {
00180       double predObjId = classifier.predict(c2Res,patchSizes.size(),NUM_PATCHES_PER_SIZE);
00181       printf("Obj id %i predicted %f",
00182           objId, predObjId);
00183     }
00184 
00185 
00186     for(unsigned int i=0;i<patchSizes.size();i++) {
00187       delete[] c2Res[i];
00188     }
00189     delete [] c2Res;
00190 
00191 
00192     ofs->writeRGB(inputImg, "input", FrameInfo("input", SRC_POS));
00193   }
00194 
00195 
00196   return 0;
00197 }
00198 
00199 
00200 // ######################################################################
00201 /* So things look consistent in everyone's emacs... */
00202 /* Local Variables: */
00203 /* indent-tabs-mode: nil */
00204 /* End: */
Generated on Sun May 8 08:04:48 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3