Unverified 提交 5948f20a authored 作者: Glenn Jocher's avatar Glenn Jocher 提交者: GitHub

Update test.py profiling (#3555)

* Update test.py profiling * half_precision to half * inplace
上级 8b5086c2
...@@ -38,7 +38,7 @@ def test(data, ...@@ -38,7 +38,7 @@ def test(data,
plots=True, plots=True,
wandb_logger=None, wandb_logger=None,
compute_loss=None, compute_loss=None,
half_precision=True, half=True,
opt=None): opt=None):
# Initialize/load model and set device # Initialize/load model and set device
training = model is not None training = model is not None
...@@ -63,7 +63,7 @@ def test(data, ...@@ -63,7 +63,7 @@ def test(data,
# model = nn.DataParallel(model) # model = nn.DataParallel(model)
# Half # Half
half = device.type != 'cpu' and half_precision # half precision only supported on CUDA half &= device.type != 'cpu' # half precision only supported on CUDA
if half: if half:
model.half() model.half()
...@@ -95,20 +95,22 @@ def test(data, ...@@ -95,20 +95,22 @@ def test(data,
names = {k: v for k, v in enumerate(model.names if hasattr(model, 'names') else model.module.names)} names = {k: v for k, v in enumerate(model.names if hasattr(model, 'names') else model.module.names)}
coco91class = coco80_to_coco91_class() coco91class = coco80_to_coco91_class()
s = ('%20s' + '%11s' * 6) % ('Class', 'Images', 'Labels', 'P', 'R', 'mAP@.5', 'mAP@.5:.95') s = ('%20s' + '%11s' * 6) % ('Class', 'Images', 'Labels', 'P', 'R', 'mAP@.5', 'mAP@.5:.95')
p, r, f1, mp, mr, map50, map, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0. p, r, f1, mp, mr, map50, map, t0, t1, t2 = 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
loss = torch.zeros(3, device=device) loss = torch.zeros(3, device=device)
jdict, stats, ap, ap_class, wandb_images = [], [], [], [], [] jdict, stats, ap, ap_class, wandb_images = [], [], [], [], []
for batch_i, (img, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)): for batch_i, (img, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)):
t_ = time_synchronized()
img = img.to(device, non_blocking=True) img = img.to(device, non_blocking=True)
img = img.half() if half else img.float() # uint8 to fp16/32 img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0 img /= 255.0 # 0 - 255 to 0.0 - 1.0
targets = targets.to(device) targets = targets.to(device)
nb, _, height, width = img.shape # batch size, channels, height, width nb, _, height, width = img.shape # batch size, channels, height, width
t = time_synchronized()
t0 += t - t_
# Run model # Run model
t = time_synchronized()
out, train_out = model(img, augment=augment) # inference and training outputs out, train_out = model(img, augment=augment) # inference and training outputs
t0 += time_synchronized() - t t1 += time_synchronized() - t
# Compute loss # Compute loss
if compute_loss: if compute_loss:
...@@ -119,7 +121,7 @@ def test(data, ...@@ -119,7 +121,7 @@ def test(data,
lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
t = time_synchronized() t = time_synchronized()
out = non_max_suppression(out, conf_thres, iou_thres, labels=lb, multi_label=True, agnostic=single_cls) out = non_max_suppression(out, conf_thres, iou_thres, labels=lb, multi_label=True, agnostic=single_cls)
t1 += time_synchronized() - t t2 += time_synchronized() - t
# Statistics per image # Statistics per image
for si, pred in enumerate(out): for si, pred in enumerate(out):
...@@ -236,9 +238,10 @@ def test(data, ...@@ -236,9 +238,10 @@ def test(data,
print(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i])) print(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i]))
# Print speeds # Print speeds
t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + (imgsz, imgsz, batch_size) # tuple t = tuple(x / seen * 1E3 for x in (t0, t1, t2)) # speeds per image
if not training: if not training:
print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t) shape = (batch_size, 3, imgsz, imgsz)
print(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)
# Plots # Plots
if plots: if plots:
...@@ -327,24 +330,25 @@ if __name__ == '__main__': ...@@ -327,24 +330,25 @@ if __name__ == '__main__':
save_txt=opt.save_txt | opt.save_hybrid, save_txt=opt.save_txt | opt.save_hybrid,
save_hybrid=opt.save_hybrid, save_hybrid=opt.save_hybrid,
save_conf=opt.save_conf, save_conf=opt.save_conf,
half_precision=opt.half, half=opt.half,
opt=opt opt=opt
) )
elif opt.task == 'speed': # speed benchmarks elif opt.task == 'speed': # speed benchmarks
for w in opt.weights: for w in opt.weights if isinstance(opt.weights, list) else [opt.weights]:
test(opt.data, w, opt.batch_size, opt.img_size, 0.25, 0.45, save_json=False, plots=False, opt=opt) test(opt.data, w, opt.batch_size, opt.img_size, 0.25, 0.45, save_json=False, plots=False, half=True,
opt=opt)
elif opt.task == 'study': # run over a range of settings and save/plot elif opt.task == 'study': # run over a range of settings and save/plot
# python test.py --task study --data coco.yaml --iou 0.7 --weights yolov5s.pt yolov5m.pt yolov5l.pt yolov5x.pt # python test.py --task study --data coco.yaml --iou 0.7 --weights yolov5s.pt yolov5m.pt yolov5l.pt yolov5x.pt
x = list(range(256, 1536 + 128, 128)) # x axis (image sizes) x = list(range(256, 1536 + 128, 128)) # x axis (image sizes)
for w in opt.weights: for w in opt.weights if isinstance(opt.weights, list) else [opt.weights]:
f = f'study_{Path(opt.data).stem}_{Path(w).stem}.txt' # filename to save to f = f'study_{Path(opt.data).stem}_{Path(w).stem}.txt' # filename to save to
y = [] # y axis y = [] # y axis
for i in x: # img-size for i in x: # img-size
print(f'\nRunning {f} point {i}...') print(f'\nRunning {f} point {i}...')
r, _, t = test(opt.data, w, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json, r, _, t = test(opt.data, w, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json,
plots=False, opt=opt) plots=False, half=True, opt=opt)
y.append(r + t) # results and times y.append(r + t) # results and times
np.savetxt(f, y, fmt='%10.4g') # save np.savetxt(f, y, fmt='%10.4g') # save
os.system('zip -r study.zip study_*.txt') os.system('zip -r study.zip study_*.txt')
......
...@@ -74,7 +74,7 @@ def train(hyp, opt, device, tb_writer=None): ...@@ -74,7 +74,7 @@ def train(hyp, opt, device, tb_writer=None):
loggers['wandb'] = wandb_logger.wandb loggers['wandb'] = wandb_logger.wandb
data_dict = wandb_logger.data_dict data_dict = wandb_logger.data_dict
if wandb_logger.wandb: if wandb_logger.wandb:
weights, epochs, hyp = opt.weights, opt.epochs, opt.hyp # WandbLogger might update weights, epochs if resuming weights, epochs, hyp = opt.weights, opt.epochs, opt.hyp # may update weights, epochs if resuming
nc = 1 if single_cls else int(data_dict['nc']) # number of classes nc = 1 if single_cls else int(data_dict['nc']) # number of classes
names = ['item'] if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names names = ['item'] if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
...@@ -354,7 +354,7 @@ def train(hyp, opt, device, tb_writer=None): ...@@ -354,7 +354,7 @@ def train(hyp, opt, device, tb_writer=None):
final_epoch = epoch + 1 == epochs final_epoch = epoch + 1 == epochs
if not opt.notest or final_epoch: # Calculate mAP if not opt.notest or final_epoch: # Calculate mAP
wandb_logger.current_epoch = epoch + 1 wandb_logger.current_epoch = epoch + 1
results, maps, times = test.test(data_dict, results, maps, _ = test.test(data_dict,
batch_size=batch_size * 2, batch_size=batch_size * 2,
imgsz=imgsz_test, imgsz=imgsz_test,
model=ema.ema, model=ema.ema,
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
import glob import glob
import math import math
import os import os
import random
from copy import copy from copy import copy
from pathlib import Path from pathlib import Path
...@@ -252,21 +251,23 @@ def plot_targets_txt(): # from utils.plots import *; plot_targets_txt() ...@@ -252,21 +251,23 @@ def plot_targets_txt(): # from utils.plots import *; plot_targets_txt()
def plot_study_txt(path='', x=None): # from utils.plots import *; plot_study_txt() def plot_study_txt(path='', x=None): # from utils.plots import *; plot_study_txt()
# Plot study.txt generated by test.py # Plot study.txt generated by test.py
fig, ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True) plot2 = False # plot additional results
# ax = ax.ravel() if plot2:
ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True)[1].ravel()
fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True) fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True)
# for f in [Path(path) / f'study_coco_{x}.txt' for x in ['yolov5s6', 'yolov5m6', 'yolov5l6', 'yolov5x6']]: # for f in [Path(path) / f'study_coco_{x}.txt' for x in ['yolov5s6', 'yolov5m6', 'yolov5l6', 'yolov5x6']]:
for f in sorted(Path(path).glob('study*.txt')): for f in sorted(Path(path).glob('study*.txt')):
y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T
x = np.arange(y.shape[1]) if x is None else np.array(x) x = np.arange(y.shape[1]) if x is None else np.array(x)
s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_inference (ms/img)', 't_NMS (ms/img)', 't_total (ms/img)'] if plot2:
# for i in range(7): s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_preprocess (ms/img)', 't_inference (ms/img)', 't_NMS (ms/img)']
# ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8) for i in range(7):
# ax[i].set_title(s[i]) ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8)
ax[i].set_title(s[i])
j = y[3].argmax() + 1 j = y[3].argmax() + 1
ax2.plot(y[6, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8, ax2.plot(y[5, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8,
label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO')) label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO'))
ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5], ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论