00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
00084
00085
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
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
00109
00110
00111
00112
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
00134 DescriptorVec descVec(*mgr, "Descriptor Vector", "DecscriptorVec", cc);
00135
00136
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
00144 printf("Biasing for %i", biasedObj);
00145
00146 Bayes bayesNet(descVec.getFVSize(), NOBJ);
00147 bayesNet.load(bayesNetFile);
00148
00149
00150
00151 if (setBias)
00152 biasVC(*cc, bayesNet, biasedObj);
00153
00154
00155
00156
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()){
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())
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
00201
00202
00203
00204
00205
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
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);
00238
00239
00240 descVec.setFovea(winner);
00241 descVec.buildRawDV();
00242
00243
00244 std::vector<double> FV = descVec.getFV();
00245
00246
00247
00248
00249
00250
00251
00252 int cls = bayesNet.classify(FV);
00253
00254
00255 if (cls == -1)
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);
00266
00267
00268 descVec.setFovea(winner);
00269 descVec.buildRawDV();
00270
00271
00272 std::vector<double> FV = descVec.getFV();
00273
00274
00275
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
00288 descVec.setFovea(loc);
00289 descVec.buildRawDV();
00290
00291
00292 std::vector<double> FV = descVec.getFV();
00293
00294
00295
00296
00297
00298
00299
00300
00301 int cls = bayesNet.classify(FV);
00302
00303
00304 if (cls == -1)
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
00316 descVec.setFovea(loc);
00317 descVec.buildRawDV();
00318
00319
00320 std::vector<double> FV = descVec.getFV();
00321
00322
00323
00324
00325
00326
00327 if (objName != NULL)
00328 bayesNet.learn(FV, objName);
00329 else
00330 bayesNet.learn(FV, cls);
00331
00332 }
00333