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/RGBConvolveChannel.H"
00039 #include "Component/ModelManager.H"
00040 #include "Image/MathOps.H"
00041 #include "Image/MatrixOps.H"
00042 #include "Image/Pixels.H"
00043 #include "Image/Transforms.H"
00044 #include "Media/FrameSeries.H"
00045 #include "Neuro/NeuroOpts.H"
00046 #include "Channels/RawVisualCortex.H"
00047 #include "Raster/Raster.H"
00048 #include "Util/sformat.H"
00049
00050 #include <fstream>
00051 #ifdef HAVE_MPI_H
00052 #include <mpi.h>
00053 #endif
00054
00055 #define ITMAX 200
00056 #define FTOL 0.000001f
00057 #define MASTER 0
00058 #define MSG_STOP 10
00059 #define MSG_PIC 1
00060 #define MSG_DATA 2
00061 #define MSG_RESULT 200
00062 #define PATH "/tmphpc-01/itti/openvision"
00063 #define INPUTFILE "/tmphpc-01/itti/openvision/start.txt"
00064 #define NB_PICS 14
00065 #define NB_FILTERS 3
00066 #define NCOEFFS 8
00067 #define MCOEFFS NCOEFFS * NCOEFFS * NB_FILTERS * 3
00068 #define NB_EVALS 10
00069 #define NB_WAVES 5
00070 #define NB_PROCS NB_PICS * NB_EVALS * 2 + 1
00071
00072 int main(int argc, char **argv)
00073 {
00074 #ifndef HAVE_MPI_H
00075
00076 LFATAL("<mpi.h> must be installed to use this program");
00077
00078 #else
00079
00080
00081 int loglev = LOG_ERR;
00082 MYLOGVERB = loglev;
00083
00084
00085 int n = NCOEFFS;
00086 int m = MCOEFFS;
00087
00088
00089 int myrank, size;
00090 MPI_Init(&argc, &argv);
00091 MPI_Comm_size(MPI_COMM_WORLD, &size);
00092
00093
00094 if(size < NB_PROCS)
00095 {
00096 LERROR("*** Error: %i Processes needed. ***", NB_PROCS);
00097 MPI_Finalize();
00098 return 1;
00099 }
00100
00101
00102 MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
00103
00104 if (myrank == MASTER)
00105 {
00106
00107
00108
00109
00110 MPI_Status status;
00111
00112
00113 float p[m];
00114 std::ifstream inputfile (INPUTFILE);
00115 if (inputfile.is_open())
00116 {
00117 for (int j = 0; j < m; j++)
00118 inputfile >> p[j];
00119 inputfile.close();
00120 }
00121 else
00122 {
00123 LERROR("*** Cannot open input file !");
00124 return 1;
00125 }
00126
00127
00128 float xi[m][m];
00129 for(int i = 0; i < m; i++)
00130 {
00131 for(int j = 0; j < m; j++)
00132 xi[j][i] = 0.0f;
00133 xi[i][i] = 1.0f;
00134 }
00135 float fp = 10.0f;
00136
00137
00138
00139 for (int iter = 0; ; iter++)
00140 {
00141 LERROR("### ITERATION %i ###", iter);
00142
00143 int stop = 0;
00144 float del = 0.0f;
00145 int ibig = 0;
00146 float fstart = fp;
00147 float start[m];
00148 for (int i = 0; i < m; i++)
00149 start[m] = p[m];
00150 float fpt = fp;
00151 int mubig = 0;
00152
00153
00154 for (int i = 0; i < m; i++)
00155 {
00156 mubig = 0;
00157 for (int wave = 0; wave < NB_WAVES; wave++)
00158 {
00159 int muplus = NB_EVALS * wave;
00160
00161 for (int part = 0; part < 2; part++)
00162 for (int mu = 1; mu <= NB_EVALS; mu++)
00163 {
00164 int mu2 = (1 - 2 * part) * (mu + muplus);
00165 float ptry[m];
00166 for (int j = 0; j < m; j++)
00167 ptry[j] = p[j] + (mu2 * 0.02f * xi[j][i]);
00168 for (int pic = 0; pic < NB_PICS; pic++)
00169 {
00170 int proc = (NB_EVALS * NB_PICS * part);
00171 proc += (NB_PICS * (mu - 1)) + pic + 1;
00172 MPI_Send(&stop, 1, MPI_INT, proc,
00173 MSG_STOP, MPI_COMM_WORLD);
00174 MPI_Send(&pic, 1, MPI_INT, proc,
00175 MSG_PIC, MPI_COMM_WORLD);
00176 MPI_Send(ptry, m, MPI_FLOAT, proc,
00177 MSG_DATA, MPI_COMM_WORLD);
00178
00179 }
00180 }
00181 LERROR("*** Data sent to slaves");
00182
00183
00184 for (int part = 0; part < 2; part++)
00185 for (int mu = 1; mu <= NB_EVALS; mu++)
00186 {
00187 float fptry = 0.0f;
00188 for (int pic = 0; pic < NB_PICS; pic++)
00189 {
00190 float value;
00191 int proc = (NB_EVALS * NB_PICS * part);
00192 proc += (NB_PICS * (mu - 1)) + pic + 1;
00193 MPI_Recv(&value, 1, MPI_FLOAT, proc,
00194 MSG_RESULT, MPI_COMM_WORLD,
00195 &status);
00196 fptry += value;
00197 }
00198 fptry /= NB_PICS;
00199 if (fptry < fpt)
00200 {
00201 fpt = fptry;
00202 mubig = (1 - 2 * part) * (mu + muplus);
00203 }
00204 }
00205 }
00206
00207 LERROR("*** Result received");
00208
00209 if (fp - fpt > del)
00210 {
00211 del = fp - fpt;
00212 ibig = i;
00213 }
00214 fp = fpt;
00215 for (int j = 0; j < m; j++)
00216 p[j] += mubig * 0.02f * xi[j][i];
00217
00218 LERROR("*** Direction %i : mu = %i", i, mubig);
00219 LERROR("*** New value : %f", fp);
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 }
00240
00241 LERROR("### Total decrease for iteration : %f", fstart - fp);
00242
00243
00244 const std::string filename =
00245 sformat("%s/results/step%03i.txt", PATH, iter);
00246 std::ofstream resultfile (filename.c_str());
00247 if (resultfile.is_open())
00248 {
00249 for (int j = 0; j < m; j++)
00250 resultfile << p[j] << "\n";
00251 resultfile.close();
00252 }
00253 LERROR("### Current result saved in %s", filename.c_str());
00254
00255
00256 if (2.0 * fabs(fstart - fp) <= FTOL * (fabs(fstart) + fabs(fp)))
00257 {
00258 stop = 1;
00259 for (int i = 1; i < NB_PROCS; i++)
00260 MPI_Send(&stop, 1, MPI_INT, i,
00261 MSG_STOP, MPI_COMM_WORLD);
00262 LERROR("### Low decrease ! ###");
00263 sleep(10);
00264
00265 return 0;
00266 }
00267 if (iter == ITMAX)
00268 {
00269 stop = 1;
00270 for (int i = 1; i < NB_PROCS; i++)
00271 MPI_Send(&stop, 1, MPI_INT, i,
00272 MSG_STOP, MPI_COMM_WORLD);
00273 LERROR("### Maximum iterations exceeded ! ###");
00274 sleep(10);
00275
00276 return 1;
00277 }
00278
00279
00280 float xit[m];
00281 float norm = 0;
00282 for (int j = 0; j < m; j++)
00283 {
00284 xit[j] = p[j] - start[j];
00285 norm += xit[j] * xit[j];
00286 }
00287 norm = sqrt(norm);
00288 for (int j = 0; j < m; j++)
00289 {
00290 xi[j][ibig] = xit[j] / norm;
00291 }
00292 }
00293 }
00294 else
00295 {
00296
00297
00298
00299
00300 MPI_Status status;
00301
00302
00303 Image<float> hmat(n, n, ZEROS);
00304 for(int i = 0; i < n; i++)
00305 {
00306 hmat.setVal(i, 0, 1.0f);
00307 }
00308 for(int i = 0; i < n / 2; i++)
00309 {
00310 hmat.setVal(i, 1, 1.0f);
00311 hmat.setVal(i + n / 2, 1, -1.0f);
00312 if (i - 2 < 0)
00313 {
00314 hmat.setVal(i, 2, 1.0f);
00315 hmat.setVal(i + 2, 2, -1.0f);
00316 }
00317 else
00318 {
00319 hmat.setVal(i + 2, 3, 1.0f);
00320 hmat.setVal(i + 4, 3, -1.0f);
00321 }
00322 hmat.setVal(2 * i, i + n / 2, 1.0f);
00323 hmat.setVal(2 * i + 1, i + n / 2, -1.0f);
00324 }
00325
00326
00327 ImageSet< PixRGB<byte> > input(NB_PICS);
00328 ImageSet<byte> mask(NB_PICS);
00329 for (int i = 0; i < NB_PICS; i++)
00330 {
00331 input[i] =
00332 Raster::ReadRGB(sformat("%s/pictures/PIC_%03i.PPM", PATH, i));
00333 mask[i] =
00334 Raster::ReadGray(sformat("%s/pictures/PIC_%03i.PGM", PATH, i));
00335 }
00336
00337 LERROR("### SLAVE %i INITIALIZED SUCCESFULLY ! ###", myrank);
00338
00339
00340
00341 for (int iter = 0; ; ++iter)
00342 {
00343
00344 int stop;
00345 MPI_Recv(&stop, 1, MPI_INT, MASTER,
00346 MSG_STOP, MPI_COMM_WORLD, &status);
00347 if (stop == 1)
00348 {
00349
00350 return 0;
00351 }
00352
00353
00354 int pic;
00355 MPI_Recv(&pic, 1, MPI_INT, MASTER,
00356 MSG_PIC, MPI_COMM_WORLD, &status);
00357 float data[m];
00358 MPI_Recv(data, m, MPI_FLOAT, MASTER,
00359 MSG_DATA, MPI_COMM_WORLD, &status);
00360
00361
00362 ImageSet<float> trans(NB_FILTERS * 3);
00363 for (int i = 0; i < NB_FILTERS * 3; i++)
00364 trans[i] = Image<float>(data + (n * n * i), n, n);
00365 ImageSet<float> filter(NB_FILTERS * 3);
00366 for (int i = 0; i < NB_FILTERS * 3; i++)
00367 filter[i] = matrixMult(transpose(hmat),
00368 matrixMult(trans[i], hmat));
00369
00370
00371 Image<float> prod1(8, 8, ZEROS);
00372 Image<float> prod2(8, 8, ZEROS);
00373 Image<float> prod3(8, 8, ZEROS);
00374 for (int i = 0; i < NB_FILTERS; i++)
00375 {
00376 prod1 += filter[3 * i] * filter[3 * i + 1];
00377 prod2 += filter[3 * i] * filter[3 * i + 2];
00378 prod3 += filter[3 * i + 2] * filter[3 * i + 1];
00379 }
00380 float dotprod = sum(prod1) + sum(prod2) + sum(prod3);
00381
00382
00383 ModelManager manager("Open Attention Model");
00384
00385
00386 nub::soft_ref<RawVisualCortex> vcx(new RawVisualCortex(manager));
00387 manager.addSubComponent(vcx);
00388
00389 for (int i = 0; i < NB_FILTERS; i++)
00390 {
00391
00392 nub::soft_ref<RGBConvolveChannel>
00393 channel(new RGBConvolveChannel(manager));
00394 channel->setDescriptiveName(sformat("RGBConvolve%d", i));
00395 channel->setTagName(sformat("rgbconv%d", i));
00396
00397
00398 channel->setFilters(filter[3 * i], filter[3 * i + 1],
00399 filter[3 * i + 2],
00400 CONV_BOUNDARY_ZERO);
00401
00402
00403 vcx->addSubChan(channel);
00404 }
00405
00406
00407 manager.exportOptions(MC_RECURSE);
00408 manager.setModelParamString("UsingFPE", "false");
00409 manager.setModelParamString("TestMode", "true");
00410
00411
00412 MYLOGVERB = loglev;
00413
00414 manager.start();
00415
00416
00417 vcx->input(InputFrame::fromRgb(&input[pic]));
00418
00419
00420 Image<float> sm = vcx->getOutput();
00421
00422
00423 inplaceNormalize(sm, 0.0f, 255.0f);
00424 Image<byte> smb = sm;
00425
00426
00427 Dims smbdim = smb.getDims();
00428 Image<byte> blur_mask = binaryReverse(chamfer34(mask[pic],
00429 (byte) 255),
00430 (byte) 255);
00431 inplaceLowThresh(blur_mask, (byte) 200);
00432 Image<byte> mask_in = scaleBlock(blur_mask, smbdim);
00433 Image<byte> mask_out = binaryReverse(mask_in, (byte) 255);
00434
00435
00436 Image<float> smb_in = (mask_in * (1.0f / 255.0f)) * smb;
00437 Image<float> smb_out = (mask_out * (1.0f / 255.0f)) * smb;
00438
00439
00440 float max_in, max_out, min;
00441 getMinMax(smb_in, min, max_in);
00442 getMinMax(smb_out, min, max_out);
00443
00444
00445 float error = 1.0f - ((max_in - max_out) / 255.0f);
00446 float result = error * error + 0.001f * fabs(dotprod);
00447
00448
00449 manager.stop();
00450
00451
00452 MPI_Send(&result, 1, MPI_FLOAT, MASTER,
00453 MSG_RESULT, MPI_COMM_WORLD);
00454 }
00455 }
00456
00457 #endif // HAVE_MPI_H
00458
00459 }
00460
00461
00462
00463
00464
00465
00466