00001 /*!@file HMAX/test-hmax5.C Test Hmax class and compare to original code */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // 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: svn://isvn.usc.edu/software/invt/trunk/saliency/src/HMAX/runhmaxfl.C $ 00035 // $Id: runhmaxfl.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "GUI/XWindow.H" 00040 #include "HMAX/HmaxFL.H" 00041 #include "HMAX/Hmax.H" 00042 #include "Image/Image.H" 00043 #include "Image/ColorOps.H" 00044 #include "Image/CutPaste.H" 00045 #include "Image/Rectangle.H" 00046 #include "Image/MathOps.H" 00047 #include "Image/MatrixOps.H" 00048 #include "Image/Transforms.H" 00049 #include "Image/Convolutions.H" 00050 #include "Learn/svm.h" 00051 #include "Media/FrameSeries.H" 00052 #include "nub/ref.h" 00053 #include "Raster/GenericFrame.H" 00054 #include "Raster/Raster.H" 00055 #include "Util/Types.H" 00056 #include "Util/log.H" 00057 00058 #include <fstream> 00059 #include <iostream> 00060 #include <iomanip> 00061 #include <string> 00062 #include <unistd.h> 00063 #include <cstdlib> 00064 00065 00066 // number of orientations to use in HmaxFL 00067 #define NORI 4 00068 #define NUM_PATCHES_PER_SIZE 250 00069 00070 int main(const int argc, const char **argv) 00071 { 00072 00073 MYLOGVERB = LOG_INFO; 00074 ModelManager *mgr = new ModelManager("Test Hmax with Feature Learning"); 00075 00076 00077 mgr->exportOptions(MC_RECURSE); 00078 00079 // required arguments 00080 // <c1patchesDir> <dir|list> <id> <outputfile> 00081 // 00082 // <id> is the given id for the given set of images 00083 // --in only needs to happen if we are loading the patches 00084 00085 if (mgr->parseCommandLine( 00086 (const int)argc, (const char**)argv, "<c1patchesDir> <dir|list:images> <id> <outputfile>", 4, 4) == false) 00087 return 1; 00088 00089 // get an HmaxFL object: 00090 std::vector<int> scss(9); 00091 scss[0] = 1; scss[1] = 3; scss[2] = 5; scss[3] = 7; scss[4] = 9; 00092 scss[5] = 11; scss[6] = 13; scss[7] = 15; scss[8] = 17; 00093 std::vector<int> spss(8); 00094 spss[0] = 8; spss[1] = 10; spss[2] = 12; spss[3] = 14; 00095 spss[4] = 16; spss[5] = 18; spss[6] = 20; spss[7] = 22; 00096 HmaxFL hmax(NORI, spss, scss); 00097 00098 std::string loadImagesOption = mgr->getExtraArg(0); 00099 std::string c1PatchesBaseDir; 00100 std::string images; 00101 std::string idArg; 00102 std::string c2FileName; 00103 00104 std::string trainPosName; // Directory where positive images are 00105 00106 int id; 00107 00108 c1PatchesBaseDir = mgr->getExtraArg(0); 00109 images = mgr->getExtraArg(1); 00110 idArg = mgr->getExtraArg(2); 00111 c2FileName = mgr->getExtraArg(3); 00112 00113 std::string::size_type dirArg=images.find("dir:",0); 00114 std::string::size_type listArg=images.find("list:",0); 00115 if((dirArg == std::string::npos && 00116 listArg == std::string::npos) || 00117 (dirArg != 0 && listArg != 0)){ 00118 LFATAL("images argument is in one of the following formats - dir:<DIRNAME> or list:<LISTOFIMAGEPATHSFILE>"); 00119 return EXIT_FAILURE; 00120 } 00121 if(dirArg == 0) 00122 images = images.substr(4); 00123 else 00124 images = images.substr(5); 00125 00126 id = strtol(idArg.c_str(),NULL,0); 00127 00128 // 00129 hmax.readInC1Patches(c1PatchesBaseDir); 00130 // Now we run if needed 00131 mgr->start(); 00132 00133 std::vector<std::string> imageNames; 00134 if(dirArg == 0) 00135 imageNames = hmax.readDir(images); 00136 else 00137 imageNames = hmax.readList(images); 00138 00139 std::ofstream c2File; 00140 c2File.open(c2FileName.c_str(),std::ios::out); 00141 00142 for(unsigned int imgInd=0;imgInd<imageNames.size();imgInd++){ 00143 Image<float> inputf = Raster::ReadGrayNTSC(imageNames[imgInd]); 00144 std::vector<int> patchSizes = hmax.getC1PatchSizes(); 00145 00146 // Output the c2 responses per patch into a libsvm 00147 // (or equivalently osusvm) style format 00148 00149 float **c2Res = new float*[patchSizes.size()]; 00150 for(unsigned int i=0;i<patchSizes.size();i++) { 00151 c2Res[i] = new float[NUM_PATCHES_PER_SIZE]; 00152 } 00153 //hmax.printCorners("inputf",inputf,1); 00154 hmax.getC2(inputf,c2Res); 00155 std::cout <<"C2 Processing Complete: " << imgInd << std::endl; 00156 if (c2File.is_open()) { 00157 c2File << id << " "; 00158 for(unsigned int i=0;i<patchSizes.size();i++) { 00159 for(int j=0;j<NUM_PATCHES_PER_SIZE;j++) { 00160 c2File << std::setiosflags(std::ios::fixed) << std::setprecision(4) << 00161 (i*NUM_PATCHES_PER_SIZE+j+1) << ":" << c2Res[i][j] << " "; 00162 } 00163 } 00164 c2File << std::endl; 00165 } 00166 std::cout <<"C2 Output Written: " << imgInd << std::endl; 00167 for(unsigned int i=0;i<patchSizes.size();i++) { 00168 delete[] c2Res[i]; 00169 } 00170 delete [] c2Res; 00171 } 00172 c2File.close(); 00173 return 0; 00174 } 00175 00176 00177 // ###################################################################### 00178 /* So things look consistent in everyone's emacs... */ 00179 /* Local Variables: */ 00180 /* indent-tabs-mode: nil */ 00181 /* End: */