一、 获取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文件:包括检测的图像名字、类别、概率、边界框位置(左上角和右下角):
二.新建两个文件:
- 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(