我在 Caffe 中训练了一个二元类 CNN,现在我想绘制 ROC 曲线并计算 AUC 值。我有两个问题: 1)如何用python在Caffe中绘制ROC曲线? 2)如何计算ROC曲线的AUC值?
请您参考如下方法:
Python 有 roc_curve和 roc_auc_score sklearn.metrics 模块中的函数,只需导入并使用它们即可。
假设您有一个二元预测层,它输出二元类概率的双向量(我们称它为 “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)
