testcudacbcl.C

00001 /*!@file HMAX/testcudacbcl.C Test Hmax CBCL model */
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/testcudacbcl.C $
00035 // $Id: testcudacbcl.C 13354 2010-05-04 00:59:00Z dparks $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "GUI/XWindow.H"
00040 #include "Learn/SVMClassifier.H"
00041 #include "CUDA/CudaHmaxCBCL.H"
00042 #include "Image/Image.H"
00043 #include "Image/ColorOps.H"
00044 #include "Image/CutPaste.H"
00045 #include "Image/Rectangle.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/MatrixOps.H"
00048 #include "Image/Transforms.H"
00049 #include "Image/Convolutions.H"
00050 #include "Learn/svm.h"
00051 #include "Media/FrameSeries.H"
00052 #include "nub/ref.h"
00053 #include "Raster/GenericFrame.H"
00054 #include "Raster/Raster.H"
00055 #include "Util/Types.H"
00056 #include "Util/log.H"
00057 #include "Util/Timer.H"
00058 
00059 #include <fstream>
00060 #include <iostream>
00061 #include <iomanip>
00062 #include <string>
00063 #include <unistd.h>
00064 #include <cstdlib>
00065 
00066 
00067 // number of orientations to use in HmaxFL
00068 #define NORI 4
00069 #define NUM_PATCHES_PER_SIZE 250
00070 
00071 int main(const int argc, const char **argv)
00072 {
00073 
00074   MYLOGVERB = LOG_INFO;
00075 
00076   ModelManager *mgr = new ModelManager("Test Hmax with Feature Learning");
00077 
00078   mgr->exportOptions(MC_RECURSE);
00079 
00080   // required arguments
00081   // <dev> <c0patches> <c1patches> <dir|list> <svmModel> <svmRange> <outputfile>
00082   //
00083 
00084   if (mgr->parseCommandLine(
00085                             (const int)argc, (const char**)argv, "<cudadev> <c0patches> <c1patches> <dir|list:images> <svmModel> <svmRange> <noprob|withprob:outputfile>", 7, 7) == false)
00086     return 1;
00087 
00088   std::string c0Patches;
00089   std::string c1Patches;
00090   std::string images,svmModel,svmRange,devArg;
00091   std::string answerFileName;
00092 
00093   devArg = mgr->getExtraArg(0);
00094   c0Patches = mgr->getExtraArg(1);
00095   c1Patches = mgr->getExtraArg(2);
00096   images = mgr->getExtraArg(3);
00097   svmModel = mgr->getExtraArg(4);
00098   svmRange = mgr->getExtraArg(5);
00099   answerFileName = mgr->getExtraArg(6);
00100 
00101   int dev = strtol(devArg.c_str(),NULL,0);
00102   CudaDevices::setCurrentDevice(dev);
00103   std::string::size_type dirArg=images.find("dir:",0);
00104   std::string::size_type listArg=images.find("list:",0);
00105   if((dirArg == std::string::npos &&
00106       listArg == std::string::npos) ||
00107      (dirArg != 0 && listArg != 0)){
00108     LFATAL("images argument is in one of the following formats -  dir:<DIRNAME>  or  list:<LISTOFIMAGEPATHSFILE>");
00109     return EXIT_FAILURE;
00110   }
00111   if(dirArg == 0)
00112     images = images.substr(4);
00113   else
00114     images = images.substr(5);
00115 
00116   std::string::size_type noProbArg=answerFileName.find("noprob:",0);
00117   std::string::size_type withProbArg=answerFileName.find("withprob:",0);
00118 
00119   if((noProbArg == std::string::npos &&
00120       withProbArg == std::string::npos) ||
00121      (noProbArg != 0 && withProbArg != 0)){
00122     LFATAL("output file argument is in one of the following formats -  noprob:<FILENAME>  or  withprob:<FILENAME>");
00123     return EXIT_FAILURE;
00124   }
00125   if(noProbArg == 0)
00126     answerFileName = answerFileName.substr(7);
00127   else
00128     answerFileName = answerFileName.substr(9);
00129 
00130 
00131 
00132   // Now we run if needed
00133   mgr->start();
00134 
00135   CudaHmaxCBCL hmax(c0Patches,c1Patches);
00136 
00137   // Load the SVM Classifier Model and Range in
00138   SVMClassifier svm;
00139   svm.readModel(svmModel);
00140   svm.readRange(svmRange);
00141 
00142   std::vector<std::string> imageNames;
00143   if(dirArg == 0)
00144     imageNames = hmax.readDir(images);
00145   else
00146     imageNames = hmax.readList(images);
00147 
00148   std::ofstream answerFile;
00149   answerFile.open(answerFileName.c_str(),std::ios::out);
00150 
00151   for(unsigned int imgInd=0;imgInd<imageNames.size();imgInd++){
00152 
00153     Image<float> inputf = Raster::ReadGrayNTSC(imageNames[imgInd]);
00154     Timer tim;
00155     tim.reset();
00156     hmax.getC2(inputf.getArrayPtr(),inputf.getWidth(),inputf.getHeight());
00157     printf("CUDA CBCL HMAX %f secs\n",tim.getSecs());
00158     int numC2 = hmax.numC2Features();
00159     float *c2 = hmax.getC2Features();
00160     double prob;
00161     double pred = svm.predict(c2,numC2,&prob);
00162     printf("Prediction is %f, prob is %f\n",pred,prob);
00163     int predId = (int) pred;
00164     answerFile << predId;
00165     // Add probabilities if desired
00166     if(withProbArg == 0)
00167       answerFile << "\t" << prob;
00168     answerFile << std::endl;
00169     hmax.clearC2();
00170   }
00171 
00172   answerFile.close();
00173   return 0;
00174 }
00175 
00176 
00177 // ######################################################################
00178 /* So things look consistent in everyone's emacs... */
00179 /* Local Variables: */
00180 /* indent-tabs-mode: nil */
00181 /* End: */
Generated on Sun May 8 08:40:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3