目标检测性能评价实现

目的

计算得到数值化指标(可能不止一个),以评判一个目标检测模型表现的好坏。

为什么要写这个?一般的项目,如yoloV4/yoloV5都会提供一些接口用来评估模型表现,如计算 TP/FP/recall/precision/AP/time cost等,但它们不一定格式统一。而且有些项目,如yoloV3,并没有提供(或者我没找到)这类接口。所以这里想要自己实现一个脚本,它可以接收目标检测模型的输出,根据标准标注文件,计算格式统一的数值化的性能指标并输出。

输出展示

统一的数值化的性能指标并输出如下图:它是一个dataframe,11列,classes+2行,dataframe中的 -1表示这个数据无意义 。

列含义:class名+GT真实框个数+TP+FP(错检)+FN(漏检)+TP的IOU均值+precision+recall+AP+time cost + 图片数量

行含义:类别平均+按每个类别的GT数量加权平均+每个类

代码使用方式

  1. 新建空白 evaluator.py文件,拷贝本文的检测代码(见下一小节),无需更改任意一行代码
  2. 新建test.py文件,拷贝下文代码,填写load model 的calss ,提供预测接口,run。

你需要自己写一个 predict 函数:这个函数要满足如下形式:入参是 cv2.imread(img_path) 的结果,出参是 标签名 + 置信度 + x + y + w +h,如:

         r = [('helmet', '98.84', (131.29471998948318, 310.70161232581506, 222.4404628460224, 224.30275770334094)),
                 ('person', '99.61', (480.44410118689905, 251.42230070554294, 199.1521101731521, 242.10511537698602))]

你需要自己改一些参数:下面代码给出了一个 2分类预测的例子,  dir path 放着待预测的图片和.txt,class_num =2 ,class_name = 标签名字数字, func 是你需要自写的预测函数,两个thershold分别是IOU和置信度的阈值。

evaluator.py还提供一些参数,如AP的计算使用11等分还是全置信度等分还是指定粒度等,有兴趣可以看parameter说明使用。

import numpy as np
import cv2
import  evaluator

# 请定义自己的class, 提供predict方法


def evaluator_val():
    evaluator.evaluator_v1(img_dir=r'./to_train/img_val_GT',
                 class_num=2,
                 class_name=('belt', 'talker'),
                 func=detect.predict,
                 threshold_IOU=0.4,
                 threshold_conf=0.25,
                 ap_num=0)



if __name__ == '__main__':
    # 实例化自己定义的class ,比如叫做detect,detect含有predict函数
    detect = Detect(metaPath=r'./model_conf_py/obj.data',
                    configPath=r'./model_conf_py/yolo-obj.cfg',
                    weightPath=r'./weights/yolo-obj_6000.weights')
    print('===========model load OK')
    evaluator_val()

检测代码

import copy
import os
import time
import pandas as pd

pd.set_option('display.max_columns', 500)
pd.set_option('max_colwidth', 500)


def evaluator_v1(img_dir, class_num, class_name, func, threshold_IOU=0.5, threshold_conf=0.25, ap_num=0):
    """
    @param img_dir: e,g, img_dir = '../model/img_test/' .
            you have to put .jpg(/.JPG /.png /.PNG) files and their labeled .txt files into this path both.
            each img must have its own .jpg(or .JPG) file and txt file in this path.
            .jpg(or .JPG) file and txt file for every img should have the same name with different suffix.
            an example: 001.jpg  001.txt 。the labeled .txt files must look like this:
                 1 130.500000 309.500000 211.000000 217.000000
                 0 488.000000 252.500000 186.000000 225.000000
    @param class_num: e,g, class_num = 2. In this case your label should be 0 or 1 only
    @param class_name:  e,g, class_name = ('person', 'helmet').
            In this case person should be labeled with 0 and helmet should be 1
    @param func: your predict function. The input of your func is the path of a img
             and the ret of your func should be like:
             r = [('helmet', '98.84', (131.29471998948318, 310.70161232581506, 222.4404628460224, 224.30275770334094)),
                 ('person', '99.61', (480.44410118689905, 251.42230070554294, 199.1521101731521, 242.10511537698602))]
    @param threshold_IOU: IOU bigger than this threshold_1 means detected, otherwise miss detected!
             If more than one detection boxes are detected for a ground truth box, detection boxes with highest
             IOU will be used!
    @param threshold_conf: boxes with probability smaller than threshold_2 will be ignored!
    @param ap_num: =0 means Interpolating all points, = 11means 11-point interpolation
    @return: nothing
    """
    print('================================= evaluat
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark Kent 2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值