// example.cpp -- illustrates calling the MRF code static const char *usage = "usage: %s [energyType] (a number between 0 and 3)\n"; // uncomment "#define COUNT_TRUNCATIONS" in energy.h to enable counting of truncations #include "mrf.h" #include "ICM.h" #include "GCoptimization.h" #include "MaxProdBP.h" #include "TRW-S.h" #include "BP-S.h" #include #include #include #include const int sizeX = 50; const int sizeY = 50; const int numLabels = 20; MRF::CostVal D[sizeX*sizeY*numLabels]; MRF::CostVal V[numLabels*numLabels]; MRF::CostVal hCue[sizeX*sizeY]; MRF::CostVal vCue[sizeX*sizeY]; #ifdef COUNT_TRUNCATIONS int truncCnt, totalCnt; #endif EnergyFunction* generate_DataARRAY_SmoothFIXED_FUNCTION() { int i, j; // generate function for (i=0; i 1) Etype = atoi(argv[1]); try { switch(Etype) { // Here are 4 sample energies to play with. case 0: energy = generate_DataARRAY_SmoothFIXED_FUNCTION(); fprintf(stderr, "using fixed (array) smoothness cost\n"); break; case 1: energy = generate_DataARRAY_SmoothTRUNCATED_LINEAR(); fprintf(stderr, "using truncated linear smoothness cost\n"); break; case 2: energy = generate_DataARRAY_SmoothTRUNCATED_QUADRATIC(); fprintf(stderr, "using truncated quadratic smoothness cost\n"); break; case 3: energy = generate_DataFUNCTION_SmoothGENERAL_FUNCTION(); fprintf(stderr, "using general smoothness functions\n"); break; default: fprintf(stderr, usage, argv[0]); exit(1); } bool runICM = true; bool runExpansion = true; bool runSwap = true; bool runMaxProdBP = true; bool runTRWS = true; bool runBPS = true; //////////////////////////////////////////////// // ICM // //////////////////////////////////////////////// if (runICM) { printf("\n*******Started ICM *****\n"); mrf = new ICM(sizeX,sizeY,numLabels,energy); mrf->initialize(); mrf->clearAnswer(); E = mrf->totalEnergy(); printf("Energy at the Start= %g (%g,%g)\n", (float)E, (float)mrf->smoothnessEnergy(), (float)mrf->dataEnergy()); tot_t = 0; for (iter=0; iter<6; iter++) { mrf->optimize(10, t); E = mrf->totalEnergy(); tot_t = tot_t + t ; printf("energy = %g (%f secs)\n", (float)E, tot_t); } delete mrf; } //////////////////////////////////////////////// // Graph-cuts expansion // //////////////////////////////////////////////// if (runExpansion) { printf("\n*******Started graph-cuts expansion *****\n"); mrf = new Expansion(sizeX,sizeY,numLabels,energy); mrf->initialize(); mrf->clearAnswer(); E = mrf->totalEnergy(); printf("Energy at the Start= %g (%g,%g)\n", (float)E, (float)mrf->smoothnessEnergy(), (float)mrf->dataEnergy()); #ifdef COUNT_TRUNCATIONS truncCnt = totalCnt = 0; #endif tot_t = 0; for (iter=0; iter<6; iter++) { mrf->optimize(1, t); E = mrf->totalEnergy(); tot_t = tot_t + t ; printf("energy = %g (%f secs)\n", (float)E, tot_t); } #ifdef COUNT_TRUNCATIONS if (truncCnt > 0) printf("***WARNING: %d terms (%.2f%%) were truncated to ensure regularity\n", truncCnt, (float)(100.0 * truncCnt / totalCnt)); #endif delete mrf; } //////////////////////////////////////////////// // Graph-cuts swap // //////////////////////////////////////////////// if (runSwap) { printf("\n*******Started graph-cuts swap *****\n"); mrf = new Swap(sizeX,sizeY,numLabels,energy); mrf->initialize(); mrf->clearAnswer(); E = mrf->totalEnergy(); printf("Energy at the Start= %g (%g,%g)\n", (float)E, (float)mrf->smoothnessEnergy(), (float)mrf->dataEnergy()); #ifdef COUNT_TRUNCATIONS truncCnt = totalCnt = 0; #endif tot_t = 0; for (iter=0; iter<8; iter++) { mrf->optimize(1, t); E = mrf->totalEnergy(); tot_t = tot_t + t ; printf("energy = %g (%f secs)\n", (float)E, tot_t); } #ifdef COUNT_TRUNCATIONS if (truncCnt > 0) printf("***WARNING: %d terms (%.2f%%) were truncated to ensure regularity\n", truncCnt, (float)(100.0 * truncCnt / totalCnt)); #endif delete mrf; } //////////////////////////////////////////////// // Belief Propagation // //////////////////////////////////////////////// if (runMaxProdBP) { printf("\n******* Started MaxProd Belief Propagation *****\n"); mrf = new MaxProdBP(sizeX,sizeY,numLabels,energy); mrf->initialize(); mrf->clearAnswer(); E = mrf->totalEnergy(); printf("Energy at the Start= %g (%g,%g)\n", (float)E, (float)mrf->smoothnessEnergy(), (float)mrf->dataEnergy()); tot_t = 0; for (iter=0; iter < 10; iter++) { mrf->optimize(1, t); E = mrf->totalEnergy(); tot_t = tot_t + t ; printf("energy = %g (%f secs)\n", (float)E, tot_t); } delete mrf; } //////////////////////////////////////////////// // TRW-S // //////////////////////////////////////////////// if (runTRWS) { printf("\n*******Started TRW-S *****\n"); mrf = new TRWS(sizeX,sizeY,numLabels,energy); // can disable caching of values of general smoothness function: //mrf->dontCacheSmoothnessCosts(); mrf->initialize(); mrf->clearAnswer(); E = mrf->totalEnergy(); printf("Energy at the Start= %g (%g,%g)\n", (float)E, (float)mrf->smoothnessEnergy(), (float)mrf->dataEnergy()); tot_t = 0; for (iter=0; iter<10; iter++) { mrf->optimize(10, t); E = mrf->totalEnergy(); lowerBound = mrf->lowerBound(); tot_t = tot_t + t ; printf("energy = %g, lower bound = %f (%f secs)\n", (float)E, lowerBound, tot_t); } delete mrf; } //////////////////////////////////////////////// // BP-S // //////////////////////////////////////////////// if (runBPS) { printf("\n*******Started BP-S *****\n"); mrf = new BPS(sizeX,sizeY,numLabels,energy); // can disable caching of values of general smoothness function: //mrf->dontCacheSmoothnessCosts(); mrf->initialize(); mrf->clearAnswer(); E = mrf->totalEnergy(); printf("Energy at the Start= %g (%g,%g)\n", (float)E, (float)mrf->smoothnessEnergy(), (float)mrf->dataEnergy()); tot_t = 0; for (iter=0; iter<10; iter++) { mrf->optimize(10, t); E = mrf->totalEnergy(); tot_t = tot_t + t ; printf("energy = %g (%f secs)\n", (float)E, tot_t); } delete mrf; } } catch (std::bad_alloc) { fprintf(stderr, "*** Error: not enough memory\n"); exit(1); } return 0; }