Team Ai
Apppublic

Adamfan/objectdetection

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
metrics.py228 linesDownload Raw Back to utils
1# Model validation metrics2 3from pathlib import Path4 5import matplotlib.pyplot as plt6import numpy as np7import torch8 9from . import general10 11 12def fitness(x):13    # Model fitness as a weighted combination of metrics14    w = [0.0, 0.0, 0.1, 0.9]  # weights for [P, R, mAP@0.5, mAP@0.5:0.95]15    return (x[:, :4] * w).sum(1)16 17 18def ap_per_class(tp, conf, pred_cls, target_cls, v5_metric=False, plot=False, save_dir='.', names=()):19    """ Compute the average precision, given the recall and precision curves.20    Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.21    # Arguments22        tp:  True positives (nparray, nx1 or nx10).23        conf:  Objectness value from 0-1 (nparray).24        pred_cls:  Predicted object classes (nparray).25        target_cls:  True object classes (nparray).26        plot:  Plot precision-recall curve at mAP@0.527        save_dir:  Plot save directory28    # Returns29        The average precision as computed in py-faster-rcnn.30    """31 32    # Sort by objectness33    i = np.argsort(-conf)34    tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]35 36    # Find unique classes37    unique_classes = np.unique(target_cls)38    nc = unique_classes.shape[0]  # number of classes, number of detections39 40    # Create Precision-Recall curve and compute AP for each class41    px, py = np.linspace(0, 1, 1000), []  # for plotting42    ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))43    for ci, c in enumerate(unique_classes):44        i = pred_cls == c45        n_l = (target_cls == c).sum()  # number of labels46        n_p = i.sum()  # number of predictions47 48        if n_p == 0 or n_l == 0:49            continue50        else:51            # Accumulate FPs and TPs52            fpc = (1 - tp[i]).cumsum(0)53            tpc = tp[i].cumsum(0)54 55            # Recall56            recall = tpc / (n_l + 1e-16)  # recall curve57            r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0)  # negative x, xp because xp decreases58 59            # Precision60            precision = tpc / (tpc + fpc)  # precision curve61            p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1)  # p at pr_score62 63            # AP from recall-precision curve64            for j in range(tp.shape[1]):65                ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j], v5_metric=v5_metric)66                if plot and j == 0:67                    py.append(np.interp(px, mrec, mpre))  # precision at mAP@0.568 69    # Compute F1 (harmonic mean of precision and recall)70    f1 = 2 * p * r / (p + r + 1e-16)71    if plot:72        plot_pr_curve(px, py, ap, Path(save_dir) / 'PR_curve.png', names)73        plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1')74        plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision')75        plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall')76 77    i = f1.mean(0).argmax()  # max F1 index78    return p[:, i], r[:, i], ap, f1[:, i], unique_classes.astype('int32')79 80 81def compute_ap(recall, precision, v5_metric=False):82    """ Compute the average precision, given the recall and precision curves83    # Arguments84        recall:    The recall curve (list)85        precision: The precision curve (list)86        v5_metric: Assume maximum recall to be 1.0, as in YOLOv5, MMDetetion etc.87    # Returns88        Average precision, precision curve, recall curve89    """90 91    # Append sentinel values to beginning and end92    if v5_metric:  # New YOLOv5 metric, same as MMDetection and Detectron2 repositories93        mrec = np.concatenate(([0.], recall, [1.0]))94    else:  # Old YOLOv5 metric, i.e. default YOLOv7 metric95        mrec = np.concatenate(([0.], recall, [recall[-1] + 0.01]))96    mpre = np.concatenate(([1.], precision, [0.]))97 98    # Compute the precision envelope99    mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))100 101    # Integrate area under curve102    method = 'interp'  # methods: 'continuous', 'interp'103    if method == 'interp':104        x = np.linspace(0, 1, 101)  # 101-point interp (COCO)105        ap = np.trapz(np.interp(x, mrec, mpre), x)  # integrate106    else:  # 'continuous'107        i = np.where(mrec[1:] != mrec[:-1])[0]  # points where x axis (recall) changes108        ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])  # area under curve109 110    return ap, mpre, mrec111 112 113class ConfusionMatrix:114    # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix115    def __init__(self, nc, conf=0.25, iou_thres=0.45):116        self.matrix = np.zeros((nc + 1, nc + 1))117        self.nc = nc  # number of classes118        self.conf = conf119        self.iou_thres = iou_thres120 121    def process_batch(self, detections, labels):122        """123        Return intersection-over-union (Jaccard index) of boxes.124        Both sets of boxes are expected to be in (x1, y1, x2, y2) format.125        Arguments:126            detections (Array[N, 6]), x1, y1, x2, y2, conf, class127            labels (Array[M, 5]), class, x1, y1, x2, y2128        Returns:129            None, updates confusion matrix accordingly130        """131        detections = detections[detections[:, 4] > self.conf]132        gt_classes = labels[:, 0].int()133        detection_classes = detections[:, 5].int()134        iou = general.box_iou(labels[:, 1:], detections[:, :4])135 136        x = torch.where(iou > self.iou_thres)137        if x[0].shape[0]:138            matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()139            if x[0].shape[0] > 1:140                matches = matches[matches[:, 2].argsort()[::-1]]141                matches = matches[np.unique(matches[:, 1], return_index=True)[1]]142                matches = matches[matches[:, 2].argsort()[::-1]]143                matches = matches[np.unique(matches[:, 0], return_index=True)[1]]144        else:145            matches = np.zeros((0, 3))146 147        n = matches.shape[0] > 0148        m0, m1, _ = matches.transpose().astype(np.int16)149        for i, gc in enumerate(gt_classes):150            j = m0 == i151            if n and sum(j) == 1:152                self.matrix[gc, detection_classes[m1[j]]] += 1  # correct153            else:154                self.matrix[self.nc, gc] += 1  # background FP155 156        if n:157            for i, dc in enumerate(detection_classes):158                if not any(m1 == i):159                    self.matrix[dc, self.nc] += 1  # background FN160 161    def matrix(self):162        return self.matrix163 164    def plot(self, save_dir='', names=()):165        try:166            import seaborn as sn167 168            array = self.matrix / (self.matrix.sum(0).reshape(1, self.nc + 1) + 1E-6)  # normalize169            array[array < 0.005] = np.nan  # don't annotate (would appear as 0.00)170 171            fig = plt.figure(figsize=(12, 9), tight_layout=True)172            sn.set(font_scale=1.0 if self.nc < 50 else 0.8)  # for label size173            labels = (0 < len(names) < 99) and len(names) == self.nc  # apply names to ticklabels174            sn.heatmap(array, annot=self.nc < 30, annot_kws={"size": 8}, cmap='Blues', fmt='.2f', square=True,175                       xticklabels=names + ['background FP'] if labels else "auto",176                       yticklabels=names + ['background FN'] if labels else "auto").set_facecolor((1, 1, 1))177            fig.axes[0].set_xlabel('True')178            fig.axes[0].set_ylabel('Predicted')179            fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)180        except Exception as e:181            pass182 183    def print(self):184        for i in range(self.nc + 1):185            print(' '.join(map(str, self.matrix[i])))186 187 188# Plots ----------------------------------------------------------------------------------------------------------------189 190def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()):191    # Precision-recall curve192    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)193    py = np.stack(py, axis=1)194 195    if 0 < len(names) < 21:  # display per-class legend if < 21 classes196        for i, y in enumerate(py.T):197            ax.plot(px, y, linewidth=1, label=f'{names[i]} {ap[i, 0]:.3f}')  # plot(recall, precision)198    else:199        ax.plot(px, py, linewidth=1, color='grey')  # plot(recall, precision)200 201    ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean())202    ax.set_xlabel('Recall')203    ax.set_ylabel('Precision')204    ax.set_xlim(0, 1)205    ax.set_ylim(0, 1)206    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")207    fig.savefig(Path(save_dir), dpi=250)208 209 210def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'):211    # Metric-confidence curve212    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)213 214    if 0 < len(names) < 21:  # display per-class legend if < 21 classes215        for i, y in enumerate(py):216            ax.plot(px, y, linewidth=1, label=f'{names[i]}')  # plot(confidence, metric)217    else:218        ax.plot(px, py.T, linewidth=1, color='grey')  # plot(confidence, metric)219 220    y = py.mean(0)221    ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}')222    ax.set_xlabel(xlabel)223    ax.set_ylabel(ylabel)224    ax.set_xlim(0, 1)225    ax.set_ylim(0, 1)226    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")227    fig.savefig(Path(save_dir), dpi=250)228