extractcudapatches.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/extractcudapatches.C $
00035 // $Id: extractcudapatches.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "GUI/XWindow.H"
00040 #include "CUDA/CudaHmaxFL.H"
00041 #include "CUDA/CudaHmax.H"
00042 #include "CUDA/CudaImage.H"
00043 #include "CUDA/CudaCutPaste.H"
00044 #include "Image/Rectangle.H"
00045 #include "CUDA/CudaMathOps.H"
00046 #include "Image/Normalize.H"
00047 #include "Image/Transforms.H"
00048 #include "CUDA/CudaConvolutions.H"
00049 #include "Learn/svm.h"
00050 #include "Media/FrameSeries.H"
00051 #include "nub/ref.h"
00052 #include "Raster/GenericFrame.H"
00053 #include "Raster/Raster.H"
00054 #include "Util/Types.H"
00055 #include "Util/log.H"
00056 
00057 #include <fstream>
00058 #include <iostream>
00059 #include <string>
00060 #include <unistd.h>
00061 #include <cstdlib>
00062 
00063 
00064 // number of orientations to use in HmaxFL
00065 #define NORI 4
00066 #define NUM_PATCHES_PER_SIZE 250
00067 
00068 
00069 int main(const int argc, const char **argv)
00070 {
00071 
00072   MYLOGVERB = LOG_INFO;
00073   ModelManager *mgr = new ModelManager("Extract Patches for Hmax with Feature Learning");
00074 
00075   mgr->exportOptions(MC_RECURSE);
00076 
00077   // required arguments
00078   // <c1patchesDir> <trainPosDir>
00079 
00080   if (mgr->parseCommandLine(
00081                             (const int)argc, (const char**)argv, "<cudadev> <c1patchesDir> <trainPosDir>", 3, 3) == false)
00082     return 1;
00083 
00084   std::vector<int> scss(9);
00085   scss[0] = 1; scss[1] = 3; scss[2] = 5; scss[3] = 7; scss[4] = 9;
00086   scss[5] = 11; scss[6] = 13; scss[7] = 15; scss[8] = 17;
00087   std::vector<int> spss(8);
00088   spss[0] = 8; spss[1] = 10; spss[2] = 12; spss[3] = 14;
00089   spss[4] = 16; spss[5] = 18; spss[6] = 20; spss[7] = 22;
00090   // std::vector<int> scss(4);
00091   // scss[0] = 3; scss[1] = 7; scss[2] = 11; scss[3] = 15;
00092   // std::vector<int> spss(4);
00093   // spss[0] = 10; spss[1] = 14; spss[2] = 18; spss[3] = 22;
00094 
00095   int dev = strtol(mgr->getExtraArg(0).c_str(),NULL,0);
00096   MemoryPolicy mp = GLOBAL_DEVICE_MEMORY;
00097   std::string c1PatchesBaseDir;
00098   std::string trainPosName; // Directory where positive images are
00099   c1PatchesBaseDir = mgr->getExtraArg(1);
00100   trainPosName = mgr->getExtraArg(2);
00101 
00102   CudaHmaxFL hmax(mp,dev,NORI, spss, scss);
00103 
00104   // Extract random patches from a set of images in a positive training directory
00105   std::vector<std::string> trainPos = hmax.readDir(trainPosName);
00106   int posTrainSize = trainPos.size();
00107 
00108   //Image<byte> inputb;
00109 
00110   Image<float> *trainPosImages = new Image<float>[posTrainSize];
00111 
00112   std::cout << "Scanned training and testing images" << std::endl;
00113 
00114   for(int imgInd = 0; imgInd < posTrainSize; imgInd++) {
00115     //inputb = Raster::ReadGray(trainPos[imgInd]);
00116     //trainPosImages[imgInd] = Image<float>(inputb);
00117     trainPosImages[imgInd] = Raster::ReadGrayNTSC(trainPos[imgInd]);
00118   }
00119 
00120   std::vector<int> pS(4);
00121   pS[0] = 4; pS[1] = 8, pS[2] = 12; pS[3] = 16;
00122 
00123   // Learn the appropriate simple S2 patches from the C1 results
00124   hmax.extractRandC1Patches(trainPosImages,posTrainSize,pS,NUM_PATCHES_PER_SIZE,NORI);
00125   std::cout << "Completed extraction of C1 Patches" << std::endl;
00126 
00127 
00128   delete [] trainPosImages;
00129 
00130   hmax.writeOutC1Patches(c1PatchesBaseDir);
00131   return 0;
00132 }
00133 
00134 
00135 
00136 
00137 // ######################################################################
00138 /* So things look consistent in everyone's emacs... */
00139 /* Local Variables: */
00140 /* indent-tabs-mode: nil */
00141 /* End: */
Generated on Sun May 8 08:04:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3