AOC的计算

news/2024/9/23 6:37:15/

AOC指的是PR(精度-召回)曲线的曲线下面积,用于评估目标检测网络的性能。
AOC的计算过程如下:
1、获得网络推断的pred_bbox、pred_class、pred_score和真值gt_bbox、gt_class
2、对于每个类别,根据iou条件,增加一个正例(TP)或负例(FP)和对应的score
3、按照score升序,计算精度和召回的前缀和向量
4、计算不同召回下的精度,累加得到AOC

参考代码:
https://github.com/garg-abhinav/FasterRCNN

import numpy as np
import itertools
from collections import defaultdictdef bbox_iou(bbox_a, bbox_b):'''This function calculates IoU (intersection of union) between bounding boxes. The IoU ratios are used to eliminate overlapping bounding boxes and for training we only take into account with IoU < 0.3 and IoU > 0.7 for labeling as foreground and background.'''    if bbox_a.shape[1] != 4 or bbox_b.shape[1] != 4:raise IndexError# top lefttl = np.maximum(bbox_a[:, None, :2], bbox_b[:, :2])  # bottom rightbr = np.minimum(bbox_a[:, None, 2:], bbox_b[:, 2:])area_i = np.prod(br - tl, axis=2) * (tl < br).all(axis=2)area_a = np.prod(bbox_a[:, 2:] - bbox_a[:, :2], axis=1)area_b = np.prod(bbox_b[:, 2:] - bbox_b[:, :2], axis=1)return area_i / (area_a[:, None] + area_b - area_i)'''
These functions are used to calculate the mAP values for testing phase
'''def eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels,iou_thresh=0.5, use_07_metric=False):prec, rec = calc_detection_voc_prec_rec(pred_bboxes, pred_labels, pred_scores,gt_bboxes, gt_labels,iou_thresh=iou_thresh) ##  计算累加的precision 和 recall 矩阵ap = calc_detection_voc_ap(prec, rec) ## 所有类别的ap## use 07 metric 就是11点形式return {'ap': ap, 'map': np.nanmean(ap)}## 预处理 precision和 recall 
def calc_detection_voc_prec_rec(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels,iou_thresh=0.5):pred_bboxes = iter(pred_bboxes)pred_labels = iter(pred_labels)pred_scores = iter(pred_scores)gt_bboxes = iter(gt_bboxes)gt_labels = iter(gt_labels)n_pos = defaultdict(int)score = defaultdict(list)match = defaultdict(list) ## 默认value为listfor pred_bbox, pred_label, pred_score, gt_bbox, gt_label in \zip(pred_bboxes, pred_labels, pred_scores,gt_bboxes, gt_labels):for l in np.unique(np.concatenate((pred_label, gt_label)).astype(int)):pred_mask_l = pred_label == lpred_bbox_l = pred_bbox[pred_mask_l]pred_score_l = pred_score[pred_mask_l]gt_mask_l = gt_label == lgt_bbox_l = gt_bbox[gt_mask_l]n_pos[l] += gt_mask_l.sum() ## 用于计算recall, tp+fn score[l].extend(pred_score_l)if len(pred_bbox_l) == 0:continueif len(gt_bbox_l) == 0:match[l].extend((0,) * pred_bbox_l.shape[0])continue# VOC evaluation follows integer typed bounding boxes.pred_bbox_l = pred_bbox_l.copy() ## pred_bbox_l[:, 2:] += 1gt_bbox_l = gt_bbox_l.copy()gt_bbox_l[:, 2:] += 1iou = bbox_iou(pred_bbox_l, gt_bbox_l) ## 删掉iou小于0.5的结果gt_index = iou.argmax(axis=1)# set -1 if there is no matching ground truthgt_index[iou.max(axis=1) < iou_thresh] = -1del iouselec = np.zeros(gt_bbox_l.shape[0], dtype=bool)for gt_idx in gt_index:if gt_idx >= 0:if not selec[gt_idx]:match[l].append(1)else:match[l].append(0)selec[gt_idx] = Trueelse:match[l].append(0)for iter_ in (pred_bboxes, pred_labels, pred_scores,gt_bboxes, gt_labels):if next(iter_, None) is not None:raise ValueError('Length of input iterables need to be same.')n_fg_class = max(n_pos.keys()) + 1prec = [None] * n_fg_classrec = [None] * n_fg_classfor l in n_pos.keys():score_l = np.array(score[l])match_l = np.array(match[l], dtype=np.int8)order = score_l.argsort()[::-1] ## 排序后保留前缀和,便于计算AOCmatch_l = match_l[order]tp = np.cumsum(match_l == 1)fp = np.cumsum(match_l == 0)# If an element of fp + tp is 0,# the corresponding element of prec[l] is nan.prec[l] = tp / (fp + tp)# If n_pos[l] is 0, rec[l] is None.if n_pos[l] > 0:rec[l] = tp / n_pos[l]return prec, rec## 计算AOC 
def calc_detection_voc_ap(prec, rec):##  prec 精度 , rec 召回n_fg_class = len(prec) ## 类别数ap = np.empty(n_fg_class)for l in range(n_fg_class):if prec[l] is None or rec[l] is None:ap[l] = np.nancontinue# 11 point metricap[l] = 0for t in np.arange(0., 1.1, 0.1):if np.sum(rec[l] >= t) == 0:p = 0else:p = np.max(np.nan_to_num(prec[l])[rec[l] >= t])ap[l] += p / 11return ap

http://www.ppmy.cn/news/334440.html

相关文章

AOC的显示器真的很烂

也不知道能用其它的什么词来形容AOC的显示器了&#xff0c;自从上次返修后不过半年多时间&#xff0c;显示器又再度出现划痕&#xff01;也不知道是怎么出来的&#xff0c;似乎是有很多道划痕划在某个薄膜&#xff0c;但这层膜又不在外表。也罢&#xff0c;再坚持上半年&#x…

LC-显示器2297

题目描述 你的一个朋友刚买了一台新电脑。 到目前为止&#xff0c;他曾经使用过的最强大的计算机是袖珍计算器。 现在&#xff0c;看着他的新电脑&#xff0c;他有点失望&#xff0c;因为他非常喜欢他的计算器的LC显示器。 所以你决定编写一个程序&#xff0c;在他的计算机上以…

睡眠脑电中的神经跨频率耦合函数

导读 人类大脑是一个紧密连接的复杂系统。虽然其结构比较固定&#xff0c;但它可以实现很多不同的功能。其中一个重要的功能是自然睡眠过程&#xff0c;这个过程可以改变意识和随意肌肉活动。在神经层面上&#xff0c;这些改变会伴随着大脑连接的变化。为了揭示这种与睡眠相关…

AOC1952 显示屏 输入不支持

选择对应的模式即可修正错误提示

固态硬盘Ghost安装Windows 10无法引导的问题

机器配置如下: 电脑型号 技嘉 B360M POWER 台式电脑操作系统 Windows 10 64位 ( DirectX 12 )处理器 英特尔 Core i7-8700 @ 3.20GHz 六核主板 技嘉 B360M POWER ( 英特尔 PCI 标准主机 CPU 桥 - CannonLake - A308 )显卡 …

计算机网络开荒3-传输层

文章目录 一、传输层概述1.1 网络层 vs 传输层 二、多路复用 多路分用三、UDP3.1 RDT3.1.1 Rdt3.1.1.1 Rdt1.03.1.1.2 Rdt2.03.1.1.3 Rdt2.13.1.1.4 Rdt2.23.11.5 Rdt 3.0 四、滑动窗口协议4.1 流水线机制4.1.2 滑动窗口协议GBNSR 五、TCP5.1 可靠数据传输5.1.1 RTT和超时 5.2 …

物联网卡有哪些规格?

按形态分为插拔式&#xff08;M2M-PlugIN&#xff0c;即MP卡&#xff09;&#xff0c;贴片式&#xff08;M2M-SMD&#xff0c;即MS卡&#xff09;&#xff1b; 按尺寸大小&#xff0c;MP卡分为普通大卡、双切卡、三切卡卡&#xff1b;MS卡有5mm6mm、5mm5mm&#xff08;不常见&a…

3分钟,把你的安卓手机/平板变成你的电脑副屏

首先下载资源包&#xff0c;直接下载&#xff0c;不需要积分&#xff0c;资源挂了记得评论或私信 https://download.csdn.net/download/VoiceKing2017/53979539 下载后先关闭电脑的VPN类软件&#xff08;如果你不知道什么是VPN&#xff0c;就跳过这一步。&#xff09; 然后双…