00001 /*! @file ObjRec/test-SceneRec.C test various scene 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-SceneRec.C $ 00035 // $Id: test-SceneRec.C 10982 2009-03-05 05:11:22Z itti $ 00036 // 00037 00038 00039 #include "Channels/SubmapAlgorithmBiased.H" 00040 #include "Component/ModelManager.H" 00041 #include "Image/Image.H" 00042 #include "Image/ImageSet.H" 00043 #include "Image/ShapeOps.H" 00044 #include "Image/DrawOps.H" 00045 #include "Image/FilterOps.H" 00046 #include "Image/ColorOps.H" 00047 #include "Image/Transforms.H" 00048 #include "Image/MathOps.H" 00049 #include "Neuro/StdBrain.H" 00050 #include "Neuro/VisualCortexConfigurator.H" 00051 #include "Neuro/VisualCortex.H" 00052 #include "Neuro/SaliencyMap.H" 00053 #include "Neuro/NeuroOpts.H" 00054 #include "Media/TestImages.H" 00055 #include "Media/SceneGenerator.H" 00056 #include "Channels/DescriptorVec.H" 00057 #include "Channels/ComplexChannel.H" 00058 #include "Simulation/SimEventQueue.H" 00059 #include "Simulation/SimulationOpts.H" 00060 #include "Simulation/SimEventQueueConfigurator.H" 00061 #include "Learn/Bayes.H" 00062 //#include "GUI/DebugWin.H" 00063 #include "ObjRec/BayesianBiaser.H" 00064 00065 00066 #define OBJSIZEX 256 00067 #define OBJSIZEY 256 00068 #define NOBJ 25 00069 00070 00071 int train(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet); 00072 int test(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet); 00073 int trainTest(TestImages &testImages, DescriptorVec &descVec, Bayes &bayesNet); 00074 int classifyImage(Image<PixRGB<byte> > & img, DescriptorVec &descVec, Bayes &bayesNet); 00075 void learnImage(Image<PixRGB<byte> > & img, int cls, DescriptorVec &descVec, Bayes &bayesNet, 00076 const char *objName = NULL); 00077 int classifyLocation(Point2D<int> &loc, DescriptorVec &descVec, Bayes &bayesNet); 00078 void learnLocation(Point2D<int> &loc, int cls, DescriptorVec &descVec, Bayes &bayesNet, 00079 const char *objName = NULL); 00080 void biasVC(ComplexChannel &vc, Bayes &bayesNet, int objId); 00081 Point2D<int> evolveBrain(Image<PixRGB<byte> > &img, DescriptorVec& descVec, int ii=-1); 00082 00083 //#include "ObjRec/evalCoil.H" 00084 //#include "ObjRec/evalCSCLAB.H" 00085 //#include "ObjRec/evalLABELME.H" 00086 #include "ObjRec/evalALOI.H" 00087 00088 00089 ModelManager *mgr; 00090 00091 00092 int main(const int argc, const char **argv) 00093 { 00094 00095 MYLOGVERB = LOG_INFO; 00096 mgr = new ModelManager("Test ObjRec"); 00097 00098 nub::soft_ref<SimEventQueueConfigurator> 00099 seqc(new SimEventQueueConfigurator(*mgr)); 00100 mgr->addSubComponent(seqc); 00101 00102 //our brain 00103 nub::ref<StdBrain> brain(new StdBrain(*mgr)); 00104 mgr->addSubComponent(brain); 00105 00106 mgr->exportOptions(MC_RECURSE); 00107 mgr->setOptionValString(&OPT_RawVisualCortexChans, "IOC"); 00108 //mgr.setOptionValString(&OPT_RawVisualCortexChans, "I"); 00109 //mgr->setOptionValString(&OPT_RawVisualCortexChans, "GNO"); 00110 //mgr.setOptionValString(&OPT_RawVisualCortexChans, "N"); 00111 //manager.setOptionValString(&OPT_UseOlderVersion, "false"); 00112 // set the FOA and fovea radii 00113 mgr->setOptionValString(&OPT_SaliencyMapType, "Fast"); 00114 mgr->setOptionValString(&OPT_SMfastInputCoeff, "1"); 00115 mgr->setOptionValString(&OPT_WinnerTakeAllType, "Fast"); 00116 mgr->setOptionValString(&OPT_SimulationTimeStep, "0.2"); 00117 00118 mgr->setModelParamVal("FOAradius", OBJSIZEX/2, MC_RECURSE); 00119 mgr->setModelParamVal("FoveaRadius", OBJSIZEY/2, MC_RECURSE); 00120 00121 mgr->setOptionValString(&OPT_IORtype, "Disc"); 00122 00123 00124 if (mgr->parseCommandLine( 00125 (const int)argc, (const char**)argv, "BayesNet biasObj bias train=1/search=0", 4, 4) == false) 00126 exit(0); 00127 00128 mgr->start(); 00129 00130 ComplexChannel *cc = 00131 &*dynCastWeak<ComplexChannel>(brain->getVC()); 00132 00133 //Get a new descriptor vector 00134 DescriptorVec descVec(*mgr, "Descriptor Vector", "DecscriptorVec", cc); 00135 00136 //get command line options 00137 const char *bayesNetFile = mgr->getExtraArg(0).c_str(); 00138 int biasedObj = mgr->getExtraArgAs<int>(1)-1; 00139 bool setBias = mgr->getExtraArgAs<int>(2); 00140 int train = mgr->getExtraArgAs<int>(3); 00141 00142 00143 //Get new classifier 00144 printf("Biasing for %i", biasedObj); 00145 00146 Bayes bayesNet(descVec.getFVSize(), NOBJ); 00147 bayesNet.load(bayesNetFile); 00148 00149 00150 //Bias the visual cortex to find the obj 00151 if (setBias) 00152 biasVC(*cc, bayesNet, biasedObj); 00153 00154 //evalCOIL(descVec, bayesNet, NOBJ); 00155 // evalCSCLAB(descVec, bayesNet, NOBJ); 00156 // evalLABELME(descVec, bayesNet, NOBJ); 00157 evalALOI(descVec, bayesNet, NOBJ, biasedObj, train); 00158 00159 if (train) 00160 bayesNet.save(bayesNetFile); 00161 00162 00163 } 00164 00165 Point2D<int> evolveBrain(Image<PixRGB<byte> > &img, DescriptorVec& descVec, int ii) 00166 { 00167 00168 nub::ref<StdBrain> brain = dynCastWeak<StdBrain>(mgr->subComponent("Brain")); 00169 nub::ref<SimEventQueueConfigurator> seqc = 00170 dynCastWeak<SimEventQueueConfigurator>(mgr->subComponent("SimEventQueueConfigurator")); 00171 nub::soft_ref<SimEventQueue> seq = seqc->getQ(); 00172 00173 LINFO("Evolve Brain"); 00174 00175 if (mgr->started()){ //give the image to the brain 00176 00177 if (img.initialized()) 00178 { 00179 brain->input(img, seq); 00180 descVec.setInputImg(img); 00181 } 00182 00183 bool keep_going = true; 00184 while (keep_going){ 00185 brain->evolve(*seq); 00186 const SimStatus status = seq->evolve(); 00187 if (status == SIM_BREAK) { 00188 LINFO("V %d\n", (int)(seq->now().msecs()) ); 00189 keep_going = false; 00190 } 00191 if (brain->gotCovertShift()) // new attended location 00192 { 00193 00194 const Point2D<int> winner = brain->getLastCovertPos(); 00195 const float winV = brain->getLastCovertAgmV(); 00196 00197 LINFO("##### Winner (%d,%d) at %fms : %.4f #####\n", 00198 winner.i, winner.j, seq->now().msecs(), winV * 1000.0f); 00199 00200 //Image<float> img = brain->getSM()->getV(false); 00201 // Image<float> img = brain->getVC()->getOutput(); 00202 // SHOWIMG(img); 00203 /* char filename[255]; 00204 sprintf(filename, "SceneSMap%i.ppm", ii++); 00205 Raster::WriteRGB(img, filename);*/ 00206 00207 return winner; 00208 00209 keep_going = false; 00210 00211 } 00212 if (seq->now().secs() > 3.0) { 00213 LINFO("##### Time limit reached #####"); 00214 keep_going = false; 00215 } 00216 LINFO("Evolve brain"); 00217 } 00218 00219 } 00220 00221 return Point2D<int>(); 00222 00223 } 00224 00225 00226 void biasVC(ComplexChannel &vc, Bayes &bayesNet, int objId) 00227 { 00228 //Set mean and sigma to bias submap 00229 BayesianBiaser bb(bayesNet, objId, -1, true); 00230 vc.accept(bb); 00231 00232 setSubmapAlgorithmBiased(vc); 00233 } 00234 00235 int classifyImage(Image<PixRGB<byte> > & img, DescriptorVec &descVec, Bayes &bayesNet) 00236 { 00237 Point2D<int> winner = evolveBrain(img, descVec); //evolve the brain 00238 00239 //get the descriptor 00240 descVec.setFovea(winner); 00241 descVec.buildRawDV(); //build the descriptor vector 00242 00243 //get the resulting feature vector 00244 std::vector<double> FV = descVec.getFV(); 00245 00246 // printf("%i %i ", winner.i, winner.j); 00247 // for(uint i=0; i<FV.size(); i++) 00248 // printf("%f ", FV[i]); 00249 00250 //classify 00251 00252 int cls = bayesNet.classify(FV); 00253 00254 00255 if (cls == -1) //check for errors 00256 return -1; 00257 else 00258 return cls; 00259 00260 } 00261 00262 void learnImage(Image<PixRGB<byte> > & img, int cls, DescriptorVec &descVec, Bayes &bayesNet, 00263 const char *objName) 00264 { 00265 Point2D<int> winner = evolveBrain(img, descVec); //evolve the brain 00266 00267 //get the descriptor 00268 descVec.setFovea(winner); 00269 descVec.buildRawDV(); //build the descriptor vector 00270 00271 //get the resulting feature vector 00272 std::vector<double> FV = descVec.getFV(); 00273 00274 // for(uint i=0; i<FV.size(); i++) 00275 // LINFO("FV: %f", FV[i]); 00276 00277 if (objName != NULL) 00278 bayesNet.learn(FV, objName); 00279 else 00280 bayesNet.learn(FV, cls); 00281 00282 } 00283 00284 int classifyLocation(Point2D<int> &loc, DescriptorVec &descVec, Bayes &bayesNet) 00285 { 00286 00287 //get the descriptor 00288 descVec.setFovea(loc); 00289 descVec.buildRawDV(); //build the descriptor vector 00290 00291 //get the resulting feature vector 00292 std::vector<double> FV = descVec.getFV(); 00293 00294 00295 //classify 00296 // printf("FV: "); 00297 // for(uint i=0; i<FV.size(); i++) 00298 // printf("%f ", FV[i]); 00299 // printf("\n"); 00300 00301 int cls = bayesNet.classify(FV); 00302 00303 00304 if (cls == -1) //check for errors 00305 return -1; 00306 else 00307 return cls; 00308 00309 } 00310 00311 void learnLocation(Point2D<int> &loc, int cls, DescriptorVec &descVec, Bayes &bayesNet, 00312 const char *objName) 00313 { 00314 00315 //get the descriptor 00316 descVec.setFovea(loc); 00317 descVec.buildRawDV(); //build the descriptor vector 00318 00319 //get the resulting feature vector 00320 std::vector<double> FV = descVec.getFV(); 00321 00322 // printf("FV: "); 00323 // for(uint i=0; i<FV.size(); i++) 00324 // printf("%f ", FV[i]); 00325 // printf("\n"); 00326 00327 if (objName != NULL) 00328 bayesNet.learn(FV, objName); 00329 else 00330 bayesNet.learn(FV, cls); 00331 00332 } 00333