InferoTemporalCudaHmax.C

00001  /*!@file Neuro/InferoTemporalHmax.C Object recognition module with Hmax */
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://dparks@isvn.usc.edu/software/invt/trunk/saliency/src/Neuro/InferoTemporalHmax.C $
00035 // $Id: InferoTemporalHmax.C 13497 2010-05-28 00:37:41Z dparks $
00036 //
00037 
00038 #include "Neuro/InferoTemporalCudaHmax.H"
00039 #include "CUDA/CudaHmaxCBCL.H"
00040 #include "Learn/SVMClassifier.H"
00041 #include "Component/OptionManager.H"
00042 #include "Component/ModelOptionDef.H"
00043 #include "Image/MathOps.H"
00044 #include "Image/ShapeOps.H"
00045 #include "Image/CutPaste.H"
00046 #include "Image/ColorOps.H"
00047 #include "Neuro/NeuroOpts.H"
00048 #include "Neuro/NeuroSimEvents.H"
00049 #include "Neuro/Brain.H"
00050 #include "Neuro/VisualCortex.H"
00051 #include "Simulation/SimEventQueue.H"
00052 #include "Media/MediaSimEvents.H"
00053 
00054 #include <cstdlib>
00055 #include <iostream>
00056 #include <iomanip>
00057 #include <fstream>
00058 
00059 const ModelOptionDef OPT_ITCUDAHMAXC0PatchesFileName =
00060   { MODOPT_ARG_STRING, "ITC C0 feature patches file", &MOC_ITC, OPTEXP_CORE,
00061     "File containing c0 patches in the following format:\n"
00062     "NUM_PATCHES\n"
00063     "DEPTH WIDTH HEIGHT\n"
00064     "D0W0H0 D0W0H1 D0W0H2 ...\n"
00065     "D0W1H0 ...\n",
00066     "it-cudahmax-c0patches-filename", '\0', "<filename>", "" };
00067 
00068 const ModelOptionDef OPT_ITCUDAHMAXC1PatchesFileName =
00069   { MODOPT_ARG_STRING, "ITC C1 feature patches file", &MOC_ITC, OPTEXP_CORE,
00070     "File containing c0 patches in the following format:\n"
00071     "NUM_PATCHES\n"
00072     "DEPTH WIDTH HEIGHT\n"
00073     "D0W0H0 D0W0H1 D0W0H2 ...\n"
00074     "D0W1H0 ...\n",
00075     "it-cudahmax-c1patches-filename", '\0', "<filename>", "" };
00076 
00077 
00078 
00079 // ######################################################################
00080 namespace
00081 {
00082   Image<PixRGB<byte> > getCroppedObject(const Image<PixRGB<byte> >& scene,
00083                                         const Image<float>& smoothMask)
00084   {
00085     if (!scene.initialized())
00086       return Image<PixRGB<byte> >();
00087 
00088     if (!smoothMask.initialized())
00089       return Image<PixRGB<byte> >();
00090 
00091     const float threshold = 1.0f;
00092 
00093     const Rectangle r = findBoundingRect(smoothMask, threshold);
00094     return crop(scene, r);
00095   }
00096 }
00097 
00098 
00099 // ######################################################################
00100 InferoTemporalCudaHmax::InferoTemporalCudaHmax(OptionManager& mgr,
00101                                      const std::string& descrName,
00102                                      const std::string& tagName) :
00103   InferoTemporalHmax(mgr, descrName, tagName),
00104   itsCUDAHMAXStoredC0PatchesFile(&OPT_ITCUDAHMAXC0PatchesFileName, this),
00105   itsCUDAHMAXStoredC1PatchesFile(&OPT_ITCUDAHMAXC1PatchesFileName, this)
00106 {
00107 
00108 }
00109 
00110 // ######################################################################
00111 void InferoTemporalCudaHmax::start1()
00112 {
00113   // Make sure old parameter is not present
00114   if(itsHMAXStoredPatchesDir.getVal().compare("") != 0) {
00115     LFATAL("This option is not valid for CUDA based Hmax use --it-cudahmax-c1patches-filename instead");
00116   }
00117   // Make sure C0 and C1 patch files are specified
00118   if(itsCUDAHMAXStoredC0PatchesFile.getVal().compare("") == 0) {
00119     LFATAL("Must specify filename containing C0 Patches using --it-cudahmax-c0patches-filename");
00120   }
00121   if(itsCUDAHMAXStoredC1PatchesFile.getVal().compare("") == 0) {
00122     LFATAL("Must specify filename containing C1 Patches using --it-cudahmax-c1patches-filename");
00123   }  
00124 
00125   // Load the patches
00126   hmax.loadC0(itsCUDAHMAXStoredC0PatchesFile.getVal());
00127   hmax.loadC1(itsCUDAHMAXStoredC1PatchesFile.getVal());
00128   InferoTemporal::start1();
00129 }
00130 
00131 
00132 // ######################################################################
00133 InferoTemporalCudaHmax::~InferoTemporalCudaHmax()
00134 {}
00135 
00136 
00137 
00138 std::vector<float> InferoTemporalCudaHmax::_convertFeatureVector(float *c2Res, int numC2)
00139 {
00140   std::vector<float> ret;
00141   // Allocate memory for c2 layer feature values
00142   for(int i=0;i<numC2;i++) {
00143       ret.push_back(c2Res[i]);
00144   }
00145   return ret;
00146 }
00147 
00148 void InferoTemporalCudaHmax::_freeFeatureVector(float *c2Res)
00149 {
00150 }
00151 
00152 // Override these inherited functions from InferoTemporaHmax
00153 void InferoTemporalCudaHmax::_freeFeatureVector(float **c2Res)
00154 {
00155   // Override
00156 }
00157 
00158 std::vector<float> InferoTemporalCudaHmax::_convertFeatureVector(float **c2Res)
00159 {
00160   // Override
00161   std::vector<float> ignore;
00162   LFATAL("Should never call this convertFeatureVector()");
00163   return ignore;
00164 }
00165 
00166 std::vector<float> InferoTemporalCudaHmax::calculateFeatureVector(Image<float> objImg)
00167 {
00168   // Allocate memory for c2 layer feature values
00169   float *c2Res;
00170   int numC2;
00171   //extract features
00172   hmax.getC2(objImg.getArrayPtr(),objImg.getWidth(),objImg.getHeight());
00173   c2Res = hmax.getC2Features();
00174   numC2 = hmax.numC2Features();
00175   std::vector<float> ret = _convertFeatureVector(c2Res,numC2);
00176   hmax.clearC2();
00177 
00178   return ret;
00179 }
00180 
00181 
00182 
00183 
00184 
00185 // ######################################################################
00186 /* So things look consistent in everyone's emacs... */
00187 /* Local Variables: */
00188 /* indent-tabs-mode: nil */
00189 /* End: */
00190 
Generated on Sun May 8 08:05:24 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3