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
00040
00041
00042
00043
00044
00045
00046 #include "CINNIC/contourRun.H"
00047
00048 #include "Util/Assert.H"
00049 #include "Image/ColorOps.H"
00050 #include "Image/ShapeOps.H"
00051 #include "Util/log.H"
00052
00053 #include <cmath>
00054 #include <cstdlib>
00055 #include <fstream>
00056
00057 static float Resistance;
00058
00059
00060
00061 contourRun::contourRun()
00062 {
00063 }
00064
00065
00066 contourRun::~contourRun()
00067 {
00068 }
00069
00070
00071 Image<float> contourRun::getSMI(int iter)
00072 {
00073 return SMI[iter];
00074 }
00075
00076
00077 void contourRun::copyCombinedSalMap(std::vector< Image<float> > &CSM)
00078 {
00079 combinedSalMap = CSM;
00080 }
00081
00082
00083 void contourRun::setConfig(readConfig &config)
00084 {
00085 iterations = (int)config.getItemValueF("iterations");
00086 timestep = config.getItemValueF("timestep");
00087 maxEnergy = config.getItemValueF("maxEnergy");
00088 BaseThreshold = config.getItemValueF("BaseThreshold");
00089 Resistance = config.getItemValueF("Resistance");
00090 imageSaveTo = config.getItemValueC("imageOutDir");
00091 logSaveTo = config.getItemValueC("logOutDir");
00092 dumpSwitchPos = (int)config.getItemValueF("dumpSwitchPos");
00093 upperLimit = config.getItemValueF("upperLimit");
00094 saveto = config.getItemValueC("imageOutDir");
00095 iTrans = config.getItemValueF("overlayDumpValue");
00096 GroupBottom = config.getItemValueF("GroupBottom");
00097 supressionAdd = config.getItemValueF("supressionAdd");
00098 supressionSub = config.getItemValueF("supressionSub");
00099 cascadeType = (int)config.getItemValueF("cascadeType");
00100 adaptType = (int)config.getItemValueF("adaptType");
00101 adaptNeuronThresh = config.getItemValueF("adaptNeuronThresh");
00102 adaptNeuronMax = config.getItemValueF("adaptNeuronMax");
00103 excMult = config.getItemValueF("excMult");
00104 leak = config.getItemValueF("leak");
00105 orThresh = config.getItemValueF("orThresh");
00106 initialGroupVal = config.getItemValueF("initialGroupVal");
00107 fastPlast = config.getItemValueF("fastPlast");
00108 doFastPlast = (int)config.getItemValueF("doFastPlast");
00109 lastIterOnly = (int)config.getItemValueF("lastIterOnly");
00110 doTableOnly = (int)config.getItemValueF("doTableOnly");
00111 passThroughGain = config.getItemValueF("passThroughGain");
00112 passThroughTaper = config.getItemValueF("passThroughTaper");
00113 }
00114
00115 void contourRun::setArraySize(long size)
00116 {
00117 storeVal = new float[size];
00118 }
00119
00120
00121 float contourRun::sigmoid(float beta, float v)
00122 {
00123 return (1.0f / (1.0f + pow(2.71828f, (-2.0f * (beta * v)))));
00124 }
00125
00126 float contourRun::sigmoid2(float beta, float v)
00127 {
00128 return(1.0f / (1.0f + pow(2.71828f, (-1.0f * (beta+v) ))));
00129 }
00130
00131 float contourRun::preSigmoid(float v, float thresh, float beta)
00132 {
00133 if(v >= thresh)
00134 {
00135 return (thresh-1);
00136 }
00137 else
00138 {
00139 float sig = sigmoid(beta,(((v/thresh)*(2*(1/beta))-(1/beta))));
00140 if((sig*thresh) > thresh){LINFO("FOO %f",(sig*thresh));}
00141 return (sig*thresh);
00142 }
00143 }
00144
00145 void contourRun::dumpEnergySigmoid(const char* filename, const char* savefile,
00146 readConfig &config, Image<float> image,
00147 int scaleNo, int scaleTot)
00148 {
00149 ASSERT(scaleNo >= 0);ASSERT(scaleTot > 0);
00150 Image< byte > Tyte;
00151 Image<PixRGB<byte> > TPyte;
00152 Image<float> SMIr;
00153 std::ofstream propFile("CINNIC.prop",std::ios::out);
00154 propFile << "scale " << scaleTot << "\n"
00155 << "iterations " << iterations << "\n"
00156 << "sizeX " << image.getWidth() << "\n"
00157 << "sizeY " << image.getHeight() << "\n";
00158 int sx = image.getWidth(); int sy = image.getHeight();
00159 propFile << "type potential\n";
00160 propFile << "type cascade\n";
00161 propFile << "type overlay\n";
00162 propFile.close();
00163 for(int i = 0; i < iterations; i++)
00164 {
00165 SMIr.resize(SMI[i].getWidth(),SMI[i].getHeight());
00166 for(int x = 0; x < SMI[i].getWidth(); x++)
00167 {
00168 for(int y = 0; y < SMI[i].getHeight(); y++)
00169 {
00170 if((x == 0) || (y == 0))
00171 {
00172 SMIr.setVal(x,y,0);
00173 }
00174 else
00175 {
00176 SMIr.setVal(x,y,SMI[i].getVal(x-1,y-1));
00177 }
00178 }
00179 }
00180
00181 SMIr = rescale(SMIr, sx,sy);
00182 Tyte = SMIr;
00183 int xx = Tyte.getWidth();
00184 int yy = Tyte.getHeight();
00185
00186 if(((lastIterOnly == 0) || (i == (iterations - 1))) && (doTableOnly == 0))
00187 {
00188 Raster::WriteRGB(Image<PixRGB<byte> >(Tyte),
00189 sformat("%s%s.potential.out.%d.%d.%d.%d.ppm"
00190 ,saveto,savefile,scaleNo,i,xx,yy));
00191 TPyte = cascadeMap[i];
00192 overlayS = overlayStain(SMIr,image,iTrans,'r');
00193 TPyte = overlayS;
00194 Raster::WriteRGB(TPyte,
00195 sformat("%s%s.overlay.out.%d.%d.%d.%d.ppm"
00196 ,saveto,savefile,scaleNo,i,xx,yy));
00197 }
00198 LINFO("CRAP");
00199 }
00200 }
00201
00202 void contourRun::setImageSize(int X, int Y)
00203 {
00204 ASSERT((X > 0) && (Y > 0));
00205 setImageSizeY = Y;
00206 setImageSizeX = X;
00207 }
00208
00209 void contourRun::setIterations(int iter)
00210 {
00211 ASSERT(iter > 0);
00212 iterations = iter;
00213 }
00214
00215
00216
00217
00218 void contourRun::deriveEnergy()
00219 {
00220 energy = maxEnergy/timestep;
00221 }
00222
00223
00224
00225 void contourRun::resetMatrix()
00226 {
00227 int it = (int)iterations+2;
00228 mat1.resize(setImageSizeY,prop);
00229 mat2.resize(setImageSizeX,mat1);
00230 mat3.resize(AnglesUsed,mat2);
00231 NeuronMatrix.resize(it,mat3);
00232
00233 float foo = BaseThreshold;
00234 for(int n = 0; n < (int)(iterations+2.0F); n++)
00235 {
00236 for(int i = 0; i < AnglesUsed; i++)
00237 {
00238 for(int j = 0; j < setImageSizeX; j++)
00239 {
00240 for(int k = 0; k < setImageSizeY; k++)
00241 {
00242 NeuronMatrix[n][i][j][k].ResetTempCharge();
00243 NeuronMatrix[n][i][j][k].ResetCharge();
00244 NeuronMatrix[n][i][j][k].setThreshold(foo,Resistance);
00245 }
00246 }
00247 }
00248 }
00249
00250 }
00251
00252
00253 void contourRun::preImage(std::vector< Image<float> > &imageMap,
00254 ContourNeuronCreate<float> &N)
00255 {
00256 iterCounter = 0;
00257 cascadeChunk = 100;
00258 deriveEnergy();
00259 SM.resize(setImageSizeX,setImageSizeY,true);
00260 SMI.resize((int)iterations,SM);
00261 cascadeMap.resize((int)iterations,
00262 Image<PixRGB<float> >(setImageSizeX, setImageSizeY, ZEROS));
00263 GroupMap.resize((int)iterations,SM);
00264 cascade.resize(cascadeChunk);
00265 cascadeSize.resize(setImageSizeX,setImageSizeY);
00266 ICH.resize(setImageSizeX,setImageSizeY,true);
00267 cascadeImage.resize(cascadeChunk,ICH);
00268 imageOpt.resize(AnglesUsed,imageMap[1]);
00269
00270
00271 for(int a = 0; a < AnglesUsed; a++)
00272 {
00273 imageOpt[a].resize(imageMap[a].getWidth(),imageMap[a].getHeight(),true);
00274 for(int i = 0; i < setImageSizeX; i++)
00275 {
00276 for(int j = 0; j < setImageSizeY; j++)
00277 {
00278 imageOpt[a].setVal(i,j,(imageMap[a].getVal(i,j)*energy));
00279 }
00280 }
00281 }
00282 #if 0
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 for(int i = 0; i < cascadeChunk; i++)
00306 {
00307 cascadeImage[i].resize(setImageSizeX,setImageSizeY);
00308 }
00309 #endif
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 void contourRun::calcGroups(std::vector< Image<float> > &imageMap,
00321 ContourNeuronCreate<float> &N, int iter)
00322 {
00323 float hold;
00324 SMI[iter].resize(setImageSizeX,setImageSizeY,true);
00325 cascadeMap[iter].resize(setImageSizeX,setImageSizeY,true);
00326 GroupMap[iter].resize(setImageSizeX,setImageSizeY,true);
00327 for(int thing = 0; thing < Groups; thing++)
00328 {
00329 GroupHold[thing] = 0;
00330 }
00331
00332
00333
00334 for(int i = 0; i < setImageSizeX; i++)
00335 {
00336 for(int j = 0; j < setImageSizeY; j++)
00337 {
00338 for(int a = 0; a < AnglesUsed; a++)
00339 {
00340
00341
00342 if(NeuronMatrix[iter][a][i][j].getCharge() < 0)
00343 {
00344 NeuronMatrix[iter][a][i][j].ResetCharge();
00345 }
00346
00347
00348 hold = SM.getVal(i,j)+
00349 NeuronMatrix[iter][a][i][j].getCharge();
00350 SM.setVal(i,j,hold);
00351
00352 }
00353
00354
00355 hold = SM.getVal(i,j) - leak;
00356 if(hold < 0){hold = 0;}
00357 SM.setVal(i,j,hold);
00358
00359 hold = preSigmoid(SM.getVal(i,j),upperLimit);
00360
00361
00362 GroupMap[iter].setVal(i,j,hold);
00363
00364 if((i > 0) && (j > 0))
00365 {
00366 SMI[iter].setVal((i-1),(j-1),((hold/upperLimit)*255));
00367 }
00368
00369 if((iter > 0) && (adaptType == 1))
00370 {
00371 int ghold = (int)Group.getVal(i,j);
00372 int ihold = iter - 1;
00373
00374 GroupHold[ghold] += GroupMap[iter].getVal(i,j) -
00375 GroupMap[ihold].getVal(i,j);
00376 }
00377 }
00378 }
00379
00380
00381 if(adaptType == 1)
00382 {
00383 for(int g = 0; g < Groups; g++)
00384 {
00385
00386
00387
00388 if(GroupHold[g] > GroupTop)
00389 {
00390 GroupMod[g] += supressionAdd*(GroupHold[g]-GroupTop);
00391 GroupMod2[g] = 1/GroupMod[g];
00392
00393 }
00394
00395 if(GroupHold[g] < GroupBottom)
00396 {
00397 GroupMod[g] -= supressionSub;
00398 }
00399 }
00400 }
00401 }
00402
00403
00404 void contourRun::runImageSigmoid(std::vector< Image<float> > &imageMap,
00405 ContourNeuronCreate<float> &N, int iter)
00406 {
00407 calcGroups(imageMap,N,iter);
00408 iterateConvolve(iter,imageMap,N);
00409 }
00410
00411
00412
00413 void contourRun::iterateConvolve(int iter,std::vector< Image<float> > &imageMap,
00414 ContourNeuronCreate<float> &N,
00415 const int node)
00416 {
00417
00418 for(int a = 0; a < AnglesUsed; a++)
00419 {
00420 for(int b = 0; b < AnglesUsed; b++)
00421 {
00422 convolveSimple(iter,imageMap,N,a,b);
00423 }
00424 }
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 void contourRun::convolveSimple(int iter,std::vector< Image<float> > &imageMap,
00440 ContourNeuronCreate<float> &N,
00441 const int a, const int b, const int node)
00442 {
00443 for(int i = 0; i < setImageSizeX; i++)
00444 {
00445 for(int j = 0; j < setImageSizeY; j++)
00446 {
00447 if(imageMap[a].getVal(i,j) > 0.001F)
00448 {
00449
00450
00451
00452 float mod;
00453 mod = NeuronMatrix[iter][a][i][j].getCharge()*fastPlast;
00454 if(mod < 1){mod = 1;}
00455 if(mod > 5){mod = 5;}
00456 int thisGroup = (int)Group.getVal(i,j);
00457 float crap = imageMap[a].getVal(i,j)*
00458 (passThroughGain/((GroupMod[thisGroup]*5)-4));
00459 NeuronMatrix[(iter+1)][a][i][j].
00460 ChargeSimple(crap);
00461 for(int k = 0; k <= XSize; k++)
00462 {
00463
00464
00465 InspX = i + (k-(int)XCenter);
00466 if(InspX >= 0)
00467 {
00468 if(InspX < setImageSizeX)
00469 {
00470 for(int l = 0; l <= YSize; l++)
00471 {
00472 InspY = j - (l-(int)YCenter);
00473
00474 if(InspY >= 0)
00475 {
00476 if(InspY < setImageSizeY)
00477 {
00478
00479 if(N.FourDNeuralMap[a][b][k][l].zero)
00480 {
00481
00482
00483 if(imageMap[b].getVal(InspX,InspY) > orThresh )
00484 {
00485
00486 float hold;
00487
00488
00489
00490
00491 if(iter == 0)
00492 {
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 storeVal[iterCounter] = ((imageOpt[a].getVal(i,j) *
00503 imageMap[b].getVal(InspX,InspY)) *
00504 N.FourDNeuralMap[a][b][k][l].angABD);
00505 }
00506 if(N.FourDNeuralMap[a][b][k][l].angABD < 0)
00507 {
00508
00509 hold =
00510 GroupMod[thisGroup] * mod * storeVal[iterCounter];
00511 }
00512 else
00513 {
00514 hold = mod * storeVal[iterCounter];
00515 }
00516
00517 NeuronMatrix[(iter+1)][b][InspX][InspY].
00518 ChargeSimple(hold);
00519 iterCounter++;
00520 }
00521 }
00522 }
00523 }
00524 }
00525 }
00526 }
00527 }
00528 }
00529 }
00530 }
00531 }
00532
00533
00534
00535 void contourRun::contourRunMain(std::vector< Image<float> > &imageMap,
00536 ContourNeuronCreate<float> &N
00537 ,readConfig &config, Image<float> &group,
00538 int groups,int iter,float groupTop)
00539 {
00540
00541 if(iter == 0)
00542 {
00543 GroupTop = groupTop;
00544 setConfig(config);
00545 setImageSize(imageMap[1].getWidth(),imageMap[1].getHeight());
00546 resetMatrix();
00547 preImage(imageMap,N);
00548 Group = group;
00549
00550
00551
00552 GroupMod.resize(groups,initialGroupVal);
00553 GroupMod2.resize(groups,initialGroupVal);
00554 GroupHold.resize(groups,0.0F);
00555 Groups = groups;
00556 }
00557 iterCounter = 0;
00558 runImageSigmoid(imageMap,N,iter);
00559
00560 }
00561
00562
00563
00564
00565
00566