testcudacbcl.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 #include "Component/ModelManager.H"
00039 #include "GUI/XWindow.H"
00040 #include "Learn/SVMClassifier.H"
00041 #include "CUDA/CudaHmaxCBCL.H"
00042 #include "Image/Image.H"
00043 #include "Image/ColorOps.H"
00044 #include "Image/CutPaste.H"
00045 #include "Image/Rectangle.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/MatrixOps.H"
00048 #include "Image/Transforms.H"
00049 #include "Image/Convolutions.H"
00050 #include "Learn/svm.h"
00051 #include "Media/FrameSeries.H"
00052 #include "nub/ref.h"
00053 #include "Raster/GenericFrame.H"
00054 #include "Raster/Raster.H"
00055 #include "Util/Types.H"
00056 #include "Util/log.H"
00057 #include "Util/Timer.H"
00058
00059 #include <fstream>
00060 #include <iostream>
00061 #include <iomanip>
00062 #include <string>
00063 #include <unistd.h>
00064 #include <cstdlib>
00065
00066
00067
00068 #define NORI 4
00069 #define NUM_PATCHES_PER_SIZE 250
00070
00071 int main(const int argc, const char **argv)
00072 {
00073
00074 MYLOGVERB = LOG_INFO;
00075
00076 ModelManager *mgr = new ModelManager("Test Hmax with Feature Learning");
00077
00078 mgr->exportOptions(MC_RECURSE);
00079
00080
00081
00082
00083
00084 if (mgr->parseCommandLine(
00085 (const int)argc, (const char**)argv, "<cudadev> <c0patches> <c1patches> <dir|list:images> <svmModel> <svmRange> <noprob|withprob:outputfile>", 7, 7) == false)
00086 return 1;
00087
00088 std::string c0Patches;
00089 std::string c1Patches;
00090 std::string images,svmModel,svmRange,devArg;
00091 std::string answerFileName;
00092
00093 devArg = mgr->getExtraArg(0);
00094 c0Patches = mgr->getExtraArg(1);
00095 c1Patches = mgr->getExtraArg(2);
00096 images = mgr->getExtraArg(3);
00097 svmModel = mgr->getExtraArg(4);
00098 svmRange = mgr->getExtraArg(5);
00099 answerFileName = mgr->getExtraArg(6);
00100
00101 int dev = strtol(devArg.c_str(),NULL,0);
00102 CudaDevices::setCurrentDevice(dev);
00103 std::string::size_type dirArg=images.find("dir:",0);
00104 std::string::size_type listArg=images.find("list:",0);
00105 if((dirArg == std::string::npos &&
00106 listArg == std::string::npos) ||
00107 (dirArg != 0 && listArg != 0)){
00108 LFATAL("images argument is in one of the following formats - dir:<DIRNAME> or list:<LISTOFIMAGEPATHSFILE>");
00109 return EXIT_FAILURE;
00110 }
00111 if(dirArg == 0)
00112 images = images.substr(4);
00113 else
00114 images = images.substr(5);
00115
00116 std::string::size_type noProbArg=answerFileName.find("noprob:",0);
00117 std::string::size_type withProbArg=answerFileName.find("withprob:",0);
00118
00119 if((noProbArg == std::string::npos &&
00120 withProbArg == std::string::npos) ||
00121 (noProbArg != 0 && withProbArg != 0)){
00122 LFATAL("output file argument is in one of the following formats - noprob:<FILENAME> or withprob:<FILENAME>");
00123 return EXIT_FAILURE;
00124 }
00125 if(noProbArg == 0)
00126 answerFileName = answerFileName.substr(7);
00127 else
00128 answerFileName = answerFileName.substr(9);
00129
00130
00131
00132
00133 mgr->start();
00134
00135 CudaHmaxCBCL hmax(c0Patches,c1Patches);
00136
00137
00138 SVMClassifier svm;
00139 svm.readModel(svmModel);
00140 svm.readRange(svmRange);
00141
00142 std::vector<std::string> imageNames;
00143 if(dirArg == 0)
00144 imageNames = hmax.readDir(images);
00145 else
00146 imageNames = hmax.readList(images);
00147
00148 std::ofstream answerFile;
00149 answerFile.open(answerFileName.c_str(),std::ios::out);
00150
00151 for(unsigned int imgInd=0;imgInd<imageNames.size();imgInd++){
00152
00153 Image<float> inputf = Raster::ReadGrayNTSC(imageNames[imgInd]);
00154 Timer tim;
00155 tim.reset();
00156 hmax.getC2(inputf.getArrayPtr(),inputf.getWidth(),inputf.getHeight());
00157 printf("CUDA CBCL HMAX %f secs\n",tim.getSecs());
00158 int numC2 = hmax.numC2Features();
00159 float *c2 = hmax.getC2Features();
00160 double prob;
00161 double pred = svm.predict(c2,numC2,&prob);
00162 printf("Prediction is %f, prob is %f\n",pred,prob);
00163 int predId = (int) pred;
00164 answerFile << predId;
00165
00166 if(withProbArg == 0)
00167 answerFile << "\t" << prob;
00168 answerFile << std::endl;
00169 hmax.clearC2();
00170 }
00171
00172 answerFile.close();
00173 return 0;
00174 }
00175
00176
00177
00178
00179
00180
00181