runcudahmaxfl.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 "CUDA/CudaHmaxFL.H"
00041 #include "CUDA/CudaHmax.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
00058 #include <fstream>
00059 #include <iostream>
00060 #include <iomanip>
00061 #include <string>
00062 #include <unistd.h>
00063 #include <cstdlib>
00064
00065
00066
00067 #define NORI 4
00068 #define NUM_PATCHES_PER_SIZE 250
00069
00070 int main(const int argc, const char **argv)
00071 {
00072
00073 MYLOGVERB = LOG_INFO;
00074
00075 ModelManager *mgr = new ModelManager("Test Hmax with Feature Learning");
00076
00077
00078 mgr->exportOptions(MC_RECURSE);
00079
00080
00081
00082
00083
00084
00085
00086 if (mgr->parseCommandLine(
00087 (const int)argc, (const char**)argv, "<cudadev> <c1patchesDir> <dir|list:images> <id> <outputfile>", 5, 5) == false)
00088 return 1;
00089
00090 std::string loadImagesOption = mgr->getExtraArg(1);
00091 std::string c1PatchesBaseDir;
00092 std::string images,idArg,devArg;
00093 std::string c2FileName;
00094
00095 std::string trainPosName;
00096
00097 int id;
00098
00099 devArg = mgr->getExtraArg(0);
00100 c1PatchesBaseDir = mgr->getExtraArg(1);
00101 images = mgr->getExtraArg(2);
00102 idArg = mgr->getExtraArg(3);
00103 c2FileName = mgr->getExtraArg(4);
00104
00105 MemoryPolicy mp = GLOBAL_DEVICE_MEMORY;
00106 int dev = strtol(devArg.c_str(),NULL,0);
00107 std::string::size_type dirArg=images.find("dir:",0);
00108 std::string::size_type listArg=images.find("list:",0);
00109 if((dirArg == std::string::npos &&
00110 listArg == std::string::npos) ||
00111 (dirArg != 0 && listArg != 0)){
00112 LFATAL("images argument is in one of the following formats - dir:<DIRNAME> or list:<LISTOFIMAGEPATHSFILE>");
00113 return EXIT_FAILURE;
00114 }
00115 if(dirArg == 0)
00116 images = images.substr(4);
00117 else
00118 images = images.substr(5);
00119
00120 id = strtol(idArg.c_str(),NULL,0);
00121
00122
00123
00124 std::vector<int> scss(9);
00125 scss[0] = 1; scss[1] = 3; scss[2] = 5; scss[3] = 7; scss[4] = 9;
00126 scss[5] = 11; scss[6] = 13; scss[7] = 15; scss[8] = 17;
00127 std::vector<int> spss(8);
00128 spss[0] = 8; spss[1] = 10; spss[2] = 12; spss[3] = 14;
00129 spss[4] = 16; spss[5] = 18; spss[6] = 20; spss[7] = 22;
00130
00131
00132
00133
00134
00135 CudaHmaxFL hmax(mp,dev,NORI, spss, scss);
00136
00137
00138
00139
00140
00141 hmax.readInC1Patches(c1PatchesBaseDir);
00142
00143 mgr->start();
00144
00145 std::vector<std::string> imageNames;
00146 if(dirArg == 0)
00147 imageNames = hmax.readDir(images);
00148 else
00149 imageNames = hmax.readList(images);
00150
00151 std::ofstream c2File;
00152 c2File.open(c2FileName.c_str(),std::ios::out);
00153
00154 std::vector<int> patchSizes = hmax.getC1PatchSizes();
00155 float **c2Res = new float*[patchSizes.size()];
00156 for(unsigned int i=0;i<patchSizes.size();i++) {
00157 c2Res[i] = new float[NUM_PATCHES_PER_SIZE];
00158 }
00159
00160 for(unsigned int imgInd=0;imgInd<imageNames.size();imgInd++){
00161 Image<float> inputf = Raster::ReadGrayNTSC(imageNames[imgInd]);
00162
00163
00164
00165
00166
00167 hmax.getC2(CudaImage<float>(inputf,mp,dev),c2Res);
00168 std::cout <<"C2 Processing Complete: " << imgInd << std::endl;
00169 if (c2File.is_open()) {
00170 c2File << id << " ";
00171 for(unsigned int i=0;i<patchSizes.size();i++) {
00172 for(int j=0;j<NUM_PATCHES_PER_SIZE;j++) {
00173 c2File << std::setiosflags(std::ios::fixed) << std::setprecision(4) <<
00174 (i*NUM_PATCHES_PER_SIZE+j+1) << ":" << c2Res[i][j] << " ";
00175 }
00176 }
00177 c2File << std::endl;
00178 }
00179 std::cout <<"C2 Output Written: " << imgInd << std::endl;
00180 }
00181
00182 for(unsigned int i=0;i<patchSizes.size();i++) {
00183 delete[] c2Res[i];
00184 }
00185 delete [] c2Res;
00186
00187 c2File.close();
00188 return 0;
00189 }
00190
00191
00192
00193
00194
00195
00196