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: John McInerney <jmcinerney6@gmail.com> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/CUDASIFT/testcudasiftfl.C $ 00035 // $Id: testcudasiftfl.C 14295 2010-12-02 20:02:32Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "GUI/XWindow.H" 00040 #include "Image/Image.H" 00041 #include "Image/ColorOps.H" 00042 #include "Image/CutPaste.H" 00043 #include "Image/Rectangle.H" 00044 #include "Image/MathOps.H" 00045 #include "Image/MatrixOps.H" 00046 #include "Image/Transforms.H" 00047 #include "Image/Convolutions.H" 00048 #include "Media/FrameSeries.H" 00049 #include "nub/ref.h" 00050 #include "Raster/GenericFrame.H" 00051 #include "Raster/Raster.H" 00052 #include "Util/Types.H" 00053 #include "Util/log.H" 00054 #include "SIFT/ScaleSpace.H" 00055 #include "SIFT/VisualObject.H" 00056 #include "SIFT/Keypoint.H" 00057 #include "SIFT/VisualObjectDB.H" 00058 //#include "CUDASIFT/CUDAVisualObjectDB.H" 00059 #include "CUDASIFT/CUDAVisualObject.H" 00060 00061 #include "CUDASIFT/tpimageutil.h" 00062 #include "CUDASIFT/tpimage.h" 00063 #include "CUDASIFT/cudaImage.h" 00064 #include "CUDASIFT/cudaSift.h" 00065 #include "CUDASIFT/cudaSiftH.h" //This one is an addition and null 00066 00067 #include "Util/log.H" 00068 00069 #include <fstream> 00070 #include <iostream> 00071 #include <iomanip> 00072 #include <string> 00073 #include <unistd.h> 00074 #include <cstdio> 00075 #include <dirent.h> 00076 00077 00078 std::vector<std::string> readDir(std::string inName) 00079 { 00080 DIR *dp = opendir(inName.c_str()); 00081 if(dp == NULL) 00082 { 00083 LFATAL("Directory does not exist %s",inName.c_str()); 00084 } 00085 dirent *dirp; 00086 std::vector<std::string> fList; 00087 while ((dirp = readdir(dp)) != NULL ) { 00088 if (dirp->d_name[0] != '.') 00089 fList.push_back(inName + '/' + std::string(dirp->d_name)); 00090 } 00091 LINFO("%"ZU" files in the directory\n", fList.size()); 00092 LINFO("file list : \n"); 00093 for (unsigned int i=0; i<fList.size(); i++) 00094 LINFO("\t%s", fList[i].c_str()); 00095 std::sort(fList.begin(),fList.end()); 00096 return fList; 00097 } 00098 00099 // ###################################################################### 00100 std::vector<std::string> readList(std::string inName) 00101 { 00102 std::ifstream inFile; 00103 inFile.open(inName.c_str(),std::ios::in); 00104 if(!inFile){ 00105 LFATAL("Unable to open image path list file: %s",inName.c_str()); 00106 } 00107 std::string sLine; 00108 std::vector<std::string> fList; 00109 while (std::getline(inFile, sLine)) { 00110 std::cout << sLine << std::endl; 00111 fList.push_back(sLine); 00112 } 00113 LINFO("%"ZU" paths in the file\n", fList.size()); 00114 LINFO("file list : \n"); 00115 for (unsigned int i=0; i<fList.size(); i++) 00116 LINFO("\t%s", fList[i].c_str()); 00117 inFile.close(); 00118 return fList; 00119 } 00120 00121 std::string matchObject(Image<float> &ima, VisualObjectDB& vdb, float &score) 00122 { 00123 std::vector< rutz::shared_ptr<VisualObjectMatch> > matches; 00124 #ifdef GPUSIFT 00125 rutz::shared_ptr<CUDAVisualObject> 00126 vo(new CUDAVisualObject("PIC", "PIC", ima, 00127 Point2D<int>(-1,-1), 00128 std::vector<float>(), 00129 std::vector< rutz::shared_ptr<Keypoint> >(), 00130 false,true)); 00131 #else 00132 rutz::shared_ptr<VisualObject> 00133 vo(new VisualObject("PIC", "PIC", ima, 00134 Point2D<int>(-1,-1), 00135 std::vector<float>(), 00136 std::vector< rutz::shared_ptr<Keypoint> >(), 00137 false,true)); 00138 #endif 00139 const uint nmatches = vdb.getObjectMatches(vo, matches, VOMA_SIMPLE, 00140 100U, //max objs to return 00141 0.5F, //keypoint distance score default 0.5F 00142 0.5F, //affine distance score default 0.5F 00143 1.0F, //minscore default 1.0F 00144 3U, //min # of keypoint match 00145 100U, //keypoint selection thershold 00146 false //sort by preattentive 00147 ); 00148 score = 0; 00149 float avgScore = 0, affineAvgDist = 0; 00150 int nkeyp = 0; 00151 int objId = -1; 00152 if (nmatches > 0) 00153 { 00154 rutz::shared_ptr<VisualObject> obj; //so we will have a ref to the last matches obj 00155 rutz::shared_ptr<VisualObjectMatch> vom; 00156 for (unsigned int i = 0; i < 1; ++i) // Loops just once? 00157 { 00158 vom = matches[i]; 00159 obj = vom->getVoTest(); 00160 score = vom->getScore(); 00161 nkeyp = vom->size(); 00162 avgScore = vom->getKeypointAvgDist(); 00163 affineAvgDist = vom->getAffineAvgDist(); 00164 00165 objId = atoi(obj->getName().c_str()+3); 00166 00167 return obj->getName(); // Everything that follows is dead? 00168 LINFO("### Object match with '%s' score=%f ID:%i", 00169 obj->getName().c_str(), vom->getScore(), objId); 00170 00171 //calculate the actual distance (location of keypoints) between 00172 //keypoints. If the same patch was found, then the distance should 00173 //be close to 0 00174 double dist = 0; 00175 for (int keyp=0; keyp<nkeyp; keyp++) 00176 { 00177 const KeypointMatch kpm = vom->getKeypointMatch(keyp); 00178 00179 float refX = kpm.refkp->getX(); 00180 float refY = kpm.refkp->getY(); 00181 00182 float tstX = kpm.tstkp->getX(); 00183 float tstY = kpm.tstkp->getY(); 00184 dist += (refX-tstX) * (refX-tstX); 00185 dist += (refY-tstY) * (refY-tstY); 00186 } 00187 00188 // printf("%i:%s %i %f %i %f %f %f\n", objNum, obj->getName().c_str(), 00189 // nmatches, score, nkeyp, avgScore, affineAvgDist, sqrt(dist)); 00190 00191 //analizeImage(); 00192 } 00193 00194 } 00195 00196 return std::string("nomatch"); 00197 } 00198 00199 int main(const int argc, const char **argv) 00200 { 00201 00202 MYLOGVERB = LOG_INFO; 00203 00204 ModelManager *mgr = new ModelManager("Test SIFT with Feature Learning"); 00205 00206 00207 mgr->exportOptions(MC_RECURSE); 00208 00209 // required arguments 00210 // <dir|list> <id> <outputfile> 00211 00212 if (mgr->parseCommandLine( (const int)argc, (const char**)argv, 00213 "<cudadev> <dir|list:images> <vdbfile>", 3, 3) == false) 00214 return 1; 00215 00216 std::string images,devArg; 00217 std::string vdbFileName; 00218 00219 std::string testPosName; // Directory where test images are 00220 00221 devArg = mgr->getExtraArg(0); 00222 images = mgr->getExtraArg(1); 00223 vdbFileName = mgr->getExtraArg(2); 00224 00225 //MemoryPolicy mp = GLOBAL_DEVICE_MEMORY; 00226 int dev = strtol(devArg.c_str(),NULL,0); 00227 std::cout << "device = " << dev << std::endl; 00228 cudaSetDevice(dev); 00229 00230 LINFO("Loading db from %s\n", vdbFileName.c_str()); 00231 VisualObjectDB vdb; 00232 vdb.loadFrom(vdbFileName,false); 00233 00234 00235 std::string::size_type dirArg=images.find("dir:",0); 00236 std::string::size_type listArg=images.find("list:",0); 00237 std::string imagename; 00238 std::string::size_type spos,dpos; 00239 00240 if((dirArg == std::string::npos && 00241 listArg == std::string::npos) || 00242 (dirArg != 0 && listArg != 0)){ 00243 LFATAL("images argument is in one of the following formats - dir:<DIRNAME> or list:<LISTOFIMAGEPATHSFILE>"); 00244 return EXIT_FAILURE; 00245 } 00246 if(dirArg == 0) 00247 images = images.substr(4); 00248 else 00249 images = images.substr(5); 00250 00251 // Now we run if needed 00252 mgr->start(); 00253 00254 std::vector<std::string> imageFileNames; 00255 if(dirArg == 0) 00256 imageFileNames = readDir(images); 00257 else 00258 imageFileNames = readList(images); 00259 00260 for(unsigned int imgInd=0;imgInd<imageFileNames.size();imgInd++){ 00261 Image<float> inputf = Raster::ReadGray(imageFileNames[imgInd]); 00262 00263 //Pick off image name from full path name 00264 spos = imageFileNames[imgInd].find_last_of('/'); 00265 dpos = imageFileNames[imgInd].find_last_of('.'); 00266 imagename = imageFileNames[imgInd].substr(spos+1,dpos-spos-1); 00267 00268 std::cout << "imageFileNames[" << imgInd << "] = " << imageFileNames[imgInd] << std::endl; 00269 std::cout << "spos = " << spos << " ,dpos = " << dpos << std::endl; 00270 std::cout << "imagename = " << imagename << std::endl; 00271 00272 float score = 0.0; 00273 std::string objName = matchObject(inputf, vdb, score); 00274 printf("objName=%s,score=%f\n",objName.c_str(),score); 00275 } 00276 00277 return 0; 00278 } 00279 00280 // ###################################################################### 00281 /* So things look consistent in everyone's emacs... */ 00282 /* Local Variables: */ 00283 /* indent-tabs-mode: nil */ 00284 /* End: */