test-ObjRec.C

Go to the documentation of this file.
00001 /*! @file ObjRec/test-ObjRec.C test various obj rec alg */
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: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/ObjRec/test-ObjRec.C $
00035 // $Id: test-ObjRec.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 
00038 
00039 #include "Component/ModelManager.H"
00040 #include "Image/Image.H"
00041 #include "Image/Transforms.H"
00042 #include "Media/FrameSeries.H"
00043 #include "Transport/FrameInfo.H"
00044 #include "Raster/GenericFrame.H"
00045 #include "ObjRec/ObjRecSPM.H"
00046 //#include "ObjRec/ObjRecSalBayes.H"
00047 #include "Media/TestImages.H"
00048 #include "GUI/DebugWin.H"
00049 
00050 int getObjName(std::vector<std::string> &objNames, const std::string &objName)
00051 {
00052   //Find the object
00053   //TODO can use hash function
00054   uint i=0;
00055   for(i=0; i<objNames.size(); i++)
00056     if (objNames[i] == objName)
00057       return i;
00058 
00059   objNames.push_back(objName);
00060   return i;
00061 }
00062 
00063 
00064 int main(const int argc, const char **argv)
00065 {
00066 
00067   MYLOGVERB = LOG_INFO;
00068   ModelManager *mgr = new ModelManager("Test ObjRec");
00069 
00070   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00071   mgr->addSubComponent(ofs);
00072 
00073   nub::ref<ObjRecSPM> objRec(new ObjRecSPM(*mgr));
00074   //nub::ref<ObjRecSalBayes> objRec(new ObjRecSalBayes(*mgr));
00075   mgr->addSubComponent(objRec);
00076 
00077   mgr->exportOptions(MC_RECURSE);
00078 
00079   if (mgr->parseCommandLine(
00080         (const int)argc, (const char**)argv, "PathToImageDB", 1, 1) == false)
00081     return 1;
00082 
00083   mgr->start();
00084 
00085   TestImages testImages(mgr->getExtraArg(0).c_str(), TestImages::CALTECH256,
00086       1, 1, 1);
00087 
00088 
00089   std::vector<std::string> objNames;
00090 
00091   //Train the object rec
00092   for(uint scene=0; scene<testImages.getNumScenes(TestImages::TRAIN); scene++)
00093   {
00094     Image<PixRGB<byte> > sceneImg = testImages.getScene(scene, TestImages::TRAIN);
00095     //ofs->writeRGB(sceneImg, "Train", FrameInfo("Train", SRC_POS));
00096 
00097     TestImages::SceneData sceneData = testImages.getSceneData(scene, TestImages::TRAIN);
00098     LINFO("Train %s with Scene %s",
00099         sceneData.description.c_str(),
00100         sceneData.filename.c_str());
00101     objRec->train(sceneImg, sceneData.description);
00102 
00103     //Add the name to the list of objects trained on if not exsists
00104     getObjName(objNames, sceneData.description);
00105   }
00106   objRec->finalizeTraining();
00107 
00108   int totalImages = 0;
00109   int correctImages = 0;
00110 
00111   //The confusion matrix
00112   Image<float> confMatrix(objNames.size(), objNames.size(), ZEROS);
00113 
00114   //Test the object rec
00115   for(uint scene=0; scene<testImages.getNumScenes(TestImages::TEST); scene++)
00116   {
00117     Image<PixRGB<byte> > sceneImg = testImages.getScene(scene, TestImages::TEST);
00118     //ofs->writeRGB(sceneImg, "Object", FrameInfo("Objects", SRC_POS));
00119 
00120     TestImages::SceneData sceneData = testImages.getSceneData(scene, TestImages::TEST);
00121     std::string classDesc = objRec->predict(sceneImg);
00122 
00123     printf("Test:r:%s p:%s\n",
00124         sceneData.description.c_str(),
00125         classDesc.c_str());
00126     if(classDesc  == sceneData.description)
00127       correctImages++;
00128     totalImages++;
00129 
00130     //Update the confusion matrix
00131     int x = getObjName(objNames, classDesc);
00132     int y = getObjName(objNames, sceneData.description);
00133 
00134     if (confMatrix.coordsOk(x,y))
00135     {
00136       float pVal = confMatrix.getVal(x,y);
00137       confMatrix.setVal(x,y, pVal+1);
00138     } else {
00139       printf("Invalid corrd %ix%i", x, y);
00140     }
00141 
00142   }
00143 
00144   for(int y=0; y<confMatrix.getHeight(); y++)
00145   {
00146     for(int x=0; x<confMatrix.getWidth(); x++)
00147       printf("%f ", confMatrix.getVal(x,y));
00148     printf("\n");
00149   }
00150 
00151 
00152   //SHOWIMG(scaleBlock(confMatrix, Dims(256,256)));
00153   //SHOWIMG(confMatrix);
00154 
00155   printf("Recognition Rate %i/%i=%f (train size=%i)\n",
00156       correctImages,
00157       totalImages,
00158       (float)correctImages/(float)totalImages,
00159       testImages.getNumScenes(TestImages::TRAIN)
00160       );
00161 
00162   // stop all our ModelComponents
00163   mgr->stop();
00164 
00165   return 0;
00166 
00167 }
00168 
Generated on Sun May 8 08:05:30 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3