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 #include "Channels/SubmapAlgorithmBiased.H"
00039 #include "Component/ModelManager.H"
00040 #include "Component/OptionManager.H"
00041 #include "Image/Image.H"
00042 #include "Image/ShapeOps.H"
00043 #include "Image/DrawOps.H"
00044 #include "Neuro/StdBrain.H"
00045 #include "Neuro/VisualCortexConfigurator.H"
00046 #include "Devices/FrameGrabberConfigurator.H"
00047 #include "Devices/IEEE1394grabber.H"
00048 #include "Transport/FrameIstream.H"
00049 #include "Neuro/VisualCortex.H"
00050 #include "Neuro/NeuroOpts.H"
00051 #include "Media/TestImages.H"
00052 #include "Channels/DescriptorVec.H"
00053 #include "Channels/ComplexChannel.H"
00054 #include "Learn/Bayes.H"
00055 #include "ObjRec/BayesianBiaser.H"
00056
00057 #include "GUI/XWinManaged.H"
00058
00059 int train(nub::soft_ref<IEEE1394grabber> &gb, DescriptorVec &descVec, Bayes &bayesNet);
00060 int test(nub::soft_ref<IEEE1394grabber> &gb, DescriptorVec &descVec, Bayes &bayesNet);
00061 int classifyImage(Image<PixRGB<byte> > & img, DescriptorVec &descVec, Bayes &bayesNet);
00062 void learnImage(Image<PixRGB<byte> > & img, int cls, DescriptorVec &descVec, Bayes &bayesNet);
00063
00064 void biasImage( bool biasVal, Bayes &bayesNet );
00065
00066 Point2D<int> evolveBrain(Image<PixRGB<byte> > &img);
00067
00068 ModelManager *mgr;
00069 #define NOBJ 2
00070
00071 XWinManaged disp(Dims(320, 240), -1, -1, "Test Output 1");
00072 XWinManaged foveaDisp(Dims(256, 256), -1, -1, "Test Output 1");
00073
00074 int main(const int argc, const char **argv)
00075 {
00076
00077 MYLOGVERB = LOG_INFO;
00078 mgr = new ModelManager("Test ObjRec");
00079
00080
00081 nub::ref<StdBrain> brain(new StdBrain(*mgr));
00082 mgr->addSubComponent(brain);
00083
00084
00085
00086 nub::soft_ref<IEEE1394grabber>
00087 gb(new IEEE1394grabber(*mgr, "colorcam", "cocam"));
00088
00089 mgr->addSubComponent(gb);
00090
00091
00092 gb->setModelParamVal("FrameGrabberSubChan", 0);
00093 gb->setModelParamVal("FrameGrabberBrightness", 128);
00094 gb->setModelParamVal("FrameGrabberHue", 180);
00095
00096 mgr->exportOptions(MC_RECURSE);
00097
00098
00099 mgr->setOptionValString(&OPT_RawVisualCortexChans, "GNO");
00100
00101
00102
00103 mgr->setOptionValString(&OPT_SaliencyMapType, "Fast");
00104 mgr->setOptionValString(&OPT_WinnerTakeAllType, "Fast");
00105 mgr->setOptionValString(&OPT_SimulationTimeStep, "0.2");
00106
00107 mgr->setModelParamVal("FOAradius", 50, MC_RECURSE);
00108 mgr->setModelParamVal("FoveaRadius", 50, MC_RECURSE);
00109
00110
00111 mgr->setOptionValString(&OPT_IORtype, "None");
00112
00113 if (mgr->parseCommandLine(
00114 (const int)argc, (const char**)argv, "", 0, 0) == false);
00115
00116 mgr->start();
00117
00118
00119
00120
00121
00122
00123
00124 ComplexChannel *cc =
00125 &*dynCastWeak<ComplexChannel>(brain->getVC());
00126
00127
00128 DescriptorVec descVec(*mgr, "Descriptor Vector", "DecscriptorVec", cc);
00129
00130
00131 Bayes bayesNet(descVec.getFVSize(), NOBJ);
00132
00133
00134
00135
00136
00137
00138 LINFO("Training");
00139 train(gb, descVec, bayesNet);
00140 bayesNet.save("objRecCoil.net");
00141
00142
00143
00144 LINFO("Testing");
00145
00146 test(gb, descVec, bayesNet);
00147
00148
00149 }
00150
00151
00152 int train(nub::soft_ref<IEEE1394grabber> &gb, DescriptorVec &descVec, Bayes &bayesNet)
00153 {
00154
00155
00156 Dims trainSize(50, 50);
00157 descVec.setFoveaSize(trainSize);
00158
00159 while(1)
00160 {
00161 Image< PixRGB<byte> > input = gb->readRGB();
00162
00163
00164 LINFO("Obj learning...");
00165 learnImage(input, 1, descVec, bayesNet);
00166 }
00167
00168 return 0;
00169 }
00170
00171 int test(nub::soft_ref<IEEE1394grabber> &gb, DescriptorVec &descVec, Bayes &bayesNet)
00172 {
00173
00174 return 1;
00175 }
00176
00177
00178 int classifyImage(Image<PixRGB<byte> > & img, DescriptorVec &descVec, Bayes &bayesNet)
00179 {
00180 Point2D<int> winner = evolveBrain(img);
00181
00182
00183 descVec.setFovea(winner);
00184 descVec.buildDV();
00185
00186
00187 std::vector<double> FV = descVec.getFV();
00188
00189
00190
00191
00192
00193
00194 int cls = bayesNet.classify(FV);
00195
00196 if (cls == -1)
00197 return -1;
00198 else
00199 return cls;
00200
00201 }
00202
00203 void learnImage(Image<PixRGB<byte> > & img, int cls, DescriptorVec &descVec, Bayes &bayesNet)
00204 {
00205
00206 Point2D<int> winner = evolveBrain(img);
00207
00208 drawCircle(img, winner, 25, PixRGB<byte>(255, 255, 0));
00209 disp.drawImage(img);
00210
00211
00212 Point2D<int> loc = disp.getLastMouseClick();
00213 if (loc.isValid())
00214 {
00215 Dims WindowDims = disp.getDims();
00216 float newi = (float)loc.i * (float)img.getWidth()/(float)WindowDims.w();
00217 float newj = (float)loc.j * (float)img.getHeight()/(float)WindowDims.h();
00218 loc.i = (int)newi;
00219 loc.j = (int)newj;
00220 descVec.setFovea(loc);
00221 foveaDisp.drawImage(descVec.getFoveaImage());
00222
00223 descVec.buildDV();
00224
00225
00226 std::vector<double> FV = descVec.getFV();
00227
00228 double confi;
00229 int cls = bayesNet.classify(FV, &confi);
00230
00231
00232
00233 LINFO("cls %i confi %f", cls, confi);
00234 bayesNet.learn(FV, 0u);
00235
00236 if (confi > -40)
00237 biasImage(true, bayesNet);
00238
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 }
00256
00257 Point2D<int> evolveBrain(Image<PixRGB<byte> > &img)
00258 {
00259
00260 nub::ref<StdBrain> brain = dynCastWeak<StdBrain>(mgr->subComponent(0));
00261
00262 if (mgr->started() && img.initialized()){
00263 brain->time();
00264
00265 brain->input(img);
00266
00267 bool keep_going = true;
00268 while (keep_going){
00269 const SimStatus status = brain->evolve();
00270 if (status == SIM_BREAK) {
00271 LINFO("V %d\n", (int)(brain->time().msecs()) );
00272 keep_going = false;
00273 }
00274 if (brain->gotCovertShift())
00275 {
00276
00277 const Point2D<int> winner = brain->getLastCovertPos();
00278 const float winV = brain->getLastCovertAgmV();
00279
00280 LINFO("##### Winner (%d,%d) at %fms : %.4f #####",
00281 winner.i, winner.j, brain->time().msecs(), winV * 1000.0f);
00282
00283 return winner;
00284
00285 keep_going = false;
00286
00287 }
00288 if (brain->time().secs() > 3.0) {
00289 LINFO("##### Time limit reached #####");
00290 keep_going = false;
00291 }
00292 LINFO("Evolve brain");
00293 }
00294
00295 }
00296
00297 return Point2D<int>();
00298
00299 }
00300
00301 void biasImage( bool biasVal, Bayes &bayesNet )
00302 {
00303 nub::ref<StdBrain> brain = dynCastWeak<StdBrain>(mgr->subComponent(0));
00304 ComplexChannel* cc = &*dynCastWeak<ComplexChannel>(brain->getVC());
00305
00306
00307 BayesianBiaser bb(bayesNet, 0, -1, biasVal);
00308 cc->accept(bb);
00309
00310
00311 setSubmapAlgorithmBiased(*cc);
00312 }
00313
00314