hank1996 commited on
Commit
821d6f2
·
1 Parent(s): 910e24a

Create new file

Browse files
Files changed (1) hide show
  1. lib/core/evaluate.py +279 -0
lib/core/evaluate.py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Model validation metrics
3
+
4
+ from pathlib import Path
5
+
6
+ import matplotlib.pyplot as plt
7
+ import numpy as np
8
+ import torch
9
+
10
+ from . import general
11
+
12
+
13
+ def fitness(x):
14
+ # Model fitness as a weighted combination of metrics
15
+ w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, [email protected], [email protected]:0.95]
16
+ return (x[:, :4] * w).sum(1)
17
+
18
+
19
+ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='precision-recall_curve.png', names=[]):
20
+ """ Compute the average precision, given the recall and precision curves.
21
+ Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
22
+ # Arguments
23
+ tp: True positives (nparray, nx1 or nx10).
24
+ conf: Objectness value from 0-1 (nparray).
25
+ pred_cls: Predicted object classes (nparray).
26
+ target_cls: True object classes (nparray).
27
+ plot: Plot precision-recall curve at [email protected]
28
+ save_dir: Plot save directory
29
+ # Returns
30
+ The average precision as computed in py-faster-rcnn.
31
+ """
32
+
33
+ # Sort by objectness
34
+ i = np.argsort(-conf) # sorted index from big to small
35
+ tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
36
+
37
+ # Find unique classes, each number just showed up once
38
+ unique_classes = np.unique(target_cls)
39
+
40
+ # Create Precision-Recall curve and compute AP for each class
41
+ px, py = np.linspace(0, 1, 1000), [] # for plotting
42
+ pr_score = 0.1 # score to evaluate P and R https://github.com/ultralytics/yolov3/issues/898
43
+ s = [unique_classes.shape[0], tp.shape[1]] # number class, number iou thresholds (i.e. 10 for mAP0.5...0.95)
44
+ ap, p, r = np.zeros(s), np.zeros((unique_classes.shape[0], 1000)), np.zeros((unique_classes.shape[0], 1000))
45
+ for ci, c in enumerate(unique_classes):
46
+ i = pred_cls == c
47
+ n_l = (target_cls == c).sum() # number of labels
48
+ n_p = i.sum() # number of predictions
49
+
50
+ if n_p == 0 or n_l == 0:
51
+ continue
52
+ else:
53
+ # Accumulate FPs and TPs
54
+ fpc = (1 - tp[i]).cumsum(0)
55
+ tpc = tp[i].cumsum(0)
56
+
57
+ # Recall
58
+ recall = tpc / (n_l + 1e-16) # recall curve
59
+ r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # r at pr_score, negative x, xp because xp decreases
60
+
61
+ # Precision
62
+ precision = tpc / (tpc + fpc) # precision curve
63
+ p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score
64
+
65
+ # AP from recall-precision curve
66
+ for j in range(tp.shape[1]):
67
+ ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j])
68
+ if plot and (j == 0):
69
+ py.append(np.interp(px, mrec, mpre)) # precision at [email protected]
70
+
71
+ # Compute F1 score (harmonic mean of precision and recall)
72
+ f1 = 2 * p * r / (p + r + 1e-16)
73
+ i = r.mean(0).argmax()
74
+
75
+ if plot:
76
+ plot_pr_curve(px, py, ap, save_dir, names)
77
+
78
+ return p[:, i], r[:, i], ap, f1, unique_classes.astype('int32')
79
+
80
+
81
+ def compute_ap(recall, precision):
82
+ """ Compute the average precision, given the recall and precision curves
83
+ # Arguments
84
+ recall: The recall curve (list)
85
+ precision: The precision curve (list)
86
+ # Returns
87
+ Average precision, precision curve, recall curve
88
+ """
89
+
90
+ # Append sentinel values to beginning and end
91
+ mrec = np.concatenate(([0.], recall, [recall[-1] + 0.01]))
92
+ mpre = np.concatenate(([1.], precision, [0.]))
93
+
94
+ # Compute the precision envelope
95
+ mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
96
+
97
+ # Integrate area under curve
98
+ method = 'interp' # methods: 'continuous', 'interp'
99
+ if method == 'interp':
100
+ x = np.linspace(0, 1, 101) # 101-point interp (COCO)
101
+ ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate
102
+ else: # 'continuous'
103
+ i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes
104
+ ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve
105
+
106
+ return ap, mpre, mrec
107
+
108
+
109
+ class ConfusionMatrix:
110
+ # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
111
+ def __init__(self, nc, conf=0.25, iou_thres=0.45):
112
+ self.matrix = np.zeros((nc + 1, nc + 1))
113
+ self.nc = nc # number of classes
114
+ self.conf = conf
115
+ self.iou_thres = iou_thres
116
+
117
+ def process_batch(self, detections, labels):
118
+ """
119
+ Return intersection-over-union (Jaccard index) of boxes.
120
+ Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
121
+ Arguments:
122
+ detections (Array[N, 6]), x1, y1, x2, y2, conf, class
123
+ labels (Array[M, 5]), class, x1, y1, x2, y2
124
+ Returns:
125
+ None, updates confusion matrix accordingly
126
+ """
127
+ detections = detections[detections[:, 4] > self.conf]
128
+ gt_classes = labels[:, 0].int()
129
+ detection_classes = detections[:, 5].int()
130
+ iou = general.box_iou(labels[:, 1:], detections[:, :4])
131
+
132
+ x = torch.where(iou > self.iou_thres)
133
+ if x[0].shape[0]:
134
+ matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
135
+ if x[0].shape[0] > 1:
136
+ matches = matches[matches[:, 2].argsort()[::-1]]
137
+ matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
138
+ matches = matches[matches[:, 2].argsort()[::-1]]
139
+ matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
140
+ else:
141
+ matches = np.zeros((0, 3))
142
+
143
+ n = matches.shape[0] > 0
144
+ m0, m1, _ = matches.transpose().astype(np.int16)
145
+ for i, gc in enumerate(gt_classes):
146
+ j = m0 == i
147
+ if n and sum(j) == 1:
148
+ self.matrix[gc, detection_classes[m1[j]]] += 1 # correct
149
+ else:
150
+ self.matrix[gc, self.nc] += 1 # background FP
151
+
152
+ if n:
153
+ for i, dc in enumerate(detection_classes):
154
+ if not any(m1 == i):
155
+ self.matrix[self.nc, dc] += 1 # background FN
156
+
157
+ def matrix(self):
158
+ return self.matrix
159
+
160
+ def plot(self, save_dir='', names=()):
161
+ try:
162
+ import seaborn as sn
163
+
164
+ array = self.matrix / (self.matrix.sum(0).reshape(1, self.nc + 1) + 1E-6) # normalize
165
+ array[array < 0.005] = np.nan # don't annotate (would appear as 0.00)
166
+
167
+ fig = plt.figure(figsize=(12, 9), tight_layout=True)
168
+ sn.set(font_scale=1.0 if self.nc < 50 else 0.8) # for label size
169
+ labels = (0 < len(names) < 99) and len(names) == self.nc # apply names to ticklabels
170
+ sn.heatmap(array, annot=self.nc < 30, annot_kws={"size": 8}, cmap='Blues', fmt='.2f', square=True,
171
+ xticklabels=names + ['background FN'] if labels else "auto",
172
+ yticklabels=names + ['background FP'] if labels else "auto").set_facecolor((1, 1, 1))
173
+ fig.axes[0].set_xlabel('True')
174
+ fig.axes[0].set_ylabel('Predicted')
175
+ fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)
176
+ except Exception as e:
177
+ pass
178
+
179
+ def print(self):
180
+ for i in range(self.nc + 1):
181
+ print(' '.join(map(str, self.matrix[i])))
182
+
183
+ class SegmentationMetric(object):
184
+ '''
185
+ imgLabel [batch_size, height(144), width(256)]
186
+ confusionMatrix [[0(TN),1(FP)],
187
+ [2(FN),3(TP)]]
188
+ '''
189
+ def __init__(self, numClass):
190
+ self.numClass = numClass
191
+ self.confusionMatrix = np.zeros((self.numClass,)*2)
192
+
193
+ def pixelAccuracy(self):
194
+ # return all class overall pixel accuracy
195
+ # acc = (TP + TN) / (TP + TN + FP + TN)
196
+ acc = np.diag(self.confusionMatrix).sum() / self.confusionMatrix.sum()
197
+ return acc
198
+
199
+ def lineAccuracy(self):
200
+ Acc = np.diag(self.confusionMatrix) / (self.confusionMatrix.sum(axis=1) + 1e-12)
201
+ return Acc[1]
202
+
203
+ def classPixelAccuracy(self):
204
+ # return each category pixel accuracy(A more accurate way to call it precision)
205
+ # acc = (TP) / TP + FP
206
+ classAcc = np.diag(self.confusionMatrix) / (self.confusionMatrix.sum(axis=0) + 1e-12)
207
+ return classAcc
208
+
209
+ def meanPixelAccuracy(self):
210
+ classAcc = self.classPixelAccuracy()
211
+ meanAcc = np.nanmean(classAcc)
212
+ return meanAcc
213
+
214
+ def meanIntersectionOverUnion(self):
215
+ # Intersection = TP Union = TP + FP + FN
216
+ # IoU = TP / (TP + FP + FN)
217
+ intersection = np.diag(self.confusionMatrix)
218
+ union = np.sum(self.confusionMatrix, axis=1) + np.sum(self.confusionMatrix, axis=0) - np.diag(self.confusionMatrix)
219
+ IoU = intersection / union
220
+ IoU[np.isnan(IoU)] = 0
221
+ mIoU = np.nanmean(IoU)
222
+ return mIoU
223
+
224
+ def IntersectionOverUnion(self):
225
+ intersection = np.diag(self.confusionMatrix)
226
+ union = np.sum(self.confusionMatrix, axis=1) + np.sum(self.confusionMatrix, axis=0) - np.diag(self.confusionMatrix)
227
+ IoU = intersection / union
228
+ IoU[np.isnan(IoU)] = 0
229
+ return IoU[1]
230
+
231
+ def genConfusionMatrix(self, imgPredict, imgLabel):
232
+ # remove classes from unlabeled pixels in gt image and predict
233
+ # print(imgLabel.shape)
234
+ mask = (imgLabel >= 0) & (imgLabel < self.numClass)
235
+ label = self.numClass * imgLabel[mask] + imgPredict[mask]
236
+ count = np.bincount(label, minlength=self.numClass**2)
237
+ confusionMatrix = count.reshape(self.numClass, self.numClass)
238
+ return confusionMatrix
239
+
240
+ def Frequency_Weighted_Intersection_over_Union(self):
241
+ # FWIOU = [(TP+FN)/(TP+FP+TN+FN)] *[TP / (TP + FP + FN)]
242
+ freq = np.sum(self.confusionMatrix, axis=1) / np.sum(self.confusionMatrix)
243
+ iu = np.diag(self.confusionMatrix) / (
244
+ np.sum(self.confusionMatrix, axis=1) + np.sum(self.confusionMatrix, axis=0) -
245
+ np.diag(self.confusionMatrix))
246
+ FWIoU = (freq[freq > 0] * iu[freq > 0]).sum()
247
+ return FWIoU
248
+
249
+
250
+ def addBatch(self, imgPredict, imgLabel):
251
+ assert imgPredict.shape == imgLabel.shape
252
+ self.confusionMatrix += self.genConfusionMatrix(imgPredict, imgLabel)
253
+
254
+ def reset(self):
255
+ self.confusionMatrix = np.zeros((self.numClass, self.numClass))
256
+
257
+
258
+
259
+
260
+
261
+ # Plots ----------------------------------------------------------------------------------------------------------------
262
+
263
+ def plot_pr_curve(px, py, ap, save_dir='.', names=()):
264
+ fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
265
+ py = np.stack(py, axis=1)
266
+
267
+ if 0 < len(names) < 21: # show mAP in legend if < 10 classes
268
+ for i, y in enumerate(py.T):
269
+ ax.plot(px, y, linewidth=1, label=f'{names[i]} %.3f' % ap[i, 0]) # plot(recall, precision)
270
+ else:
271
+ ax.plot(px, py, linewidth=1, color='grey') # plot(recall, precision)
272
+
273
+ ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f [email protected]' % ap[:, 0].mean())
274
+ ax.set_xlabel('Recall')
275
+ ax.set_ylabel('Precision')
276
+ ax.set_xlim(0, 1)
277
+ ax.set_ylim(0, 1)
278
+ plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
279
+ fig.savefig(Path(save_dir) / 'precision_recall_curve.png', dpi=250)