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
“”“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)
reval_voc_py3.py
#!/usr/bin/env python
“”“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)