都2020年了还有人写python2的求解教程,python2都快停止维护了好嘛?
本教程针对darknet版本的yolov3进行计算mAP
第一步:通过darknet valid命令计算yolo3推断结果
在这之前你要在xxx.data里面配置vaild那一条的路径,是一个全是文件名不包含后缀和路径的txt文档,如果你按照标准流程训练的yolo3一定会有这个东西
./darknet detector valid cfg/voc.data cfg/yolo3-spp.cfg backup/yolov3.backup -out "" -thresh .5
关于这个.5 改了好像不生效,貌似是写死在detector.c里面的,好几处,我不知道怎么改。
运行结束后再你的yolov3根目录下面的results里面会有几个txt
第二步:编写计算文件
文件名:voc_eval_py3.py
# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Bharath Hariharan
# --------------------------------------------------------
import xml.etree.ElementTree as ET
import os
#import cPickle
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, -