Merge branch 'master' into advanced_logging
This commit is contained in:
20 files changed
+481
-297
No files matched your search
+104
-79
@@ -20,7 +20,7 @@ import yaml
|
||||
from scipy.signal import butter, filtfilt
|
||||
from tqdm import tqdm
|
||||
|
||||
from . import torch_utils, google_utils # torch_utils, google_utils
|
||||
from . import torch_utils # torch_utils, google_utils
|
||||
|
||||
# Set printoptions
|
||||
torch.set_printoptions(linewidth=320, precision=5, profile='long')
|
||||
@@ -53,24 +53,52 @@ def check_git_status():
|
||||
|
||||
def check_img_size(img_size, s=32):
|
||||
# Verify img_size is a multiple of stride s
|
||||
if img_size % s != 0:
|
||||
print('WARNING: --img-size %g must be multiple of max stride %g' % (img_size, s))
|
||||
return make_divisible(img_size, s) # nearest gs-multiple
|
||||
new_size = make_divisible(img_size, s) # ceil gs-multiple
|
||||
if new_size != img_size:
|
||||
print('WARNING: --img-size %g must be multiple of max stride %g, updating to %g' % (img_size, s, new_size))
|
||||
return new_size
|
||||
|
||||
|
||||
def check_best_possible_recall(dataset, anchors, thr=4.0, imgsz=640):
|
||||
# Check best possible recall of dataset with current anchors
|
||||
def check_anchors(dataset, model, thr=4.0, imgsz=640):
|
||||
# Check anchor fit to data, recompute if necessary
|
||||
print('\nAnalyzing anchors... ', end='')
|
||||
m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect()
|
||||
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
|
||||
ratio = wh[:, None] / anchors.view(-1, 2).cpu()[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(('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))
|
||||
scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale
|
||||
wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh
|
||||
|
||||
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 metric(k): # compute metric
|
||||
r = wh[:, None] / k[None]
|
||||
x = torch.min(r, 1. / r).min(2)[0] # ratio metric
|
||||
best = x.max(1)[0] # best_x
|
||||
return (best > 1. / thr).float().mean() # best possible recall
|
||||
|
||||
bpr = metric(m.anchor_grid.clone().cpu().view(-1, 2))
|
||||
print('Best Possible Recall (BPR) = %.4f' % bpr, end='')
|
||||
if bpr < 0.99: # threshold to recompute
|
||||
print('. Attempting to generate improved anchors, please wait...' % bpr)
|
||||
na = m.anchor_grid.numel() // 2 # number of anchors
|
||||
new_anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False)
|
||||
new_bpr = metric(new_anchors.reshape(-1, 2))
|
||||
if new_bpr > bpr: # replace anchors
|
||||
new_anchors = torch.tensor(new_anchors, device=m.anchors.device).type_as(m.anchors)
|
||||
m.anchor_grid[:] = new_anchors.clone().view_as(m.anchor_grid) # for inference
|
||||
m.anchors[:] = new_anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1) # loss
|
||||
check_anchor_order(m)
|
||||
print('New anchors saved to model. Update model *.yaml to use these anchors in the future.')
|
||||
else:
|
||||
print('Original anchors better than new anchors. Proceeding with original anchors.')
|
||||
print('') # newline
|
||||
|
||||
|
||||
def check_anchor_order(m):
|
||||
# Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
|
||||
a = m.anchor_grid.prod(-1).view(-1) # anchor area
|
||||
da = a[-1] - a[0] # delta a
|
||||
ds = m.stride[-1] - m.stride[0] # delta s
|
||||
if da.sign() != ds.sign(): # same order
|
||||
m.anchors[:] = m.anchors.flip(0)
|
||||
m.anchor_grid[:] = m.anchor_grid.flip(0)
|
||||
|
||||
|
||||
def check_file(file):
|
||||
@@ -517,11 +545,11 @@ def build_targets(p, targets, model):
|
||||
return tcls, tbox, indices, anch
|
||||
|
||||
|
||||
def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, fast=False, classes=None, agnostic=False):
|
||||
"""
|
||||
Performs Non-Maximum Suppression on inference results
|
||||
Returns detections with shape:
|
||||
nx6 (x1, y1, x2, y2, conf, cls)
|
||||
def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, merge=False, classes=None, agnostic=False):
|
||||
"""Performs Non-Maximum Suppression (NMS) on inference results
|
||||
|
||||
Returns:
|
||||
detections with shape: nx6 (x1, y1, x2, y2, conf, cls)
|
||||
"""
|
||||
if prediction.dtype is torch.float16:
|
||||
prediction = prediction.float() # to FP32
|
||||
@@ -534,12 +562,7 @@ def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, fast=False, c
|
||||
max_det = 300 # maximum number of detections per image
|
||||
time_limit = 10.0 # seconds to quit after
|
||||
redundant = True # require redundant detections
|
||||
fast |= conf_thres > 0.001 # fast mode
|
||||
multi_label = nc > 1 # multiple labels per box (adds 0.5ms/img)
|
||||
if fast:
|
||||
merge = False
|
||||
else:
|
||||
merge = True # merge for best mAP (adds 0.5ms/img)
|
||||
|
||||
t = time.time()
|
||||
output = [None] * prediction.shape[0]
|
||||
@@ -610,24 +633,24 @@ def strip_optimizer(f='weights/best.pt'): # from utils.utils import *; strip_op
|
||||
# Strip optimizer from *.pt files for lighter files (reduced by 1/2 size)
|
||||
x = torch.load(f, map_location=torch.device('cpu'))
|
||||
x['optimizer'] = None
|
||||
x['model'].half() # to FP16
|
||||
torch.save(x, f)
|
||||
print('Optimizer stripped from %s' % f)
|
||||
|
||||
|
||||
def create_backbone(f='weights/best.pt', s='weights/backbone.pt'): # from utils.utils import *; create_backbone()
|
||||
# create backbone 's' from 'f'
|
||||
def create_pretrained(f='weights/best.pt', s='weights/pretrained.pt'): # from utils.utils import *; create_pretrained()
|
||||
# create pretrained checkpoint 's' from 'f' (create_pretrained(x, x) for x in glob.glob('./*.pt'))
|
||||
device = torch.device('cpu')
|
||||
x = torch.load(f, map_location=device)
|
||||
torch.save(x, s) # update model if SourceChangeWarning
|
||||
x = torch.load(s, map_location=device)
|
||||
|
||||
x['optimizer'] = None
|
||||
x['training_results'] = None
|
||||
x['epoch'] = -1
|
||||
x['model'].half() # to FP16
|
||||
for p in x['model'].parameters():
|
||||
p.requires_grad = True
|
||||
torch.save(x, s)
|
||||
print('%s modified for backbone use and saved as %s' % (f, s))
|
||||
print('%s saved as pretrained checkpoint %s' % (f, s))
|
||||
|
||||
|
||||
def coco_class_count(path='../coco/labels/train2014/'):
|
||||
@@ -695,14 +718,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
|
||||
|
||||
|
||||
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
|
||||
|
||||
Arguments:
|
||||
path: path to dataset *.yaml
|
||||
path: path to dataset *.yaml, or a loaded dataset
|
||||
n: number of anchors
|
||||
img_size: (min, max) image size used for multi-scale training (can be same values)
|
||||
thr: IoU threshold hyperparameter used for training (0.0 - 1.0)
|
||||
img_size: image size used for training
|
||||
thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
|
||||
gen: generations to evolve anchors using genetic algorithm
|
||||
|
||||
Return:
|
||||
@@ -711,52 +734,47 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
|
||||
Usage:
|
||||
from utils.utils import *; _ = kmean_anchors()
|
||||
"""
|
||||
thr = 1. / thr
|
||||
|
||||
from utils.datasets import LoadImagesAndLabels
|
||||
def metric(k, wh): # 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
|
||||
|
||||
def fitness(k): # mutation fitness
|
||||
_, best = metric(torch.tensor(k, dtype=torch.float32), wh)
|
||||
return (best * (best > thr).float()).mean() # fitness
|
||||
|
||||
def print_results(k):
|
||||
k = k[np.argsort(k.prod(1))] # sort small to large
|
||||
iou = wh_iou(wh, torch.Tensor(k))
|
||||
max_iou = iou.max(1)[0]
|
||||
bpr, aat = (max_iou > thr).float().mean(), (iou > thr).float().mean() * n # best possible recall, anch > thr
|
||||
|
||||
# thr = 5.0
|
||||
# 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='')
|
||||
x, best = metric(k, wh0)
|
||||
bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
|
||||
print('thr=%.2f: %.4f 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: ' %
|
||||
(n, img_size, x.mean(), best.mean(), x[x > thr].mean()), end='')
|
||||
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
|
||||
return k
|
||||
|
||||
def fitness(k): # mutation fitness
|
||||
iou = wh_iou(wh, torch.Tensor(k)) # iou
|
||||
max_iou = iou.max(1)[0]
|
||||
return (max_iou * (max_iou > thr).float()).mean() # product
|
||||
|
||||
# def fitness_ratio(k): # mutation fitness
|
||||
# # wh(5316,2), k(9,2)
|
||||
# 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
|
||||
if isinstance(path, str): # *.yaml file
|
||||
with open(path) as f:
|
||||
data_dict = yaml.load(f, Loader=yaml.FullLoader) # model dict
|
||||
from utils.datasets import LoadImagesAndLabels
|
||||
dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
|
||||
else:
|
||||
dataset = path # dataset
|
||||
|
||||
# Get label wh
|
||||
wh = []
|
||||
with open(path) as f:
|
||||
data_dict = yaml.load(f, Loader=yaml.FullLoader) # model dict
|
||||
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)
|
||||
shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
||||
wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh
|
||||
|
||||
# Filter
|
||||
i = (wh0 < 4.0).any(1).sum()
|
||||
if i:
|
||||
print('WARNING: Extremely small objects found. '
|
||||
'%g of %g labels are < 4 pixels in width or height.' % (i, len(wh0)))
|
||||
wh = wh0[(wh0 >= 4.0).any(1)] # filter > 2 pixels
|
||||
|
||||
# Kmeans calculation
|
||||
from scipy.cluster.vq import kmeans
|
||||
@@ -764,10 +782,11 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
|
||||
s = wh.std(0) # sigmas for whitening
|
||||
k, dist = kmeans(wh / s, n, iter=30) # points, mean distance
|
||||
k *= s
|
||||
wh = torch.Tensor(wh)
|
||||
wh = torch.tensor(wh, dtype=torch.float32) # filtered
|
||||
wh0 = torch.tensor(wh0, dtype=torch.float32) # unflitered
|
||||
k = print_results(k)
|
||||
|
||||
# # Plot
|
||||
# Plot
|
||||
# k, d = [None] * 20, [None] * 20
|
||||
# for i in tqdm(range(1, 21)):
|
||||
# k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
|
||||
@@ -783,7 +802,8 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
|
||||
# Evolve
|
||||
npr = np.random
|
||||
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'):
|
||||
pbar = tqdm(range(gen), desc='Evolving anchors with Genetic Algorithm') # progress bar
|
||||
for _ in pbar:
|
||||
v = np.ones(sh)
|
||||
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)
|
||||
@@ -791,9 +811,11 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
|
||||
fg = fitness(kg)
|
||||
if fg > f:
|
||||
f, k = fg, kg.copy()
|
||||
print_results(k)
|
||||
k = print_results(k)
|
||||
return k
|
||||
pbar.desc = 'Evolving anchors with Genetic Algorithm: fitness = %.4f' % f
|
||||
if verbose:
|
||||
print_results(k)
|
||||
|
||||
return print_results(k)
|
||||
|
||||
|
||||
def print_mutation(hyp, results, bucket=''):
|
||||
@@ -1078,12 +1100,14 @@ def plot_study_txt(f='study.txt', x=None): # from utils.utils import *; plot_st
|
||||
|
||||
ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [33.5, 39.1, 42.5, 45.9, 49., 50.5],
|
||||
'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet')
|
||||
|
||||
ax2.grid()
|
||||
ax2.set_xlim(0, 30)
|
||||
ax2.set_ylim(25, 50)
|
||||
ax2.set_xlabel('GPU Latency (ms)')
|
||||
ax2.set_ylim(28, 50)
|
||||
ax2.set_yticks(np.arange(30, 55, 5))
|
||||
ax2.set_xlabel('GPU Speed (ms/img)')
|
||||
ax2.set_ylabel('COCO AP val')
|
||||
ax2.legend(loc='lower right')
|
||||
ax2.grid()
|
||||
plt.savefig('study_mAP_latency.png', dpi=300)
|
||||
plt.savefig(f.replace('.txt', '.png'), dpi=200)
|
||||
|
||||
@@ -1110,6 +1134,7 @@ def plot_labels(labels, save_dir= '.'):
|
||||
ax[2].set_xlabel('width')
|
||||
ax[2].set_ylabel('height')
|
||||
plt.savefig(os.path.join(save_dir,'labels.png'), dpi=200)
|
||||
plt.close()
|
||||
|
||||
|
||||
def plot_evolution_results(hyp): # from utils.utils import *; plot_evolution_results(hyp)
|
||||
|
||||
Reference in new issue
Block a user