00001 /*!@file src/Features/test-LocalBinaryPatterns.C */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Dan Parks <danielfp@usc.edu> 00034 // $HeadURL$ 00035 // $Id$ 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 // Create random number generator 00074 rutz::urand rgen(time((time_t*)0)+getpid()); 00075 00076 // Create log likelihood classifier and local binary patterns objects 00077 LogLikelihoodClassifier ll = LogLikelihoodClassifier(7); 00078 std::vector<LocalBinaryPatterns> lbp; 00079 //lbp.push_back(LocalBinaryPatterns(1,8,0,false,true)); 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 // Load textures 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 // Take disjoint crops from left side of textures to train as models 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 // Build model 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 // Take random crops from right side of texture to test models 00130 for(uint s=0;s<TEST_SIZE;s++) 00131 { 00132 // Pick a random texture 00133 int idx = rgen.idraw(numCategories); 00134 // Select crop from the right side of the textures to test 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 // Load crop 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 /* So things look consistent in everyone's emacs... */ 00176 /* Local Variables: */ 00177 /* indent-tabs-mode: nil */ 00178 /* End: */ 00179 00180 00181