码迷,mamicode.com
首页 > 其他好文 > 详细

yolo3的ap、mAP计算

时间:2020-05-23 13:27:28      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:format   目录   category   arguments   chef   根据   reference   技术   desktop   

yolo3的ap、mAP计算

一、准备工作

继上次探索的结果,我们成功编译了darknet,后来惊讶发现在darknet\build\darknet\x64目录下就有这两个py文件用来算ap值:reval_voc_py3.py,voc_eval_py3.py

二、先在\64\results中生成测试结果文件

首先通过valid命令,遍历一遍测试数据集,跑出来训练好的网络在这个测试数据集的结果,命令如下

darknet detector valid cfg/voc.data  cfg/tiny_yolo_voc.cfg tiny_yolo_voc.weights

首先进入x64目录下,

cmd或者terminal输入例如

$ darknet detector valid data/voc.data yolov3.cfg yolo_5141.weights

分为四部分,darknet detector valid;data/voc.data表示我要用data文件夹下的voc.data;yolov3.cfg表示训练weights时用到cfg;yolo_5141.weights则是你要测试的weights了。

注意:在执行该命令的时候,需要你的当前路径下有一个results的文件夹,不然会报segmentation fault的错误,如果没有可以手动新建。

技术图片

接下来就会在results文件夹下看到

技术图片

对应有几个class就会有几个这种文件

三、开始测试

下面两个就是核心测试py

有些地方我根据自己的情况做了修改,然运行更加方便

"""Reval = re-eval. Re-evaluate saved detections.

usage:
        input with the command: $python reval_voc.py --voc_dir VOCdevkit --year 2007 --image_set test --class ./data/voc.names
                            Actually we input $ python reval_voc.py --voc_dir C:\\Users\\Breeze\\Desktop\                                                    Mask-or-Not\\darknet\\build\\darknet\\x64\\results
                            will be okay,since I have got the default value changed to my path.
                            注释里面的\得换成\\否则会报错
NOTE:this .py has to be opened with the results.file,otherwise the import of voc_eval would break with error
"""

import argparse
import os
import pickle as cPickle
import sys

import numpy as np

from voc_eval import voc_eval

def parse_args():
    """
    Parse input arguments
    """
    #以下几个argument,我在原来基础上都加了default,其实看着我改过的就很容易理解
    #voc_dir就是VOCdevkit的路径
    #year默认成你文件夹对应的,我是2020
	#几个可选变量都设成了默认,所以在cmd就只需要输入必选变量output_dir 即可,也就是生成文件保存在这个地#方
    parser = argparse.ArgumentParser(description=‘Re-evaluate results‘)
    parser.add_argument(‘output_dir‘, nargs=1, help=‘results directory‘,
                        type=str)
    parser.add_argument(‘--voc_dir‘, dest=‘voc_dir‘, default=‘C:\\Users\\Breeze\\Desktop‘
                                                             ‘\\keras-yolo3-master\\VOCdevkit‘, type=str)
    parser.add_argument(‘--year‘, dest=‘year‘, default=‘2020‘, type=str)
    parser.add_argument(‘--image_set‘, dest=‘image_set‘, default=‘test‘, type=str)

    parser.add_argument(‘--classes‘, dest=‘class_file‘, default=‘C:\\Users\\Breeze\\Desktop\\darknet\\‘
                                                                ‘darknet-master\\build\darknet\\x64\\data\\voc.names‘,
                        type=str)

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args


def get_voc_results_file_template(image_set, out_dir=‘results‘):
    filename = ‘comp4_det_‘ + image_set + ‘_{:s}.txt‘
    path = os.path.join(out_dir, filename)
    return path


def do_python_eval(devkit_path, year, image_set, classes, output_dir=‘results‘):
    annopath = os.path.join(
        devkit_path,
        ‘VOC‘ + year,
        ‘Annotations‘,
        ‘{:s}.xml‘)
    imagesetfile = os.path.join(
        devkit_path,
        ‘VOC‘ + year,
        ‘ImageSets‘,
        ‘Main‘,
        image_set + ‘.txt‘)
    cachedir = os.path.join(devkit_path, ‘annotations_cache‘)
    aps = []
    # The PASCAL VOC metric changed in 2010
    use_07_metric = True if int(year) < 2010 else 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

        filename = get_voc_results_file_template(image_set).format(cls)
        rec, prec, ap = voc_eval(
            filename, annopath, imagesetfile, cls, cachedir, ovthresh=0.5,
            use_07_metric=use_07_metric)
        print("HERE")
        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)))
    print(‘~~~~~~~~‘)
    print(‘‘)
    print(‘--------------------------------------------------------------‘)
    print(‘Results computed with the **unofficial** Python eval code.‘)
    print(‘Results should be very close to the official MATLAB eval code.‘)
    print(‘-- Thanks, The Management‘)
    print(‘--------------------------------------------------------------‘)


if __name__ == ‘__main__‘:
    args = parse_args()

    output_dir = os.path.abspath(args.output_dir[0])
    print(args.class_file)
    with open(args.class_file, ‘r‘) as f:
        lines = f.readlines()

    classes = [t.strip(‘\n‘) for t in lines]

    print(‘Evaluating detections‘)
    do_python_eval(args.voc_dir, args.year, args.image_set, classes, output_dir)
import xml.etree.ElementTree as ET
import os
import pickle as cPickle
import numpy as np


def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall(‘object‘):
        obj_struct = {}
        obj_struct[‘name‘] = obj.find(‘name‘).text
        #obj_struct[‘pose‘] = obj.find(‘pose‘).text
        #obj_struct[‘truncated‘] = int(obj.find(‘truncated‘).text)
        obj_struct[‘difficult‘] = int(obj.find(‘difficult‘).text)
        bbox = obj.find(‘bndbox‘)
        obj_struct[‘bbox‘] = [int(bbox.find(‘xmin‘).text),
                              int(bbox.find(‘ymin‘).text),
                              int(bbox.find(‘xmax‘).text),
                              int(bbox.find(‘ymax‘).text)]
        objects.append(obj_struct)

    return objects

def voc_ap(rec, prec, use_07_metric=False):
    """ ap = voc_ap(rec, prec, [use_07_metric])
    Compute VOC AP given precision and recall.
    If use_07_metric is true, uses the
    VOC 07 11 point method (default: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(detpath,
             annopath,
             imagesetfile,
             classname,
             cachedir,
             ovthresh=0.5,
             use_07_metric=False):
    """rec, prec, ap = voc_eval(detpath,
                                annopath,
                                imagesetfile,
                                classname,
                                [ovthresh],
                                [use_07_metric])

    Top level function that does the PASCAL VOC evaluation.

    detpath: Path to detections
        detpath.format(classname) should produce the detection results file.
    annopath: Path to annotations
        annopath.format(imagename) should be the xml annotations file.
    imagesetfile: Text file containing the list of images, one image per line.
    classname: Category name (duh)
    cachedir: Directory for caching the annotations
    [ovthresh]: Overlap threshold (default = 0.5)
    [use_07_metric]: Whether to use VOC07‘s 11 point AP computation
        (default False)
    """
    # assumes detections are in detpath.format(classname)
    # assumes annotations are in annopath.format(imagename)
    # assumes imagesetfile is a text file with each line an image name
    # cachedir caches the annotations in a pickle file

    # first load gt
    if not os.path.isdir(cachedir):
        os.mkdir(cachedir)

    cachefile = os.path.join(cachedir, ‘annots.pkl‘)
    # read list of images
    with open(imagesetfile, ‘r‘) as f:
        lines = f.readlines()
    imagenames = [x.strip() for x in lines]

    if not os.path.isfile(cachefile):
        # load annots

        recs = {}
        for i, imagename in enumerate(imagenames):
            #print(annopath.format(imagename))
            #print(imagenames)
            recs[imagename] = parse_rec("C:\\Users\\Breeze\\Desktop\\keras-yolo3-master\\VOCdevkit\\VOC2020"
                                        "\\Annotations\\"+annopath.format(imagename))
            #print("PY")
            if i % 100 == 0:
                print(‘Reading annotation for {:d}/{:d}‘.format(
                    i + 1, len(imagenames)))
        # save
        print(‘Saving cached annotations to {:s}‘.format(cachefile))
        with open(cachefile, ‘wb‘) as f:
            cPickle.dump(recs, f)
    else:
        # load
        with open(cachefile, ‘rb‘) as f:
            #print(cachefile)
            recs = cPickle.load(f)

    # extract gt objects for this class
    class_recs = {}
    npos = 0
    for imagename in imagenames:
        R = [obj for obj in recs[imagename] 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[imagename] = {‘bbox‘: bbox,
                                 ‘difficult‘: difficult,
                                 ‘det‘: det}

    # read dets
    detfile = detpath.format(classname)
    #原文用的相对路径,不是太好控制,所以这里直接改成绝对路径
    detfile="C:\\Users\\Breeze\\Desktop\\darknet\\darknet-master\\build\\darknet\\x64\\"+detfile
    #print(detfile)
    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)
    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

技术图片

我认为理解这段话是关键

由于一些文件位置原因,我把所有路径都换成了绝对路径

最后在results目录下

$python reval_voc.py C:\Users\Breeze\Desktop\Mask-or-Not\darknet\build\darknet\x64\results

因为output_dir是必选,所以不用加--output_dir

最终结果:

技术图片

好像是很不错的样子

REFERENCE

http://pjreddie.com/darknet/

https://www.jianshu.com/p/7ae10c8f7d77/

(最早的时候是看了简书这篇文章,用的也是他的代码,然而一堆错误,主要是因为python3和python2的原因。python3只有Pickle,并且在读写二进制文件的时候要rb,wb)

yolo3的ap、mAP计算

标签:format   目录   category   arguments   chef   根据   reference   技术   desktop   

原文地址:https://www.cnblogs.com/fragrant-breeze/p/12941991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!