evalALOI.H

Go to the documentation of this file.
00001 /*! @file ObjRec/evalALOI.H get images from the coil data set to evaluate 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/evalALOI.H $
00035 // $Id: evalALOI.H 9412 2008-03-10 23:10:15Z farhan $
00036 //
00037 
00038 #include "Media/SceneGenerator.H"
00039 
00040 #define DEBUG 1
00041 
00042 #ifdef DEBUG
00043 #include "GUI/DebugWin.H"
00044 #endif
00045 
00046 void evalALOI(DescriptorVec &descVec, Bayes &bayesNet, int nobj);
00047 int trainALOI(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet, int nobj);
00048 int testALOI(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet, int nobj);
00049 int testSceneALOI(DescriptorVec &descVec, Bayes &bayesNet, int nobj);
00050 int testBiasedSceneALOI(DescriptorVec &descVec, Bayes &bayesNet, int nobj, int biasedObj);
00051 int trainTestALOI(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet, int nobj);
00052 
00053 //Max lum 12, col 24, rot 72
00054 #define MAXLUM 12
00055 #define MAXCOL 24
00056 #define MAXROT 72
00057 
00058 void evalALOI(DescriptorVec &descVec, Bayes &bayesNet, int nobj, int biasedObj, int train)
00059 {
00060   TestImages testImages("/lab/ilab15/tmp/objectsDB/png/png", TestImages::ALOI);
00061 
00062   //////////////////////////////////////////////// Main test Loop //////////////////////////////////////
00063   printf("Training\n");
00064 
00065   if (train)
00066     trainALOI(testImages, descVec, bayesNet, nobj);
00067   //  trainSceneALOI(descVec, bayesNet, nobj);
00068 
00069 
00070   //for(uint i=0; i<bayesNet.getNumFeatures(); i++)
00071   //   LINFO("F:%i: Mean %f sigma:%f", i, bayesNet.getMean(9, i), bayesNet.getStdevSq(9,i));
00072 
00073  // printf("Testing Obj rec\n");
00074  // getchar();
00075  //testALOI(testImages, descVec, bayesNet, nobj);
00076  //
00077 
00078  // printf("Testing Scene rec\n");
00079  // getchar();
00080   //testSceneALOI(descVec, bayesNet, nobj);
00081 
00082  if (!train)
00083   //testBiasedSceneALOI(descVec, bayesNet, nobj, biasedObj);
00084    testALOI(testImages, descVec, bayesNet, nobj);
00085  //
00086  //
00087 
00088   printf("Training while testing");
00089 
00090  // trainTestALOI(testImages, descVec, bayesNet, nobj);
00091   //bayesNet.save("objRecALOI.net");
00092 
00093 }
00094 
00095 
00096 int trainALOI(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet, int nobj)
00097 {
00098 
00099   Dims trainSize(OBJSIZEX, OBJSIZEY);
00100   descVec.setFoveaSize(trainSize);
00101 
00102   //Train on half the objects veriations
00103   for(int obj=0; obj<nobj; obj++){ //for each object
00104     for(uint lum=0; lum<testImages.getMaxLum(); lum+=2 ){
00105       LINFO("Training on object %i lum: %i", obj, lum);
00106       Image< PixRGB<byte> > input = testImages.getObject(obj, lum,-2, -2) ;
00107       input = rescale(input, trainSize); //resize the image
00108       learnImage(input, obj, descVec, bayesNet);
00109     }
00110 
00111     for(uint col=0; col<testImages.getMaxCol(); col+=2){
00112       LINFO("Training on object %i col: %i", obj, col);
00113       Image< PixRGB<byte> > input = testImages.getObject(obj, -2, col, -2) ;
00114       input = rescale(input, trainSize); //resize the image
00115       learnImage(input, obj, descVec, bayesNet);
00116     }
00117 
00118     for(uint rot=0; rot<testImages.getMaxRot(); rot+=2 ){
00119       LINFO("Training on object %i rot: %i", obj, rot);
00120       Image< PixRGB<byte> > input = testImages.getObject(obj, -2,-2, rot) ;
00121       input = rescale(input, trainSize); //resize the image
00122       learnImage(input, obj, descVec, bayesNet);
00123     }
00124   }
00125 
00126   return 0;
00127 }
00128 
00129 int testALOI(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet, int nobj)
00130 {
00131 
00132   //Train with only one image
00133   Dims testSize(OBJSIZEX, OBJSIZEY);
00134   descVec.setFoveaSize(testSize);
00135   bool testLum=true, testCol=true, testRot = true, testScale = false;
00136   int numLumTargetMatches = 0; int totalLumImages = 0;
00137   int numColTargetMatches = 0; int totalColImages = 0;
00138   int numRotTargetMatches = 0; int totalRotImages = 0;
00139 
00140   for(int obj=0; obj<nobj; obj++){ //for each object
00141     if (testLum)
00142     {
00143       printf("# Checking obj %i over variations in LUM\n", obj);
00144       for(uint lum=0; lum<testImages.getMaxLum(); lum++){
00145         Image< PixRGB<byte> > input = testImages.getObject(obj, lum,-2, -2) ;
00146         input = rescale(input, testSize); //resize the image
00147 
00148         int cls = classifyImage(input, descVec, bayesNet);
00149         totalLumImages++;
00150         if (cls == obj)
00151           numLumTargetMatches++;
00152         printf("LUM: Obj %i is class %i\n", obj, cls);
00153       }
00154     }
00155 
00156     if (testCol)
00157     {
00158       printf("# Checking obj %i over variations in COLOR\n", obj);
00159       for(uint col=0; col<testImages.getMaxCol(); col++){
00160         Image< PixRGB<byte> > input = testImages.getObject(obj, -2, col, -2) ;
00161         input = rescale(input, testSize); //resize the image
00162 
00163         int cls = classifyImage(input, descVec, bayesNet);
00164         totalColImages++;
00165         if (cls == obj)
00166           numColTargetMatches++;
00167         printf("COL: Obj %i is class %i\n", obj, cls);
00168       }
00169     }
00170 
00171     if (testRot)
00172     {
00173       printf("# Checking obj %i over variations in ROT\n", obj);
00174       for(uint rot=0; rot<testImages.getMaxRot(); rot++){
00175         Image< PixRGB<byte> > input = testImages.getObject(obj, -2,-2, rot) ;
00176         input = rescale(input, testSize); //resize the image
00177 
00178         int cls = classifyImage(input, descVec, bayesNet);
00179         totalRotImages++;
00180         if (cls == obj)
00181           numRotTargetMatches++;
00182 
00183         printf("Rot: Obj %i is class %i\n", obj, cls);
00184 
00185       }
00186     }
00187 
00188     if (testScale)
00189     {
00190     }
00191 
00192     int numTargetMatches = numLumTargetMatches + numColTargetMatches + numRotTargetMatches;
00193     int totalImages = totalLumImages + totalColImages + totalRotImages;
00194 
00195     printf("Number Lum of matches %i/%i %0.2f\n",
00196         numLumTargetMatches, totalLumImages,
00197         (float)numLumTargetMatches/(float)totalLumImages);
00198     printf("Number Col of matches %i/%i %0.2f\n",
00199         numColTargetMatches, totalColImages,
00200         (float)numColTargetMatches/(float)totalColImages);
00201     printf("Number Rot of matches %i/%i %0.2f\n",
00202         numRotTargetMatches, totalRotImages,
00203         (float)numRotTargetMatches/(float)totalRotImages);
00204     printf("Number total of matches %i/%i %0.2f\n",
00205         numTargetMatches, totalImages,
00206         (float)numTargetMatches/(float)totalImages);
00207 
00208   }
00209 
00210   return 0; //numTargetMatches;
00211 }
00212 
00213 int testSceneALOI(DescriptorVec &descVec, Bayes &bayesNet, int nobj)
00214 {
00215   Dims objSize(80, 80);
00216   int numOfObjects = nobj;
00217   int spacing = objSize.w();
00218 
00219   int numTargetMatches = 0; int totalObjects = 0;
00220 
00221   int sceneW = (int)((objSize.w() + spacing) * sqrt(numOfObjects));
00222   int sceneH = (int)((objSize.h() + spacing) * sqrt(numOfObjects));
00223   LINFO("Scene size %ix%i", sceneW, sceneH);
00224 
00225   SceneGenerator testScenes(SceneGenerator::ALOI_OBJECTS, sceneW, sceneH);
00226   testScenes.setObjectSize(objSize.w());
00227 
00228   printf("# Testing over variations in LUM\n");
00229   for(int lum=0; lum<MAXLUM; lum++)
00230   {
00231     Image<PixRGB<byte> > scene = testScenes.getScene(nobj, lum, -2, -2);
00232 
00233     Point2D<int> winner = evolveBrain(scene, descVec); //evolve the biased brain
00234 
00235     for(uint obj=0; obj<testScenes.getNumObj(); obj++)
00236     {
00237 
00238       Point2D<int> objLoc = testScenes.getObjLocation(obj);
00239       int objId = testScenes.getObjId(obj);
00240 
00241       int cls = classifyLocation(objLoc, descVec, bayesNet);
00242       totalObjects++;
00243       if (cls == objId)
00244         numTargetMatches++;
00245       printf("Obj %i is class %i\n", objId, cls);
00246 
00247      // Image<PixRGB<byte> > tmp = scene;
00248      // drawCircle(tmp, objLoc, objSize.w()/2, PixRGB<byte>(255, 0, 0), 3);
00249      // SHOWIMG(rescale(tmp, 512, 512));
00250 
00251     }
00252   }
00253 
00254   printf("# Testing over variations in COL\n");
00255   for(int col=0; col<MAXCOL; col++)
00256   {
00257     Image<PixRGB<byte> > scene = testScenes.getScene(nobj, -2, col, -2);
00258 
00259     Point2D<int> winner = evolveBrain(scene, descVec); //evolve the biased brain
00260 
00261     for(uint obj=0; obj<testScenes.getNumObj(); obj++)
00262     {
00263 
00264       Point2D<int> objLoc = testScenes.getObjLocation(obj);
00265       int objId = testScenes.getObjId(obj);
00266 
00267       int cls = classifyLocation(objLoc, descVec, bayesNet);
00268       totalObjects++;
00269       if (cls == objId)
00270         numTargetMatches++;
00271       printf("Obj %i is class %i\n", objId, cls);
00272 
00273      // Image<PixRGB<byte> > tmp = scene;
00274      // drawCircle(tmp, objLoc, objSize.w()/2, PixRGB<byte>(255, 0, 0), 3);
00275      // SHOWIMG(rescale(tmp, 512, 512));
00276 
00277     }
00278   }
00279 
00280   printf("# Testing over variations in Rot\n");
00281   for(int rot=0; rot<MAXROT; rot++)
00282   {
00283     Image<PixRGB<byte> > scene = testScenes.getScene(nobj, -2, -2, rot);
00284 
00285     Point2D<int> winner = evolveBrain(scene, descVec); //evolve the biased brain
00286 
00287     for(uint obj=0; obj<testScenes.getNumObj(); obj++)
00288     {
00289 
00290       Point2D<int> objLoc = testScenes.getObjLocation(obj);
00291       int objId = testScenes.getObjId(obj);
00292 
00293       int cls = classifyLocation(objLoc, descVec, bayesNet);
00294       totalObjects++;
00295       if (cls == objId)
00296         numTargetMatches++;
00297       printf("Obj %i is class %i\n", objId, cls);
00298 
00299      // Image<PixRGB<byte> > tmp = scene;
00300      // drawCircle(tmp, objLoc, objSize.w()/2, PixRGB<byte>(255, 0, 0), 3);
00301      // SHOWIMG(rescale(tmp, 512, 512));
00302 
00303     }
00304   }
00305 
00306   printf("Number of matches %i/%i %0.2f\n",
00307       numTargetMatches, totalObjects,
00308       (float)numTargetMatches/(float)totalObjects);
00309 
00310   return 0;
00311 }
00312 
00313 int testBiasedSceneALOI(DescriptorVec &descVec, Bayes &bayesNet, int nobj, int biasedObj)
00314 {
00315   Dims objSize(80, 80);
00316   int numOfObjects = nobj;
00317   int spacing = objSize.w();
00318 
00319   int numTargetMatches = 0; int totalObjects = 0;
00320   int totalSaccFound = 0; int totalScenes = 0;
00321 
00322   int sceneW = (int)((objSize.w() + spacing) * sqrt(numOfObjects));
00323   int sceneH = (int)((objSize.h() + spacing) * sqrt(numOfObjects));
00324   LINFO("Scene size %ix%i", sceneW, sceneH);
00325 
00326   SceneGenerator testScenes(SceneGenerator::ALOI_OBJECTS, sceneW, sceneH);
00327   testScenes.setObjectSize(objSize.w());
00328   testScenes.setTargetObject(biasedObj);
00329 
00330   int ii=0;
00331   printf("# Testing over variations in LUM\n");
00332  for(int lum=0; lum<MAXLUM; lum++)
00333   {
00334     Image<PixRGB<byte> > scene = testScenes.getScene(nobj, lum, -2, -2);
00335 
00336     //SHOWIMG(rescale(scene, 512, 512));
00337     Point2D<int> winner = evolveBrain(scene, descVec); //evolve the biased brain
00338     totalScenes++;
00339 
00340     int sacc = 0;
00341     for(sacc=0; sacc<numOfObjects; sacc++)
00342     {
00343       int objId = testScenes.getObjFromPos(winner, objSize);
00344 
00345       int cls = classifyLocation(winner, descVec, bayesNet);
00346       totalObjects++;
00347       if (cls == objId)
00348         numTargetMatches++;
00349       printf("lum %i Obj %i is class %i BiasedObj %i\n", lum, objId, cls, biasedObj);
00350 
00351     //  Image<PixRGB<byte> > tmp = scene;
00352     //  drawCircle(tmp, winner, objSize.w()/2, PixRGB<byte>(255, 0, 0), 3);
00353     //  SHOWIMG(rescale(tmp, 512, 512));
00354       //
00355 
00356       if (objId == biasedObj)
00357         break;
00358 
00359       Image<PixRGB<byte> > nullImg;
00360       winner = evolveBrain(nullImg, descVec); //evolve the biased brain to get a new winner
00361 
00362     }
00363     totalSaccFound += (sacc+1);
00364     printf("Found the object in %i saccads (Total %i)\n", sacc+1, totalSaccFound);
00365   }
00366 
00367 
00368   printf("# Testing over variations in COL\n");
00369   for(int col=2; col<MAXCOL; col++)
00370   {
00371     Image<PixRGB<byte> > scene = testScenes.getScene(nobj, -2, col, -2);
00372 
00373     Point2D<int> winner = evolveBrain(scene, descVec); //evolve the biased brain
00374     totalScenes++;
00375 
00376     int sacc = 0;
00377     for(sacc=0; sacc<numOfObjects; sacc++)
00378     {
00379       int objId = testScenes.getObjFromPos(winner, objSize);
00380 
00381       int cls = classifyLocation(winner, descVec, bayesNet);
00382       totalObjects++;
00383       if (cls == objId)
00384         numTargetMatches++;
00385       printf("Col: %i Obj %i is class %i\n", col, objId, cls);
00386 
00387    //  Image<PixRGB<byte> > tmp = scene;
00388    //  drawCircle(tmp, winner, objSize.w()/2, PixRGB<byte>(255, 0, 0), 3);
00389    //  SHOWIMG(rescale(tmp, 512, 512));
00390 
00391       if (objId == biasedObj)
00392         break;
00393 
00394       Image<PixRGB<byte> > nullImg;
00395       winner = evolveBrain(nullImg, descVec); //evolve the biased brain to get a new winner
00396 
00397     }
00398     totalSaccFound += (sacc+1);
00399     printf("Found the object in %i saccads (Total %i)\n", sacc+1, totalSaccFound);
00400   }
00401 
00402   printf("# Testing over variations in ROT\n");
00403   for(int rot=0; rot<MAXROT; rot++)
00404   {
00405     Image<PixRGB<byte> > scene = testScenes.getScene(nobj, -2, -2, rot);
00406 
00407     Point2D<int> winner = evolveBrain(scene, descVec, ii); //evolve the biased brain
00408     totalScenes++;
00409 
00410     int sacc = 0;
00411     for(sacc=0; sacc<numOfObjects; sacc++)
00412     {
00413       int objId = testScenes.getObjFromPos(winner, objSize);
00414 
00415       int cls = classifyLocation(winner, descVec, bayesNet);
00416       totalObjects++;
00417       if (cls == objId)
00418         numTargetMatches++;
00419       printf("Obj %i is class %i\n", objId, cls);
00420 
00421      // SHOWIMG(rescale(tmp, 512, 512));
00422 
00423       if (objId == biasedObj) //foudnd the obj we are looking for
00424         break;
00425 
00426       Image<PixRGB<byte> > nullImg;
00427       winner = evolveBrain(nullImg, descVec); //evolve the biased brain to get a new winner
00428 
00429     }
00430 
00431     totalSaccFound += (sacc+1);
00432     printf("Found the object in %i saccads (Total %i)\n", sacc+1, totalSaccFound);
00433   }
00434 
00435   printf("Number of matches %i/%i %0.2f\n",
00436       numTargetMatches, totalObjects,
00437       (float)numTargetMatches/(float)totalObjects);
00438 
00439   printf("Found the object in %i saccads out of %i scenes\n",
00440       totalSaccFound, totalScenes);
00441 
00442   return 0;
00443 }
00444 
00445 int trainTestALOI(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet, int nobj)
00446 {
00447 
00448   Dims trainSize(255, 255);
00449   descVec.setFoveaSize(trainSize);
00450 
00451   float thresh = 0.90;
00452   float classRate = 0.0;
00453   int epochs = 0;
00454   while(classRate < thresh)
00455   {
00456     int correctClassify = 0;
00457     int totalImages = 0;
00458     for(int obj=0; obj<nobj; obj++){ //for each object
00459 
00460       for(uint lum=0; lum<testImages.getMaxLum(); lum += 2){
00461         Image< PixRGB<byte> > input = testImages.getObject(obj, lum,-2, -2) ;
00462         input = rescale(input, trainSize); //resize the image
00463         totalImages++; //the # of images we seen so far
00464 
00465         //try to classify the image. If we fail, then learn the image
00466         int cls = classifyImage(input, descVec, bayesNet);
00467         if (cls != obj)
00468         {
00469           printf("Learning obj:%i lum:%i\n", obj, lum);
00470           learnImage(input, obj, descVec, bayesNet);
00471         } else {
00472           correctClassify++;
00473         }
00474 
00475       }
00476 
00477       for(uint col=0; col<testImages.getMaxCol(); col+=2){
00478         Image< PixRGB<byte> > input = testImages.getObject(obj, -2, col, -2) ;
00479         input = rescale(input, trainSize); //resize the image
00480         totalImages++; //the # of images we seen so far
00481 
00482         //try to classify the image. If we fail, then learn the image
00483         int cls = classifyImage(input, descVec, bayesNet);
00484         if (cls != obj)
00485         {
00486           printf("Learning obj:%i col:%i\n", obj, col);
00487           learnImage(input, obj, descVec, bayesNet);
00488         } else {
00489           correctClassify++;
00490         }
00491 
00492       }
00493 
00494       for(uint rot=0; rot<testImages.getMaxRot(); rot += 2 ){
00495         Image< PixRGB<byte> > input = testImages.getObject(obj, -2,-2, rot) ;
00496         input = rescale(input, trainSize); //resize the image
00497 
00498         totalImages++; //the # of images we seen so far
00499 
00500         //try to classify the image. If we fail, then learn the image
00501         int cls = classifyImage(input, descVec, bayesNet);
00502         if (cls != obj)
00503         {
00504           printf("Learning obj:%i rot:%i\n", obj, rot);
00505           learnImage(input, obj, descVec, bayesNet);
00506         } else {
00507           correctClassify++;
00508         }
00509       }
00510     }
00511     epochs++;
00512     classRate = (float)correctClassify/(float)totalImages;
00513     printf("pass %i: correctClassify %i total %i rate:%0.2f\n",
00514         epochs, correctClassify, totalImages, classRate);
00515     bayesNet.save("partIncrementalObjRecALOI.net");
00516 
00517   }
00518 
00519   return 0;
00520 }
00521 
00522 
Generated on Sun May 8 08:41:08 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3