runcudasiftfl.C
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 #include "Component/ModelManager.H"
00040 #include "GUI/XWindow.H"
00041 #include "Image/Image.H"
00042 #include "Image/ColorOps.H"
00043 #include "Image/CutPaste.H"
00044 #include "Image/Rectangle.H"
00045 #include "Image/MathOps.H"
00046 #include "Image/MatrixOps.H"
00047 #include "Image/Transforms.H"
00048 #include "Image/Convolutions.H"
00049 #include "Media/FrameSeries.H"
00050 #include "nub/ref.h"
00051 #include "Raster/GenericFrame.H"
00052 #include "Raster/Raster.H"
00053 #include "Util/Types.H"
00054 #include "Util/log.H"
00055 #include "SIFT/ScaleSpace.H"
00056 #include "SIFT/VisualObject.H"
00057 #include "SIFT/Keypoint.H"
00058 #include "SIFT/VisualObjectDB.H"
00059 
00060 #include "CUDASIFT/CUDAVisualObject.H"
00061 
00062 #include "CUDASIFT/tpimageutil.h"
00063 #include "CUDASIFT/tpimage.h"
00064 #include "CUDASIFT/cudaImage.h"
00065 #include "CUDASIFT/cudaSift.h"
00066 #include "CUDASIFT/cudaSiftH.h" 
00067 
00068 #include "Util/log.H"
00069 
00070 #include <fstream>
00071 #include <iostream>
00072 #include <iomanip>
00073 #include <string>
00074 #include <unistd.h>
00075 #include <cstdio>
00076 #include <dirent.h>
00077 
00078 
00079 std::vector<std::string> readDir(std::string inName)
00080 {
00081   DIR *dp = opendir(inName.c_str());
00082   if(dp == NULL)
00083     {
00084       LFATAL("Directory does not exist %s",inName.c_str());
00085     }
00086   dirent *dirp;
00087   std::vector<std::string> fList;
00088   while ((dirp = readdir(dp)) != NULL ) {
00089     if (dirp->d_name[0] != '.')
00090       fList.push_back(inName + '/' + std::string(dirp->d_name));
00091   }
00092   LINFO("%"ZU" files in the directory\n", fList.size());
00093   LINFO("file list : \n");
00094   for (unsigned int i=0; i<fList.size(); i++)
00095     LINFO("\t%s", fList[i].c_str());
00096   std::sort(fList.begin(),fList.end());
00097   return fList;
00098 }
00099 
00100 
00101 std::vector<std::string> readList(std::string inName)
00102 {
00103   std::ifstream inFile;
00104   inFile.open(inName.c_str(),std::ios::in);
00105   if(!inFile){
00106     LFATAL("Unable to open image path list file: %s",inName.c_str());
00107   }
00108   std::string sLine;
00109   std::vector<std::string> fList;
00110   while (std::getline(inFile, sLine)) {
00111     std::cout << sLine << std::endl;
00112     fList.push_back(sLine);
00113   }
00114   LINFO("%"ZU" paths in the file\n", fList.size());
00115   LINFO("file list : \n");
00116   for (unsigned int i=0; i<fList.size(); i++)
00117     LINFO("\t%s", fList[i].c_str());
00118   inFile.close();
00119   return fList;
00120 }
00121 
00122 
00123 int main(const int argc, const char **argv)
00124 {
00125 
00126   MYLOGVERB = LOG_INFO;
00127 
00128   ModelManager *mgr = new ModelManager("Test SIFT with Feature Learning");
00129 
00130 
00131   mgr->exportOptions(MC_RECURSE);
00132 
00133   
00134   
00135 
00136   if (mgr->parseCommandLine(
00137                             (const int)argc, (const char**)argv,
00138                             "<cudadev> <dir|list:images> <vdbfile>", 3, 3) == false)
00139     return 1;
00140 
00141   std::string images,devArg;
00142   std::string vdbFileName;
00143 
00144   std::string trainPosName; 
00145 
00146   devArg = mgr->getExtraArg(0);
00147   images = mgr->getExtraArg(1);
00148   vdbFileName = mgr->getExtraArg(2);
00149 
00150   
00151   int dev = strtol(devArg.c_str(),NULL,0);
00152   std::cout << "device = " << dev << std::endl;
00153   cudaSetDevice(dev);
00154 
00155   LINFO("Loading db from %s\n", vdbFileName.c_str());
00156   VisualObjectDB vdb;
00157   vdb.loadFrom(vdbFileName,false);
00158 
00159 
00160   std::string::size_type dirArg=images.find("dir:",0);
00161   std::string::size_type listArg=images.find("list:",0);
00162   std::string imagename;
00163   std::string::size_type spos,dpos;
00164 
00165   if((dirArg == std::string::npos &&
00166       listArg == std::string::npos) ||
00167      (dirArg != 0 && listArg != 0)){
00168     LFATAL("images argument is in one of the following formats -  dir:<DIRNAME>  or  list:<LISTOFIMAGEPATHSFILE>");
00169     return EXIT_FAILURE;
00170   }
00171   if(dirArg == 0)
00172     images = images.substr(4);
00173   else
00174     images = images.substr(5);
00175 
00176   
00177   mgr->start();
00178 
00179   std::vector<std::string> imageFileNames;
00180   if(dirArg == 0)
00181     imageFileNames = readDir(images);
00182   else
00183     imageFileNames = readList(images);
00184 
00185   for(unsigned int imgInd=0;imgInd<imageFileNames.size();imgInd++){
00186     Image<float> inputf = Raster::ReadGray(imageFileNames[imgInd]);
00187 
00188     
00189     spos = imageFileNames[imgInd].find_last_of('/');
00190     dpos = imageFileNames[imgInd].find_last_of('.');
00191     imagename = imageFileNames[imgInd].substr(spos+1,dpos-spos-1);
00192 
00193     std::cout << "imageFileNames[" << imgInd << "] = " << imageFileNames[imgInd] << std::endl;
00194     std::cout << "spos = " << spos << " ,dpos = " << dpos << std::endl;
00195     std::cout << "imagename = " << imagename << std::endl;
00196 #ifdef GPUSIFT
00197     rutz::shared_ptr<CUDAVisualObject>
00198       vo(new CUDAVisualObject(imagename, imageFileNames[imgInd], inputf,
00199                               Point2D<int>(-1,-1),
00200                               std::vector<float>(),
00201                               std::vector< rutz::shared_ptr<Keypoint> >(),
00202                               false,true));
00203 
00204 #else
00205       rutz::shared_ptr<VisualObject>
00206         vo(new VisualObject(imagename,imageFileNames[imgInd], inputf,
00207                               Point2D<int>(-1,-1),
00208                               std::vector<float>(),
00209                               std::vector< rutz::shared_ptr<Keypoint> >(),
00210                               false,true));
00211 #endif
00212     vdb.addObject(vo);
00213   }
00214 
00215   std::cout << "Keypoints computed for all images." << std::endl;
00216   std::cout << "Saving visual objects to database." << std::endl;
00217   vdb.saveTo(vdbFileName);
00218 
00219   return 0;
00220 }
00221 
00222 
00223 
00224 
00225 
00226