void Saliency::Evaluate(const string& resultW, const string >ImgW, vecD &precision, vecD &recall) { vecS names; string inDir; int imgNum = CmFile::GetNames(resultW, names, inDir); int COLOR_NUM = 256; precision.resize(COLOR_NUM, 0); recall.resize(COLOR_NUM, 0); string truthDir = CmFile::GetFolder(gtImgW); size_t pathSize = gtImgW.size(), nPos = gtImgW.find_last_of('.'); string ext = gtImgW.substr(nPos, pathSize - nPos); for (int i = 0; i < imgNum; i++) { CmLog::LogProgress("Processing %-40s\r", names[i].c_str()); Mat resS = imread(inDir + names[i], CV_LOAD_IMAGE_GRAYSCALE); CV_Assert_(resS.data != NULL, ("Can't load saliency map: %s\n", names[i].c_str())); names[i].resize(names[i].find_last_of("_")); Mat truM = imread(truthDir + CmFile::GetNameNE(names[i]) + ext, CV_LOAD_IMAGE_GRAYSCALE); compare(truM, 128, truM, CMP_GE); if (truM.data == NULL) { CmLog::LogLine("Evaluation stopped due to missing ground truth results\n"); exit(1); } CV_Assert_(resS.size() == truM.size(), ("Saliency map and ground truth image size mismatch\n")); double groundTruth = sum(truM).val[0]; #pragma omp parallel for for (int thr = 0; thr < COLOR_NUM; thr++) { Mat resM; compare(resS, thr, resM, CMP_GE); double res = sum(resM).val[0]; bitwise_and(resM, truM, resM); double common = sum(resM).val[0]; precision[thr] += common/(res + 1e-8); recall[thr] += common/(groundTruth + 1e-8); } } for (int thr = 0; thr < COLOR_NUM; thr++) { precision[thr] /= imgNum; recall[thr] /= imgNum; } }
// Load mask image and threshold thus noisy by compression can be removed Mat CmFile::LoadMask(CStr& fileName) { Mat mask = imread(fileName, CV_LOAD_IMAGE_GRAYSCALE); CV_Assert_(mask.data != NULL, ("Can't find mask image: %s", _S(fileName))); compare(mask, 128, mask, CV_CMP_GT); return mask; }
// Get potential bounding boxes, each of which is represented by a Vec4i for (minX, minY, maxX, maxY). // The trained model should be prepared before calling this function: loadTrainedModel() or trainStageI() + trainStageII(). // Use numDet to control the final number of proposed bounding boxes, and number of per size (scale and aspect ratio) void Objectness::getObjBndBoxes(CMat &img3u, ValStructVec<float, Vec4i> &valBoxes, int numDetPerSize) { CV_Assert_(filtersLoaded() , ("SVM filters should be initialized before getting object proposals\n")); vecI sz; predictBBoxSI(img3u, valBoxes, sz, numDetPerSize, false); predictBBoxSII(valBoxes, sz); return; }
void DataSetVOC::loadBox(const FileNode &fn, vector<Vec4i> &boxes, vecI &clsIdx){ string isDifficult; fn["difficult"]>>isDifficult; if (isDifficult == "1") return; string strXmin, strYmin, strXmax, strYmax; fn["bndbox"]["xmin"] >> strXmin; fn["bndbox"]["ymin"] >> strYmin; fn["bndbox"]["xmax"] >> strXmax; fn["bndbox"]["ymax"] >> strYmax; boxes.push_back(Vec4i(atoi(_S(strXmin)), atoi(_S(strYmin)), atoi(_S(strXmax)), atoi(_S(strYmax)))); string clsName; fn["name"]>>clsName; clsIdx.push_back(findFromList(clsName, classNames)); CV_Assert_(clsIdx[clsIdx.size() - 1] >= 0, ("Invalidate class name\n")); }
vecD BenchMarkLatex::readVectorFromMatlabeFile(CStr &fileName, CStr &vecName) { ifstream fin(fileName); CV_Assert(fin.is_open()); string lineStr, token = vecName + " = ["; while (getline(fin, lineStr)) if (strncmp(_S(lineStr), _S(token), token.size() - 1) == 0) break; CV_Assert_(lineStr.size(), ("Can't load vector '%s' from: %s\n", _S(vecName), _S(fileName))); lineStr = lineStr.substr(token.size()); istringstream sIn(lineStr); vecD scores; double s; while (sIn>>s) scores.push_back(s); return scores; }
void CmEvaluation::MeanAbsoluteError(CStr inDir, CStr salDir, vecS des, bool zeroMapIfMissing) { vecS namesNE; int imgNum = CmFile::GetNamesNE(inDir + "*.jpg", namesNE), count = 0, methodNum = des.size(); vecD costs(des.size()); for (int i = 0; i < imgNum; i++){ Mat gt = imread(inDir + namesNE[i] + ".png", CV_LOAD_IMAGE_GRAYSCALE); if (gt.empty()) { if (zeroMapIfMissing){ Mat img = imread(inDir + namesNE[i] + ".jpg"); gt = Mat::zeros(img.size(), CV_8U); } else continue; } //printf("%s.jpg ", _S(namesNE[i])); gt.convertTo(gt, CV_32F, 1.0/255); #pragma omp parallel for for (int j = 0; j < methodNum; j++){ Mat res = imread(salDir + namesNE[i] + "_" + des[j] + ".png", CV_LOAD_IMAGE_GRAYSCALE); CV_Assert_(res.data != NULL, ("Can't load file %s\n", _S(namesNE[i] + "_" + des[j] + ".png"))); if (res.size != gt.size){ printf("size don't match %s\n", _S(namesNE[i] + "_" + des[j] + ".png")); resize(res, res, gt.size()); CmFile::MkDir(salDir + "Out/"); imwrite(salDir + "Out/" + namesNE[i] + "_" + des[j] + ".png", res); } res.convertTo(res, CV_32F, 1.0/255); cv::absdiff(gt, res, res); costs[j] += sum(res).val[0] / (gt.rows * gt.cols); } count++; } for (size_t j = 0; j < des.size(); j++) printf("%4s:%5.3f ", _S(des[j]), costs[j]/count); printf("\n"); }
// Return mean absolute error (MAE) double CmEvaluation::Evaluate_(CStr >ImgW, CStr &inDir, CStr& resExt, vecD &precision, vecD &recall, vecD &tpr, vecD &fpr) { vecS names; string truthDir, gtExt; int imgNum = CmFile::GetNamesNE(gtImgW, names, truthDir, gtExt); precision.resize(NUM_THRESHOLD, 0); recall.resize(NUM_THRESHOLD, 0); tpr.resize(NUM_THRESHOLD, 0); fpr.resize(NUM_THRESHOLD, 0); if (imgNum == 0){ printf("Can't load ground truth images %s\n", _S(gtImgW)); return 10^20; } else printf("Evaluating %d saliency maps ... \r", imgNum); double mea = 0; for (int i = 0; i < imgNum; i++){ if(i % 100 == 0) printf("Evaluating %03d/%d %-40s\r", i, imgNum, _S(names[i] + resExt)); Mat resS = imread(inDir + names[i] + resExt, CV_LOAD_IMAGE_GRAYSCALE); CV_Assert_(resS.data != NULL, ("Can't load saliency map: %s\n", _S(names[i] + resExt))); normalize(resS, resS, 0, 255, NORM_MINMAX); Mat gtFM = imread(truthDir + names[i] + gtExt, CV_LOAD_IMAGE_GRAYSCALE), gtBM; if (gtFM.data == NULL) continue; CV_Assert_(resS.size() == gtFM.size(), ("Saliency map and ground truth image size mismatch: %s\n", _S(names[i]))); compare(gtFM, 128, gtFM, CMP_GT); bitwise_not(gtFM, gtBM); double gtF = sum(gtFM).val[0]; double gtB = resS.cols * resS.rows * 255 - gtF; #pragma omp parallel for for (int thr = 0; thr < NUM_THRESHOLD; thr++){ Mat resM, tpM, fpM; compare(resS, thr * STEP, resM, CMP_GE); bitwise_and(resM, gtFM, tpM); bitwise_and(resM, gtBM, fpM); double tp = sum(tpM).val[0]; double fp = sum(fpM).val[0]; //double fn = gtF - tp; //double tn = gtB - fp; recall[thr] += tp/(gtF+EPS); double total = EPS + tp + fp; precision[thr] += (tp+EPS)/total; tpr[thr] += (tp + EPS) / (gtF + EPS); fpr[thr] += (fp + EPS) / (gtB + EPS); } gtFM.convertTo(gtFM, CV_32F, 1.0/255); resS.convertTo(resS, CV_32F, 1.0/255); cv::absdiff(gtFM, resS, resS); mea += sum(resS).val[0] / (gtFM.rows * gtFM.cols); } int thrS = 0, thrE = NUM_THRESHOLD, thrD = 1; for (int thr = thrS; thr != thrE; thr += thrD){ precision[thr] /= imgNum; recall[thr] /= imgNum; tpr[thr] /= imgNum; fpr[thr] /= imgNum; } return mea/imgNum; }