00001 /*! @file Qt/BiasImageForm.ui.h Main form for viewing smap and channels */ 00002 00003 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Qt/BiasImageForm.ui.h $ 00004 // $Id: BiasImageForm.ui.h 14376 2011-01-11 02:44:34Z pez $ 00005 00006 /**************************************************************************** 00007 ** ui.h extension file, included from the uic-generated form implementation. 00008 ** 00009 ** If you want to add, delete, or rename functions or slots, use 00010 ** Qt Designer to update this file, preserving your code. 00011 ** 00012 ** You should not define a constructor or destructor in this file. 00013 ** Instead, write your code in functions called init() and destroy(). 00014 ** These will automatically be called by the form's constructor and 00015 ** destructor. 00016 *****************************************************************************/ 00017 00018 #include "Channels/OptimalGains.H" 00019 #include "Channels/SubmapAlgorithmBiased.H" 00020 #include "ObjRec/BayesianBiaser.H" 00021 #include "Neuro/NeuroSimEvents.H" 00022 #include "Media/MediaSimEvents.H" 00023 #include "ObjRec/MaskBiaser.H" 00024 #include "GUI/DebugWin.H" 00025 #include "Image/MathOps.H" 00026 00027 void BiasImageForm::fileOpen() 00028 { 00029 00030 QString file = QFileDialog::getOpenFileName( "xmlFiles/all.xml", 00031 "ImageSet (*.xml)", 00032 this, "OpenImageDialog", 00033 "Choose Image" ); 00034 if( !file.isEmpty() ) { 00035 itsTestScenes = new TestImages(file.ascii(), TestImages::XMLFILE ); 00036 itsCurrentScene = 0; 00037 itsImg = itsTestScenes->getScene(itsCurrentScene); //load scene 0 by default 00038 itsSceneNumber->setMaxValue(itsTestScenes->getNumScenes()); 00039 imgDisp->setImage(itsImg); 00040 00041 //fill in the objects 00042 itsSelectObject->clear(); 00043 for (uint obj=0; obj<itsTestScenes->getNumObj(itsCurrentScene); obj++) //look at all the objects 00044 { 00045 TestImages::ObjData objData = itsTestScenes->getObjectData(itsCurrentScene, obj, true); 00046 00047 itsSelectObject->insertItem(QString(objData.description)); 00048 } 00049 00050 00051 itsNewImage = true; 00052 } 00053 } 00054 00055 00056 void BiasImageForm::fileSave() 00057 { 00058 00059 } 00060 00061 00062 void BiasImageForm::fileExit() 00063 { 00064 } 00065 00066 00067 void BiasImageForm::init( ModelManager & manager, nub::ref<Brain> brain, 00068 nub::ref<SimEventQueue> seq) 00069 { 00070 00071 itsMgr = &manager; 00072 00073 //Init the descriptor vector 00074 itsBrain = brain; 00075 itsSEQ = seq; 00076 00077 //Build the descriptor vector 00078 00079 //generate a complexChannel from Visual cortex to pass to DescriptorVec 00080 00081 LFATAL("descriptor vecs should be handled via a SimReq to VisualCortex, which will then propagate to the channels"); 00082 /* 00083 ComplexChannel *cc = 00084 &*dynCastWeak<ComplexChannel>(itsBrain->getVC()); 00085 00086 itsDescriptorVec = new DescriptorVec(*itsMgr, 00087 "Descriptor Vector", "DecscriptorVec", cc); 00088 00089 //set up featuers and 2 classes 00090 itsBayesNetwork = new Bayes(itsDescriptorVec->getFVSize(), 0); 00091 itsDescriptorVecDialog.init(*itsDescriptorVec); 00092 00093 //set the feature names 00094 for (uint i = 0; i < cc->numSubmaps(); i++) 00095 { 00096 const std::string name = cc->getSubmapName(i); 00097 itsBayesNetwork->setFeatureName(i, name.c_str()); 00098 } 00099 */ 00100 00101 //itsTrainScene = new SceneGenerator(SceneGenerator::ALOI_OBJECTS, 500, 500); 00102 //itsTestScene = new SceneGenerator(SceneGenerator::ALOI_OBJECTS, 500, 500); 00103 00104 clickClass = -1; 00105 00106 } 00107 00108 00109 void BiasImageForm::showBiasSettings() 00110 { 00111 itsBiasSettingsDialog.init(*itsMgr); 00112 itsBiasSettingsDialog.show(); 00113 00114 } 00115 00116 00117 void BiasImageForm::evolveBrain() 00118 { 00119 if (itsMgr->started()){ 00120 00121 if (itsImg.initialized() && itsNewImage) 00122 { 00123 rutz::shared_ptr<SimEventInputFrame> 00124 e(new SimEventInputFrame(itsBrain.get(), GenericFrame(itsImg), 0)); 00125 itsSEQ->post(e); //post the image to the brain 00126 itsDescriptorVec->setInputImg(itsImg); 00127 itsNewImage = false; 00128 } 00129 00130 00131 SimTime end_time = itsSEQ->now() + SimTime::MSECS(3.0); 00132 00133 while (itsSEQ->now() < end_time) 00134 { 00135 // Any new WTA winner? 00136 if (SeC<SimEventWTAwinner> e = itsSEQ->check<SimEventWTAwinner>(itsBrain.get())) 00137 { 00138 const Point2D<int> winner = e->winner().p; 00139 itsCurrentWinner = winner; 00140 LINFO("winner\n"); 00141 classifyFovea(winner.i, winner.j,-1); 00142 00143 00144 if (true) showSMap(); 00145 if (true) showTraj(); 00146 //if (true) showChannels(); 00147 00148 itsSEQ->evolve(); 00149 return; //return after one saccade 00150 00151 } 00152 00153 00154 itsSEQ->evolve(); 00155 00156 } 00157 } 00158 00159 } 00160 00161 void BiasImageForm::showTraj(){ 00162 static bool setupTab = true; 00163 static ImageCanvas* disp = NULL; 00164 static Point2D<int> lastWinner = itsCurrentWinner; 00165 00166 if (!itsOutputImg.initialized()) 00167 itsOutputImg = itsImg; 00168 00169 00170 drawCircle(itsOutputImg, itsCurrentWinner, 50, PixRGB<byte>(255,0,0)); 00171 drawCircle(itsOutputImg, itsCurrentWinner, 3, PixRGB<byte>(255,0,0),3); 00172 drawLine(itsOutputImg, lastWinner, itsCurrentWinner, PixRGB<byte>(255,0,0),1); 00173 lastWinner = itsCurrentWinner; 00174 00175 if (setupTab){ 00176 00177 QWidget* tab = new QWidget(tabDisp); 00178 QHBoxLayout* tabLayout = new QHBoxLayout(tab, 11, 6); 00179 disp = new ImageCanvas(tab); 00180 tabLayout->addWidget(disp); 00181 tabDisp->insertTab(tab, "Trajectory" ); 00182 setupTab = false; 00183 00184 //set bias connection 00185 connect(disp, SIGNAL(mousePressed(int,int) ), 00186 this, SLOT( getDescriptor(int,int) ) ); 00187 00188 } 00189 00190 if (disp) 00191 disp->setImage(itsOutputImg); 00192 00193 } 00194 00195 00196 void BiasImageForm::showSMap() 00197 { 00198 static bool setupTab = true; 00199 static ImageCanvas* disp = NULL; 00200 00201 if (setupTab){ 00202 QWidget* tab = new QWidget(tabDisp); 00203 QHBoxLayout* tabLayout = new QHBoxLayout(tab, 11, 6); 00204 disp = new ImageCanvas(tab); 00205 tabLayout->addWidget(disp); 00206 tabDisp->insertTab(tab, "SMap" ); 00207 setupTab = false; 00208 } 00209 00210 if (disp) { 00211 Image<PixRGB<byte> > img; 00212 00213 LINFO("Show SMap\n"); 00214 if (SeC<SimEventSaliencyMapOutput> smo = 00215 itsSEQ->check<SimEventSaliencyMapOutput>(itsBrain.get(), SEQ_ANY)) 00216 { 00217 Image<float> tmp = smo->sm(); 00218 inplaceNormalize(tmp, 0.0F, 255.0F); 00219 img = toRGB(rescale(tmp, tmp.getWidth()*16, tmp.getHeight()*16)); 00220 } 00221 00222 disp->setImage(img); 00223 } 00224 00225 } 00226 00227 00228 void BiasImageForm::showChannels() 00229 { 00230 LFATAL("update to use a SimReq for ChannelMaps -- see SimulationViewerStd"); 00231 /* 00232 static bool setupTab = true; 00233 static ImageCanvas* disp[10]; 00234 int numChans = itsBrain->getVC()->numChans(); 00235 00236 if (setupTab){ 00237 for (int i=0; i<numChans; i++) 00238 { 00239 QWidget* tab = new QWidget(tabDisp); 00240 QHBoxLayout* tabLayout = new QHBoxLayout(tab, 11, 6); 00241 disp[i] = new ImageCanvas(tab); 00242 tabLayout->addWidget(disp[i]); 00243 tabDisp->insertTab(tab, 00244 itsBrain->getVC()->subChan(i)->descriptiveName()); 00245 setupTab = false; 00246 } 00247 } 00248 00249 for( int i=0; i<numChans; i++){ 00250 Image<PixRGB<byte> > img = toRGB(itsBrain->getVC()->subChan(i)->getOutput()); 00251 disp[i]->setImage(img); 00252 } 00253 */ 00254 } 00255 00256 00257 void BiasImageForm::configureView( QAction * action ) 00258 { 00259 LINFO("Configure view %i %i %i", itsViewTraj, itsViewSMap, itsViewChannels); 00260 if (viewTrajAction->isOn()) itsViewTraj = true; else itsViewTraj = false; 00261 if (viewSMapAction->isOn()) itsViewSMap = true; else itsViewSMap = false; 00262 if (viewChannelsAction->isOn()) itsViewChannels = true; else itsViewChannels = false; 00263 } 00264 00265 00266 void BiasImageForm::getDescriptor( int x, int y, int button ) 00267 { 00268 LFATAL("fixme"); 00269 /* 00270 ComplexChannel* cc = &*dynCastWeak<ComplexChannel>(itsBrain->getVC()); 00271 //check if the itsBrain has evolved 00272 if (!cc->hasInput()) 00273 evolveBrain(); 00274 00275 //Classify the object under the fovea 00276 if (cc->hasInput()) //check if we got an input (could be a blank image) 00277 { 00278 clickClass = 0; //let the system knwo we want to learn the featuers as target 00279 classifyFovea(x, y, button); 00280 } 00281 */ 00282 } 00283 00284 void BiasImageForm::showDescriptorVec() 00285 { 00286 itsDescriptorVecDialog.show(); 00287 } 00288 00289 00290 void BiasImageForm::classifyFovea(int x, int y, int button) 00291 { 00292 //build descriptive vector 00293 itsDescriptorVec->setFovea(Point2D<int>(x,y)); 00294 itsDescriptorVec->buildRawDV(); 00295 LINFO("Update Descriptior %i\n", button); 00296 itsDescriptorVecDialog.update(); 00297 00298 //get the resulting feature vector 00299 std::vector<double> FV = itsDescriptorVec->getFV(); 00300 00301 // for(uint i=0; i<FV.size(); i++) 00302 // LINFO("FV: %f", FV[i]); 00303 00304 int cls = itsBayesNetwork->classify(FV); 00305 00306 if (cls != -1) 00307 { 00308 std::string clsName(itsBayesNetwork->getClassName(cls)); 00309 msgLabel->setText(QString("Object is %L1").arg(clsName)); 00310 LINFO("Object belong to class %i %s", cls,clsName.c_str()); 00311 if (1) 00312 logFixation(clsName.c_str(), x, y, FV); 00313 } else { 00314 LINFO("Unknown Object"); 00315 } 00316 00317 00318 if (!editConfigureTestAction->isOn()) { //Train Mode 00319 00320 if (button == 4) 00321 { 00322 QString objName = itsObjectName->text().lower(); 00323 LINFO("Learning %s\n", objName.ascii()); 00324 msgLabel->setText(QString("Learning %L1").arg(objName)); 00325 itsBayesNetwork->learn(FV, objName.ascii()); 00326 00327 if (1) 00328 logFixation(objName.ascii(), x, y, FV); 00329 } 00330 00331 } else { //Test Mode 00332 } 00333 00334 00335 00336 //write stats on window 00337 // msgLabel->setText( 00338 // QString("correct Matches(T:%L1/D:%L2) %L3/%L4 %L5% class= %L5") 00339 // .arg(correctTargetMatches) 00340 // .arg(correctDistracterMatches) 00341 // .arg(correctTargetMatches + correctDistracterMatches) 00342 // .arg(totalObjClassified) 00343 // .arg(int((float(correctTargetMatches+correctDistracterMatches)/float(totalObjClassified))*100)) 00344 // .arg(cls)); 00345 00346 } 00347 00348 00349 00350 00351 void BiasImageForm::run() 00352 { 00353 for (int i=0; i<timesSpinBox->value(); i++){ 00354 } 00355 00356 } 00357 00358 00359 void BiasImageForm::loadBayesNetwork() 00360 { 00361 QString file = QFileDialog::getOpenFileName( QString::null, 00362 "Bayes network (*.net)", 00363 this, "OpenImageDialog", 00364 "Choose Bayes Network" ); 00365 if( !file.isEmpty() ) 00366 itsBayesNetwork->load(file.ascii()); 00367 00368 } 00369 00370 00371 void BiasImageForm::saveBayesNetwork() 00372 { 00373 QString file = QFileDialog::getSaveFileName( QString::null, 00374 "Bayes network (*.net)", 00375 this, "SaveImageDialog", 00376 "Choose Bayes Network" ); 00377 if( !file.isEmpty() ) 00378 itsBayesNetwork->save(file.ascii()); 00379 00380 00381 } 00382 00383 00384 void BiasImageForm::viewBayesNetwork() 00385 { 00386 itsBayesNetworkDialog.show(); 00387 itsBayesNetworkDialog.init(*itsBayesNetwork); 00388 00389 } 00390 00391 00392 void BiasImageForm::setBiasImage( bool biasVal ) 00393 { 00394 LFATAL("fixme"); 00395 /* 00396 LINFO("Bias Image %i\n", biasVal); 00397 ComplexChannel* cc = &*dynCastWeak<ComplexChannel>(itsBrain->getVC()); 00398 00399 00400 if (biasVal) 00401 { 00402 //Get the object we are biasing for 00403 QString objName = itsObjectName->text().lower(); 00404 int cls = itsBayesNetwork->getClassId(objName.ascii()); 00405 00406 if (cls != -1) 00407 { 00408 msgLabel->setText(QString("Biasing for %L1").arg(objName)); 00409 //Set mean and sigma to bias submap 00410 BayesianBiaser bb(*itsBayesNetwork, cls, -1, biasVal); 00411 // with distractor: BayesianBiaser bb(*itsBayesNetwork, 0, 1, biasVal); 00412 cc->accept(bb); 00413 00414 //set the bias 00415 setSubmapAlgorithmBiased(*cc); 00416 } else { 00417 msgLabel->setText(QString("%L1 is not known").arg(objName)); 00418 } 00419 } else { 00420 msgLabel->setText(QString("Unbiasing")); 00421 //Set mean and sigma to bias submap 00422 BayesianBiaser bb(*itsBayesNetwork, 0, -1, biasVal); 00423 // with distractor: BayesianBiaser bb(*itsBayesNetwork, 0, 1, biasVal); 00424 cc->accept(bb); 00425 00426 //set the bias 00427 setSubmapAlgorithmBiased(*cc); 00428 } 00429 */ 00430 } 00431 00432 00433 void BiasImageForm::showSceneSettings() 00434 { 00435 //itsSceneSettingsDialog.init(itsTrainScene, itsTestScene); 00436 //itsSceneSettingsDialog.show(); 00437 00438 } 00439 00440 00441 void BiasImageForm::getScene( int scene ) 00442 { 00443 00444 //save the image of the old scene 00445 if (itsOutputImg.initialized()) 00446 { 00447 char filename[255]; 00448 TestImages::SceneData sceneData = itsTestScenes->getSceneData(itsCurrentScene); 00449 sprintf(filename, "%s.fix.ppm", sceneData.filename.c_str()); 00450 Raster::WriteRGB(itsOutputImg, filename); 00451 } 00452 00453 00454 LINFO("get Scene\n"); 00455 itsCurrentScene = scene; 00456 itsImg = itsTestScenes->getScene(itsCurrentScene); //load scene 0 by default 00457 itsSceneNumber->setMaxValue(itsTestScenes->getNumScenes()); 00458 Image<PixRGB<byte> > tmp = itsImg; 00459 if (viewShow_LabelsAction->isOn()) 00460 itsTestScenes->labelScene(scene, tmp); 00461 imgDisp->setImage(tmp); 00462 itsNewImage = true; 00463 00464 //fill in the objects 00465 itsSelectObject->clear(); 00466 for (uint obj=0; obj<itsTestScenes->getNumObj(itsCurrentScene); obj++) //look at all the objects 00467 { 00468 TestImages::ObjData objData = itsTestScenes->getObjectData(itsCurrentScene, obj, true); 00469 00470 itsSelectObject->insertItem(QString(objData.description)); 00471 } 00472 itsOutputImg = tmp; 00473 00474 evolveBrain(); 00475 } 00476 00477 00478 void BiasImageForm::showLabels( bool show ) 00479 { 00480 itsImg = itsTestScenes->getScene(itsCurrentScene); //load scene 0 by default 00481 itsSceneNumber->setMaxValue(itsTestScenes->getNumScenes()); 00482 Image<PixRGB<byte> > tmp = itsImg; 00483 if (viewShow_LabelsAction->isOn()) 00484 itsTestScenes->labelScene(itsCurrentScene, tmp); 00485 imgDisp->setImage(tmp); 00486 00487 } 00488 00489 00490 void BiasImageForm::biasForObject( int obj ) 00491 { 00492 LFATAL("fixme"); 00493 /* 00494 TestImages::ObjData objData = itsTestScenes->getObjectData(itsCurrentScene, obj, true); 00495 LINFO("Biasing for %i %s\n", obj, objData.description.c_str()); 00496 Image<float> objMask = itsTestScenes->getObjMask(itsCurrentScene, obj, itsImg.getDims()); 00497 MaskBiaser mb(objMask, true); 00498 ComplexChannel* vc = &*dynCastWeak<ComplexChannel>(itsBrain->getVC()); 00499 vc->accept(mb); 00500 setSubmapAlgorithmBiased(*vc); 00501 showObjectLabel(true); 00502 */ 00503 } 00504 00505 00506 void BiasImageForm::showObjectLabel( bool show ) 00507 { 00508 int lineWidth = int(itsImg.getWidth()*0.005); 00509 00510 itsImg = itsTestScenes->getScene(itsCurrentScene); //load scene 0 by default 00511 Image<PixRGB<byte> > tmp = itsImg; 00512 if (viewShow_Object_LabelAction->isOn()) 00513 { 00514 TestImages::ObjData objData = itsTestScenes->getObjectData(itsCurrentScene, itsSelectObject->currentItem()); 00515 std::vector<Point2D<int> > objPoly = objData.polygon; 00516 Point2D<int> p1 = objPoly[0]; 00517 for(uint i=1; i<objPoly.size(); i++) 00518 { 00519 drawLine(tmp, p1, objPoly[i], PixRGB<byte>(255, 0, 0), lineWidth); 00520 p1 = objPoly[i]; 00521 } 00522 drawLine(tmp, p1, objPoly[0], PixRGB<byte>(255, 0, 0), lineWidth); //close the polygon 00523 00524 } 00525 imgDisp->setImage(tmp); 00526 00527 } 00528 00529 void BiasImageForm::logFixation(const char *name, const int x, const int y, const std::vector<double> &FV) 00530 { 00531 FILE *fp = fopen("fixation.log", "a"); 00532 00533 TestImages::SceneData sceneData = itsTestScenes->getSceneData(itsCurrentScene); 00534 00535 fprintf(fp, "T: %s %s %i %i ", sceneData.filename.c_str(), name, x, y); 00536 for(uint i=0; i<FV.size(); i++) 00537 { 00538 fprintf(fp, "%f ", FV[i]); 00539 printf( "%f ", FV[i]); 00540 } 00541 fprintf(fp, "\n"); 00542 printf("\n"); 00543 00544 fclose(fp); 00545 }