对caffe跑前向后生成的预测文件画ROC曲线

在前面一篇python版的caffe前向中,生成了一个用于画ROC曲线的txt文件,作为本代码的输入:

bash 复制代码
# -*- coding:utf-8
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve,auc
import numpy as np

colors = ['r', 'g', 'b', 'yellow', 'pink', 'black', 'purple', 'lime']

def get_output_file1(output_file, classNumber):
    prod_all=[]
    label_all=[]
    for line in open(output_file):
        x = line.split()
        prod=[]
        label=[]
        for i in range(int(classNumber)):
            prod.append(float(x[i]))
        tag = int(x[classNumber])#int(x[classNumber])
        for j in range(classNumber):
            if (j == tag):
                label.append(1)
            else:
                label.append(0)
        prod_all.append(prod)
        label_all.append(label)
    return  prod_all,label_all

def ROC(prod_all, label_all, classLabel, output_txtname, rgb="r", leged="line"):
    y_true = np.array(label_all)
    y_predict = np.array(prod_all)
    fpr, tpr, thr = roc_curve(y_true[:, classLabel], y_predict[:, classLabel])
    fid = open(output_txtname, 'a+')
    fid.writelines(str(classLabel)+"\n"+" fpr  tpr   thr"+"\n")
    for i in range(len(fpr)):
        fid.writelines( str(fpr[i])+" "+str(tpr[i])+" "+str(thr[i])+"\n")
    AUC=auc(fpr, tpr)
    plt.plot(fpr, tpr,  clip_on=False,color=rgb,label=leged+'-'+str(AUC)[0:6])
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.0])
    plt.legend(loc='best')
    return AUC

if __name__ == '__main__':

    classNumber = 3
    classes = ['eye','nose', 'ear']

    input_txtname = '/.../roc.txt'

    output_txtname = './roc1_th.txt'
    output_imgname = './roc8.jpg'

    prod_all, label_all = get_output_file1(input_txtname, classNumber)
    AUC = []
    for i in range(classNumber):
        AUC.append(ROC(prod_all, label_all, i, output_txtname, rgb=colors[i], leged=classes[i]))
    #plt.title("AUC")
    #plt.show()
    plt.savefig(output_imgname)

这里的colors为每个类的曲线的颜色,颜色的数量要多于类别个数否则不够用。

相关推荐
匠测AI说44 分钟前
AI for Testing 提效实战 · 执行自动化(五):这次该跑哪些用例,让 AI 圈回归范围
人工智能·测试
PingCAP2 小时前
从一句需求到一套可追溯机制:我们这样推进电子病历改造
人工智能·agent·工作流引擎
PingCAP2 小时前
平凯星辰刘松:稳定交付严肃任务正成为企业级智能体的重要考验
人工智能·agent·工作流引擎
米小虾2 小时前
模型即 Agent?那 Harness 还有用吗
人工智能·agent
米小虾2 小时前
Harness 五要素:从"能跑"到"可靠"
人工智能·agent
孟健3 小时前
LLM API 安全防线:从 OpenCode 盗刷事件到 LiteLLM 多模型容灾实操
人工智能·后端·架构
程序员cxuan3 小时前
我用 WorkBuddy + ima 把公众号文章做成了知识库。
人工智能·后端·程序员
Code_Artist3 小时前
AI 终于学会写正确的汉字了?GPT Image 2.0 到底做对了什么?
人工智能·chatgpt·openai
程序员cxuan3 小时前
玩了一天 REA,我号快没了
人工智能·后端·程序员