GSnavResult.H

00001 /*!@file Beobot/GSNavResult.H read and summarize result files           */
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 // Primary maintainer for this file: Christian Siagian <siagian@usc.edu>
00032 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/GSnavResult.H $
00033 // $Id: GSnavResult.H 14376 2011-01-11 02:44:34Z pez $
00034 
00035 // ######################################################################
00036 
00037 #ifndef BEOBOT_GSNAV_RESULT_H_DEFINED
00038 #define BEOBOT_GSNAV_RESULT_H_DEFINED
00039 
00040 #include "Image/Image.H"
00041 #include "Image/MathOps.H"      // for inPlaceNormalize()
00042 
00043 // ######################################################################
00044 
00045 /*! This class is to read the GSnav result file                        */
00046 class GSnavResult
00047 {
00048 public:
00049 
00050   // input information (on segment)
00051   std::vector<double> segError;
00052   std::vector<uint>   segCount;
00053   std::vector<uint>   eSegCount;
00054 
00055   std::vector<uint> nSegObject;
00056   std::vector<uint> nSegObjectSearch;
00057 
00058   std::vector<uint> nSegObjectFound;
00059   std::vector<uint> nSegObjectFoundSearch;
00060 
00061   std::vector<uint> nSegObjectNotFound;
00062   std::vector<uint> nSegObjectNotFoundSearch;
00063 
00064   // input information (on frame)
00065   std::vector<double> error;
00066   double stdevError;
00067 
00068   std::vector<uint> nObject;
00069   double stdevNObject;
00070   std::vector<uint> nObjectSearch;
00071 
00072   std::vector<uint> nObjectFound;
00073   double stdevNObjectFound;
00074   std::vector<uint> nObjectFoundSearch;
00075 
00076   std::vector<uint> nObjectNotFound;
00077   double stdevNObjectNotFound;
00078   std::vector<uint> nObjectNotFoundSearch;
00079 
00080   // file information
00081   uint nsegment;
00082   std::string savePrefix;
00083 
00084   // total across segment information
00085   double terror;  uint tcount;
00086   uint tfobj;     uint tfsearch;
00087   uint tnfobj;    uint tnfsearch;
00088   uint tobj;      uint tsearch;
00089   uint itsSearchCount;
00090 
00091   inline void combine(std::string inSavePrefix);
00092   inline void read(std::string inSavePrefix, uint inNsegment);
00093   inline void createSummaryResult();
00094   inline void getSearchResult(std::vector<uint> index);
00095 };
00096 
00097 // ######################################################################
00098 inline void GSnavResult::combine(std::string inSavePrefix)
00099 {
00100   savePrefix = inSavePrefix;
00101 
00102   // open the result file
00103   std::string resFName = savePrefix + sformat("_results.txt");
00104   std::string resGSfName = savePrefix + sformat("_GS_results.txt");
00105   FILE *rfp; LINFO("res files: %s", resFName.c_str());
00106   if((rfp = fopen(resFName.c_str(),"rb")) == NULL)LFATAL("not found");
00107   FILE *gfp; LINFO("GS res files: %s", resGSfName.c_str());
00108   if((gfp = fopen(resGSfName.c_str(),"rb")) == NULL)LFATAL("not found");
00109 
00110   // get each line from result file
00111   std::vector<std::string> rfpLines;
00112   char inLine[200];
00113   while(fgets(inLine, 200, rfp) != NULL)
00114     {
00115       rfpLines.push_back(std::string(inLine));
00116     }
00117 
00118   // get each line from GS result file
00119   uint lind = 0;
00120   std::vector<std::string> cfpLines;
00121 
00122   std::string resCfName = savePrefix + sformat("_comb_results.txt");
00123   FILE *rFile = fopen(resCfName.c_str(), "at");
00124   if (rFile == NULL) LFATAL("can't create res file: %s",
00125                             resCfName.c_str());
00126 
00127   while(fgets(inLine, 200, gfp) != NULL)
00128     {
00129       // read the line
00130       char tLine[200]; uint cind;
00131       sscanf(inLine, "%d %s", &cind, tLine);
00132       LDEBUG("%6d <<%s>>", cind, tLine);
00133 
00134       // copy the non end-search indexes
00135       for(uint i = lind; i < cind; i++)
00136         {
00137           cfpLines.push_back(rfpLines[i]);
00138           LDEBUG("%s", rfpLines[i].c_str());
00139           fputs(rfpLines[i].c_str(), rFile);
00140         }
00141 
00142       // append the two lines of the end-search index
00143       std::string temp(inLine);
00144       std::string::size_type lzpos = rfpLines[cind].find_last_of('0');
00145       std::string::size_type pos = temp.find_first_not_of(' ');
00146       temp = temp.substr(pos);
00147       pos = temp.find_first_of(' ');
00148       temp = rfpLines[cind].substr(0, lzpos) + temp.substr(pos+1);
00149       cfpLines.push_back(temp);
00150       LDEBUG("[%d] %s", itsSearchCount, temp.c_str());
00151 
00152       fputs(temp.c_str(), rFile);
00153 
00154       lind = cind+1;
00155     }
00156   fclose (rFile);
00157 }
00158 
00159 // ######################################################################
00160 inline void GSnavResult::read(std::string inSavePrefix, uint inNsegment)
00161 {
00162   savePrefix = inSavePrefix;
00163   nsegment = inNsegment;
00164 
00165   // open the result file
00166   //std::string resFName = savePrefix + sformat("_comb_results.txt");
00167   std::string resFName = savePrefix + sformat("_results.txt");
00168   FILE *fp; LINFO("result file: %s", resFName.c_str());
00169   if((fp = fopen(resFName.c_str(),"rb")) == NULL)LFATAL("not found");
00170 
00171   // initialize information storage
00172   segError.resize(nsegment);
00173   segCount.resize(nsegment);
00174   eSegCount.resize(nsegment);
00175 
00176   nSegObject.resize(nsegment);
00177   nSegObjectSearch.resize(nsegment);
00178 
00179   nSegObjectFound.resize(nsegment);
00180   nSegObjectFoundSearch.resize(nsegment);
00181 
00182   nSegObjectNotFound.resize(nsegment);
00183   nSegObjectNotFoundSearch.resize(nsegment);
00184 
00185   for(uint i = 0;  i < nsegment; i++) segError[i] = 0.0F;
00186   for(uint i = 0;  i < nsegment; i++) segCount[i] = 0;
00187   for(uint i = 0;  i < nsegment; i++) eSegCount[i] = 0;
00188 
00189   for(uint i = 0;  i < nsegment; i++) nSegObjectFound[i] = 0;
00190   for(uint i = 0;  i < nsegment; i++) nSegObjectFoundSearch[i] = 0;
00191 
00192   for(uint i = 0;  i < nsegment; i++) nSegObjectNotFound[i] = 0;
00193   for(uint i = 0;  i < nsegment; i++) nSegObjectNotFoundSearch[i] = 0;
00194 
00195   // get each line result
00196   char inLine[200];
00197   itsSearchCount = 0;
00198   while(fgets(inLine, 200, fp) != NULL)
00199     {
00200       int index; uint ninput;
00201       uint snumGT; float ltravGT;
00202       uint snumRes; float ltravRes; float err;
00203 
00204       // read the line
00205       char tLine[200];
00206       sscanf(inLine, "%d %d %f %d %f %f %d %s", &index,
00207              &snumGT, &ltravGT, &snumRes, &ltravRes, &err, &ninput, tLine);
00208       LDEBUG("%6d %6d %10.6f %6d %10.6f %10.6f %6d", index,
00209              snumGT, ltravGT, snumRes, ltravRes, err, ninput);
00210 
00211       if(ninput != 0) itsSearchCount++;
00212 
00213       segError[snumGT] += double(err);
00214       segCount[snumGT]++;
00215       error.push_back(err);
00216 
00217       if(snumGT != snumRes) eSegCount[snumGT]++;
00218 
00219       // get the individual object search
00220       uint nFound  = 0; uint nFoundSearch    = 0;
00221       uint nNFound = 0; uint nNotFoundSearch = 0;
00222       std::string line(inLine);
00223       for(uint i = 0; i < ninput; i++)
00224         {
00225           // do it backwards
00226           int pos = line.find_last_of(' ');
00227           uint tnsearch = atoi(line.substr(pos+1).c_str());
00228           line = line.substr(0, pos);
00229           line = line.substr(0, line.find_last_not_of(' ')+1);
00230 
00231           pos = line.find_last_of(' ');
00232           bool tfound = !(line.substr(pos+1).compare("1"));
00233           line = line.substr(0, pos);
00234           line = line.substr(0, line.find_last_not_of(' ')+1);
00235 
00236           LDEBUG("%d %d", tfound, tnsearch);
00237 
00238           if(tfound)
00239             {
00240               nSegObjectFound[snumGT]++;
00241               nSegObjectFoundSearch[snumGT] += tnsearch;
00242               nFound++;
00243               nFoundSearch += tnsearch;
00244             }
00245           else
00246             {
00247               nSegObjectNotFound[snumGT]++;
00248               nSegObjectNotFoundSearch[snumGT] += tnsearch;
00249               nNFound++;
00250               nNotFoundSearch += tnsearch;
00251             }
00252 
00253           LDEBUG("%d %d - %d %d",
00254                 nSegObjectFound[snumGT], nSegObjectFoundSearch[snumGT],
00255                 nSegObjectNotFound[snumGT], nSegObjectNotFoundSearch[snumGT]);
00256         }
00257 
00258       // store for data of each frame
00259       nObjectFound.push_back(nFound);
00260       nObjectNotFound.push_back(nNFound);
00261       nObject.push_back(nFound + nNFound);
00262 
00263       nObjectFoundSearch.push_back(nFoundSearch);
00264       nObjectNotFoundSearch.push_back(nNotFoundSearch);
00265       nObjectSearch.push_back(nFoundSearch + nNotFoundSearch);
00266     }
00267   fclose(fp);
00268 
00269   // get across segment total
00270   terror = 0.0; tcount    = 0;
00271   tfobj  = 0;   tfsearch  = 0;
00272   tnfobj = 0;   tnfsearch = 0;
00273   tobj   = 0;   tsearch   = 0;
00274   for(uint i = 0; i < nsegment; i++)
00275     {
00276       terror+= segError[i];
00277       tcount+= segCount[i];
00278 
00279       // search related numbers
00280       nSegObject[i] = nSegObjectFound[i] + nSegObjectNotFound[i];
00281       nSegObjectSearch[i] =
00282         nSegObjectFoundSearch[i] + nSegObjectNotFoundSearch[i];
00283 
00284       tfobj     += nSegObjectFound[i];
00285       tfsearch  += nSegObjectFoundSearch[i];
00286       tnfobj    += nSegObjectNotFound[i];
00287       tnfsearch += nSegObjectNotFoundSearch[i];
00288       tobj      += nSegObject[i];
00289       tsearch   += nSegObjectSearch[i];
00290     }
00291 
00292   // get total standard deviation
00293   Image<double> t1(1,nObjectFound.size(),NO_INIT);
00294   for(uint i = 0; i < nObjectFound.size(); i++)
00295     t1.setVal(0, i, double(nObjectFound[i]));
00296   stdevNObjectFound = stdev(t1);
00297 
00298   for(uint i = 0; i < nObjectNotFound.size(); i++)
00299     t1.setVal(0, i, double(nObjectNotFound[i]));
00300   stdevNObjectNotFound = stdev(t1);
00301 
00302   for(uint i = 0; i < nObjectFound.size(); i++)
00303     t1.setVal(0, i, double(nObject[i]));
00304   stdevNObject = stdev(t1);
00305 
00306   Image<double> t2(1,error.size(),NO_INIT);
00307   for(uint i = 0; i < error.size(); i++) t2.setVal(0, i, error[i]);
00308   stdevError = stdev(t2);
00309 }
00310 
00311 // ######################################################################
00312 inline void GSnavResult::createSummaryResult()
00313 {
00314   std::string sresFName = savePrefix + sformat("_summary_results.txt");
00315   FILE *rfp; LINFO("result file: %s", sresFName.c_str());
00316   if((rfp = fopen(sresFName.c_str(),"wt")) == NULL)LFATAL("not found");
00317 
00318   // segment line
00319   for(uint i = 0; i < nsegment; i++)
00320     {
00321       std::string line(sformat("[%6d]:     0        0.0      0",i));
00322       if(segCount[i] > 0)
00323         {
00324           line = sformat
00325             ("[%6d]: %6d %10.6f %6d  -- %6d %10d: %10.3f + %6d %10d: %10.3f"
00326              " = %6d %10d %10.3f",
00327              i, segCount[i], segError[i]/segCount[i], eSegCount[i],
00328              nSegObjectFound[i],  nSegObjectFoundSearch[i],
00329              nSegObjectFoundSearch[i]/double(nSegObjectFound[i]),
00330              nSegObjectNotFound[i], nSegObjectNotFoundSearch[i],
00331              nSegObjectNotFoundSearch[i]/double(nSegObjectNotFound[i]),
00332              nSegObject[i], nSegObjectSearch[i],
00333              double(nSegObjectSearch[i])/nSegObject[i]);
00334         }
00335       LINFO("%s",line.c_str());
00336       line += std::string("\n");
00337       fputs(line.c_str(), rfp);
00338     }
00339 
00340   // total line
00341   std::string lline =
00342     sformat
00343     ("total   : %6d %10.6f         -- %6d %10d: %10.3f + %6d %10d: %10.3f"
00344      " = %6d %10d %10.3f",
00345      tcount, terror/tcount, tfobj, tfsearch, tfsearch/double(tfobj),
00346      tnfobj, tnfsearch, tnfsearch/double(tnfobj),
00347      tobj, tsearch, tsearch/double(tobj));
00348   LINFO("%s", lline.c_str());
00349   LINFO("search count: %d: %f + %f = %f",
00350         itsSearchCount, double(tfobj)/itsSearchCount,
00351         double(tnfobj)/itsSearchCount, double(tobj)/itsSearchCount);
00352   lline += std::string("\n");
00353   fputs(lline.c_str(), rfp);
00354   fclose (rfp);
00355 }
00356 
00357 // ######################################################################
00358 inline void GSnavResult::getSearchResult(std::vector<uint> index)
00359 {
00360   uint tfObj  = 0;    uint tfSearch  = 0;
00361   uint tnfObj = 0;    uint tnfSearch = 0;
00362   uint tObj   = 0;    uint tSearch   = 0;
00363 
00364   // go through the indexes in question
00365   for(uint i = 0; i < index.size(); i++)
00366     {
00367       tfObj  += nObjectFound[index[i]];
00368       tnfObj += nObjectNotFound[index[i]];
00369       tObj   += nObject[index[i]];
00370 
00371       tfSearch  += nObjectFoundSearch[index[i]];
00372       tnfSearch += nObjectNotFoundSearch[index[i]];
00373       tSearch   += nObjectSearch[index[i]];
00374     }
00375 
00376   // report the result
00377   LINFO("%6d %10d: %10.3f + %6d %10d: %10.3f = %6d %10d %10.3f| ",
00378         tfObj,  tfSearch,  tfSearch/float(tfObj),
00379         tnfObj, tnfSearch, tnfSearch/float(tnfObj),
00380         tObj,   tSearch,   tSearch/float(tObj));
00381 }
00382 
00383 // ######################################################################
00384 
00385 // ######################################################################
00386 
00387 /*! This class is to read the GSnav M_result file                      */
00388 class GSnav_M_Result
00389 {
00390 public:
00391 
00392   // input information
00393   std::vector<double> bbmtTime;
00394   double meanBbmtTime, minBbmtTime, maxBbmtTime, stdevBbmtTime;
00395 
00396   std::vector<double> dorsalTime;
00397   double meanDorsalTime, minDorsalTime, maxDorsalTime, stdevDorsalTime;
00398 
00399   std::vector<double> ventralTime;
00400   double meanVentralTime, minVentralTime, maxVentralTime, stdevVentralTime;
00401 
00402   std::vector<double> inputTime;
00403   double meanInputTime, minInputTime, maxInputTime, stdevInputTime;
00404 
00405   // ventral searches
00406   std::vector<uint> ventralSearchEndFrame;
00407   std::vector<uint> ventralSearchNumFrame;
00408   std::vector<double> ventralSearchTime;
00409   double meanVentralSearchTime, minVentralSearchTime,
00410     maxVentralSearchTime, stdevVentralSearchTime;
00411 
00412   // file information
00413   std::string savePrefix;
00414 
00415   inline void read(std::string inSavePrefix);
00416 };
00417 
00418 // ######################################################################
00419 inline void GSnav_M_Result::read(std::string inSavePrefix)
00420 {
00421   savePrefix = inSavePrefix;
00422 
00423   // open the result file
00424   std::string resFName = savePrefix + sformat("_M_results.txt");
00425   FILE *fp; LINFO("result file: %s", resFName.c_str());
00426   if((fp = fopen(resFName.c_str(),"rb")) == NULL)LFATAL("not found");
00427 
00428   // get each line result
00429   char inLine[200];
00430   while(fgets(inLine, 200, fp) != NULL)
00431     {
00432       uint index; float bTime, dTime, vTime, iTime;
00433       int rCL, rNL;  bool resetNL;
00434 
00435       // read the line
00436       sscanf(inLine, "%d %f %d %d %f %f %f",
00437              &index, &bTime, &rCL, &rNL, &dTime, &vTime, &iTime);
00438       LDEBUG("%5d %11.5f %d %d %11.5f %11.5f %11.5f",
00439             index, bTime, rCL, rNL, dTime, vTime, iTime);
00440       if(rNL > 0) resetNL = true; else resetNL = false;
00441 
00442       // store the results
00443       bbmtTime.push_back(bTime);
00444       dorsalTime.push_back(dTime);
00445       ventralTime.push_back(vTime);
00446       inputTime.push_back(iTime);
00447 
00448       // store the frames at end of ventral search
00449       if(resetNL)
00450         {
00451           LDEBUG("[%4"ZU"]: %d line: %s",
00452                  ventralSearchEndFrame.size(), index-1, inLine);
00453 
00454           ventralSearchEndFrame.push_back(index-1);
00455 
00456           if(ventralSearchNumFrame.size() == 0)
00457             ventralSearchNumFrame.push_back(0);
00458           else
00459             {
00460               uint tNum = ventralSearchNumFrame.back();
00461               ventralSearchNumFrame.push_back(index-1 - tNum);
00462             }
00463           ventralSearchTime.push_back(vTime);
00464         }
00465     }
00466 
00467   // delete the default -1.0 data entries
00468   bbmtTime.pop_back();
00469   dorsalTime.erase(dorsalTime.begin());
00470   ventralTime.erase(ventralTime.begin());
00471 
00472   ventralSearchEndFrame.erase(ventralSearchEndFrame.begin());
00473   ventralSearchNumFrame.erase(ventralSearchNumFrame.begin());
00474   ventralSearchTime.erase(ventralSearchTime.begin());
00475 
00476   // get the statistics (mean, min, max, stdev) for each vector
00477   Image<double> t1(1, bbmtTime.size(),NO_INIT);
00478   for(uint i = 0; i < bbmtTime.size(); i++)
00479     t1.setVal(0, i, double(bbmtTime[i]));
00480   meanBbmtTime = mean(t1);
00481   getMinMax(t1, minBbmtTime, maxBbmtTime);
00482   stdevBbmtTime = stdev(t1);
00483 
00484   Image<double> t2(1, dorsalTime.size(),NO_INIT);
00485   for(uint i = 0; i < dorsalTime.size(); i++)
00486     t2.setVal(0, i, double(dorsalTime[i]));
00487   meanDorsalTime = mean(t2);
00488   getMinMax(t2, minDorsalTime, maxDorsalTime);
00489   stdevDorsalTime = stdev(t2);
00490 
00491   Image<double> t3(1, ventralSearchTime.size(),NO_INIT);
00492   for(uint i = 0; i < ventralSearchTime.size(); i++)
00493     t3.setVal(0, i, double(ventralSearchTime[i]));
00494   meanVentralSearchTime = mean(t3);
00495   getMinMax(t3, minVentralSearchTime, maxVentralSearchTime);
00496   stdevVentralSearchTime = stdev(t3);
00497 
00498   Image<double> t4(1, inputTime.size(),NO_INIT);
00499   for(uint i = 0; i < inputTime.size(); i++)
00500     t4.setVal(0, i, double(inputTime[i]));
00501   meanInputTime = mean(t4);
00502   getMinMax(t4, minInputTime, maxInputTime);
00503   stdevInputTime = stdev(t4);
00504 }
00505 
00506 #endif
00507 
00508 // ######################################################################
00509 /* So things look consistent in everyone's emacs... */
00510 /* Local Variables: */
00511 /* indent-tabs-mode: nil */
00512 /* End: */
Generated on Sun May 8 08:40:12 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3