如何计算Caffe中的ROC和AUC?

时间:2017-06-10 07:27:02

标签: python machine-learning neural-network caffe roc

我已经在Caffe中训练了一个二进制类CNN,现在我想绘制ROC曲线并计算AUC值。我有两个问题: 1)如何使用python绘制Caffe中的ROC曲线? 2)如何计算ROC曲线的AUC值?

1 个答案:

答案 0 :(得分:2)

Python在sklearn.metrics模块中有roc_curveroc_auc_score个函数,只需导入并使用它们。

假设你有一个二进制预测层,它输出一个二元类概率的二向量(我们称之为"prob"),那么你的代码应该是这样的:

import caffe
from sklearn import metrics
# load the net with trained weights
net = caffe.Net('/path/to/deploy.prototxt', '/path/to/weights.caffemodel', caffe.TEST)
y_score = []  
y_true = []
for i in xrange(N): # assuming you have N validation samples
    x_i = ... # get i-th validation sample
    y_true.append( y_i )  # y_i is 0 or 1 the TRUE label of x_i
    out = net.forward( data=x_i )  # get prediction for x_i
    y_score.append( out['prob'][1] ) # get score for "1" class
# once you have N y_score and y_true values
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_score, pos_label=1)
auc = metrics.roc_auc_score(y_true, y_scores)