You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
359 lines
16 KiB
359 lines
16 KiB
2 years ago
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||
2 years ago
|
"""
|
||
|
Benchmark a YOLO model formats for speed and accuracy
|
||
|
|
||
|
Usage:
|
||
1 year ago
|
from ultralytics.utils.benchmarks import ProfileModels, benchmark
|
||
2 years ago
|
ProfileModels(['yolov8n.yaml', 'yolov8s.yaml']).profile()
|
||
2 years ago
|
run_benchmarks(model='yolov8n.pt', imgsz=160)
|
||
|
|
||
|
Format | `format=argument` | Model
|
||
|
--- | --- | ---
|
||
|
PyTorch | - | yolov8n.pt
|
||
|
TorchScript | `torchscript` | yolov8n.torchscript
|
||
|
ONNX | `onnx` | yolov8n.onnx
|
||
|
OpenVINO | `openvino` | yolov8n_openvino_model/
|
||
|
TensorRT | `engine` | yolov8n.engine
|
||
|
CoreML | `coreml` | yolov8n.mlmodel
|
||
|
TensorFlow SavedModel | `saved_model` | yolov8n_saved_model/
|
||
|
TensorFlow GraphDef | `pb` | yolov8n.pb
|
||
|
TensorFlow Lite | `tflite` | yolov8n.tflite
|
||
|
TensorFlow Edge TPU | `edgetpu` | yolov8n_edgetpu.tflite
|
||
|
TensorFlow.js | `tfjs` | yolov8n_web_model/
|
||
|
PaddlePaddle | `paddle` | yolov8n_paddle_model/
|
||
1 year ago
|
ncnn | `ncnn` | yolov8n_ncnn_model/
|
||
2 years ago
|
"""
|
||
|
|
||
2 years ago
|
import glob
|
||
2 years ago
|
import platform
|
||
|
import time
|
||
|
from pathlib import Path
|
||
|
|
||
2 years ago
|
import numpy as np
|
||
|
import torch.cuda
|
||
|
from tqdm import tqdm
|
||
|
|
||
2 years ago
|
from ultralytics import YOLO
|
||
1 year ago
|
from ultralytics.cfg import TASK2DATA, TASK2METRIC
|
||
|
from ultralytics.engine.exporter import export_formats
|
||
|
from ultralytics.utils import LINUX, LOGGER, MACOS, ROOT, SETTINGS
|
||
|
from ultralytics.utils.checks import check_requirements, check_yolo
|
||
|
from ultralytics.utils.downloads import download
|
||
|
from ultralytics.utils.files import file_size
|
||
|
from ultralytics.utils.torch_utils import select_device
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def benchmark(model=Path(SETTINGS['weights_dir']) / 'yolov8n.pt',
|
||
|
imgsz=160,
|
||
|
half=False,
|
||
|
int8=False,
|
||
|
device='cpu',
|
||
|
hard_fail=False):
|
||
|
"""
|
||
|
Benchmark a YOLO model across different formats for speed and accuracy.
|
||
|
|
||
|
Args:
|
||
1 year ago
|
model (str | Path | optional): Path to the model file or directory. Default is
|
||
2 years ago
|
Path(SETTINGS['weights_dir']) / 'yolov8n.pt'.
|
||
|
imgsz (int, optional): Image size for the benchmark. Default is 160.
|
||
|
half (bool, optional): Use half-precision for the model if True. Default is False.
|
||
|
int8 (bool, optional): Use int8-precision for the model if True. Default is False.
|
||
|
device (str, optional): Device to run the benchmark on, either 'cpu' or 'cuda'. Default is 'cpu'.
|
||
1 year ago
|
hard_fail (bool | float | optional): If True or a float, assert benchmarks pass with given metric.
|
||
2 years ago
|
Default is False.
|
||
|
|
||
|
Returns:
|
||
|
df (pandas.DataFrame): A pandas DataFrame with benchmark results for each format, including file size,
|
||
|
metric, and inference time.
|
||
|
"""
|
||
|
|
||
2 years ago
|
import pandas as pd
|
||
|
pd.options.display.max_columns = 10
|
||
|
pd.options.display.width = 120
|
||
2 years ago
|
device = select_device(device, verbose=False)
|
||
|
if isinstance(model, (str, Path)):
|
||
|
model = YOLO(model)
|
||
2 years ago
|
|
||
|
y = []
|
||
|
t0 = time.time()
|
||
|
for i, (name, format, suffix, cpu, gpu) in export_formats().iterrows(): # index, (name, format, suffix, CPU, GPU)
|
||
2 years ago
|
emoji, filename = '❌', None # export defaults
|
||
2 years ago
|
try:
|
||
2 years ago
|
assert i != 9 or LINUX, 'Edge TPU export only supported on Linux'
|
||
2 years ago
|
if i == 10:
|
||
|
assert MACOS or LINUX, 'TF.js export only supported on macOS and Linux'
|
||
2 years ago
|
if 'cpu' in device.type:
|
||
|
assert cpu, 'inference not supported on CPU'
|
||
|
if 'cuda' in device.type:
|
||
|
assert gpu, 'inference not supported on GPU'
|
||
|
|
||
|
# Export
|
||
|
if format == '-':
|
||
2 years ago
|
filename = model.ckpt_path or model.cfg
|
||
2 years ago
|
export = model # PyTorch format
|
||
|
else:
|
||
2 years ago
|
filename = model.export(imgsz=imgsz, format=format, half=half, int8=int8, device=device, verbose=False)
|
||
2 years ago
|
export = YOLO(filename, task=model.task)
|
||
2 years ago
|
assert suffix in str(filename), 'export failed'
|
||
2 years ago
|
emoji = '❎' # indicates export succeeded
|
||
2 years ago
|
|
||
2 years ago
|
# Predict
|
||
1 year ago
|
assert model.task != 'pose' or i != 7, 'GraphDef Pose inference is not supported'
|
||
1 year ago
|
assert i not in (9, 10), 'inference not supported' # Edge TPU and TF.js are unsupported
|
||
2 years ago
|
assert i != 5 or platform.system() == 'Darwin', 'inference only supported on macOS>=10.13' # CoreML
|
||
2 years ago
|
if not (ROOT / 'assets/bus.jpg').exists():
|
||
|
download(url='https://ultralytics.com/images/bus.jpg', dir=ROOT / 'assets')
|
||
2 years ago
|
export.predict(ROOT / 'assets/bus.jpg', imgsz=imgsz, device=device, half=half)
|
||
2 years ago
|
|
||
2 years ago
|
# Validate
|
||
1 year ago
|
data = TASK2DATA[model.task] # task to dataset, i.e. coco8.yaml for task=detect
|
||
|
key = TASK2METRIC[model.task] # task to metric, i.e. metrics/mAP50-95(B) for task=detect
|
||
2 years ago
|
results = export.val(data=data,
|
||
|
batch=1,
|
||
|
imgsz=imgsz,
|
||
|
plots=False,
|
||
|
device=device,
|
||
|
half=half,
|
||
|
int8=int8,
|
||
|
verbose=False)
|
||
2 years ago
|
metric, speed = results.results_dict[key], results.speed['inference']
|
||
|
y.append([name, '✅', round(file_size(filename), 1), round(metric, 4), round(speed, 2)])
|
||
|
except Exception as e:
|
||
|
if hard_fail:
|
||
2 years ago
|
assert type(e) is AssertionError, f'Benchmark hard_fail for {name}: {e}'
|
||
2 years ago
|
LOGGER.warning(f'ERROR ❌️ Benchmark failure for {name}: {e}')
|
||
2 years ago
|
y.append([name, emoji, round(file_size(filename), 1), None, None]) # mAP, t_inference
|
||
2 years ago
|
|
||
|
# Print results
|
||
|
check_yolo(device=device) # print system info
|
||
2 years ago
|
df = pd.DataFrame(y, columns=['Format', 'Status❔', 'Size (MB)', key, 'Inference time (ms/im)'])
|
||
2 years ago
|
|
||
2 years ago
|
name = Path(model.ckpt_path).name
|
||
|
s = f'\nBenchmarks complete for {name} on {data} at imgsz={imgsz} ({time.time() - t0:.2f}s)\n{df}\n'
|
||
|
LOGGER.info(s)
|
||
2 years ago
|
with open('benchmarks.log', 'a', errors='ignore', encoding='utf-8') as f:
|
||
2 years ago
|
f.write(s)
|
||
|
|
||
|
if hard_fail and isinstance(hard_fail, float):
|
||
2 years ago
|
metrics = df[key].array # values to compare to floor
|
||
2 years ago
|
floor = hard_fail # minimum metric floor to pass, i.e. = 0.29 mAP for YOLOv5n
|
||
|
assert all(x > floor for x in metrics if pd.notna(x)), f'HARD FAIL: one or more metric(s) < floor {floor}'
|
||
2 years ago
|
|
||
2 years ago
|
return df
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
class ProfileModels:
|
||
|
"""
|
||
|
ProfileModels class for profiling different models on ONNX and TensorRT.
|
||
|
|
||
|
This class profiles the performance of different models, provided their paths. The profiling includes parameters such as
|
||
|
model speed and FLOPs.
|
||
|
|
||
|
Attributes:
|
||
|
paths (list): Paths of the models to profile.
|
||
|
num_timed_runs (int): Number of timed runs for the profiling. Default is 100.
|
||
2 years ago
|
num_warmup_runs (int): Number of warmup runs before profiling. Default is 10.
|
||
|
min_time (float): Minimum number of seconds to profile for. Default is 60.
|
||
2 years ago
|
imgsz (int): Image size used in the models. Default is 640.
|
||
|
|
||
|
Methods:
|
||
|
profile(): Profiles the models and prints the result.
|
||
|
"""
|
||
|
|
||
2 years ago
|
def __init__(self,
|
||
|
paths: list,
|
||
|
num_timed_runs=100,
|
||
|
num_warmup_runs=10,
|
||
|
min_time=60,
|
||
|
imgsz=640,
|
||
|
trt=True,
|
||
|
device=None):
|
||
2 years ago
|
self.paths = paths
|
||
|
self.num_timed_runs = num_timed_runs
|
||
|
self.num_warmup_runs = num_warmup_runs
|
||
2 years ago
|
self.min_time = min_time
|
||
2 years ago
|
self.imgsz = imgsz
|
||
|
self.trt = trt # run TensorRT profiling
|
||
2 years ago
|
self.device = device or torch.device(0 if torch.cuda.is_available() else 'cpu')
|
||
2 years ago
|
|
||
|
def profile(self):
|
||
|
files = self.get_files()
|
||
|
|
||
|
if not files:
|
||
|
print('No matching *.pt or *.onnx files found.')
|
||
|
return
|
||
|
|
||
|
table_rows = []
|
||
2 years ago
|
output = []
|
||
2 years ago
|
for file in files:
|
||
2 years ago
|
engine_file = file.with_suffix('.engine')
|
||
2 years ago
|
if file.suffix in ('.pt', '.yaml'):
|
||
|
model = YOLO(str(file))
|
||
2 years ago
|
model.fuse() # to report correct params and GFLOPs in model.info()
|
||
2 years ago
|
model_info = model.info()
|
||
2 years ago
|
if self.trt and self.device.type != 'cpu' and not engine_file.is_file():
|
||
2 years ago
|
engine_file = model.export(format='engine',
|
||
|
half=True,
|
||
|
imgsz=self.imgsz,
|
||
|
device=self.device,
|
||
|
verbose=False)
|
||
|
onnx_file = model.export(format='onnx',
|
||
|
half=True,
|
||
|
imgsz=self.imgsz,
|
||
|
simplify=True,
|
||
|
device=self.device,
|
||
|
verbose=False)
|
||
2 years ago
|
elif file.suffix == '.onnx':
|
||
2 years ago
|
model_info = self.get_onnx_model_info(file)
|
||
2 years ago
|
onnx_file = file
|
||
|
else:
|
||
|
continue
|
||
|
|
||
|
t_engine = self.profile_tensorrt_model(str(engine_file))
|
||
|
t_onnx = self.profile_onnx_model(str(onnx_file))
|
||
2 years ago
|
table_rows.append(self.generate_table_row(file.stem, t_onnx, t_engine, model_info))
|
||
2 years ago
|
output.append(self.generate_results_dict(file.stem, t_onnx, t_engine, model_info))
|
||
2 years ago
|
|
||
|
self.print_table(table_rows)
|
||
2 years ago
|
return output
|
||
2 years ago
|
|
||
|
def get_files(self):
|
||
|
files = []
|
||
|
for path in self.paths:
|
||
|
path = Path(path)
|
||
|
if path.is_dir():
|
||
|
extensions = ['*.pt', '*.onnx', '*.yaml']
|
||
|
files.extend([file for ext in extensions for file in glob.glob(str(path / ext))])
|
||
|
elif path.suffix in {'.pt', '.yaml'}: # add non-existing
|
||
|
files.append(str(path))
|
||
|
else:
|
||
|
files.extend(glob.glob(str(path)))
|
||
|
|
||
|
print(f'Profiling: {sorted(files)}')
|
||
|
return [Path(file) for file in sorted(files)]
|
||
|
|
||
|
def get_onnx_model_info(self, onnx_file: str):
|
||
2 years ago
|
# return (num_layers, num_params, num_gradients, num_flops)
|
||
|
return 0.0, 0.0, 0.0, 0.0
|
||
|
|
||
2 years ago
|
def iterative_sigma_clipping(self, data, sigma=2, max_iters=3):
|
||
2 years ago
|
data = np.array(data)
|
||
|
for _ in range(max_iters):
|
||
|
mean, std = np.mean(data), np.std(data)
|
||
|
clipped_data = data[(data > mean - sigma * std) & (data < mean + sigma * std)]
|
||
|
if len(clipped_data) == len(data):
|
||
|
break
|
||
|
data = clipped_data
|
||
|
return data
|
||
2 years ago
|
|
||
|
def profile_tensorrt_model(self, engine_file: str):
|
||
2 years ago
|
if not self.trt or not Path(engine_file).is_file():
|
||
2 years ago
|
return 0.0, 0.0
|
||
|
|
||
2 years ago
|
# Model and input
|
||
2 years ago
|
model = YOLO(engine_file)
|
||
2 years ago
|
input_data = np.random.rand(self.imgsz, self.imgsz, 3).astype(np.float32) # must be FP32
|
||
2 years ago
|
|
||
|
# Warmup runs
|
||
|
elapsed = 0.0
|
||
|
for _ in range(3):
|
||
|
start_time = time.time()
|
||
|
for _ in range(self.num_warmup_runs):
|
||
2 years ago
|
model(input_data, imgsz=self.imgsz, verbose=False)
|
||
2 years ago
|
elapsed = time.time() - start_time
|
||
|
|
||
|
# Compute number of runs as higher of min_time or num_timed_runs
|
||
|
num_runs = max(round(self.min_time / elapsed * self.num_warmup_runs), self.num_timed_runs * 50)
|
||
2 years ago
|
|
||
|
# Timed runs
|
||
|
run_times = []
|
||
2 years ago
|
for _ in tqdm(range(num_runs), desc=engine_file):
|
||
2 years ago
|
results = model(input_data, imgsz=self.imgsz, verbose=False)
|
||
2 years ago
|
run_times.append(results[0].speed['inference']) # Convert to milliseconds
|
||
|
|
||
2 years ago
|
run_times = self.iterative_sigma_clipping(np.array(run_times), sigma=2, max_iters=3) # sigma clipping
|
||
2 years ago
|
return np.mean(run_times), np.std(run_times)
|
||
|
|
||
|
def profile_onnx_model(self, onnx_file: str):
|
||
|
check_requirements('onnxruntime')
|
||
|
import onnxruntime as ort
|
||
|
|
||
|
# Session with either 'TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider'
|
||
|
sess_options = ort.SessionOptions()
|
||
|
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
|
||
2 years ago
|
sess_options.intra_op_num_threads = 8 # Limit the number of threads
|
||
2 years ago
|
sess = ort.InferenceSession(onnx_file, sess_options, providers=['CPUExecutionProvider'])
|
||
|
|
||
|
input_tensor = sess.get_inputs()[0]
|
||
2 years ago
|
input_type = input_tensor.type
|
||
|
|
||
|
# Mapping ONNX datatype to numpy datatype
|
||
|
if 'float16' in input_type:
|
||
|
input_dtype = np.float16
|
||
|
elif 'float' in input_type:
|
||
|
input_dtype = np.float32
|
||
|
elif 'double' in input_type:
|
||
|
input_dtype = np.float64
|
||
|
elif 'int64' in input_type:
|
||
|
input_dtype = np.int64
|
||
|
elif 'int32' in input_type:
|
||
|
input_dtype = np.int32
|
||
|
else:
|
||
|
raise ValueError(f'Unsupported ONNX datatype {input_type}')
|
||
|
|
||
|
input_data = np.random.rand(*input_tensor.shape).astype(input_dtype)
|
||
2 years ago
|
input_name = input_tensor.name
|
||
|
output_name = sess.get_outputs()[0].name
|
||
|
|
||
|
# Warmup runs
|
||
2 years ago
|
elapsed = 0.0
|
||
|
for _ in range(3):
|
||
|
start_time = time.time()
|
||
|
for _ in range(self.num_warmup_runs):
|
||
|
sess.run([output_name], {input_name: input_data})
|
||
|
elapsed = time.time() - start_time
|
||
|
|
||
|
# Compute number of runs as higher of min_time or num_timed_runs
|
||
|
num_runs = max(round(self.min_time / elapsed * self.num_warmup_runs), self.num_timed_runs)
|
||
2 years ago
|
|
||
|
# Timed runs
|
||
|
run_times = []
|
||
2 years ago
|
for _ in tqdm(range(num_runs), desc=onnx_file):
|
||
2 years ago
|
start_time = time.time()
|
||
|
sess.run([output_name], {input_name: input_data})
|
||
|
run_times.append((time.time() - start_time) * 1000) # Convert to milliseconds
|
||
|
|
||
2 years ago
|
run_times = self.iterative_sigma_clipping(np.array(run_times), sigma=2, max_iters=5) # sigma clipping
|
||
2 years ago
|
return np.mean(run_times), np.std(run_times)
|
||
|
|
||
2 years ago
|
def generate_table_row(self, model_name, t_onnx, t_engine, model_info):
|
||
|
layers, params, gradients, flops = model_info
|
||
|
return f'| {model_name:18s} | {self.imgsz} | - | {t_onnx[0]:.2f} ± {t_onnx[1]:.2f} ms | {t_engine[0]:.2f} ± {t_engine[1]:.2f} ms | {params / 1e6:.1f} | {flops:.1f} |'
|
||
2 years ago
|
|
||
2 years ago
|
def generate_results_dict(self, model_name, t_onnx, t_engine, model_info):
|
||
|
layers, params, gradients, flops = model_info
|
||
|
return {
|
||
|
'model/name': model_name,
|
||
|
'model/parameters': params,
|
||
|
'model/GFLOPs': round(flops, 3),
|
||
|
'model/speed_ONNX(ms)': round(t_onnx[0], 3),
|
||
|
'model/speed_TensorRT(ms)': round(t_engine[0], 3)}
|
||
|
|
||
2 years ago
|
def print_table(self, table_rows):
|
||
|
gpu = torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'GPU'
|
||
|
header = f'| Model | size<br><sup>(pixels) | mAP<sup>val<br>50-95 | Speed<br><sup>CPU ONNX<br>(ms) | Speed<br><sup>{gpu} TensorRT<br>(ms) | params<br><sup>(M) | FLOPs<br><sup>(B) |'
|
||
|
separator = '|-------------|---------------------|--------------------|------------------------------|-----------------------------------|------------------|-----------------|'
|
||
|
|
||
2 years ago
|
print(f'\n\n{header}')
|
||
2 years ago
|
print(separator)
|
||
|
for row in table_rows:
|
||
|
print(row)
|
||
|
|
||
|
|
||
2 years ago
|
if __name__ == '__main__':
|
||
2 years ago
|
# Benchmark all export formats
|
||
2 years ago
|
benchmark()
|
||
2 years ago
|
|
||
|
# Profiling models on ONNX and TensorRT
|
||
|
ProfileModels(['yolov8n.yaml', 'yolov8s.yaml'])
|