test-hmax2.C

Go to the documentation of this file.
00001 /*!@file HMAX/test-hmax2.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: Daesu Chung <dchung@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/HMAX/test-hmax2.C $
00035 // $Id: test-hmax2.C 6191 2006-02-01 23:56:12Z rjpeters $
00036 //
00037 
00038 #include "HMAX/Hmax.H"
00039 #include "Image/Image.H"
00040 #include "Image/MathOps.H"
00041 #include "Raster/Raster.H"
00042 #include "Util/Timer.H"
00043 #include "Util/Types.H"
00044 #include "Util/log.H"
00045 
00046 #include <fstream>
00047 #include <iostream>
00048 
00049 int tItr = 1;
00050 int c2Size = 0;
00051 int target = 0;
00052 std::string targetS1 = "tri";
00053 std::string targetS2 = "Tri";
00054 int imgHeight = 0;
00055 int imgWidth = 0;
00056 int trnFlag = 0;
00057 
00058 float w[] = {1,1};
00059 //## img.../data/tstData//cir9.pgm itr 41 eta 0.00477165
00060 //the sum ... 206.082   the mean 0.805006
00061 
00062 #define NORI 4
00063 
00064 int main(int argc, char **argv)
00065 {
00066         // parse options : read directory
00067         char imname[1024]; imname[0] = '\0';
00068         strncpy(imname, argv[1], 1023);
00069         trnFlag = atoi(argv[2]);
00070         std::string inName = imname;
00071 
00072         if (argc != 3)
00073                 { std::cerr<<"USAGE: test-hmax2 <dir> <trnFlag(1 or 0)>"<<std::endl; exit(1); }
00074 
00075 
00076         // weighting for IT
00077         float eta = 0.3;
00078         std::ofstream wFile("weightFile");
00079         std::ofstream c2File("c2ResSum");
00080         Image<float> wt;
00081         Image<float> wtC2res;
00082 
00083         Image<byte> input;
00084 
00085 /*
00086         Image<byte> tImg1;
00087         Image<byte> nImg1;
00088         Raster::ReadGray(tImg1, "tImg1.pgm");
00089         Raster::ReadGray(nImg1, "nImg1.pgm");
00090         Image<float> tstImg(tImg1);
00091         Image<float> nosImg(nImg1);
00092         Image<float> rstImg(tstImg+(nosImg*0.2f));
00093         Raster::VisuFloat(tstImg, FLOAT_NORM_0_255, "tstImg.pgm");
00094         Raster::VisuFloat(nosImg, FLOAT_NORM_0_255, "nosImg.pgm");
00095         Raster::VisuFloat(rstImg, FLOAT_NORM_0_255, "rstImg.pgm");
00096 */
00097 
00098         // pass image through Hmax model:
00099         std::vector<int> scss(5);
00100         scss[0] = 0; scss[1] = 2; scss[2] = 5; scss[3] = 8; scss[4] = 12;
00101         std::vector<int> spss(4);
00102         spss[0] = 4; spss[1] = 6; spss[2] = 9; spss[3] = 12;
00103         Hmax hmax(NORI, spss, scss);
00104         Timer tim; tim.reset();
00105 
00106         if (trnFlag == 0)       tItr = 1;
00107 
00108         //read a directory for train/rest imgs.
00109         std::vector<std::string> fileList = hmax.readDir(inName);
00110         int listSize = fileList.size();
00111         Image<float> oc2resp[listSize];
00112 
00113         //for batch process : read and do hmax for all imgs at once.
00114         for(int imgInd = 0; imgInd < listSize; imgInd++) {
00115                 input = Raster::ReadGray(fileList[imgInd], RASFMT_PNM);
00116                 Image<float> inputf(input);
00117                 oc2resp[imgInd]=hmax.origGetC2(inputf);
00118                 //Raster::VisuFloat(oc2resp, FLOAT_NORM_0_255, "oc2resp.pgm");
00119                 imgHeight = inputf.getHeight();
00120                 imgWidth = inputf.getWidth();
00121                 c2Size = oc2resp[imgInd].getHeight();
00122 
00123                 //write the C2 result to a file
00124                 /*
00125                 if (c2File.is_open()) {
00126                         c2File << "## image " << fileList[imgInd] << std::endl;
00127                         for(int y = 0; y < 16; y ++) {
00128                                 for(int x = 0; x < 16; x ++)
00129                                         c2File << oc2resp[imgInd].getVal(x, y) << " ";
00130                                 }
00131                         c2File << std::endl;
00132                         c2File <<"the sum "<<sum(oc2resp[imgInd])<<" the mean "<<mean(oc2resp[imgInd])<<std::endl;
00133                 }//end of C2 result writing
00134                 */
00135                 if (c2File.is_open()) {
00136                         c2File << sum(oc2resp[imgInd]) << ", ";
00137                         if (imgInd%10 == 0)
00138                                 c2File << std::endl;
00139                 }//end of C2 result writing
00140         }
00141 
00142         //Initialize the weight with rand. number b/w 0 and 1
00143         wt.resize(c2Size, c2Size, true);
00144         srand(time(NULL));
00145         for(int y = 0; y < c2Size; y++) {
00146                 for(int x = 0; x < c2Size; x++) {
00147                         float r = rand()%10;
00148                         wt.setVal(x,y,r/10);
00149                 }
00150         }
00151 
00152         for(int itr = 0; itr < tItr; itr++) {
00153                 eta = eta - (itr * (9*eta)/(10*tItr) );
00154                 for(int imgInd = 0; imgInd < listSize; imgInd++) {
00155                         //training****************************
00156                         if(trnFlag == 1) {
00157                                 float udWt = 0;
00158                                 //set the target
00159                                 std::string::size_type where1 = fileList[imgInd].find(targetS1);
00160                                 std::string::size_type where2 = fileList[imgInd].find(targetS2);
00161                                 if ((where1 == std::string::npos) && (where2 == std::string::npos))
00162                                         target = -1;
00163                                 else target = 1;
00164                                 //std::cout << "target...  " << target << std::endl;
00165 
00166                                 //update the weight mat.
00167                                 for(int y = 0; y < c2Size; y++) {
00168                                         for(int x = 0; x < c2Size; x++) {
00169                                                 if (target == 1)
00170                                                         udWt = wt.getVal(x,y) * (1 + eta*oc2resp[imgInd].getVal(x,y));
00171                                                 else if (target == -1)
00172                                                         udWt = wt.getVal(x,y) * (1 - eta*oc2resp[imgInd].getVal(x,y));
00173                                                 wt.setVal(x,y,udWt);
00174                                         }
00175                                 } // end of update the weight
00176 
00177                                 inplaceNormalize(wt, 0.0f, 1.0f);
00178                         } //end of training
00179 
00180                         //testing*****************************
00181                         else {
00182                                 wtC2res.resize(c2Size, c2Size, true);
00183                                 for(int y = 0; y < 16; y ++) {
00184                                         for(int x = 0; x < 16; x ++)
00185                                                 wtC2res.setVal(x,y,w[(y*16) + x]);
00186                                 }
00187                                 wt = wtC2res * oc2resp[imgInd];
00188                         } //end of testing
00189 
00190                         //write the result to a file
00191                         if (wFile.is_open()) {
00192 
00193                                 wFile <<"## img.." <<fileList[imgInd]<<" itr "<<itr<<" eta "<<eta<<std::endl;
00194                                 for(int y = 0; y < 16; y ++) {
00195                                         for(int x = 0; x < 16; x ++)
00196                                                 wFile << wt.getVal(x, y) << ", ";
00197                                 }
00198                                 wFile << std::endl;
00199                                 wFile << "the sum ... "<<sum(wt)<<"   the mean "<<mean(wt)<<std::endl;
00200 
00201                                 /*
00202                                 wFile << sum(wt) << ", ";
00203                                 if(imgInd%10 == 0)
00204                                         wFile << std::endl;
00205                                 */
00206 
00207                         }//end of file writing
00208                 } // itr
00209         } // imgInd
00210 
00211   //Image<float> c2resp = hmax.getC2(inputf);
00212   //LDEBUG("c2Resp computed in %llums", tim.getReset());
00213   //Image<float> oc2resp=hmax.origGetC2(inputf);
00214   //LDEBUG("original c2Resp computed in %llums", tim.get());
00215   //Raster::VisuFloat(c2resp, FLOAT_NORM_0_255, "c2resp.pgm");
00216   return 0;
00217 } //end of main
00218 
00219 // ######################################################################
00220 /* So things look consistent in everyone's emacs... */
00221 /* Local Variables: */
00222 /* indent-tabs-mode: nil */
00223 /* End: */
Generated on Sun May 8 08:40:41 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3