对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为每个类的曲线的颜色,颜色的数量要多于类别个数否则不够用。

相关推荐
AntBlack9 分钟前
每周学点 AI:ComfyUI + Modal 的一键部署脚本
人工智能·后端·aigc
l1t1 小时前
张泽鹏先生手搓的纯ANSI处理UTF-8与美团龙猫调用expat库读取Excel xml对比测试
xml·人工智能·excel·utf8·expat
THMAIL1 小时前
量化基金从小白到大师 - 金融数据获取大全:从免费API到Tick级数据实战指南
人工智能·python·深度学习·算法·机器学习·金融·kafka
zzywxc7871 小时前
AI在金融、医疗、教育、制造业等领域的落地案例(含代码、流程图、Prompt示例与图表)
人工智能·spring·机器学习·金融·数据挖掘·prompt·流程图
周末程序猿2 小时前
谈谈Vibe编程(氛围编程)
人工智能
Tiger Z2 小时前
《动手学深度学习v2》学习笔记 | 2.4 微积分 & 2.5 自动微分
pytorch·深度学习·ai
软件算法开发2 小时前
基于LSTM深度学习的网络流量测量算法matlab仿真
深度学习·matlab·lstm·网络流量测量
水印云2 小时前
AI配音工具哪个好用?7款热门配音软件推荐指南!
人工智能·语音识别
Luke Ewin2 小时前
FunASR的Java实现Paraformer实时语音识别 | 一款无需联网的本地实时字幕软件
java·人工智能·语音识别·asr·funasr·paraformer·sensevoice
先做个垃圾出来………2 小时前
PyTorch 模型文件介绍
人工智能·pytorch·python