Diff of /utils/metrics.py [000000] .. [190ca4]

Switch to unified view

a b/utils/metrics.py
1
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
2
"""
3
Model validation metrics
4
"""
5
6
import math
7
import warnings
8
from pathlib import Path
9
10
import matplotlib.pyplot as plt
11
import numpy as np
12
import torch
13
14
from utils import TryExcept, threaded
15
16
17
def fitness(x):
18
    # Model fitness as a weighted combination of metrics
19
    w = [0.0, 0.0, 0.1, 0.9]  # weights for [P, R, mAP@0.5, mAP@0.5:0.95]
20
    return (x[:, :4] * w).sum(1)
21
22
23
def smooth(y, f=0.05):
24
    # Box filter of fraction f
25
    nf = round(len(y) * f * 2) // 2 + 1  # number of filter elements (must be odd)
26
    p = np.ones(nf // 2)  # ones padding
27
    yp = np.concatenate((p * y[0], y, p * y[-1]), 0)  # y padded
28
    return np.convolve(yp, np.ones(nf) / nf, mode='valid')  # y-smoothed
29
30
31
def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=(), eps=1e-16, prefix=''):
32
    """ Compute the average precision, given the recall and precision curves.
33
    Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
34
    # Arguments
35
        tp:  True positives (nparray, nx1 or nx10).
36
        conf:  Objectness value from 0-1 (nparray).
37
        pred_cls:  Predicted object classes (nparray).
38
        target_cls:  True object classes (nparray).
39
        plot:  Plot precision-recall curve at mAP@0.5
40
        save_dir:  Plot save directory
41
    # Returns
42
        The average precision as computed in py-faster-rcnn.
43
    """
44
45
    # Sort by objectness
46
    i = np.argsort(-conf)
47
    tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
48
49
    # Find unique classes
50
    unique_classes, nt = np.unique(target_cls, return_counts=True)
51
    nc = unique_classes.shape[0]  # number of classes, number of detections
52
53
    # Create Precision-Recall curve and compute AP for each class
54
    px, py = np.linspace(0, 1, 1000), []  # for plotting
55
    ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))
56
    for ci, c in enumerate(unique_classes):
57
        i = pred_cls == c
58
        n_l = nt[ci]  # number of labels
59
        n_p = i.sum()  # number of predictions
60
        if n_p == 0 or n_l == 0:
61
            continue
62
63
        # Accumulate FPs and TPs
64
        fpc = (1 - tp[i]).cumsum(0)
65
        tpc = tp[i].cumsum(0)
66
67
        # Recall
68
        recall = tpc / (n_l + eps)  # recall curve
69
        r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0)  # negative x, xp because xp decreases
70
71
        # Precision
72
        precision = tpc / (tpc + fpc)  # precision curve
73
        p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1)  # p at pr_score
74
75
        # AP from recall-precision curve
76
        for j in range(tp.shape[1]):
77
            ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j])
78
            if plot and j == 0:
79
                py.append(np.interp(px, mrec, mpre))  # precision at mAP@0.5
80
81
    # Compute F1 (harmonic mean of precision and recall)
82
    f1 = 2 * p * r / (p + r + eps)
83
    names = [v for k, v in names.items() if k in unique_classes]  # list: only classes that have data
84
    names = dict(enumerate(names))  # to dict
85
    if plot:
86
        plot_pr_curve(px, py, ap, Path(save_dir) / f'{prefix}PR_curve.png', names)
87
        plot_mc_curve(px, f1, Path(save_dir) / f'{prefix}F1_curve.png', names, ylabel='F1')
88
        plot_mc_curve(px, p, Path(save_dir) / f'{prefix}P_curve.png', names, ylabel='Precision')
89
        plot_mc_curve(px, r, Path(save_dir) / f'{prefix}R_curve.png', names, ylabel='Recall')
90
91
    i = smooth(f1.mean(0), 0.1).argmax()  # max F1 index
92
    p, r, f1 = p[:, i], r[:, i], f1[:, i]
93
    tp = (r * nt).round()  # true positives
94
    fp = (tp / (p + eps) - tp).round()  # false positives
95
    return tp, fp, p, r, f1, ap, unique_classes.astype(int)
96
97
98
def compute_ap(recall, precision):
99
    """ Compute the average precision, given the recall and precision curves
100
    # Arguments
101
        recall:    The recall curve (list)
102
        precision: The precision curve (list)
103
    # Returns
104
        Average precision, precision curve, recall curve
105
    """
106
107
    # Append sentinel values to beginning and end
108
    mrec = np.concatenate(([0.0], recall, [1.0]))
109
    mpre = np.concatenate(([1.0], precision, [0.0]))
110
111
    # Compute the precision envelope
112
    mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
113
114
    # Integrate area under curve
115
    method = 'interp'  # methods: 'continuous', 'interp'
116
    if method == 'interp':
117
        x = np.linspace(0, 1, 101)  # 101-point interp (COCO)
118
        ap = np.trapz(np.interp(x, mrec, mpre), x)  # integrate
119
    else:  # 'continuous'
120
        i = np.where(mrec[1:] != mrec[:-1])[0]  # points where x axis (recall) changes
121
        ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])  # area under curve
122
123
    return ap, mpre, mrec
124
125
126
class ConfusionMatrix:
127
    # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
128
    def __init__(self, nc, conf=0.25, iou_thres=0.45):
129
        self.matrix = np.zeros((nc + 1, nc + 1))
130
        self.nc = nc  # number of classes
131
        self.conf = conf
132
        self.iou_thres = iou_thres
133
134
    def process_batch(self, detections, labels):
135
        """
136
        Return intersection-over-union (Jaccard index) of boxes.
137
        Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
138
        Arguments:
139
            detections (Array[N, 6]), x1, y1, x2, y2, conf, class
140
            labels (Array[M, 5]), class, x1, y1, x2, y2
141
        Returns:
142
            None, updates confusion matrix accordingly
143
        """
144
        if detections is None:
145
            gt_classes = labels.int()
146
            for gc in gt_classes:
147
                self.matrix[self.nc, gc] += 1  # background FN
148
            return
149
150
        detections = detections[detections[:, 4] > self.conf]
151
        gt_classes = labels[:, 0].int()
152
        detection_classes = detections[:, 5].int()
153
        iou = box_iou(labels[:, 1:], detections[:, :4])
154
155
        x = torch.where(iou > self.iou_thres)
156
        if x[0].shape[0]:
157
            matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
158
            if x[0].shape[0] > 1:
159
                matches = matches[matches[:, 2].argsort()[::-1]]
160
                matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
161
                matches = matches[matches[:, 2].argsort()[::-1]]
162
                matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
163
        else:
164
            matches = np.zeros((0, 3))
165
166
        n = matches.shape[0] > 0
167
        m0, m1, _ = matches.transpose().astype(int)
168
        for i, gc in enumerate(gt_classes):
169
            j = m0 == i
170
            if n and sum(j) == 1:
171
                self.matrix[detection_classes[m1[j]], gc] += 1  # correct
172
            else:
173
                self.matrix[self.nc, gc] += 1  # true background
174
175
        if n:
176
            for i, dc in enumerate(detection_classes):
177
                if not any(m1 == i):
178
                    self.matrix[dc, self.nc] += 1  # predicted background
179
180
    def tp_fp(self):
181
        tp = self.matrix.diagonal()  # true positives
182
        fp = self.matrix.sum(1) - tp  # false positives
183
        # fn = self.matrix.sum(0) - tp  # false negatives (missed detections)
184
        return tp[:-1], fp[:-1]  # remove background class
185
186
    @TryExcept('WARNING ⚠️ ConfusionMatrix plot failure')
187
    def plot(self, normalize=True, save_dir='', names=()):
188
        import seaborn as sn
189
190
        array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1E-9) if normalize else 1)  # normalize columns
191
        array[array < 0.005] = np.nan  # don't annotate (would appear as 0.00)
192
193
        fig, ax = plt.subplots(1, 1, figsize=(12, 9), tight_layout=True)
194
        nc, nn = self.nc, len(names)  # number of classes, names
195
        sn.set(font_scale=1.0 if nc < 50 else 0.8)  # for label size
196
        labels = (0 < nn < 99) and (nn == nc)  # apply names to ticklabels
197
        ticklabels = (names + ['background']) if labels else 'auto'
198
        with warnings.catch_warnings():
199
            warnings.simplefilter('ignore')  # suppress empty matrix RuntimeWarning: All-NaN slice encountered
200
            sn.heatmap(array,
201
                       ax=ax,
202
                       annot=nc < 30,
203
                       annot_kws={
204
                           'size': 8},
205
                       cmap='Blues',
206
                       fmt='.2f',
207
                       square=True,
208
                       vmin=0.0,
209
                       xticklabels=ticklabels,
210
                       yticklabels=ticklabels).set_facecolor((1, 1, 1))
211
        ax.set_xlabel('True')
212
        ax.set_ylabel('Predicted')
213
        ax.set_title('Confusion Matrix')
214
        fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)
215
        plt.close(fig)
216
217
    def print(self):
218
        for i in range(self.nc + 1):
219
            print(' '.join(map(str, self.matrix[i])))
220
221
222
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
223
    # Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
224
225
    # Get the coordinates of bounding boxes
226
    if xywh:  # transform from xywh to xyxy
227
        (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
228
        w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
229
        b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
230
        b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
231
    else:  # x1, y1, x2, y2 = box1
232
        b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
233
        b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
234
        w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
235
        w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
236
237
    # Intersection area
238
    inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
239
            (b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
240
241
    # Union Area
242
    union = w1 * h1 + w2 * h2 - inter + eps
243
244
    # IoU
245
    iou = inter / union
246
    if CIoU or DIoU or GIoU:
247
        cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1)  # convex (smallest enclosing box) width
248
        ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1)  # convex height
249
        if CIoU or DIoU:  # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
250
            c2 = cw ** 2 + ch ** 2 + eps  # convex diagonal squared
251
            rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4  # center dist ** 2
252
            if CIoU:  # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
253
                v = (4 / math.pi ** 2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2)
254
                with torch.no_grad():
255
                    alpha = v / (v - iou + (1 + eps))
256
                return iou - (rho2 / c2 + v * alpha)  # CIoU
257
            return iou - rho2 / c2  # DIoU
258
        c_area = cw * ch + eps  # convex area
259
        return iou - (c_area - union) / c_area  # GIoU https://arxiv.org/pdf/1902.09630.pdf
260
    return iou  # IoU
261
262
263
def box_iou(box1, box2, eps=1e-7):
264
    # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
265
    """
266
    Return intersection-over-union (Jaccard index) of boxes.
267
    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
268
    Arguments:
269
        box1 (Tensor[N, 4])
270
        box2 (Tensor[M, 4])
271
    Returns:
272
        iou (Tensor[N, M]): the NxM matrix containing the pairwise
273
            IoU values for every element in boxes1 and boxes2
274
    """
275
276
    # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
277
    (a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2)
278
    inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2)
279
280
    # IoU = inter / (area1 + area2 - inter)
281
    return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps)
282
283
284
def bbox_ioa(box1, box2, eps=1e-7):
285
    """ Returns the intersection over box2 area given box1, box2. Boxes are x1y1x2y2
286
    box1:       np.array of shape(4)
287
    box2:       np.array of shape(nx4)
288
    returns:    np.array of shape(n)
289
    """
290
291
    # Get the coordinates of bounding boxes
292
    b1_x1, b1_y1, b1_x2, b1_y2 = box1
293
    b2_x1, b2_y1, b2_x2, b2_y2 = box2.T
294
295
    # Intersection area
296
    inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \
297
                 (np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0)
298
299
    # box2 area
300
    box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + eps
301
302
    # Intersection over box2 area
303
    return inter_area / box2_area
304
305
306
def wh_iou(wh1, wh2, eps=1e-7):
307
    # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
308
    wh1 = wh1[:, None]  # [N,1,2]
309
    wh2 = wh2[None]  # [1,M,2]
310
    inter = torch.min(wh1, wh2).prod(2)  # [N,M]
311
    return inter / (wh1.prod(2) + wh2.prod(2) - inter + eps)  # iou = inter / (area1 + area2 - inter)
312
313
314
# Plots ----------------------------------------------------------------------------------------------------------------
315
316
317
@threaded
318
def plot_pr_curve(px, py, ap, save_dir=Path('pr_curve.png'), names=()):
319
    # Precision-recall curve
320
    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
321
    py = np.stack(py, axis=1)
322
323
    if 0 < len(names) < 21:  # display per-class legend if < 21 classes
324
        for i, y in enumerate(py.T):
325
            ax.plot(px, y, linewidth=1, label=f'{names[i]} {ap[i, 0]:.3f}')  # plot(recall, precision)
326
    else:
327
        ax.plot(px, py, linewidth=1, color='grey')  # plot(recall, precision)
328
329
    ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean())
330
    ax.set_xlabel('Recall')
331
    ax.set_ylabel('Precision')
332
    ax.set_xlim(0, 1)
333
    ax.set_ylim(0, 1)
334
    ax.legend(bbox_to_anchor=(1.04, 1), loc='upper left')
335
    ax.set_title('Precision-Recall Curve')
336
    fig.savefig(save_dir, dpi=250)
337
    plt.close(fig)
338
339
340
@threaded
341
def plot_mc_curve(px, py, save_dir=Path('mc_curve.png'), names=(), xlabel='Confidence', ylabel='Metric'):
342
    # Metric-confidence curve
343
    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
344
345
    if 0 < len(names) < 21:  # display per-class legend if < 21 classes
346
        for i, y in enumerate(py):
347
            ax.plot(px, y, linewidth=1, label=f'{names[i]}')  # plot(confidence, metric)
348
    else:
349
        ax.plot(px, py.T, linewidth=1, color='grey')  # plot(confidence, metric)
350
351
    y = smooth(py.mean(0), 0.05)
352
    ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}')
353
    ax.set_xlabel(xlabel)
354
    ax.set_ylabel(ylabel)
355
    ax.set_xlim(0, 1)
356
    ax.set_ylim(0, 1)
357
    ax.legend(bbox_to_anchor=(1.04, 1), loc='upper left')
358
    ax.set_title(f'{ylabel}-Confidence Curve')
359
    fig.savefig(save_dir, dpi=250)
360
    plt.close(fig)