test-gbcOnMSRC.C

Go to the documentation of this file.
00001 /*!@file Learn/test-gbcOnMSRC.C QuadTree Multi-Class Classifier */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: John Shen <shenjohn@usc.edu>
00033 // $HeadURL$
00034 // $Id$
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   // LINFO("%"ZU" files in the directory\n", fList.size());
00065   // LINFO("file list : \n");
00066   // for (unsigned int i=0; i<fList.size(); i++)
00067   //         LINFO("\t%s", fList[i].c_str());
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   // manager.exportOptions(MC_RECURSE);
00085   
00086   // step 1: extract features
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   //  std::vector<std::string> files_GT = readDir(dir_GT.c_str());
00094 
00095   vc->setModelParamVal("RawVisualCortexChans",std::string("IQCOETL"));
00096   vc->setModelParamVal("LevelSpec",LevelSpec(0,0,3,3,0),MC_RECURSE); // bug in resetting pyramids, need to check this 
00097   //vc->setModelParamVal("LevelSpec",LevelSpec(2,4,3,4,4));
00098 
00099   // fix image size
00100   const Dims dims(320,240);
00101   const Dims dims_portrait(240,320);
00102   
00103   manager.start();
00104   
00105   // defs
00106   const uint Nfiles = files_in.size();
00107   const uint Nmaps = vc->numSubmaps();
00108 
00109   // Image<double> traindata(Dims(Nfiles*dims.sz(),Nmaps),ZEROS);
00110   std::vector<double> labels(Nfiles*dims.sz());
00111   Timer t(1000000);
00112   //  int id_ctr = 0;
00113   for(uint i = 0; i < Nfiles; i++) {   
00114     // get the ground truth file name 
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     // import the images
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     //convert to images and resize with correct orientation
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     // provide input to visual cortex
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         // LINFO("memory allocation of features: %zu",sizeof(features));
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     // LINFO("memory allocation of gbc: %zu", sizeof(gbc));
00163   }
00164     
00165   LINFO("training gbc");
00166   gbc->train(0);
00167   LINFO("saving gbc");
00168   gbc->save(0);
00169   //   // double * prev_ptr = traindata.beginw();
00170   //   LINFO("running through %u features for file %s", vc->numSubmaps(), fil.c_str());
00171   //   LINFO("training data array is dims %s, size %d", 
00172   //         toStr(traindata.getDims()).c_str(),traindata.size());
00173   //   for(uint i = 0; i < Nmaps; i++) {
00174       
00175   //     // naming the files
00176   //     // char * fil_feature = new char[100];
00177   //     //      sprintf(fil_feature,"/lab/jshen/Pictures/MSRC/Features/%s-map%02d",fil_nopath.c_str(),i);
00178 
00179   //     LINFO("processing feature %s", toStr(vc->getSubmapName(i)).c_str());
00180   //     // push out the raw features  
00181   //     //  Image<double> im_feat = vc->getSubmap(i); 
00182 
00183   //     // normalize the features (could be slow)
00184   //     inplaceNormalize(im_feat,0.0,1.0);
00185 
00186       
00187   //     // add entries to table
00188   //     // double * data_ptr = traindata.beginw() +
00189   //     //   dims.sz() * sizeof(double) * h + // move to the correct subcolumn
00190   //     //   traindata.getWidth() * sizeof(double) * i; // move to the correct row
00191   //     // LINFO("range is %p - %p (len %zu), pointer @ %p (jump %zu)", traindata.begin(), traindata.end(), (size_t)(traindata.end()-traindata.begin()) ,data_ptr, (size_t)(data_ptr-prev_ptr));
00192   //     // prev_ptr = data_ptr;
00193      
00194 
00195   //     // memcpy(data_ptr, im_feat.begin(), sizeof(double)*im_feat.size());
00196       
00197   //     // LINFO("copied to memory");
00198   //     //Raster::WriteGray(im_out,fil_out,RASFMT_PNG);
00199   //     //      LINFO("saved file %s for map %s", fil_out, vc->getSubmapName(i).c_str());   
00200   //   }
00201 
00202   //   LINFO("writing the GT data");
00203   //   double * label_ptr = &labels[0] + h * sizeof(double) * im_GT.size();
00204   //   memcpy(label_ptr, im_GT.begin(),sizeof(double)*im_GT.size());
00205   // }
00206   // SVMClassifier svm;
00207   // LINFO("starting training");
00208   // svm.train(traindata,labels);
00209   // LINFO("training complete");
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 /* So things look consistent in everyone's emacs... */
00230 /* Local Variables: */
00231 /* indent-tabs-mode: nil */
00232 /* End: */
Generated on Sun May 8 08:40:59 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3