testhmaxfl.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 "HMAX/HmaxFL.H"
00041 #include "Learn/SVMClassifier.H"
00042 #include "HMAX/Hmax.H"
00043 #include "Image/Image.H"
00044 #include "Image/ColorOps.H"
00045 #include "Image/CutPaste.H"
00046 #include "Image/Rectangle.H"
00047 #include "Image/MathOps.H"
00048 #include "Image/MatrixOps.H"
00049 #include "Image/Transforms.H"
00050 #include "Image/Convolutions.H"
00051 #include "Learn/svm.h"
00052 #include "Media/FrameSeries.H"
00053 #include "nub/ref.h"
00054 #include "Raster/GenericFrame.H"
00055 #include "Raster/Raster.H"
00056 #include "Util/Types.H"
00057 #include "Util/log.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
00079 mgr->exportOptions(MC_RECURSE);
00080
00081
00082
00083
00084
00085
00086
00087 if (mgr->parseCommandLine(
00088 (const int)argc, (const char**)argv, "<c1patchesDir> <dir|list:images> <svmModel> <svmRange> <outputfile>", 5, 5) == false)
00089 return 1;
00090
00091 std::string c1PatchesBaseDir;
00092 std::string images,svmModel,svmRange,devArg;
00093 std::string answerFileName;
00094
00095 c1PatchesBaseDir = mgr->getExtraArg(0);
00096 images = mgr->getExtraArg(1);
00097 svmModel = mgr->getExtraArg(2);
00098 svmRange = mgr->getExtraArg(3);
00099 answerFileName = mgr->getExtraArg(4);
00100
00101 std::string::size_type dirArg=images.find("dir:",0);
00102 std::string::size_type listArg=images.find("list:",0);
00103 if((dirArg == std::string::npos &&
00104 listArg == std::string::npos) ||
00105 (dirArg != 0 && listArg != 0)){
00106 LFATAL("images argument is in one of the following formats - dir:<DIRNAME> or list:<LISTOFIMAGEPATHSFILE>");
00107 return EXIT_FAILURE;
00108 }
00109 if(dirArg == 0)
00110 images = images.substr(4);
00111 else
00112 images = images.substr(5);
00113
00114
00115 mgr->start();
00116
00117
00118
00119 std::vector<int> scss(9);
00120 scss[0] = 1; scss[1] = 3; scss[2] = 5; scss[3] = 7; scss[4] = 9;
00121 scss[5] = 11; scss[6] = 13; scss[7] = 15; scss[8] = 17;
00122 std::vector<int> spss(8);
00123 spss[0] = 8; spss[1] = 10; spss[2] = 12; spss[3] = 14;
00124 spss[4] = 16; spss[5] = 18; spss[6] = 20; spss[7] = 22;
00125
00126
00127
00128
00129
00130 HmaxFL hmax(NORI, spss, scss);
00131
00132
00133 SVMClassifier svm;
00134 svm.readModel(svmModel);
00135 svm.readRange(svmRange);
00136
00137
00138 hmax.readInC1Patches(c1PatchesBaseDir);
00139
00140 std::vector<std::string> imageNames;
00141 if(dirArg == 0)
00142 imageNames = hmax.readDir(images);
00143 else
00144 imageNames = hmax.readList(images);
00145
00146 std::ofstream answerFile;
00147 answerFile.open(answerFileName.c_str(),std::ios::out);
00148
00149
00150
00151
00152 std::vector<int> patchSizes = hmax.getC1PatchSizes();
00153 float **c2Res = new float*[patchSizes.size()];
00154 for(unsigned int i=0;i<patchSizes.size();i++) {
00155 c2Res[i] = new float[NUM_PATCHES_PER_SIZE];
00156 }
00157
00158 for(unsigned int imgInd=0;imgInd<imageNames.size();imgInd++){
00159
00160 Image<float> inputf = Raster::ReadGrayNTSC(imageNames[imgInd]);
00161 hmax.getC2(inputf,c2Res);
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 double pred = svm.predict(c2Res,patchSizes.size(),NUM_PATCHES_PER_SIZE);
00175 printf("Prediction is %f\n",pred);
00176 int predId = (int) pred;
00177 answerFile << predId;
00178 answerFile << std::endl;
00179
00180 }
00181
00182 for(unsigned int i=0;i<patchSizes.size();i++) {
00183 delete[] c2Res[i];
00184 }
00185 delete [] c2Res;
00186
00187 answerFile.close();
00188 return 0;
00189 }
00190
00191
00192
00193
00194
00195
00196