00001 /*!@file src/Features/test-JunctionHOG.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/JunctionHOG.H" 00050 #include "Learn/SVMClassifier.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 <cstdio> 00059 #include <cstdlib> 00060 #include <dirent.h> 00061 00062 #define TRAIN_WIDTH 160 00063 #define TRAIN_HEIGHT 160 00064 #define SAMPLE_WIDTH 160 00065 #define SAMPLE_HEIGHT 160 00066 00067 #define TEST_SIZE 20 00068 00069 std::vector<std::string> readDir(std::string inName) 00070 { 00071 DIR *dp = opendir(inName.c_str()); 00072 if(dp == NULL) 00073 { 00074 LFATAL("Directory does not exist %s",inName.c_str()); 00075 } 00076 dirent *dirp; 00077 std::vector<std::string> fList; 00078 while ((dirp = readdir(dp)) != NULL ) { 00079 if (dirp->d_name[0] != '.') 00080 fList.push_back(inName + '/' + std::string(dirp->d_name)); 00081 } 00082 LINFO("%"ZU" files in the directory\n", fList.size()); 00083 LINFO("file list : \n"); 00084 for (unsigned int i=0; i<fList.size(); i++) 00085 LINFO("\t%s", fList[i].c_str()); 00086 std::sort(fList.begin(),fList.end()); 00087 return fList; 00088 } 00089 00090 00091 00092 int main(const int argc, const char **argv) 00093 { 00094 00095 MYLOGVERB = LOG_INFO; 00096 ModelManager manager("Test JunctionHOG"); 00097 00098 // Create random number generator 00099 rutz::urand rgen(time((time_t*)0)+getpid()); 00100 00101 // Create svm class so we can write out feature vectors 00102 SVMClassifier svm; 00103 00104 if (manager.parseCommandLine( 00105 (const int)argc, (const char**)argv, "<usejunctions> <outputdir> <obj1dir> ... <objNdir>", 4, 20) == false) 00106 return 0; 00107 00108 manager.start(); 00109 00110 00111 00112 uint numCategories = manager.numExtraArgs()-2; 00113 int useJunc = atoi(manager.getExtraArg(0).c_str()); 00114 std::string outputDir = manager.getExtraArg(1); 00115 HistogramOfGradients *hog; 00116 bool normalizeHistogram = true; 00117 bool fixedHistogram = true; // if false, cell size fixed 00118 Dims cellSize = Dims(8,8); // if fixedHist is true, this is hist size, if false, this is cell size 00119 // Create Junction Histogram of Gradients Class or vanilla HOG 00120 if(useJunc == 1) 00121 { 00122 LINFO("Creating JunctionHOG class, useJunc %d, orig str %s",useJunc,manager.getExtraArg(0).c_str()); 00123 hog = new JunctionHOG(normalizeHistogram,cellSize,fixedHistogram); 00124 } 00125 else 00126 { 00127 LINFO("Creating HistogramOfGradients class, useJunc %d, orig str %s",useJunc,manager.getExtraArg(0).c_str()); 00128 hog = new HistogramOfGradients(normalizeHistogram,cellSize,fixedHistogram); 00129 } 00130 00131 for(uint i=0;i<numCategories;i++) 00132 { 00133 // Create output file 00134 uint argIdx = i+2; 00135 uint idx = i+1; 00136 std::string fName = outputDir; 00137 fName.append(sformat("/Obj%u.out",idx)); 00138 std::string inputDir = manager.getExtraArg(argIdx); 00139 LINFO("Will append files from dir %s to output file %s",inputDir.c_str(),fName.c_str()); 00140 std::vector<std::string> fileList = readDir(inputDir); 00141 // For each file in the directory, calculate a histogram 00142 for(uint f=0;f<fileList.size();f++) 00143 { 00144 Image<PixRGB<byte> > img = Raster::ReadRGB(fileList[f]); 00145 Image<float> lum,rg,by; 00146 getLAB(img, lum, rg, by); 00147 std::vector<float> hist=hog->createHistogram(lum,rg,by); 00148 svm.train(fName,idx,hist); 00149 } 00150 } 00151 LINFO("Use the files to test the classification performance of this feature vector"); 00152 manager.stop(); 00153 00154 } 00155 00156 00157 00158 // ###################################################################### 00159 /* So things look consistent in everyone's emacs... */ 00160 /* Local Variables: */ 00161 /* indent-tabs-mode: nil */ 00162 /* End: */ 00163 00164 00165