// 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); }
// 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); }
void Objectness::trainStateII(int numPerSz) { loadTrainedModel(); const int NUM_TRAIN = _voc.trainNum; vector<vecI> SZ(NUM_TRAIN), Y(NUM_TRAIN); vector<vecF> VAL(NUM_TRAIN); #pragma omp parallel for for (int i = 0; i < _voc.trainNum; i++) { const vector<Vec4i> &bbgts = _voc.gtTrainBoxes[i]; ValStructVec<float, Vec4i> valBoxes; vecI &sz = SZ[i], &y = Y[i]; vecF &val = VAL[i]; CStr imgPath = format(_S(_voc.imgPathW), _S(_voc.trainSet[i])); predictBBoxSI(imread(imgPath), valBoxes, sz, numPerSz, false); const int num = valBoxes.size(); CV_Assert(sz.size() == num); y.resize(num), val.resize(num); for (int j = 0; j < num; j++) { Vec4i bb = valBoxes[j]; val[j] = valBoxes(j); y[j] = maxIntUnion(bb, bbgts) >= 0.5 ? 1 : -1; } } const int NUM_SZ = _svmSzIdxs.size(); const int maxTrainNum = 100000; vector<vecM> rXP(NUM_SZ), rXN(NUM_SZ); for (int r = 0; r < NUM_SZ; r++) { rXP[r].reserve(maxTrainNum); rXN[r].reserve(1000000); } for (int i = 0; i < NUM_TRAIN; i++) { const vecI &sz = SZ[i], &y = Y[i]; vecF &val = VAL[i]; int num = sz.size(); for (int j = 0; j < num; j++) { int r = sz[j]; CV_Assert(r >= 0 && r < NUM_SZ); if (y[j] == 1) rXP[r].push_back(Mat(1, 1, CV_32F, &val[j])); else rXN[r].push_back(Mat(1, 1, CV_32F, &val[j])); } } Mat wMat(NUM_SZ, 2, CV_32F); for (int i = 0; i < NUM_SZ; i++) { const vecM &xP = rXP[i], &xN = rXN[i]; if (xP.size() < 10 || xN.size() < 10) printf("Warning %s:%d not enough training sample for r[%d] = %d. P = %d, N = %d\n", __FILE__, __LINE__, i, _svmSzIdxs[i], xP.size(), xN.size()); for (size_t k = 0; k < xP.size(); k++) CV_Assert(xP[k].size() == Size(1, 1) && xP[k].type() == CV_32F); Mat wr = trainSVM(xP, xN, L1R_L2LOSS_SVC, 100, 1); CV_Assert(wr.size() == Size(2, 1)); wr.copyTo(wMat.row(i)); } matWrite(_modelName + ".wS2", wMat); _svmReW1f = wMat; }