【发布时间】:2017-11-12 05:39:32
【问题描述】:
我已经在 Caffe 中训练了一个二元类 CNN,现在我想绘制 ROC 曲线并计算 AUC 值。我有两个问题: 1)如何用python绘制Caffe中的ROC曲线? 2) 如何计算ROC曲线的AUC值?
【问题讨论】:
标签: python machine-learning neural-network caffe roc
我已经在 Caffe 中训练了一个二元类 CNN,现在我想绘制 ROC 曲线并计算 AUC 值。我有两个问题: 1)如何用python绘制Caffe中的ROC曲线? 2) 如何计算ROC曲线的AUC值?
【问题讨论】:
标签: python machine-learning neural-network caffe roc
Python 在sklearn.metrics 模块中有roc_curve 和roc_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)
【讨论】: