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