runcudacbcl.C

Go to the documentation of this file.
00001 /*!@file CUDA/runcudacbcl.C Test CUDA 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/runcudacbcl.C $
00035 // $Id: runcudacbcl.C 13658 2010-07-13 23:43:20Z lior $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "CUDA/CudaHmaxCBCL.H"
00040 #include "CUDA/CudaDevices.H"
00041 #include "GUI/XWindow.H"
00042 #include "Image/Image.H"
00043 #include "Image/ColorOps.H"
00044 #include "Image/ShapeOps.H"
00045 #include "Image/CutPaste.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/svm.h"
00052 #include "Media/FrameSeries.H"
00053 #include "nub/ref.h"
00054 #include "Raster/GenericFrame.H"
00055 #include "Raster/Raster.H"
00056 #include "Util/Types.H"
00057 #include "Util/log.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 
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> <c01patches> <c1patches> <dir|list:images> <id> <outputfile>", 6, 6) == false)
00089     return 1;
00090 
00091   std::string c0Patches;
00092   std::string c1Patches;
00093   std::string images,idArg,devArg;
00094   std::string c2FileName;
00095 
00096   std::string trainPosName; // Directory where positive images are
00097 
00098   int id;
00099 
00100   devArg = mgr->getExtraArg(0);
00101   c0Patches = mgr->getExtraArg(1);
00102   c1Patches = mgr->getExtraArg(2);
00103   images = mgr->getExtraArg(3);
00104   idArg = mgr->getExtraArg(4);
00105   c2FileName = mgr->getExtraArg(5);
00106 
00107   int dev = strtol(devArg.c_str(),NULL,0);
00108   CudaDevices::setCurrentDevice(dev);
00109   std::string::size_type dirArg=images.find("dir:",0);
00110   std::string::size_type listArg=images.find("list:",0);
00111   if((dirArg == std::string::npos &&
00112       listArg == std::string::npos) ||
00113      (dirArg != 0 && listArg != 0)){
00114     LFATAL("images argument is in one of the following formats -  dir:<DIRNAME>  or  list:<LISTOFIMAGEPATHSFILE>");
00115     return EXIT_FAILURE;
00116   }
00117   if(dirArg == 0)
00118     images = images.substr(4);
00119   else
00120     images = images.substr(5);
00121 
00122   id = strtol(idArg.c_str(),NULL,0);
00123 
00124   CudaHmaxCBCL hmax(c0Patches,c1Patches);
00125 
00126   // Now we run if needed
00127   mgr->start();
00128 
00129   std::vector<std::string> imageNames;
00130   if(dirArg == 0)
00131     imageNames = hmax.readDir(images);
00132   else
00133     imageNames = hmax.readList(images);
00134 
00135   bool clearFile = true;
00136   for(unsigned int imgInd=0;imgInd<imageNames.size();imgInd++){
00137     Image<float> inputf = Raster::ReadGrayNTSC(imageNames[imgInd]);
00138     // Really dumb hack to make sure CBCL code doesn't break
00139     inputf = rescaleBilinear(inputf,256,256);
00140     // Output the c2 responses per patch into a libsvm
00141     hmax.getC2(inputf.getArrayPtr(),inputf.getWidth(),inputf.getHeight());
00142     //std::cout <<"C2 Processing Complete: " << imgInd << std::endl;
00143     hmax.writeOutFeatures(c2FileName,id,clearFile);
00144     clearFile = false;
00145     std::cout <<"C2 Output Written: " << imgInd << std::endl;
00146     hmax.clearC2();
00147   }
00148 
00149   return 0;
00150 }
00151 
00152 
00153 // ######################################################################
00154 /* So things look consistent in everyone's emacs... */
00155 /* Local Variables: */
00156 /* indent-tabs-mode: nil */
00157 /* End: */
Generated on Sun May 8 08:04:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3