YOLOv5-4.0-metrics.py 源代码导读

YOLOv5介绍

YOLOv5为兼顾速度与性能的目标检测算法。笔者将在近期更新一系列YOLOv5的代码导读博客。YOLOv5为2021.1.5日发布的4.0版本。
YOLOv5开源项目github网址
源代码导读汇总网址
本博客导读的代码为utils文件夹下的metrics.py

metrics.py

该文件通过获得到的预测结果与ground truth表现计算指标P、R、F1-score、AP、不同阈值下的mAP等。同时,该文件将上述指标进行了可视化,绘制了混淆矩阵以及P-R曲线。
相关导入模块及说明如下所示。

from pathlib import Path        #调用路径操作模块
import matplotlib.pyplot as plt #matplotlib画图软件
import numpy as np              #numpy矩阵处理模块
import torch                    #pytorch
from . import general           #从当前文件所处的相对路径调用general.py

fitness函数 通过指标加权的形式返回适应度

def fitness(x):
    # 以矩阵的加权组合作为模型的适应度
    w = [0.0, 0.0, 0.1, 0.9]  # 每个变量对应的权重 [P, R, [email protected], [email protected]:0.95]
    # (torch.tensor).sum(1) 每一行求和tensor为二维时返回一个以每一行求和为结果的行向量 
    return (x[:, :4] * w).sum(1)

ap_per_class 函数计算每一个类的AP指标

def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='precision-recall_curve.png', names=[]):
    """ 计算平均精度(AP),并绘制P-R曲线
    源代码来源: https://github.com/rafaelpadilla/Object-Detection-Metrics.
    # Arguments(变量)
        tp:  True positives (nparray, nx1 or nx10).   真阳
        conf:  Objectness value from 0-1 (nparray).   目标的置信度取值0-1
        pred_cls:  Predicted object classes (nparray).预测目标类别
        target_cls:  True object classes (nparray).   真实目标类别
        plot:  Plot precision-recall curve at [email protected] 是否绘制P-R曲线 在[email protected]的情况下
        save_dir:  P-R曲线图的保存路径
    # Returns(返回)
        像faster-rcnn那种方式计算AP (这里涉及计算AP的两种不同方式 建议查询)
        The average precision as computed in py-faster-rcnn.
    """

    # 将目标进行排序
    # np.argsort(-conf)函数返回一个索引数组 其中每一个数按照conf中元素从大到小 置为 0,1...n
    i = np.argsort(-conf)
    # tp conf pred_cls 三个矩阵均按照置信度从大到小进行排列
    tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]

    # 找到各个独立的类别
    # np.unique()会返回输入array中出现至少一次的变量 这里返回所有独立的类别
    unique_classes = np.unique(target_cls)

    # 创建P-R曲线 并 计算每一个类别的AP
    px, py = np.linspace(0, 1, 1000), []  # for plotting
    pr_score = 0.1  # 评估P和R的分数 参考论坛https://github.com/ultralytics/yolov3/issues/898
    
    # 第一个为类别数目, 第二为IOU loss阈值的类别的 (i.e. 10 for mAP0.5...0.95)
    s = [unique_classes.shape[0], tp.shape[1]]  
    #初始化 对每一个类别在每一个IOU阈值下面 计算P R AP参数
    ap, p, r = np.zeros(s), np.zeros(s), np.zeros(s) 
    for ci, c in enumerate(unique_classes): # ci为类别对应索引 c为具体的类别
        # i为一个包含True/False 的列表 代表 pred_cls array 各元素是否与 类别c 相同
        i = pred_cls == c 
        n_l = (target_cls == c).sum()  # ground truth中 类别c 的个数 all_results
        n_p = i.sum()  # 预测类别中为 类别c 的个数

        if n_p == 0 or n_l == 0: #如果没有预测到 或者 ground truth没有标注 则略过类别c
            continue
        else:
            """ 
            计算 FP(False Positive) 和 TP(Ture Positive)
            tp[i] 会根据i中对应位置是否为False来决定是否删除这一位的内容,如下所示:
            a = np.array([0,1,0,1]) i = np.array([True,False,False,True]) b = a[i]
            则b为:[0 1]
            而.cumsum(0)函数会 按照对象进行累加操作,如下所示:
            a = np.array([0,1,0,1]) b = a.cumsum(0)
            则b为:[0,1,1,2]
            (FP + TP = all_detections 所以有 fp[i] = 1 - tp[i])
            所以fpc为 类别c 按照置信度从大到小排列 截止到每一位的FP数目
                tpc为 类别c 按照置信度从大到小排列 截止到每一位的TP数目
            recall 和 precision 均按照元素从小到大排列
            """
            fpc = (1 - tp[i]).cumsum(0)
            tpc = tp[i].cumsum(0) 

            # 计算Recall
            # Recall = TP / (TP + FN) = TP / all_results = TP / n_l
            recall = tpc / (n_l + 1e-16)  # 加一个1e-16的目的是防止n_l为0 时除不开
            """
            np.interp() 函数第一个输入值为数值 第二第三个变量为一组x y坐标 返回结果为一个数值
            这个数值为 找寻该数值左右两边的x值 并将两者对应的y值取平均 如果在左侧或右侧 则取 边界值
            如果第一个输入为数组 则返回一个数组 其中每一个元素按照上述计算规则产生
            """
            r[ci] = np.interp(-pr_score, -conf[i], recall[:, 0])     # pr_score 处的y值

            # 计算Precision
            # Precision = TP / TP + FP = TP / all_detections 
            precision = tpc / (tpc + fpc)  
            p[ci] = np.interp(-pr_score, -conf[i], precision[:, 0])  # pr_score 处的y值

            # 从P-R曲线中计算AP
            for j in range(tp.shape[1]): #这里对每一个IOU阈值 下的参数进行计算 
                ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) #取每一个阈值计算AP
                if plot and (j == 0):
                    py.append(np.interp(px, mrec, mpre))  # [email protected]处的P

    # 计算F1分数 P和R的调和平均值
    f1 = 2 * p * r / (p + r + 1e-16)

    if plot:
        plot_pr_curve(px, py, ap, save_dir, names) # plot函数在本代码末尾

    return p, r, ap, f1, unique_classes.astype('int32')

compute_ap 通过输入P和R的值来计算AP

def compute_ap(recall, precision): #计算AP
    """ 通过输入 P-R 来计算AP
    Source: https://github.com/rbgirshick/py-faster-rcnn.
    # Arguments(变量)
        recall:    The recall curve (list).
        precision: The precision curve (list).
    # Returns
        The average precision as computed in py-faster-rcnn.
    """

    # 在开头和末尾添加保护值 防止全零的情况出现
    mrec = recall  # np.concatenate(([0.], recall, [recall[-1] + 1E-3]))
    mpre = precision  # np.concatenate(([0.], precision, [0.]))

    """
    此处需要关注precision列表输入时元素为从小到大排列(由上一个函数)
    np.filp()函数会把一维数组每个元素的顺序进行翻转 第一个翻转成为最后一个
    np.maximum.accumulate() 函数会返回输入
    mpre = np.flip(np.maximum.accumulate(np.flip(recall)))
    Q?:此处mpre返回的是是否由输入数组中最大的元素组成的数组如
    recall = np.array([0.1,0.2,0.2,0.3,0.4])
    final_1 = np.flip(np.maximum.accumulate(np.flip(recall)))
    final_2 = np.flip(np.maximum.accumulate(recall))
    final_1:[0.4 0.4 0.4 0.4 0.4]
    final_2:[0.4 0.3 0.2 0.2 0.1]
    """
    mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))

    # Integrate area under curve
    method = 'interp'  # methods: 'continuous', 'interp'
    if method == 'interp': #计算 AP 的方法为间断性的
        # x 为0-1 101个点组成的等差数列数组 为间断点
        x = np.linspace(0, 1, 101)  
        # np.trapz(list,list) 计算两个list对应点与点之间四边形的面积 以定积分形式估算AP
        #按照P-R曲线的定义 R近似为递增数组 P为近似递减数组 如上中final_2结果
        ap = np.trapz(np.interp(x, mrec, mpre), x)  # 前一个数组为纵坐标 第二个为横坐标
    else:  # 'continuous' #采用连续的方法计算AP
        """
        通过错位的方式 判断哪个点发生了改变并通过!=判断 返回一个布尔数组 
        再通过np.where()函数找出 mrec中对应发生的改变点 i为一个数组 每一个
        元素代表当前位置到下一个位置发生改变
        """
        i = np.where(mrec[1:] != mrec[:-1])[0]  # points where x axis (recall) changes
        ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])  # area under curve

    return ap, mpre, mrec

ConfusionMatrix 类为求解混淆矩阵并进行绘图

class ConfusionMatrix: # nc为训练的类别 conf为置信度 iou_thres 为IOU loss的阈值
    # 更新版: https://github.com/kaanakan/object_detection_confusion_matrix
    def __init__(self, nc, conf=0.25, iou_thres=0.45):
        self.matrix = np.zeros((nc + 1, nc + 1))
        self.nc = nc  # number of classes
        self.conf = conf
        self.iou_thres = iou_thres

    def process_batch(self, detections, labels):
        """
        Return intersection-over-union (Jaccard index) of boxes.
        返回 各个box之间的交并比(iou)
        Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
        每一个box的集合都被期望使用(x1,y1,x2,y2)的形式 这两个点为box的对角顶点
        Arguments: detections 和 labels的数据结构
            detections (Array[N, 6]), x1, y1, x2, y2, conf, class
            labels (Array[M, 5]), class, x1, y1, x2, y2
        Returns:
            None, updates confusion matrix accordingly
            无返回 更新混淆矩阵
        """
        # detections (Array[N, 6]), x1, y1, x2, y2, conf, class
        detections = detections[detections[:, 4] > self.conf] # 返回检测大于阈值的框
        # gt_classes (Array[M, 1]), ground_truth class
        gt_classes = labels[:, 0].int()                       # 返回ground truth的类别
        # detection_classes (Array[M, 1]), predicted class
        detection_classes = detections[:, 5].int()            # 返回检测到的类别
        # iou计算	box1 (Array[N, 4]), x1, y1, x2, y2
        #           box2 (Array[M, 4]), x1, y1, x2, y2
        # iou (Tensor[N, M]) NxM矩阵包含了 box1中每一个框和box2中每一个框的iou值 
        # 非常重要! iou中坐标 (n1,m1) 代表 第n1个ground truth 框 和 第m1个 预测框的 
        iou = general.box_iou(labels[:, 1:], detections[:, :4]) #调用general中计算iou的方式计算iou
        # x为一个含有两个tensor的tuple表示iou中大于阈值的值的坐标,第一个tensor为第几行,第二个为第几列 
        x = torch.where(iou > self.iou_thres) #找到iou中大于阈值的那部分并提取
        if x[0].shape[0]:  # 当大于阈值的坐标不止一个的时候
            """
            torch.cat(inputs,dimension=0) 为在指定的维度对 张量inputs进行堆叠 
            二维情况下 0代表按照行 1代表按照列 0时会增加行 1时会增加列
            torch.stack(x,1) 当x为二维张量的时候 本质上是对x做转置操作
            .cpu()是将变量转移到cpu上进行运算.numpy()是转换为numpy数组
            matches (Array[N, 3]), row,col,iou_value !!!
                    row为大于阈值的iou张量中点的横坐标 col为纵坐标 iou_value为对应的iou值
            """
            matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
            if x[0].shape[0] > 1: # 当box个数大于1时进行以下过程 此处matches的过滤过程见下文 补充部分
                matches = matches[matches[:, 2].argsort()[::-1]] 
                matches = matches[np.unique(matches[:, 1], return_index=True)[1]] 
                matches = matches[matches[:, 2].argsort()[::-1]] 
                matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
        else:
            matches = np.zeros((0, 3)) # 这里返回一个0行3列全0的二维数组 ?因为没有一个例子满足这个要求

        n = matches.shape[0] > 0 #这里n为 True 或 False 用于判断是否存在满足阈值要求的对象是否至少有一个
        """
        a.transpose()是numpy中轮换维度索引的方法 对二维数组表示为转置
        此处matches (Array[N, 3]), row,col,iou_value
        物理意义:在大于阈值的前提下,N*M种label与预测框的组合可能下,每一种预测框与所有label框iou值最大的那个
        m0,m1  (Array[1, N])
        m0代表 满足上述条件的第i个label框   (也即类别)
        m1代表 满足上述条件的第j个predict框 (也即类别)
        """
        m0, m1, _ = matches.transpose().astype(np.int16)
        for i, gc in enumerate(gt_classes): #解析ground truth 中的类别
            j = m0 == i
            if n and sum(j) == 1: #检测到的目标至少有1个 且 groundtruth对应只有一个
                self.matrix[gc, detection_classes[m1[j]]] += 1  # TP 判断正确的数目加1
            else:
                self.matrix[gc, self.nc] += 1  # 背景 FP(false positive) 个数加1 背景被误认为目标

        if n: # 当目标不止一个时
            for i, dc in enumerate(detection_classes): # i为索引 dc为每一个目标检测到的类别
                if not any(m1 == i): # 检测到目标 但是目标与groundtruth的iou小于之前要求的阈值则
                    self.matrix[self.nc, dc] += 1  # 背景 FN 个数加1 (目标被检测成了背景)

    def matrix(self): #返回matrix变量 该matrix为混淆矩阵
        return self.matrix

    def plot(self, save_dir='', names=()):
        try:
            import seaborn as sn #seaborn 为易于可视化的一个模块

            array = self.matrix / (self.matrix.sum(0).reshape(1, self.nc + 1) + 1E-6)  # 矩阵归一化为0-1
            array[array < 0.005] = np.nan  # 小于0.005的值被认为NaN

            fig = plt.figure(figsize=(12, 9), tight_layout=True) #初始化画布
            sn.set(font_scale=1.0 if self.nc < 50 else 0.8)  # 设置标签的尺寸
            labels = (0 < len(names) < 99) and len(names) == self.nc  # 用于绘制过程中判断是否应用names
            # 绘制热力图 即混淆矩阵可视化
            sn.heatmap(array, annot=self.nc < 30, annot_kws={
     "size": 8}, cmap='Blues', fmt='.2f', square=True,
                       xticklabels=names + ['background FN'] if labels else "auto",
                       yticklabels=names + ['background FP'] if labels else "auto").set_facecolor((1, 1, 1))
            # 下三行代码为设置figure的横坐标 纵坐标及保存该图片
            fig.axes[0].set_xlabel('True')
            fig.axes[0].set_ylabel('Predicted')
            fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)
        except Exception as e:
            pass

    def print(self): # 打印出每一个元素对应的数据
        for i in range(self.nc + 1):
            print(' '.join(map(str, self.matrix[i])))

关于上述四步matches处理的详细解释

import torch
import numpy as np

iou = torch.tensor([[0.16512, 0.04280,  0.7912, 0.06599,  0.0755,  0.4665],
        [0.014043,  0.3173,  0.4420,  1.2253, 0.206817,  0.5997],
        [ 0.4398,  0.1185,  1.2385,  0.2133,  0.7412, 0.06974],
        [ 0.7442,  0.9128,  1.0040,  2.0243,  1.0281,  1.3334],
        [ 1.0045,  0.7125, 0.03617,  0.0962,  0.7367,  0.6041]])

iou_thres = 0.2
x = torch.where(iou > iou_thres )
x_stack = torch.stack(x,1)

print("first matches第一列为横坐标(label框) 第二列为纵坐标(predict框) 第三列为iou")
matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
print(matches)
print("          ")
print("second 按照第三列iou值从大到小对matches各个行重新排列")
matches2 = matches[matches[:, 2].argsort()[::-1]]
print(matches2)
print("          ")
print("third 取第二列中各个框首次出现(此处为不同预测到的框)的行(即每一种预测的框中iou值最大的那个)")
#print(np.unique(matches2[:, 1], return_index=True))
matches3 = matches2[np.unique(matches2[:, 1], return_index=True)[1]]
print(matches3)
print("          ")
print("forth 按照第三列iou值从大到小对matches各个行重新排列")
matches4 = matches3[matches3[:, 2].argsort()[::-1]] 
print(matches4)
print("          ")
print("fifth 取第一列中各个框首次出现(此处为不同label的框)的行(即每一种label框中iou值最大的那个)")
matches5 = matches4[np.unique(matches4[:, 0], return_index=True)[1]]
print(matches5)
print("经过这样的处理,最终得到每一种预测框与所有label框iou值最大的那个(在大于阈值的前提下)")

first matches第一列为横坐标(label框) 第二列为纵坐标(predict框) 第三列为iou
[[0.       2.       0.7912  ]
 [0.       5.       0.4665  ]
 [1.       1.       0.3173  ]
 [1.       2.       0.442   ]
 [1.       3.       1.2253  ]
 [1.       4.       0.206817]
 [1.       5.       0.5997  ]
 [2.       0.       0.4398  ]
 [2.       2.       1.2385  ]
 [2.       3.       0.2133  ]
 [2.       4.       0.7412  ]
 [3.       0.       0.7442  ]
 [3.       1.       0.9128  ]
 [3.       2.       1.004   ]
 [3.       3.       2.0243  ]
 [3.       4.       1.0281  ]
 [3.       5.       1.3334  ]
 [4.       0.       1.0045  ]
 [4.       1.       0.7125  ]
 [4.       4.       0.7367  ]
 [4.       5.       0.6041  ]]
          
second 按照第三列iou值从大到小对matches各个行重新排列
[[3.       3.       2.0243  ]
 [3.       5.       1.3334  ]
 [2.       2.       1.2385  ]
 [1.       3.       1.2253  ]
 [3.       4.       1.0281  ]
 [4.       0.       1.0045  ]
 [3.       2.       1.004   ]
 [3.       1.       0.9128  ]
 [0.       2.       0.7912  ]
 [3.       0.       0.7442  ]
 [2.       4.       0.7412  ]
 [4.       4.       0.7367  ]
 [4.       1.       0.7125  ]
 [4.       5.       0.6041  ]
 [1.       5.       0.5997  ]
 [0.       5.       0.4665  ]
 [1.       2.       0.442   ]
 [2.       0.       0.4398  ]
 [1.       1.       0.3173  ]
 [2.       3.       0.2133  ]
 [1.       4.       0.206817]]
          
third 取第二列中各个框首次出现(此处为不同预测到的框)的行(即每一种预测的框中iou值最大的那个)
[[4.     0.     1.0045]
 [3.     1.     0.9128]
 [2.     2.     1.2385]
 [3.     3.     2.0243]
 [3.     4.     1.0281]
 [3.     5.     1.3334]]
          
forth 按照第三列iou值从大到小对matches各个行重新排列
[[3.     3.     2.0243]
 [3.     5.     1.3334]
 [2.     2.     1.2385]
 [3.     4.     1.0281]
 [4.     0.     1.0045]
 [3.     1.     0.9128]]
          
fifth 取第一列中各个框首次出现(此处为不同label的框)的行(即每一种label框中iou值最大的那个)
[[2.     2.     1.2385]
 [3.     3.     2.0243]
 [4.     0.     1.0045]]
经过这样的处理,最终得到每一种预测框与所有label框iou值最大的那个(在大于阈值的前提下)

plot_pr_curve 函数用于绘制P-R曲线

def plot_pr_curve(px, py, ap, save_dir='.', names=()): # 绘制P-R曲线
    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) #初始化坐标纸
    py = np.stack(py, axis=1)

    if 0 < len(names) < 21:  # 类别小于10类的时候 写上mAP
        for i, y in enumerate(py.T):
            ax.plot(px, y, linewidth=1, label=f'{names[i]} %.3f' % ap[i, 0])  # 绘制(recall, precision)
    else:
        ax.plot(px, py, linewidth=1, color='grey')  # 绘制(recall, precision)
    # 下一行代码为添加[email protected]的信息到图片之中
    ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f [email protected]' % ap[:, 0].mean())
    # 以下四行设置图片x、y坐标轴的标签和刻度
    ax.set_xlabel('Recall')
    ax.set_ylabel('Precision')
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    # 把上图移动到整张图片的左上角
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    # 保存图片
    fig.savefig(Path(save_dir) / 'precision_recall_curve.png', dpi=250) 

混淆矩阵和P-R曲线的图例如下所示:(只有person一类作为检测)
YOLOv5-4.0-metrics.py 源代码导读_第1张图片
YOLOv5-4.0-metrics.py 源代码导读_第2张图片

你可能感兴趣的:(YOLOv5源代码导读,python,深度学习,pytorch,机器学习,自动驾驶)