// Get potential bounding boxes for all test images
void Objectness::getObjBndBoxesForTests(vector<vector<Vec4i>> &_boxesTests, int numDetPerSize)
{
	const int TestNum = _voc.testSet.size();
	vecM imgs3u(TestNum);
	vector<ValStructVec<float, Vec4i>> boxesTests;
	boxesTests.resize(TestNum);

#pragma omp parallel for
	for (int i = 0; i < TestNum; i++){
		//imgs3u[i] = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i])));
		imgs3u[i]  =  imread("C:/Users/TerryChen/Desktop/M.jpg");
		boxesTests[i].reserve(10000);
	}

	int scales[3] = {1, 3, 5};
	for (int clr = MAXBGR; clr <= G; clr++){
		setColorSpace(clr);
		trainObjectness(numDetPerSize);
		loadTrainedModel();
		CmTimer tm("Predict");
		tm.Start();

#pragma omp parallel for
		for (int i = 0; i < TestNum; i++){
			ValStructVec<float, Vec4i> boxes;
			getObjBndBoxes(imgs3u[i], boxes, numDetPerSize);
			boxesTests[i].append(boxes, scales[clr]);
		}

		tm.Stop();
		printf("Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum);
	}

	_boxesTests.resize(TestNum);
	CmFile::MkDir(_bbResDir);
#pragma omp parallel for
	for (int i = 0; i < TestNum; i++){
		CStr fName = _bbResDir + _voc.testSet[i];
		ValStructVec<float, Vec4i> &boxes = boxesTests[i];
		FILE *f = fopen(_S(fName + ".txt"), "w");
		fprintf(f, "%d\n", boxes.size());
		for (size_t k = 0; k < boxes.size(); k++)
			fprintf(f, "%g, %s\n", boxes(k), _S(strVec4i(boxes[k])));
		fclose(f);

		_boxesTests[i].resize(boxesTests[i].size());
		for (int j = 0; j < boxesTests[i].size(); j++)
			_boxesTests[i][j] = boxesTests[i][j];
	}

	evaluatePerImgRecall(_boxesTests, "PerImgAllNS.m", 5000);

#pragma omp parallel for
	for (int i = 0; i < TestNum; i++){
		boxesTests[i].sort(false);
		for (int j = 0; j < boxesTests[i].size(); j++)
			_boxesTests[i][j] = boxesTests[i][j];
	}
	evaluatePerImgRecall(_boxesTests, "PerImgAllS.m", 5000);
}
Ejemplo n.º 2
0
// Get potential bounding boxes for all test images
void Objectness::getObjBndBoxesForTestsFast(vector<vector<Vec4i> > &_boxesTests, int numDetPerSize, bool preloadModel, bool preloadImages)
{
    //setColorSpace(HSV);
    if (!preloadModel)
        trainObjectness(numDetPerSize);
    loadTrainedModel();
    illustrate();


    const int TestNum = _voc.testSet.size();
    vecM imgs3u;
    if (preloadImages)
        imgs3u.resize(TestNum);
    vector<ValStructVec<float, Vec4i> > boxesTests;
    boxesTests.resize(TestNum);

    //Reading all images beforehand needs a lot of memory.
    //The timing results are affected based on when the image is loaded.
    if (preloadImages) {
        #pragma omp parallel for
        for (int i = 0; i < TestNum; i++) {
            imgs3u[i] = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i])));
            boxesTests[i].reserve(10000);
        }
    }

    printf("Start predicting\n");
    CmTimer tm("Predict");
    tm.Start();

    #pragma omp parallel for
    for (int i = 0; i < TestNum; i++) {
        if (!preloadImages) {
            Mat img = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i])));
            getObjBndBoxes(img, boxesTests[i], numDetPerSize);
        } else {
            getObjBndBoxes(imgs3u[i], boxesTests[i], numDetPerSize);
        }
    }

    tm.Stop();
    if (!preloadImages) {
        printf("Average time for loading and predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum);
    } else {
        printf("Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum);
    }
    _boxesTests.resize(TestNum);
    CmFile::MkDir(_bbResDir);

    #pragma omp parallel for
    for (int i = 0; i < TestNum; i++) {
        CStr fName = _bbResDir + _voc.testSet[i];
        ValStructVec<float, Vec4i> &boxes = boxesTests[i];
        FILE *f = fopen(_S(fName + ".txt"), "w");
        fprintf(f, "%d\n", boxes.size());
        for (size_t k = 0; k < boxes.size(); k++)
            fprintf(f, "%g, %s\n", boxes(k), _S(strVec4i(boxes[k])));
        fclose(f);

        _boxesTests[i].resize(boxesTests[i].size());
        for (int j = 0; j < boxesTests[i].size(); j++)
            _boxesTests[i][j] = boxesTests[i][j];
    }

    evaluatePerImgRecall(_boxesTests, "PerImgAll.m", 5000);
}