Esempio n. 1
0
int main(int argc, char **argv)
{
    try {
        // parse command line
        if (argc != 3)
            throw CError(usage, argv[0]);
        int argn = 1;
        char *dataFileName = argv[argn++];
	char *outstem = argv[argn++];

	int writeParams = 1;
	int writeTimings = 1;

	FILE *debugfile = createDebugFile(writeParams, outstem, verbose, argc, argv);

	// Load datafile 
	int width, height, nLabels;
	std::vector<int> gt, data, lrPairwise, udPairwise;
	MRF::CostVal *dataCostArray, *hCue, *vCue;

	if (verbose)
	    fprintf(stderr, "Loading datafile...\n");
	
	LoadDataFile(dataFileName, width, height, nLabels, dataCostArray, hCue, vCue);

	DataCost *dcost = new DataCost(dataCostArray);
	SmoothnessCost *scost = new SmoothnessCost(1, 1, 1, hCue, vCue);
	EnergyFunction *energy = new EnergyFunction(dcost, scost);

	if (verbose)
	    fprintf(stderr, "Running optimization...\n");
	fflush(stderr);

	int MRFalg = aRunAll;

	int outerIter, innerIter;
	MRF *mrf = NULL;
	for (int numAlg = aICM; numAlg <= aBPM; numAlg++) {
	    outerIter = MAXITER;
	    innerIter = 1;
	    if (MRFalg < aRunAll && numAlg != MRFalg) continue;

	    startAlgInitTimer();

	    switch (numAlg) {
	    case aICM:       mrf = new ICM(width, height, nLabels, energy); innerIter = 5; break;
	    case aExpansion: mrf = new Expansion(width, height, nLabels, energy); break;
	    case aSwap:      mrf = new Swap(width, height, nLabels, energy); break;
	    case aTRWS:      mrf = new TRWS(width, height, nLabels, energy); break;
	    case aBPS:       mrf = new BPS(width, height, nLabels, energy);  
		//innerIter = 5; 
		break;
	    case aBPM:       mrf = new MaxProdBP(width, height, nLabels, energy);
		//innerIter = 2; 
		break;
	    default: throw new CError("unknown algorithm number");
	    }
	    if (debugfile)
		fprintf(debugfile, "******* Running %s for up to %d x %d iterations\n",
			algs[numAlg], outerIter, innerIter);

	    mrf->initialize();
	    mrf->clearAnswer();

	    bool initializeToWTA = false;
	    if (initializeToWTA) {
		if (debugfile)
		    fprintf(debugfile, "performing WTA\n");
		CByteImage disp;
		WTA(dataCostArray, width, height, nLabels, disp);
		writeDisparities(disp, 255, "WTA.png", debugfile);
		setDisparities(disp, mrf);
	    } else {
		mrf->clearAnswer();
	    }

	    float initTime = getAlgInitTime();
	    
	    FILE *timefile = createTimeFile(writeTimings, outstem, algs[numAlg], debugfile);

	    runAlg(mrf, numAlg, debugfile, timefile, outerIter, innerIter, initTime);

	    // save resulting labels as image
	    CShape sh(width, height, 1);
	    CByteImage outimg(sh);
	    int n = 0;
	    for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
		    outimg.Pixel(x, y, 0) = 255* mrf->getLabel(n);
		    n++;
		}
	    }

	    char fname[500];
	    sprintf(fname, "%s-%s.png", outstem, algs[numAlg]);
	    WriteImageVerb(outimg, fname, 1);
	    delete mrf;
	}

	if (writeParams)
	    fclose(debugfile);

	delete energy;
	delete scost;
	delete dcost;
	delete [] dataCostArray;
	delete [] hCue;
	delete [] vCue;
    }
    catch (CError &err) {
        fprintf(stderr, err.message);
        fprintf(stderr, "\n");
        return -1;
    }
    catch (bad_alloc) {
	fprintf(stderr, "*** Error: not enough memory\n");
	exit(1);
    }

    return 0;
}
int main(int argc, char **argv)
{
    MRF* mrf;
    EnergyFunction *energy;
    MRF::EnergyVal E;
    double lowerBound;
    float t,tot_t;
    int iter;

    int seed = 1124285485;
    srand(seed);

    int Etype = 0;

    if (argc != 2) {
	fprintf(stderr, usage, argv[0]);
	exit(1);
    }
    
    if (argc > 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;
}
Esempio n. 3
0
int main()
{
    MRF* mrf;
    EnergyFunction *eng;
    MRF::EnergyVal E;
    float t,tot_t;
    int iter;

    int seed = 1124285485;
    srand(seed);

    // There are 4 sample energies below to play with. Uncomment 1 at a time 

    //eng = generate_DataARRAY_SmoothFIXED_FUNCTION();
    //eng = generate_DataARRAY_SmoothTRUNCATED_LINEAR();
    eng = generate_DataARRAY_SmoothTRUNCATED_QUADRATIC();
    //eng = generate_DataFUNCTION_SmoothGENERAL_FUNCTION();



    ////////////////////////////////////////////////
    //                     ICM                    //
    ////////////////////////////////////////////////
    printf("\n*******Started ICM *****\n");

    mrf = new ICM(sizeX,sizeY,K,eng);
    mrf->initialize();
    mrf->clearAnswer();
    
    E = mrf->totalEnergy();
    printf("Energy at the Start= %d (%d,%d)\n", E,mrf->smoothnessEnergy(),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 = %d (%f secs)\n", E, tot_t);
    }

    delete mrf;

    ////////////////////////////////////////////////
    //          Graph-cuts expansion              //
    ////////////////////////////////////////////////
    printf("\n*******Started the graph-cuts expansion *****\n");
    mrf = new Expansion(sizeX,sizeY,K,eng);
    mrf->initialize();
    mrf->clearAnswer();
    
    E = mrf->totalEnergy();
    printf("Energy at the Start= %d (%d,%d)\n", E,mrf->smoothnessEnergy(),mrf->dataEnergy());

    tot_t = 0;
    for (iter=0; iter<6; iter++)
    {
        mrf->optimize(1, t);

        E = mrf->totalEnergy();
        tot_t = tot_t + t ;
        printf("energy = %d (%f secs)\n", E, tot_t);
    }

    delete mrf;

    ////////////////////////////////////////////////
    //          Graph-cuts swap                   //
    ////////////////////////////////////////////////

    printf("\n*******Started the graph-cuts swap *****\n");
    mrf = new Swap(sizeX,sizeY,K,eng);
    mrf->initialize();
    mrf->clearAnswer();
    
    E = mrf->totalEnergy();
    printf("Energy at the Start= %d (%d,%d)\n", E,mrf->smoothnessEnergy(),mrf->dataEnergy());

    tot_t = 0;
    for (iter=0; iter<6; iter++)
    {
        mrf->optimize(1, t);

        E = mrf->totalEnergy();
        tot_t = tot_t + t ;
        printf("energy = %d (%f secs)\n", E, tot_t);
    }

    
    delete mrf;


    ////////////////////////////////////////////////
    //          Belief Propagation                //
    ////////////////////////////////////////////////

    printf("\n*******  Started MaxProd Belief Propagation *****\n");
    mrf = new MaxProdBP(sizeX,sizeY,K,eng);
    mrf->initialize();
    mrf->clearAnswer();
    
    E = mrf->totalEnergy();
    printf("Energy at the Start= %d (%d,%d)\n", E,mrf->smoothnessEnergy(),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 = %d (%f secs)\n", E, tot_t);
    }

    
    delete mrf;

    return 0;
}
Esempio n. 4
0
int main(int argc, char **argv)
{
    MRF* mrf;
    EnergyFunction *energy;
    MRF::EnergyVal E;
    double lowerBound;
    float t,tot_t;
    int iter;

    int seed = 1124285485;
    srand(seed);

    int Etype = 0;

    if (argc != 2) {
	fprintf(stderr, usage, argv[0]);
	exit(1);
    }
    
    if (argc > 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);
	}

	////////////////////////////////////////////////
	//          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;
	}
    return 0;
}