CudaHmaxFLSal.C

00001 /*!@file CUDA/CudaHmaxFL.C Riesenhuber & Poggio's HMAX model for object recognition */
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: Laurent Itti <itti@usc.edu>
00034 //
00035 //
00036 #include "CUDA/CudaSaliency.H"
00037 #include "CUDA/CudaHmaxFLSal.H"
00038 #include "CUDA/CudaHmaxFL.H"
00039 #include "CUDA/CudaHmax.H"
00040 #include "CUDA/CudaFilterOps.H"
00041 #include "CUDA/CudaImage.H"
00042 //#include "CUDA/CudaKernels.H"   // for cudaDogFilterHmax()
00043 #include "Image/Kernels.H"   // for dogFilterHmax()
00044 #include "CUDA/CudaMathOps.H"
00045 #include "CUDA/CudaConvolutions.H" // for cudaConvolve() etc.
00046 #include "Image/Normalize.H"
00047 #include "CUDA/CudaCutPaste.H"
00048 #include "Raster/Raster.H"
00049 #include "Raster/RasterFileFormat.H"
00050 #include "Util/MathFunctions.H"
00051 #include "Util/Types.H"
00052 #include "Util/log.H"
00053 #include "Util/safecopy.H"
00054 
00055 #include <cmath>
00056 #include <cstdio>
00057 #include <cstdlib>
00058 #include <cfloat>
00059 #include <limits>
00060 #include <sstream>
00061 #include <stdexcept>
00062 
00063 // #########################################################################
00064 
00065 CudaHmaxFLSal::CudaHmaxFLSal(MemoryPolicy mp, int dev, nub::ref<CudaSaliency> sal_in) :
00066   sal(sal_in)
00067 {
00068   CudaHmaxFL(mp,dev,0,std::vector<int>(),std::vector<int>());
00069 }
00070 
00071 CudaHmaxFLSal::~CudaHmaxFLSal()
00072 {
00073 }
00074 
00075 // ######################################################################
00076 void CudaHmaxFLSal::extractRandC1Patches(Image<float> *&  posTrainingImages, int numPosTrainImages, std::vector<int> patchSizes, int numPatchesPerSize, int no)
00077 {
00078   // Create a temp CudaHmaxFL object to extract C1Patches
00079   std::vector<int> c1ScaleSS(2);
00080   c1ScaleSS[0] = 1; c1ScaleSS[1] = 3;
00081   std::vector<int> c1SpaceSS(2);
00082   c1SpaceSS[0] = 10; c1SpaceSS[1] = 11;
00083   // desired frame sizes [11 and 13]
00084   CudaHmaxFL hmax(itsMp,itsDev,no,c1SpaceSS,c1ScaleSS,2,true,1.0F,1.0F,0.3F,4.05F,-0.05F,11,2);
00085 
00086 
00087   CudaImage<float> ***patches = new CudaImage<float>**[patchSizes.size()];
00088   for(unsigned int i=0;i<patchSizes.size();i++){
00089     patches[i] = new CudaImage<float>*[numPatchesPerSize];
00090     for(int j=0;j<numPatchesPerSize;j++) {
00091       patches[i][j] = new CudaImage<float>[no];
00092       for(int k=0;k<no;k++) {
00093         // Set patches to be all zeros
00094         patches[i][j][k].resize(patchSizes[i],patchSizes[i],ZEROS);
00095       }
00096     }
00097   }
00098 
00099   CudaImage<float> **c1Res;
00100   CudaImage<float> stim;
00101   std::srand(time(0));
00102   int sb = 0; // Only one scale band
00103 
00104   for(int i=0;i<numPatchesPerSize;i++){
00105     // Randomly select an image from the list
00106     unsigned int imInd = static_cast<unsigned int>(floor((rand()-1.0F)/RAND_MAX*numPosTrainImages));
00107     stim = CudaImage<float>(posTrainingImages[imInd],itsMp,itsDev);
00108 
00109 
00110     //hmax.initC1(c1Res);
00111     hmax.getC1(stim,c1Res);
00112 
00113     int bsize1 = c1Res[sb][0].getWidth();
00114     int bsize2 = c1Res[sb][0].getHeight();
00115     hmax.printCorners("input",stim,i<5);
00116     for(unsigned int j=0;j<patchSizes.size();j++) {
00117       int xy1 = int(floor((rand()-1.0F)/RAND_MAX*(bsize1-patchSizes[j])));
00118       int xy2 = int(floor((rand()-1.0F)/RAND_MAX*(bsize2-patchSizes[j])));
00119       Rectangle r = Rectangle::tlbrI(xy2,xy1,xy2+patchSizes[j]-1,xy1+patchSizes[j]-1);
00120       for(int k=0;k<no;k++) {
00121         patches[j][i][k] = cudaCrop(c1Res[sb][k],r);
00122         patches[j][i][k] *= 255*10;
00123       }
00124     }
00125     hmax.printCorners("patch[3][i][0]",patches[3][i][0],i<5);
00126     hmax.clearC1(c1Res);
00127 
00128   }
00129 setC1Patches(patches,patchSizes,numPatchesPerSize);
00130 }
00131 
00132 // ######################################################################
00133 void CudaHmaxFLSal::getC1(CudaImage<float>**& c1Res)
00134 {
00135   //Combination 1
00136   c1Res[0][0] = sal->getIMap();
00137   c1Res[0][1] = sal->getCMap();
00138   c1Res[0][2] = sal->getOMap();
00139   c1Res[0][3] = sal->getFMap();
00140   c1Res[0][4] = sal->getMMap();
00141 
00142 
00143 
00144 }
00145 
00146 // ######################################################################
00147 void CudaHmaxFLSal::getC1(const CudaImage<float>& input, CudaImage<float>**& c1Res)
00148 {
00149   sal->doInput(input.exportToImage());
00150   //Combination 1
00151   c1Res[0][0] = sal->getIMap();
00152   c1Res[0][1] = sal->getCMap();
00153   c1Res[0][2] = sal->getOMap();
00154   c1Res[0][3] = sal->getFMap();
00155   c1Res[0][4] = sal->getMMap();
00156 
00157 }
00158 // ######################################################################
00159 /* So things look consistent in everyone's emacs... */
00160 /* Local Variables: */
00161 /* indent-tabs-mode: nil */
00162 /* End: */
Generated on Sun May 8 08:04:43 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3