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 "Image/DrawOps.H"
00040 #include "Image/Kernels.H"
00041 #include "Image/CutPaste.H"
00042 #include "Image/ColorOps.H"
00043 #include "Image/FilterOps.H"
00044 #include "Raster/Raster.H"
00045 #include "Media/FrameSeries.H"
00046 #include "Util/Timer.H"
00047 #include "Util/CpuTimer.H"
00048 #include "Util/StringUtil.H"
00049 #include "Features/LocalBinaryPatterns.H"
00050 #include "Learn/LogLikelihoodClassifier.H"
00051 #include "rutz/rand.h"
00052 #include "rutz/trace.h"
00053
00054 #include <math.h>
00055 #include <fcntl.h>
00056 #include <limits>
00057 #include <string>
00058 #include <stdio.h>
00059
00060 #define TRAIN_WIDTH 160
00061 #define TRAIN_HEIGHT 160
00062 #define SAMPLE_WIDTH 160
00063 #define SAMPLE_HEIGHT 160
00064
00065 #define TEST_SIZE 20
00066
00067 int main(const int argc, const char **argv)
00068 {
00069
00070 MYLOGVERB = LOG_INFO;
00071 ModelManager manager("Test LocalBinaryPatterns");
00072
00073
00074 rutz::urand rgen(time((time_t*)0)+getpid());
00075
00076
00077 LogLikelihoodClassifier ll = LogLikelihoodClassifier(7);
00078 std::vector<LocalBinaryPatterns> lbp;
00079
00080 lbp.push_back(LocalBinaryPatterns(2,16,0,false,true));
00081 lbp.push_back(LocalBinaryPatterns(3,24,0,false,true));
00082
00083 if (manager.parseCommandLine(
00084 (const int)argc, (const char**)argv, "<texture1file> ... <textureNfile>", 2, 200) == false)
00085 return 0;
00086
00087 manager.start();
00088
00089 uint numCategories = manager.numExtraArgs();
00090 std::vector<std::string> texFile;
00091 std::vector<Image<float> > tex;
00092 for(uint i=0;i<numCategories;i++)
00093 {
00094 texFile.push_back(manager.getExtraArg(i));
00095
00096 tex.push_back(Raster::ReadGray(texFile[i]));
00097 }
00098
00099 const Dims trainDims = Dims(TRAIN_WIDTH,TRAIN_HEIGHT);
00100 for(uint idx=0;idx<numCategories;idx++)
00101 {
00102
00103 float tw = std::min(TRAIN_WIDTH,int(tex[idx].getWidth()/2.0));
00104 float th = std::min(TRAIN_HEIGHT,int(tex[idx].getHeight()));
00105 for(uint xp=0;xp<=uint(tex[idx].getWidth()/2.0-tw);xp+=tw)
00106 for(uint yp=0;yp<=uint(tex[idx].getHeight()-th);yp+=th)
00107 {
00108 LINFO("Adding crop for id[%u] at pos [%ux%u]",idx,xp,yp);
00109 Image<float> samp = crop(tex[idx],Rectangle(Point2D<int>(xp,yp),trainDims));
00110 for(uint o=0;o<lbp.size();o++)
00111 lbp[o].addModel(toRGB(samp),idx+1);
00112 }
00113 }
00114
00115
00116 std::vector<LocalBinaryPatterns::MapModelVector> allModels;
00117 for(uint o=0;o<lbp.size();o++)
00118 {
00119 LINFO("Building variance model for LBP class [%u]",o);
00120 lbp[o].buildModels();
00121 allModels.push_back(lbp[o].getModels());
00122 }
00123 LocalBinaryPatterns::MapModelVector completeModel;
00124 lbp[0].combineModels(allModels,completeModel);
00125 ll.setModels(completeModel);
00126
00127 int numCorrect=0;
00128 const Dims sampleDims = Dims(SAMPLE_WIDTH,SAMPLE_HEIGHT);
00129
00130 for(uint s=0;s<TEST_SIZE;s++)
00131 {
00132
00133 int idx = rgen.idraw(numCategories);
00134
00135 float tw = std::min(SAMPLE_WIDTH,int(tex[idx].getWidth()/2.0));
00136 float th = std::min(SAMPLE_HEIGHT,int(tex[idx].getHeight()));
00137 int xp,yp;
00138 xp=rgen.idraw_range(tex[idx].getWidth()/2.0,int(tex[idx].getWidth()-tw));
00139 yp=rgen.idraw_range(0,int(tex[idx].getHeight()-th));
00140 Image<float> samp = crop(tex[idx],Rectangle(Point2D<int>(xp,yp),sampleDims));
00141
00142 std::vector<float> hist;
00143 for(uint o=0;o<lbp.size();o++)
00144 {
00145 std::vector<float> tmpHist = lbp[o].createHistogram(samp);
00146 hist.insert(hist.begin(),tmpHist.begin(),tmpHist.end());
00147 }
00148 int gtIdx = idx+1;
00149 std::map<int,double> pdf = ll.predictPDF(hist);
00150 std::map<int,double>::const_iterator piter;
00151 int predIdx=-1;
00152 double predMax = -std::numeric_limits<double>::max();
00153 printf("Class Probabilities: ");
00154 for(piter=pdf.begin();piter!=pdf.end();piter++)
00155 {
00156 printf("%d [%f], ",piter->first,piter->second);
00157 if(piter->second > predMax)
00158 {
00159 predMax = piter->second;
00160 predIdx = piter->first;
00161 }
00162 }
00163 printf("\n");
00164 LINFO("Index Ground Truth [%d], Predicted [%d]",gtIdx,predIdx);
00165 if(predIdx == gtIdx) numCorrect++;
00166 }
00167 LINFO("Test Accuracy %f, Random chance would be %f",float(numCorrect)/TEST_SIZE,1.0F/numCategories);
00168 manager.stop();
00169
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181