Skip to content

Instantly share code, notes, and snippets.

@berak
Created December 25, 2016 08:08
Show Gist options
  • Select an option

  • Save berak/cb6da5ec8c69394c084f63630c0d7fbc to your computer and use it in GitHub Desktop.

Select an option

Save berak/cb6da5ec8c69394c084f63630c0d7fbc to your computer and use it in GitHub Desktop.
ROC opencv
namespace ROC {
void curve(const Mat &probs, const Mat &truth, vector<Point2f> &roc, int N, const float eps=1e-1) {
for (int i=0; i<N; i++) {
float thresh = float(N-i) / N;
float TP = countNonZero((probs > thresh) & (truth > eps));
float TN = countNonZero((probs <= thresh) & (truth <= eps));
float FP = countNonZero((probs > thresh) & (truth <= eps));
float FN = countNonZero((probs <= thresh) & (truth > eps));
float FPR = FP / (FP + TN);
float TPR = TP / (TP + FN);
roc.push_back(Point2f(FPR, TPR));
}
}
float auc(vector<Point2f> &roc) {
float _auc = 0.0f;
for (int i=0; i<int(roc.size())-1; i++) {
_auc += (roc[i+1].y + roc[i].y) * (roc[i+1].x - roc[i].x);
}
return _auc * 0.5f;
}
void draw(vector<Point2f> &roc, Mat &img, const Scalar &color) {
int N = roc.size();
float S = float(img.rows) / N;
Point2f prev;
for (size_t i=0; i<roc.size(); i++) {
Point2f cur(roc[i].x*N*S, (1.0-roc[i].y)*N*S); // opencv y axis points down
if (i>0)
line(img, prev, cur, color, 1);
prev = cur;
}
}
} // ROC
/*
svm->predict(vdata, results, ml::StatModel::RAW_OUTPUT);
normalize(results, results, 1, 0, NORM_MINMAX);
// svm gives distances, needed are probs
results = 1.0f - results;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment