Add smoothing to curve plots and max F1 index (#7798)
Browse files- utils/metrics.py +10 -2
utils/metrics.py
CHANGED
@@ -18,6 +18,14 @@ def fitness(x):
|
|
18 |
return (x[:, :4] * w).sum(1)
|
19 |
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=(), eps=1e-16):
|
22 |
""" Compute the average precision, given the recall and precision curves.
|
23 |
Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
|
@@ -79,7 +87,7 @@ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names
|
|
79 |
plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision')
|
80 |
plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall')
|
81 |
|
82 |
-
i = f1.mean(0).argmax() # max F1 index
|
83 |
p, r, f1 = p[:, i], r[:, i], f1[:, i]
|
84 |
tp = (r * nt).round() # true positives
|
85 |
fp = (tp / (p + eps) - tp).round() # false positives
|
@@ -337,7 +345,7 @@ def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence'
|
|
337 |
else:
|
338 |
ax.plot(px, py.T, linewidth=1, color='grey') # plot(confidence, metric)
|
339 |
|
340 |
-
y = py.mean(0)
|
341 |
ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}')
|
342 |
ax.set_xlabel(xlabel)
|
343 |
ax.set_ylabel(ylabel)
|
|
|
18 |
return (x[:, :4] * w).sum(1)
|
19 |
|
20 |
|
21 |
+
def smooth(y, f=0.05):
|
22 |
+
# Box filter of fraction f
|
23 |
+
nf = round(len(y) * f * 2) // 2 + 1 # number of filter elements (must be odd)
|
24 |
+
p = np.ones(nf // 2) # ones padding
|
25 |
+
yp = np.concatenate((p * y[0], y, p * y[-1]), 0) # y padded
|
26 |
+
return np.convolve(yp, np.ones(nf) / nf, mode='valid') # y-smoothed
|
27 |
+
|
28 |
+
|
29 |
def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=(), eps=1e-16):
|
30 |
""" Compute the average precision, given the recall and precision curves.
|
31 |
Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
|
|
|
87 |
plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision')
|
88 |
plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall')
|
89 |
|
90 |
+
i = smooth(f1.mean(0), 0.1).argmax() # max F1 index
|
91 |
p, r, f1 = p[:, i], r[:, i], f1[:, i]
|
92 |
tp = (r * nt).round() # true positives
|
93 |
fp = (tp / (p + eps) - tp).round() # false positives
|
|
|
345 |
else:
|
346 |
ax.plot(px, py.T, linewidth=1, color='grey') # plot(confidence, metric)
|
347 |
|
348 |
+
y = smooth(py.mean(0), 0.05)
|
349 |
ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}')
|
350 |
ax.set_xlabel(xlabel)
|
351 |
ax.set_ylabel(ylabel)
|