assert best possible recall > 0.9 before training

This commit is contained in:
Glenn Jocher committed 2020-06-13 15:05:41 -07:00
1 parent 19e68e8a7b
commit 31f3310029
3 files changed
+30 -12

No files matched your search

+13
View File
@@ -51,6 +51,19 @@ def check_img_size(img_size, s=32):
return make_divisible(img_size, s) # nearest gs-multiple
def check_best_possible_recall(dataset, anchors, thr):
# Check best possible recall of dataset with current anchors
wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(dataset.shapes, dataset.labels)])) # width-height
ratio = wh[:, None] / anchors.view(-1, 2)[None] # ratio
m = torch.max(ratio, 1. / ratio).max(2)[0] # max ratio
bpr = (m.min(1)[0] < thr).float().mean() # best possible recall
mr = (m < thr).float().mean() # match ratio
print(('Label width-height:' + '%10s' * 6) % ('n', 'mean', 'min', 'max', 'matching', 'recall'))
print((' ' + '%10.4g' * 6) % (wh.shape[0], wh.mean(), wh.min(), wh.max(), mr, bpr))
assert bpr > 0.9, 'Best possible recall %.3g (BPR) below 0.9 threshold. Training cancelled. ' \
'Compute new anchors with utils.utils.kmeans_anchors() and update model before training.' % bpr
def make_divisible(x, divisor):
# Returns x evenly divisble by divisor
return math.ceil(x / divisor) * divisor