ultralytics 8.3.51 AutoBach logspace fit and checks (#18283)

Signed-off-by: UltralyticsAssistant <web@ultralytics.com>
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Laughing 2024-12-18 03:06:28 +08:00 committed by GitHub
parent fd8159339c
commit 31aaf0e057
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 6 deletions

View file

@ -1,6 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license # Ultralytics YOLO 🚀, AGPL-3.0 license
__version__ = "8.3.50" __version__ = "8.3.51"
import os import os

View file

@ -77,18 +77,26 @@ def autobatch(model, imgsz=640, fraction=0.60, batch_size=DEFAULT_CFG.batch, max
results = profile(img, model, n=1, device=device, max_num_obj=max_num_obj) results = profile(img, model, n=1, device=device, max_num_obj=max_num_obj)
# Fit a solution # Fit a solution
y = [x[2] for x in results if x] # memory [2] xy = [
p = np.polyfit(batch_sizes[: len(y)], y, deg=1) # first degree polynomial fit [x, y[2]]
b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size) for i, (x, y) in enumerate(zip(batch_sizes, results))
if y # valid result
and isinstance(y[2], (int, float)) # is numeric
and 0 < y[2] < t # between 0 and GPU limit
and (i == 0 or not results[i - 1] or y[2] > results[i - 1][2]) # first item or increasing memory
]
fit_x, fit_y = zip(*xy) if xy else ([], [])
p = np.polyfit(np.log(fit_x), np.log(fit_y), deg=1) # first-degree polynomial fit in log space
b = int(round(np.exp((np.log(f * fraction) - p[1]) / p[0]))) # y intercept (optimal batch size)
if None in results: # some sizes failed if None in results: # some sizes failed
i = results.index(None) # first fail index i = results.index(None) # first fail index
if b >= batch_sizes[i]: # y intercept above failure point if b >= batch_sizes[i]: # y intercept above failure point
b = batch_sizes[max(i - 1, 0)] # select prior safe point b = batch_sizes[max(i - 1, 0)] # select prior safe point
if b < 1 or b > 1024: # b outside of safe range if b < 1 or b > 1024: # b outside of safe range
LOGGER.info(f"{prefix}WARNING ⚠️ batch={b} outside safe range, using default batch-size {batch_size}.")
b = batch_size b = batch_size
LOGGER.info(f"{prefix}WARNING ⚠️ CUDA anomaly detected, using default batch-size {batch_size}.")
fraction = (np.polyval(p, b) + r + a) / t # actual fraction predicted fraction = (np.exp(np.polyval(p, np.log(b))) + r + a) / t # predicted fraction
LOGGER.info(f"{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%) ✅") LOGGER.info(f"{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%) ✅")
return b return b
except Exception as e: except Exception as e: