// 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);
}
示例#2
0
void Objectness::getRandomBoxes(vector<vector<Vec4i> > &boxesTests, int num)
{
    const int TestNum = _voc.testSet.size();
    boxesTests.resize(TestNum);
    #pragma omp parallel for
    for (int i = 0; i < TestNum; i++) {
        Mat imgs3u = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i])));
        int H = imgs3u.cols, W = imgs3u.rows;
        boxesTests[i].reserve(num);
        for (int k = 0; k < num; k++) {
            int x1 = rand()%W + 1, x2 = rand()%W + 1;
            int y1 = rand()%H + 1, y2 = rand()%H + 1;
            boxesTests[i].push_back(Vec4i(min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)));
        }
    }
    evaluatePerImgRecall(boxesTests, "PerImgAll.m", num);
}
示例#3
0
void Objectness::evaluatePerClassRecall(vector<vector<Vec4i> > &boxesTests, CStr &saveName, const int WIN_NUM)
{
    const int TEST_NUM = _voc.testSet.size(), CLS_NUM = _voc.classNames.size();
    if (boxesTests.size() != TEST_NUM) {
        boxesTests.resize(TEST_NUM);
        for (int i = 0; i < TEST_NUM; i++) {
            Mat boxes;
            matRead(_voc.localDir + _voc.testSet[i] + ".dat", boxes);
            Vec4i* d = (Vec4i*)boxes.data;
            boxesTests[i].resize(boxes.rows, WIN_NUM);
            memcpy(&boxesTests[i][0], boxes.data, sizeof(Vec4i)*boxes.rows);
        }
    }

    for (int i = 0; i < TEST_NUM; i++)
        if ((int)boxesTests[i].size() < WIN_NUM) {
            printf("%s.dat: %d, %d\n", _S(_voc.testSet[i]), boxesTests[i].size(), WIN_NUM);
            boxesTests[i].resize(WIN_NUM);
        }


    // #class by #win matrix for saving correct detection number and gt number
    Mat crNum1i = Mat::zeros(CLS_NUM, WIN_NUM, CV_32S);
    vecD gtNums(CLS_NUM);
    {
        for (int i = 0; i < TEST_NUM; i++) {
            const vector<Vec4i> &boxes = boxesTests[i];
            const vector<Vec4i> &boxesGT = _voc.gtTestBoxes[i];
            const vecI &clsGT = _voc.gtTestClsIdx[i];
            CV_Assert((int)boxes.size() >= WIN_NUM);
            const int gtNumCrnt = boxesGT.size();
            for (int j = 0; j < gtNumCrnt; j++) {
                gtNums[clsGT[j]]++;
                double maxIntUni = 0;
                int* crNum = crNum1i.ptr<int>(clsGT[j]);
                for (int k = 0; k < WIN_NUM; k++) {
                    double val = DataSetVOC::interUnio(boxes[k], boxesGT[j]);
                    maxIntUni = max(maxIntUni, val);
                    crNum[k] += maxIntUni >= 0.5 ? 1 : 0;
                }
            }
        }
    }

    FILE* f = fopen(_S(_voc.resDir + saveName), "w");
    {
        CV_Assert(f != NULL);
        fprintf(f, "figure(1);\nhold on;\n\n\n");
        vecD val(WIN_NUM), recallObjs(WIN_NUM), recallClss(WIN_NUM);
        for (int i = 0; i < WIN_NUM; i++)
            val[i] = i;
        PrintVector(f, gtNums, "GtNum");
        PrintVector(f, val, "WinNum");
        fprintf(f, "\n");
        string leglendStr("legend(");
        double sumObjs = 0;
        for (int c = 0; c < CLS_NUM; c++) {
            sumObjs += gtNums[c];
            memset(&val[0], 0, sizeof(double)*WIN_NUM);
            int* crNum = crNum1i.ptr<int>(c);
            for (int i = 0; i < WIN_NUM; i++) {
                val[i] = crNum[i]/(gtNums[c] + 1e-200);
                recallClss[i] += val[i];
                recallObjs[i] += crNum[i];
            }
            CStr className = _voc.classNames[c];
            PrintVector(f, val, className);
            fprintf(f, "plot(WinNum, %s, %s, 'linewidth', 2);\n", _S(className), COLORs[c % CN]);
            leglendStr += format("'%s', ", _S(className));
        }
        for (int i = 0; i < WIN_NUM; i++) {
            recallClss[i] /= CLS_NUM;
            recallObjs[i] /= sumObjs;
        }
        PrintVector(f, recallClss, "class");
        fprintf(f, "plot(WinNum, %s, %s, 'linewidth', 2);\n", "class", COLORs[CLS_NUM % CN]);
        leglendStr += format("'%s', ", "class");
        PrintVector(f, recallObjs, "objects");
        fprintf(f, "plot(WinNum, %s, %s, 'linewidth', 2);\n", "objects", COLORs[(CLS_NUM+1) % CN]);
        leglendStr += format("'%s', ", "objects");
        leglendStr.resize(leglendStr.size() - 2);
        leglendStr += ");";
        fprintf(f, "%s\nhold off;\nxlabel('#WIN');\nylabel('Recall');\ngrid on;\naxis([0 %d 0 1]);\n", _S(leglendStr), WIN_NUM);
        fprintf(f, "[class([1,10,100,1000]);objects([1,10,100,1000])]\ntitle('%s')\n", _S(saveName));
        fclose(f);
        printf("%-70s\r", "");
    }
    evaluatePerImgRecall(boxesTests, CmFile::GetNameNE(saveName) + "_PerI.m", WIN_NUM);
}
示例#4
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);
}