yolo模型(四)绘制PR曲线所用到的reval_voc_py.py代码

laiyuan:https://blog.csdn.net/xue_csdn/article/details/95519585







在绘制PR曲线时要自己生成一个pkl文件,需要reval_voc_py.py代码来生。现贴出基于python2和python3的代码,按照自己的python版本直接复制即可。拿走不谢~

reval_voc_py.py

#!/usr/bin/env python

Adapt from ->

--------------------------------------------------------

Fast R-CNN

Copyright © 2015 Microsoft

Licensed under The MIT License [see LICENSE for details]

Written by Ross Girshick

--------------------------------------------------------

<- Written by Yaping Sun

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

import os, sys, argparse
import numpy as np
import cPickle

from voc_eval import voc_eval

def parse_args():
“”"
Parse input arguments
“”"
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=‘data/VOCdevkit’, type=str)
parser.add_argument(’–year’, dest=‘year’, default=‘2017’, type=str)
parser.add_argument(’–image_set’, dest=‘image_set’, default=‘test’, type=str)
parser.add_argument(’–classes’, dest=‘class_file’, default=‘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 + ‘_{}.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’,
‘{}.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)
aps += [ap]
print(‘AP for {} = {:.4f}’.format(cls, ap))
with open(os.path.join(output_dir, cls + ‘_pr.pkl’), ‘w’) 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])
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

reval_voc_py3.py

#!/usr/bin/env python

Adapt from ->

--------------------------------------------------------

Fast R-CNN

Copyright © 2015 Microsoft

Licensed under The MIT License [see LICENSE for details]

Written by Ross Girshick

--------------------------------------------------------

<- Written by Yaping Sun

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

import os, sys, argparse
import numpy as np
import _pickle as cPickle
#import cPickle

from voc_eval_py3 import voc_eval

def parse_args():
“”"
Parse input arguments
“”"
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=‘data/VOCdevkit’, type=str)
parser.add_argument(’–year’, dest=‘year’, default=‘2017’, type=str)
parser.add_argument(’–image_set’, dest=‘image_set’, default=‘test’, type=str)
parser.add_argument(’–classes’, dest=‘class_file’, default=‘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 + ‘_{}.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’,
‘{}.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’))
print(‘devkit_path=’,devkit_path,’, year = ',year)

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)
    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])
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

你可能感兴趣的:(yolov3)