绘制PR曲线[通俗易懂]

绘制PR曲线[通俗易懂]一、获取txt文件运行darknet官方代码中的darknetdetectorvaliddatacfgweight指令(例如:darknet.exedetectorvaliddata/koujian/koujian.datacfg/yolov3-tiny11.cfgbackup/yolov3-tiny11_last.weights),可以在result/目录下得到网络检测的输出txt文件:包括检测的图像名字、类别、概率、边界框位置(左上角和右下角):二.新建两个文件:rev

大家好,又见面了,我是你们的朋友全栈君。

一、 获取txt文件

运行darknet官方代码中的darknet detector valid data cfg weight指令(例如:
darknet.exe detector valid data/koujian/koujian.data cfg/yolov3-tiny11.cfg backup/yolov3-tiny11_last.weights),可以在result/目录下得到网络检测的输出txt文件:包括检测的图像名字、类别、概率、边界框位置(左上角和右下角):
在这里插入图片描述

二.新建两个文件:

  1. reval_voc_py3.py
#!/usr/bin/env python

import os, sys, argparse
import numpy as np
import _pickle as cPickle
from voc_eval_py3 import voc_eval
import matplotlib.pyplot as plt

def do_python_eval(label_path, valid_file, classes, output_dir = 'results'):
    cachedir = os.path.join('./', 'annotations_cache')
    aps = []
    use_07_metric = False
    print('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))
    
    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)
    for i, cls in enumerate(classes):
        if cls == '__background__':
            continue
        rec, prec, ap = voc_eval(
            label_path,
            valid_file,  cls, cachedir, ovthresh=0.5,
            use_07_metric=use_07_metric)
        aps += [ap]
        print('AP for {} = {:.4f}'.format(cls, ap))
        with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
            cPickle.dump({ 
   'rec': rec, 'prec': prec, 'ap': ap}, f)
    print('Mean AP = {:.4f}'.format(np.mean(aps)))
    print('~~~~~~~~')
    print('Results:')
    for ap in aps:
        print('{:.3f}'.format(ap))
    print('{:.3f}'.format(np.mean(aps)))
  

    fr = open(cls + '_pr.pkl','rb')
    inf = cPickle.load(fr)
    fr.close()

    x=inf['rec']
    y=inf['prec']
    fig = plt.figure(1, dpi=160)
    ax = fig.add_subplot(1,1,1)
    ax.plot(x, y, label='PR')
    #ax.plot(result['Avg Recall'].values, label='Avg Recall')
    #plt.grid()
    ax.legend(loc='upper right')
    ax.set_ylim([0.6, 1.04])
    ax.set_xlim([0.0, 1.05])
    ax.set_title('PR curves')
    ax.set_xlabel('recall')
    ax.set_ylabel('precision')
    ax.spines['top'].set_visible(False)     #去掉上边框
    ax.spines['right'].set_visible(False)   #去掉右边框
    fig.savefig('PR')
    plt.savefig("PR.svg", format="svg")
''' x=inf['rec'] y=inf['prec'] plt.figure() plt.xlabel('recall') plt.ylabel('precision') plt.title('PR cruve') plt.ylim([0.6, 1.05]) plt.plot(x,y, label='PR') plt.legend(loc= 'upper right') plt.show() print('AP:',inf['ap']) '''
if __name__ == '__main__':
    label_path = r'D:\darknet\darknet-master\build\darknet\x64\data\koujian\valid'  		#label文件夹(验证集所在位置)
    valid_file = r'comp4_det_test_koujian.txt'						#valid命令生成的txt文件,在result/目录下。 
    name_path = r'D:\darknet\darknet-master\build\darknet\x64\data\koujian\koujian.names' 	#name文件
    output_dir = os.path.abspath('./')			 		#pkl保存路径
    with open(name_path, 'r') as f:	     
        lines = f.readlines()
    classes = [t.strip('\n') for t in lines]
    print('Evaluating detections')
    do_python_eval(label_path, valid_file, classes, output_dir)

其中需要修改:
(1). label_path # label文件夹,标注txt和图像应在同一目录下
(2). valid_file # valid命令生成的txt文件,在result/目录下。
(3). name_path # name文件
(4). output_dir # 生成的pkl保存路径

2 voc_eval_py3.py

import xml.etree.ElementTree as ET
import os
import _pickle as cPickle
import numpy as np
import cv2

def parse_rec(label_path, label_name):
    objects = []
    label_file = os.path.join(label_path, label_name + '.txt')
    img_file = os.path.join(label_path, label_name + '.jpg')
    height, width, _ = cv2.imread(img_file).shape
    with open(label_file) as f:
        for line in f.readlines():
            obj_struct = { 
   }
            obj_struct['name'] = 'koujian'	#需要修改成自己的names
            obj_struct['difficult'] = int(0)
            center_x, center_y, width_b, height_b =[float(x) for x in line.split()[1:]]
            obj_struct['bbox'] = [int(center_x * width - width * width_b / 2.0),
                                  int(center_y * height - height * height_b / 2.0),
                                  int(center_x * width + width * width_b / 2.0),
                                  int(center_y * height + height * height_b / 2.0)]
            objects.append(obj_struct)
    return objects

def voc_ap(rec, prec, use_07_metric=False):
    if use_07_metric:
        # 11 point metric
        ap = 0.
        for t in np.arange(0., 1.1, 0.1):
            if np.sum(rec >= t) == 0:
                p = 0
            else:
                p = np.max(prec[rec >= t])
            ap = ap + p / 11.
    else:
        # correct AP calculation
        # first append sentinel values at the end
        mrec = np.concatenate(([0.], rec, [1.]))
        mpre = np.concatenate(([0.], prec, [0.]))

        # compute the precision envelope
        for i in range(mpre.size - 1, 0, -1):
            mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])

        # to calculate area under PR curve, look for points
        # where X axis (recall) changes value
        i = np.where(mrec[1:] != mrec[:-1])[0]

        # and sum (\Delta recall) * prec
        ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
    return ap

def voc_eval(label_path,
             detpath,
             classname,
             cachedir,
             ovthresh=0.5,
             use_07_metric=False):
    # first load gt
    if not os.path.isdir(cachedir):
        os.mkdir(cachedir)
    cachefile = os.path.join(cachedir, 'annots.pkl')

    label_file = []
    for f in os.listdir(label_path):
        file, tmp = f.split('.')
        if  tmp == 'txt':
            label_file.append(file)
    
    if not os.path.isfile(cachefile):
        # load annots
        recs = { 
   }
        for label_name in label_file:
            recs[label_name] = parse_rec(label_path, label_name)
        with open(cachefile, 'wb') as f:
            cPickle.dump(recs, f)
    else:
        # load
        print('!!! cachefile = ',cachefile)
        with open(cachefile, 'rb') as f:
            recs = cPickle.load(f)

    # extract gt objects for this class
    class_recs = { 
   }
    npos = 0   #修改
    for label_name in label_file:
    # for imagename in imagenames:
        R = [obj for obj in recs[label_name] if obj['name'] == classname]
        bbox = np.array([x['bbox'] for x in R])
        difficult = np.array([x['difficult'] for x in R]).astype(np.bool)
        det = [False] * len(R)
        npos = npos + sum(~difficult)
        class_recs[label_name] = { 
   'bbox': bbox,
                                 'difficult': difficult,
                                 'det': det}

    # read dets
    detfile = detpath
    # detfile = detpath.format(classname)
    with open(detfile, 'r') as f:
        lines = f.readlines()

    splitlines = [x.strip().split(' ') for x in lines]
    image_ids = [x[0] for x in splitlines]
    confidence = np.array([float(x[1]) for x in splitlines])
    BB = np.array([[float(z) for z in x[2:]] for x in splitlines])

    # sort by confidence
    sorted_ind = np.argsort(-confidence)
    sorted_scores = np.sort(-confidence)
    BB = BB[sorted_ind, :]
    image_ids = [image_ids[x] for x in sorted_ind]
    # go down dets and mark TPs and FPs
    nd = len(image_ids)
    # print(image_ids)
    # print(nd)
    tp = np.zeros(nd)
    fp = np.zeros(nd)
    for d in range(nd):
        R = class_recs[image_ids[d]]
        bb = BB[d, :].astype(float)
        ovmax = -np.inf
        BBGT = R['bbox'].astype(float)

        if BBGT.size > 0:
            # compute overlaps
            # intersection
            ixmin = np.maximum(BBGT[:, 0], bb[0])
            iymin = np.maximum(BBGT[:, 1], bb[1])
            ixmax = np.minimum(BBGT[:, 2], bb[2])
            iymax = np.minimum(BBGT[:, 3], bb[3])
            iw = np.maximum(ixmax - ixmin + 1., 0.)
            ih = np.maximum(iymax - iymin + 1., 0.)
            inters = iw * ih

            # union
            uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +
                   (BBGT[:, 2] - BBGT[:, 0] + 1.) *
                   (BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)

            overlaps = inters / uni
            ovmax = np.max(overlaps)
            jmax = np.argmax(overlaps)

        if ovmax > ovthresh:
            if not R['difficult'][jmax]:
                if not R['det'][jmax]:
                    tp[d] = 1.
                    R['det'][jmax] = 1
                else:
                    fp[d] = 1.
        else:
            fp[d] = 1.

    # compute precision recall
    fp = np.cumsum(fp)
    tp = np.cumsum(tp)
    rec = tp / float(npos)
    # avoid divide by zero in case the first detection matches a difficult
    # ground truth
    prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)
    ap = voc_ap(rec, prec, use_07_metric)

    return rec, prec, ap

三、运行

将以上valid命令生成的txt文件和py文件放在同一个文件夹下,在python中终端运行reval_voc_py.py,可在该文件夹下得到一个pkl文件,名字为你检测的物体名字。如果没错的话应该会直接在终端生成一个PR曲线图。
当然,你也可以根据上面生成的这个pkl文件,再新建一个PR_draw.py文件:

import _pickle as cPickle
import matplotlib.pyplot as plt
fr = open('koujian_pr.pkl','rb')#这里open中第一个参数需要修改成自己生产的pkl文件
#fr1= open('quexian_pr.pkl','rb')
inf = cPickle.load(fr)

fr.close()

 
x=inf['rec']
y=inf['prec']

plt.figure()
plt.xlabel('recall')
plt.ylabel('precision')
plt.title('PR cruve')
plt.plot(x,y, label='PR')
plt.legend(loc='upper right')
plt.show()
 
print('AP:',inf['ap'])

运行该文件,同样可以得到PR曲线图。

参考链接

https://blog.csdn.net/Mr_kuilei/article/details/105641774
https://blog.csdn.net/qq_33350808/article/details/83178002

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148791.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux操作系统常用操作命令_运行cmd命令大全

    linux操作系统常用操作命令_运行cmd命令大全一.文件操作命令1.df 显示磁盘使用情况2.du 显示文件系统使用情况3.ls 显示目录普通使用:ls  ls-l  ll查看多个目录:ls/ /ect 查看/和/etc下的文件查看隐藏文件:ls-a 4.cd 切换工作目录切换到根目录:cd/切换到上一级目录:cd..切换到当前用户家目录:cd切换到普…

    2022年9月1日
    6
  • docker镜像操作_docker 本地镜像

    docker镜像操作_docker 本地镜像前言Docker的三大核心概念:镜像、容器、仓库。初学者对镜像和容器往往分不清楚,学过面向对象的应该知道类和实例,这跟面向对象里面的概念很相似我们可以把镜像看作类,把容器看作类实例化后的对象。|

    2022年7月31日
    9
  • rtsp 获取视频流 java_OpenCV – 如何捕获rtsp视频流

    rtsp 获取视频流 java_OpenCV – 如何捕获rtsp视频流例如,我们有工作rtsp流测试像:“rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov”(它在发布这篇文章的时候工作)现在我想在openCV中捕获这个视频流(opencv2.4.7/2.4.8)我的代码完全适用于本地电影文件,但当我尝试捕获rtsp时,我得到的信息如下:“无法读取电影文件RTSP://184.72.239.149/vod/m…

    2022年10月17日
    3
  • 彻底解决鼠标单击变双击问题的方法(图例)「建议收藏」

    彻底解决鼠标单击变双击问题的方法(图例)「建议收藏」两个月前,己“服役”了几年的鼠标出现了故障,单击经常变成双击,这样想用鼠标移动文件的时候就很麻烦,常常要移动几次才能成功。起初我怀疑是系统的问题,但鼠标在别的电脑上使用也出现同样的问题,因此确认鼠标本

    2022年8月1日
    16
  • java编程常用软件

    java编程常用软件有大神曾说“给我一个记事本,我还你一个项目”,作为小白的我,以前也对这句话深信不疑,但当我参加人生第一次编程考试的时候,我发现我用记事本码代码的速度实在是太慢了,一样的代码,别人用eclipseIED编写用了5分钟,而我至少半小时。虽然有点强行甩锅IDE的嫌疑,但有款好的编程软件,就会让你打代码速度更快,让你的头发掉的更少……废话讲完了,以下是我推荐的几款编程常用软件:…

    2022年6月14日
    38
  • Hadoop集群搭建,14张过程截图超详细教程

    Hadoop集群搭建,14张过程截图超详细教程作者 大数据小禅 文章简介 本篇文章主要讲解 Hadoop 集群的搭建 为了方便大家理解与操作 关键的步骤博主都进行了截图 减少小伙伴的出错概率 文章源码获取 本文的搭建 PDF 相关安装包 小伙伴们可以关注文章底部的公众号 点击 联系我 备注 Hadoop 搭建获取哦 欢迎小伙伴们点赞 收藏 留言 Hadoop 集群搭建过程 1 Hadoop 简介以及集群规划 2 基础环境准备 3 关闭防火墙 4 配置 IP 地址映射 5 添加 Hadoop 用

    2025年6月1日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号