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 "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
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
00063 printf("Training\n");
00064
00065 if (train)
00066 trainALOI(testImages, descVec, bayesNet, nobj);
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 if (!train)
00083
00084 testALOI(testImages, descVec, bayesNet, nobj);
00085
00086
00087
00088 printf("Training while testing");
00089
00090
00091
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
00103 for(int obj=0; obj<nobj; obj++){
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);
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);
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);
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
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++){
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);
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);
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);
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;
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);
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
00248
00249
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);
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
00274
00275
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);
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
00300
00301
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
00337 Point2D<int> winner = evolveBrain(scene, descVec);
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
00352
00353
00354
00355
00356 if (objId == biasedObj)
00357 break;
00358
00359 Image<PixRGB<byte> > nullImg;
00360 winner = evolveBrain(nullImg, descVec);
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);
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
00388
00389
00390
00391 if (objId == biasedObj)
00392 break;
00393
00394 Image<PixRGB<byte> > nullImg;
00395 winner = evolveBrain(nullImg, descVec);
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);
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
00422
00423 if (objId == biasedObj)
00424 break;
00425
00426 Image<PixRGB<byte> > nullImg;
00427 winner = evolveBrain(nullImg, descVec);
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++){
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);
00463 totalImages++;
00464
00465
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);
00480 totalImages++;
00481
00482
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);
00497
00498 totalImages++;
00499
00500
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