test-gbcOnMSRC.C
Go to the documentation of this file.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 #include "Channels/RawVisualCortex.H"
00038 #include "Component/ModelComponent.H"
00039 #include "Component/ModelManager.H"
00040 #include "GUI/DebugWin.H"
00041 #include "Image/Dims.H"
00042 #include "Image/Image.H"
00043 #include "Image/ShapeOps.H"
00044 #include "Learn/GentleBoostComponent.H"
00045 #include "Raster/GenericFrame.H"
00046 #include "Raster/Raster.H"
00047 #include "Util/FileUtil.H"
00048 #include "Util/Timer.H"
00049 #include <iomanip>
00050
00051 std::vector<std::string> readDir(std::string inName)
00052 {
00053 DIR *dp = opendir(inName.c_str());
00054 if(dp == NULL)
00055 {
00056 LFATAL("Directory does not exist %s",inName.c_str());
00057 }
00058 dirent *dirp;
00059 std::vector<std::string> fList;
00060 while ((dirp = readdir(dp)) != NULL ) {
00061 if (dirp->d_name[0] != '.')
00062 fList.push_back(inName + '/' + std::string(dirp->d_name));
00063 }
00064
00065
00066
00067
00068 std::sort(fList.begin(),fList.end());
00069 return fList;
00070 }
00071
00072 int submain(const int argc, char** argv)
00073 {
00074 MYLOGVERB = LOG_INFO;
00075
00076 ModelManager manager("Test SVM on MSRC database");
00077
00078 nub::ref<RawVisualCortex> vc(new RawVisualCortex(manager));
00079 manager.addSubComponent(vc);
00080
00081 nub::ref<GentleBoostComponent> gbc(new GentleBoostComponent(manager));
00082 manager.addSubComponent(gbc);
00083
00084
00085
00086
00087 if (manager.parseCommandLine((const int)argc, (const char**)argv, "<image directory> <ground truth directory>", 2, 2) == false)
00088 return 1;
00089
00090 std::string dir_in = manager.getExtraArg(0).c_str();
00091 std::string dir_GT = manager.getExtraArg(1).c_str();
00092 std::vector<std::string> files_in = readDir(dir_in.c_str());
00093
00094
00095 vc->setModelParamVal("RawVisualCortexChans",std::string("IQCOETL"));
00096 vc->setModelParamVal("LevelSpec",LevelSpec(0,0,3,3,0),MC_RECURSE);
00097
00098
00099
00100 const Dims dims(320,240);
00101 const Dims dims_portrait(240,320);
00102
00103 manager.start();
00104
00105
00106 const uint Nfiles = files_in.size();
00107 const uint Nmaps = vc->numSubmaps();
00108
00109
00110 std::vector<double> labels(Nfiles*dims.sz());
00111 Timer t(1000000);
00112
00113 for(uint i = 0; i < Nfiles; i++) {
00114
00115 std::string fil = files_in[i].c_str();
00116 std::string fil_nopath;
00117 splitPath(fil, dir_in, fil_nopath);
00118
00119 std::string fil_GT = fil_nopath;
00120 fil_GT.insert(fil_GT.length()-4, "_GT");
00121 fil_GT = dir_GT + fil_GT;
00122
00123
00124 const GenericFrame input = Raster::ReadFrame(fil.c_str());
00125 const GenericFrame input_GT = Raster::ReadFrame(fil_GT.c_str());
00126
00127 Image<PixRGB<byte> > im(dims,NO_INIT);
00128 Image<byte> im_GT(dims, NO_INIT);
00129
00130 Dims currdims;
00131
00132 if(im.getWidth() > im.getHeight())
00133 currdims = dims;
00134 else
00135 currdims = dims_portrait;
00136 im = rescale(input.asRgb(), currdims);
00137 im_GT = rescale(input_GT.asGray(), currdims);
00138
00139
00140 vc->input(InputFrame::fromRgb(&im));
00141
00142 Point2D<int> P(0,0);
00143
00144 t.reset();
00145 std::vector<float> features;
00146 std::vector<Image<float> > feat_maps;
00147 for(uint j = 0; j < Nmaps; j++) feat_maps.push_back(vc->getSubmap(j));
00148
00149 for(P.i = 0; P.i < currdims.w(); P.i++)
00150 for(P.j = 0; P.j < currdims.h(); P.j++) {
00151 for(uint j = 0; j < Nmaps; j++)
00152 features.push_back(feat_maps[j][P]);
00153
00154 gbc->addTrainVector(features,im_GT[P],0);
00155
00156 features.clear();
00157 }
00158 feat_maps.clear();
00159 t.pause();
00160 LINFO("redone with image %d/%d, %s elapsed",i+1,Nfiles,
00161 toStr(t.getSimTime()).c_str());
00162
00163 }
00164
00165 LINFO("training gbc");
00166 gbc->train(0);
00167 LINFO("saving gbc");
00168 gbc->save(0);
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 manager.stop();
00211 return 0;
00212 }
00213
00214 extern "C" int main(const int argc, char** argv)
00215 {
00216 try
00217 {
00218 return submain(argc, argv);
00219 }
00220 catch (...)
00221 {
00222 REPORT_CURRENT_EXCEPTION;
00223 }
00224
00225 return 1;
00226 }
00227
00228
00229
00230
00231
00232