testcudahmaxflsal.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: Dan Parks <danielfp@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/CUDA/testcudahmaxflsal.C $
00035 // $Id: testcudahmaxflsal.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 #include "CUDA/CudaHmaxFLSal.H"
00038 #include "CUDA/CudaSaliency.H"
00039 #include "Component/ModelManager.H"
00040 #include "GUI/XWindow.H"
00041 #include "CUDA/CudaHmaxFL.H"
00042 #include "Learn/SVMClassifier.H"
00043 #include "CUDA/CudaHmax.H"
00044 #include "Image/Image.H"
00045 #include "Image/ColorOps.H"
00046 #include "Image/CutPaste.H"
00047 #include "Image/Rectangle.H"
00048 #include "Image/MathOps.H"
00049 #include "Image/MatrixOps.H"
00050 #include "Image/Transforms.H"
00051 #include "Image/Convolutions.H"
00052 #include "Learn/svm.h"
00053 #include "Media/FrameSeries.H"
00054 #include "nub/ref.h"
00055 #include "Raster/GenericFrame.H"
00056 #include "Raster/Raster.H"
00057 #include "Util/Types.H"
00058 #include "Util/log.H"
00059 
00060 #include <fstream>
00061 #include <iostream>
00062 #include <iomanip>
00063 #include <string>
00064 #include <unistd.h>
00065 #include <cstdlib>
00066 // number of orientations to use in HmaxFL
00067 #define NORI 4
00068 #define NUM_PATCHES_PER_SIZE 250
00069 
00070 int main(const int argc, const char **argv)
00071 {
00072 
00073   MYLOGVERB = LOG_INFO;
00074 
00075   ModelManager *mgr = new ModelManager("Test Hmax with Feature Learning");
00076 
00077   //Is this initialisation of the cuda saliency device correct !!!how does the neovision2cuda communicate with this class then ??
00078   nub::ref<CudaSaliency> smt(new CudaSaliency(*mgr));
00079   mgr->exportOptions(MC_RECURSE);
00080 
00081   // required arguments
00082   // <c1patchesDir> <dir|list> <id> <outputfile>
00083   //
00084   // <id> is the given id for the given set of images
00085   // --in only needs to happen if we are loading the patches
00086 
00087   if (mgr->parseCommandLine(
00088                             (const int)argc, (const char**)argv, "<cudadev> <c1patchesDir> <dir|list:images> <svmModel> <svmRange> <outputfile>", 6, 6) == false)
00089     return 1;
00090 
00091   std::string c1PatchesBaseDir;
00092   std::string images,svmModel,svmRange,devArg;
00093   std::string answerFileName;
00094 
00095   devArg = mgr->getExtraArg(0);
00096   c1PatchesBaseDir = mgr->getExtraArg(1);
00097   images = mgr->getExtraArg(2);
00098   svmModel = mgr->getExtraArg(3);
00099   svmRange = mgr->getExtraArg(4);
00100   answerFileName = mgr->getExtraArg(5);
00101 
00102   MemoryPolicy mp = GLOBAL_DEVICE_MEMORY;
00103   int dev = strtol(devArg.c_str(),NULL,0);
00104   std::string::size_type dirArg=images.find("dir:",0);
00105   std::string::size_type listArg=images.find("list:",0);
00106   if((dirArg == std::string::npos &&
00107       listArg == std::string::npos) ||
00108      (dirArg != 0 && listArg != 0)){
00109     LFATAL("images argument is in one of the following formats -  dir:<DIRNAME>  or  list:<LISTOFIMAGEPATHSFILE>");
00110     return EXIT_FAILURE;
00111   }
00112   if(dirArg == 0)
00113     images = images.substr(4);
00114   else
00115     images = images.substr(5);
00116 
00117   // Now we run if needed
00118   mgr->start();
00119 
00120 
00121   // get an HmaxFL object:
00122   std::vector<int> scss(9);
00123   scss[0] = 1; scss[1] = 3; scss[2] = 5; scss[3] = 7; scss[4] = 9;
00124   scss[5] = 11; scss[6] = 13; scss[7] = 15; scss[8] = 17;
00125   std::vector<int> spss(8);
00126   spss[0] = 8; spss[1] = 10; spss[2] = 12; spss[3] = 14;
00127   spss[4] = 16; spss[5] = 18; spss[6] = 20; spss[7] = 22;
00128   // std::vector<int> scss(4);
00129   // scss[0] = 3; scss[1] = 7; scss[2] = 11; scss[3] = 15;
00130   // std::vector<int> spss(4);
00131   // spss[0] = 10; spss[1] = 14; spss[2] = 18; spss[3] = 22;
00132 
00133   CudaHmaxFLSal hmax(mp,dev,smt);
00134 
00135   // Load the SVM Classifier Model and Range in
00136   SVMClassifier svm;
00137   svm.readModel(svmModel);
00138   svm.readRange(svmRange);
00139 
00140   //
00141   hmax.readInC1Patches(c1PatchesBaseDir);
00142 
00143   std::vector<std::string> imageNames;
00144   if(dirArg == 0)
00145     imageNames = hmax.readDir(images);
00146   else
00147     imageNames = hmax.readList(images);
00148 
00149   std::ofstream answerFile;
00150   answerFile.open(answerFileName.c_str(),std::ios::out);
00151 
00152   std::vector<int> patchSizes = hmax.getC1PatchSizes();
00153   float **c2Res = new float*[patchSizes.size()];
00154   for(unsigned int i=0;i<patchSizes.size();i++) {
00155     c2Res[i] = new float[NUM_PATCHES_PER_SIZE];
00156   }
00157 
00158   for(unsigned int imgInd=0;imgInd<imageNames.size();imgInd++){
00159 
00160     Image<float> inputf = Raster::ReadGrayNTSC(imageNames[imgInd]);
00161     hmax.getC2(CudaImage<float>(inputf,mp,dev),c2Res);
00162 
00163     double pred = svm.predict(c2Res,patchSizes.size(),NUM_PATCHES_PER_SIZE);
00164     printf("Prediction is %f\n",pred);
00165     int predId = (int) pred;
00166     answerFile << predId;
00167     answerFile << std::endl;
00168 
00169   }
00170 
00171   for(unsigned int i=0;i<patchSizes.size();i++) {
00172     delete[] c2Res[i];
00173   }
00174   delete [] c2Res;
00175 
00176   answerFile.close();
00177   return 0;
00178 }
00179 
00180 
00181 // ######################################################################
00182 /* So things look consistent in everyone's emacs... */
00183 /* Local Variables: */
00184 /* indent-tabs-mode: nil */
00185 /* End: */
Generated on Sun May 8 08:04:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3