AutoAnchor implementation

pull/1/head
Glenn Jocher 5 years ago
parent 95c46f7245
commit 57a0ae3350

@ -53,18 +53,23 @@ def check_img_size(img_size, s=32):
def check_anchors(dataset, model, thr=4.0, imgsz=640): def check_anchors(dataset, model, thr=4.0, imgsz=640):
# Check best possible recall of dataset with current anchors # Check anchor fit to data, recompute if necessary
print('\nAnalyzing anchors... ', end='')
anchors = model.module.model[-1].anchor_grid if hasattr(model, 'module') else model.model[-1].anchor_grid anchors = model.module.model[-1].anchor_grid if hasattr(model, 'module') else model.model[-1].anchor_grid
shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True) shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True)
wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)])).float() # wh wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)])).float() # wh
ratio = wh[:, None] / anchors.view(-1, 2).cpu()[None] # ratio ratio = wh[:, None] / anchors.view(-1, 2).cpu()[None] # ratio
m = torch.max(ratio, 1. / ratio).max(2)[0] # max ratio m = torch.max(ratio, 1. / ratio).max(2)[0] # max ratio
bpr = (m.min(1)[0] < thr).float().mean() # best possible recall bpr = (m.min(1)[0] < thr).float().mean() # best possible recall
mr = (m < thr).float().mean() # match ratio # mr = (m < thr).float().mean() # match ratio
print(('AutoAnchor labels:' + '%10s' * 6) % ('n', 'mean', 'min', 'max', 'matching', 'recall'))
print((' ' + '%10.4g' * 6) % (wh.shape[0], wh.mean(), wh.min(), wh.max(), mr, bpr)) print('Best Possible Recall (BPR) = %.3f' % bpr, end='')
assert bpr > 0.9, 'Best possible recall %.3g (BPR) below 0.9 threshold. Training cancelled. ' \ if bpr < 0.99: # threshold to recompute
'Compute new anchors with utils.utils.kmeans_anchors() and update model before training.' % bpr print('. Generating new anchors for improved recall, please wait...' % bpr)
new_anchors = kmean_anchors(dataset, n=9, img_size=640, thr=4.0, gen=1000, verbose=False)
anchors[:] = torch.tensor(new_anchors).view_as(anchors).type_as(anchors)
print('New anchors saved to model. Update model *.yaml to use these anchors in the future.')
print('') # newline
def check_file(file): def check_file(file):
@ -689,14 +694,14 @@ def coco_single_class_labels(path='../coco/labels/train2014/', label_class=43):
shutil.copyfile(src=img_file, dst='new/images/' + Path(file).name.replace('txt', 'jpg')) # copy images shutil.copyfile(src=img_file, dst='new/images/' + Path(file).name.replace('txt', 'jpg')) # copy images
def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20, gen=1000): def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True):
""" Creates kmeans-evolved anchors from training dataset """ Creates kmeans-evolved anchors from training dataset
Arguments: Arguments:
path: path to dataset *.yaml path: path to dataset *.yaml, or a loaded dataset
n: number of anchors n: number of anchors
img_size: (min, max) image size used for multi-scale training (can be same values) img_size: image size used for training
thr: IoU threshold hyperparameter used for training (0.0 - 1.0) thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
gen: generations to evolve anchors using genetic algorithm gen: generations to evolve anchors using genetic algorithm
Return: Return:
@ -705,52 +710,41 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
Usage: Usage:
from utils.utils import *; _ = kmean_anchors() from utils.utils import *; _ = kmean_anchors()
""" """
thr = 1. / thr
def metric(k): # compute metrics
r = wh[:, None] / k[None]
x = torch.min(r, 1. / r).min(2)[0] # ratio metric
# x = wh_iou(wh, torch.tensor(k)) # iou metric
return x, x.max(1)[0] # x, best_x
from utils.datasets import LoadImagesAndLabels def fitness(k): # mutation fitness
_, best = metric(k)
return (best * (best > thr).float()).mean() # fitness
def print_results(k): def print_results(k):
k = k[np.argsort(k.prod(1))] # sort small to large k = k[np.argsort(k.prod(1))] # sort small to large
iou = wh_iou(wh, torch.Tensor(k)) x, best = metric(k)
max_iou = iou.max(1)[0] bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
bpr, aat = (max_iou > thr).float().mean(), (iou > thr).float().mean() * n # best possible recall, anch > thr print('thr=%.2f: %.3f best possible recall, %.2f anchors past thr' % (thr, bpr, aat))
print('n=%g, img_size=%s, metric_all=%.3f/%.3f-mean/best, past_thr=%.3f-mean: ' %
# thr = 5.0 (n, img_size, x.mean(), best.mean(), x[x > thr].mean()), end='')
# r = wh[:, None] / k[None]
# ar = torch.max(r, 1. / r).max(2)[0]
# max_ar = ar.min(1)[0]
# bpr, aat = (max_ar < thr).float().mean(), (ar < thr).float().mean() * n # best possible recall, anch > thr
print('%.2f iou_thr: %.3f best possible recall, %.2f anchors > thr' % (thr, bpr, aat))
print('n=%g, img_size=%s, IoU_all=%.3f/%.3f-mean/best, IoU>thr=%.3f-mean: ' %
(n, img_size, iou.mean(), max_iou.mean(), iou[iou > thr].mean()), end='')
for i, x in enumerate(k): for i, x in enumerate(k):
print('%i,%i' % (round(x[0]), round(x[1])), end=', ' if i < len(k) - 1 else '\n') # use in *.cfg print('%i,%i' % (round(x[0]), round(x[1])), end=', ' if i < len(k) - 1 else '\n') # use in *.cfg
return k return k
def fitness(k): # mutation fitness if isinstance(path, str): # *.yaml file
iou = wh_iou(wh, torch.Tensor(k)) # iou with open(path) as f:
max_iou = iou.max(1)[0] data_dict = yaml.load(f, Loader=yaml.FullLoader) # model dict
return (max_iou * (max_iou > thr).float()).mean() # product from utils.datasets import LoadImagesAndLabels
dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
# def fitness_ratio(k): # mutation fitness else:
# # wh(5316,2), k(9,2) dataset = path # dataset
# r = wh[:, None] / k[None]
# x = torch.max(r, 1. / r).max(2)[0]
# m = x.min(1)[0]
# return 1. / (m * (m < 5).float()).mean() # product
# Get label wh # Get label wh
wh = [] shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
with open(path) as f: wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)])).float() # wh
data_dict = yaml.load(f, Loader=yaml.FullLoader) # model dict wh = wh[(wh > 2.0).all(1)].numpy() # filter > 2 pixels
dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
nr = 1 if img_size[0] == img_size[1] else 3 # number augmentation repetitions
for s, l in zip(dataset.shapes, dataset.labels):
# wh.append(l[:, 3:5] * (s / s.max())) # image normalized to letterbox normalized wh
wh.append(l[:, 3:5] * s) # image normalized to pixels
wh = np.concatenate(wh, 0).repeat(nr, axis=0) # augment 3x
# wh *= np.random.uniform(img_size[0], img_size[1], size=(wh.shape[0], 1)) # normalized to pixels (multi-scale)
wh = wh[(wh > 2.0).all(1)] # remove below threshold boxes (< 2 pixels wh)
# Kmeans calculation # Kmeans calculation
from scipy.cluster.vq import kmeans from scipy.cluster.vq import kmeans
@ -758,10 +752,10 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
s = wh.std(0) # sigmas for whitening s = wh.std(0) # sigmas for whitening
k, dist = kmeans(wh / s, n, iter=30) # points, mean distance k, dist = kmeans(wh / s, n, iter=30) # points, mean distance
k *= s k *= s
wh = torch.Tensor(wh) wh = torch.tensor(wh)
k = print_results(k) k = print_results(k)
# # Plot # Plot
# k, d = [None] * 20, [None] * 20 # k, d = [None] * 20, [None] * 20
# for i in tqdm(range(1, 21)): # for i in tqdm(range(1, 21)):
# k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance # k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
@ -777,7 +771,7 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
# Evolve # Evolve
npr = np.random npr = np.random
f, sh, mp, s = fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma f, sh, mp, s = fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma
for _ in tqdm(range(gen), desc='Evolving anchors'): for _ in tqdm(range(gen), desc='Evolving anchors with Genetic Algorithm:'):
v = np.ones(sh) v = np.ones(sh)
while (v == 1).all(): # mutate until a change occurs (prevent duplicates) while (v == 1).all(): # mutate until a change occurs (prevent duplicates)
v = ((npr.random(sh) < mp) * npr.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0) v = ((npr.random(sh) < mp) * npr.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0)
@ -785,7 +779,8 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
fg = fitness(kg) fg = fitness(kg)
if fg > f: if fg > f:
f, k = fg, kg.copy() f, k = fg, kg.copy()
print_results(k) if verbose:
print_results(k)
k = print_results(k) k = print_results(k)
return k return k

Loading…
Cancel
Save