00001 /*! @file ObjRec/test-ObjRec.C test the objrec for the VOC challange */ 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-ObjRecVOC.C $ 00035 // $Id: test-ObjRecVOC.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/ObjRecBOF.H" 00046 #include "Media/TestImages.H" 00047 #include "GUI/DebugWin.H" 00048 #include <dirent.h> 00049 #include <vector> 00050 00051 struct ObjInfo 00052 { 00053 std::string name; 00054 int cls; 00055 }; 00056 00057 std::vector<ObjInfo> readImageSet(const char* filename) 00058 { 00059 std::vector<ObjInfo> imageSet; 00060 00061 FILE* fp = fopen(filename, "r"); 00062 if (!fp) 00063 LFATAL("Error reading %s\n", filename); 00064 00065 char name[255]; 00066 int cls; 00067 while (fp != NULL) 00068 { 00069 if (fscanf(fp, "%s %i", name, &cls) != 2) break; 00070 ObjInfo objInf; 00071 objInf.name = std::string(name); 00072 if (cls == -1) 00073 objInf.cls = -1; 00074 else 00075 objInf.cls = 1; 00076 imageSet.push_back(objInf); 00077 } 00078 00079 return imageSet; 00080 } 00081 00082 int main(const int argc, const char **argv) 00083 { 00084 00085 MYLOGVERB = LOG_INFO; 00086 ModelManager *mgr = new ModelManager("Test ObjRec"); 00087 00088 nub::ref<ObjRecBOF> objRec(new ObjRecBOF(*mgr)); 00089 mgr->addSubComponent(objRec); 00090 00091 mgr->exportOptions(MC_RECURSE); 00092 00093 if (mgr->parseCommandLine( 00094 (const int)argc, (const char**)argv, "PathToTrain PathToTest", 2, 2) == false) 00095 return 1; 00096 00097 mgr->start(); 00098 00099 std::vector<ObjInfo> trainSet = readImageSet(mgr->getExtraArg(0).c_str()); 00100 for(uint scene=0; scene<trainSet.size(); scene++) 00101 { 00102 LINFO("Train %s %i", 00103 trainSet[scene].name.c_str(), 00104 trainSet[scene].cls); 00105 objRec->train(trainSet[scene].name, trainSet[scene].cls); 00106 } 00107 objRec->finalizeTraining(); 00108 00109 //Get the code words 00110 for(uint scene=0; scene<trainSet.size(); scene++) 00111 { 00112 LINFO("Train %s %i", 00113 trainSet[scene].name.c_str(), 00114 trainSet[scene].cls); 00115 objRec->getObjCodeWords(trainSet[scene].name); 00116 } 00117 std::vector<ObjInfo> testSet = readImageSet(mgr->getExtraArg(1).c_str()); 00118 for(uint scene=0; scene<testSet.size(); scene++) 00119 { 00120 LINFO("Test %s %i", 00121 testSet[scene].name.c_str(), 00122 testSet[scene].cls); 00123 objRec->getObjCodeWords(testSet[scene].name); 00124 } 00125 //objRec->finalizeTesting(); 00126 00127 /* 00128 int totalImages = 0; 00129 int correctImages = 0; 00130 00131 //The confusion matrix 00132 Image<float> confMatrix(objNames.size(), objNames.size(), ZEROS); 00133 00134 //Test the object rec 00135 for(uint scene=0; scene<testImages.getNumScenes(TestImages::TEST); scene++) 00136 { 00137 Image<PixRGB<byte> > sceneImg = testImages.getScene(scene, TestImages::TEST); 00138 //ofs->writeRGB(sceneImg, "Object", FrameInfo("Objects", SRC_POS)); 00139 00140 TestImages::SceneData sceneData = testImages.getSceneData(scene, TestImages::TEST); 00141 std::string classDesc = objRec->predict(sceneImg); 00142 00143 printf("Test:r:%s p:%s\n", 00144 sceneData.description.c_str(), 00145 classDesc.c_str()); 00146 if(classDesc == sceneData.description) 00147 correctImages++; 00148 totalImages++; 00149 00150 //Update the confusion matrix 00151 int x = getObjName(objNames, classDesc); 00152 int y = getObjName(objNames, sceneData.description); 00153 00154 if (confMatrix.coordsOk(x,y)) 00155 { 00156 float pVal = confMatrix.getVal(x,y); 00157 confMatrix.setVal(x,y, pVal+1); 00158 } else { 00159 printf("Invalid corrd %ix%i", x, y); 00160 } 00161 00162 } 00163 00164 for(int y=0; y<confMatrix.getHeight(); y++) 00165 { 00166 for(int x=0; x<confMatrix.getWidth(); x++) 00167 printf("%f ", confMatrix.getVal(x,y)); 00168 printf("\n"); 00169 } 00170 00171 00172 //SHOWIMG(scaleBlock(confMatrix, Dims(256,256))); 00173 //SHOWIMG(confMatrix); 00174 00175 printf("Recognition Rate %i/%i=%f (train size=%i)\n", 00176 correctImages, 00177 totalImages, 00178 (float)correctImages/(float)totalImages, 00179 testImages.getNumScenes(TestImages::TRAIN) 00180 ); 00181 */ 00182 00183 // stop all our ModelComponents 00184 mgr->stop(); 00185 00186 return 0; 00187 00188 } 00189