`ultralytics 8.0.126` Ray Tune refactoring (#3511)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>single_channel
parent
69a2d70a78
commit
4c22273198
@ -1,44 +1,120 @@
|
|||||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||||
|
from ultralytics.yolo.cfg import TASK2DATA, TASK2METRIC
|
||||||
|
from ultralytics.yolo.utils import DEFAULT_CFG_DICT, LOGGER, NUM_THREADS
|
||||||
|
|
||||||
from ultralytics.yolo.utils import LOGGER
|
|
||||||
|
def run_ray_tune(model,
|
||||||
try:
|
space: dict = None,
|
||||||
from ray import tune
|
grace_period: int = 10,
|
||||||
from ray.air import RunConfig, session # noqa
|
gpu_per_trial: int = None,
|
||||||
from ray.air.integrations.wandb import WandbLoggerCallback # noqa
|
max_samples: int = 10,
|
||||||
from ray.tune.schedulers import ASHAScheduler # noqa
|
**train_args):
|
||||||
from ray.tune.schedulers import AsyncHyperBandScheduler as AHB # noqa
|
"""
|
||||||
|
Runs hyperparameter tuning using Ray Tune.
|
||||||
except ImportError:
|
|
||||||
LOGGER.info("Tuning hyperparameters requires ray/tune. Install using `pip install 'ray[tune]'`")
|
Args:
|
||||||
tune = None
|
model (YOLO): Model to run the tuner on.
|
||||||
|
space (dict, optional): The hyperparameter search space. Defaults to None.
|
||||||
default_space = {
|
grace_period (int, optional): The grace period in epochs of the ASHA scheduler. Defaults to 10.
|
||||||
# 'optimizer': tune.choice(['SGD', 'Adam', 'AdamW', 'NAdam', 'RAdam', 'RMSProp']),
|
gpu_per_trial (int, optional): The number of GPUs to allocate per trial. Defaults to None.
|
||||||
'lr0': tune.uniform(1e-5, 1e-1),
|
max_samples (int, optional): The maximum number of trials to run. Defaults to 10.
|
||||||
'lrf': tune.uniform(0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
train_args (dict, optional): Additional arguments to pass to the `train()` method. Defaults to {}.
|
||||||
'momentum': tune.uniform(0.6, 0.98), # SGD momentum/Adam beta1
|
|
||||||
'weight_decay': tune.uniform(0.0, 0.001), # optimizer weight decay 5e-4
|
Returns:
|
||||||
'warmup_epochs': tune.uniform(0.0, 5.0), # warmup epochs (fractions ok)
|
(dict): A dictionary containing the results of the hyperparameter search.
|
||||||
'warmup_momentum': tune.uniform(0.0, 0.95), # warmup initial momentum
|
|
||||||
'box': tune.uniform(0.02, 0.2), # box loss gain
|
Raises:
|
||||||
'cls': tune.uniform(0.2, 4.0), # cls loss gain (scale with pixels)
|
ModuleNotFoundError: If Ray Tune is not installed.
|
||||||
'hsv_h': tune.uniform(0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
"""
|
||||||
'hsv_s': tune.uniform(0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
if train_args is None:
|
||||||
'hsv_v': tune.uniform(0.0, 0.9), # image HSV-Value augmentation (fraction)
|
train_args = {}
|
||||||
'degrees': tune.uniform(0.0, 45.0), # image rotation (+/- deg)
|
|
||||||
'translate': tune.uniform(0.0, 0.9), # image translation (+/- fraction)
|
try:
|
||||||
'scale': tune.uniform(0.0, 0.9), # image scale (+/- gain)
|
from ray import tune
|
||||||
'shear': tune.uniform(0.0, 10.0), # image shear (+/- deg)
|
from ray.air import RunConfig
|
||||||
'perspective': tune.uniform(0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
from ray.air.integrations.wandb import WandbLoggerCallback
|
||||||
'flipud': tune.uniform(0.0, 1.0), # image flip up-down (probability)
|
from ray.tune.schedulers import ASHAScheduler
|
||||||
'fliplr': tune.uniform(0.0, 1.0), # image flip left-right (probability)
|
except ImportError:
|
||||||
'mosaic': tune.uniform(0.0, 1.0), # image mixup (probability)
|
raise ModuleNotFoundError("Tuning hyperparameters requires Ray Tune. Install with: pip install 'ray[tune]'")
|
||||||
'mixup': tune.uniform(0.0, 1.0), # image mixup (probability)
|
|
||||||
'copy_paste': tune.uniform(0.0, 1.0)} # segment copy-paste (probability)
|
try:
|
||||||
|
import wandb
|
||||||
task_metric_map = {
|
|
||||||
'detect': 'metrics/mAP50-95(B)',
|
assert hasattr(wandb, '__version__')
|
||||||
'segment': 'metrics/mAP50-95(M)',
|
except (ImportError, AssertionError):
|
||||||
'classify': 'metrics/accuracy_top1',
|
wandb = False
|
||||||
'pose': 'metrics/mAP50-95(P)'}
|
|
||||||
|
default_space = {
|
||||||
|
# 'optimizer': tune.choice(['SGD', 'Adam', 'AdamW', 'NAdam', 'RAdam', 'RMSProp']),
|
||||||
|
'lr0': tune.uniform(1e-5, 1e-1),
|
||||||
|
'lrf': tune.uniform(0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
||||||
|
'momentum': tune.uniform(0.6, 0.98), # SGD momentum/Adam beta1
|
||||||
|
'weight_decay': tune.uniform(0.0, 0.001), # optimizer weight decay 5e-4
|
||||||
|
'warmup_epochs': tune.uniform(0.0, 5.0), # warmup epochs (fractions ok)
|
||||||
|
'warmup_momentum': tune.uniform(0.0, 0.95), # warmup initial momentum
|
||||||
|
'box': tune.uniform(0.02, 0.2), # box loss gain
|
||||||
|
'cls': tune.uniform(0.2, 4.0), # cls loss gain (scale with pixels)
|
||||||
|
'hsv_h': tune.uniform(0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
||||||
|
'hsv_s': tune.uniform(0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
||||||
|
'hsv_v': tune.uniform(0.0, 0.9), # image HSV-Value augmentation (fraction)
|
||||||
|
'degrees': tune.uniform(0.0, 45.0), # image rotation (+/- deg)
|
||||||
|
'translate': tune.uniform(0.0, 0.9), # image translation (+/- fraction)
|
||||||
|
'scale': tune.uniform(0.0, 0.9), # image scale (+/- gain)
|
||||||
|
'shear': tune.uniform(0.0, 10.0), # image shear (+/- deg)
|
||||||
|
'perspective': tune.uniform(0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
||||||
|
'flipud': tune.uniform(0.0, 1.0), # image flip up-down (probability)
|
||||||
|
'fliplr': tune.uniform(0.0, 1.0), # image flip left-right (probability)
|
||||||
|
'mosaic': tune.uniform(0.0, 1.0), # image mixup (probability)
|
||||||
|
'mixup': tune.uniform(0.0, 1.0), # image mixup (probability)
|
||||||
|
'copy_paste': tune.uniform(0.0, 1.0)} # segment copy-paste (probability)
|
||||||
|
|
||||||
|
def _tune(config):
|
||||||
|
"""
|
||||||
|
Trains the YOLO model with the specified hyperparameters and additional arguments.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config (dict): A dictionary of hyperparameters to use for training.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None.
|
||||||
|
"""
|
||||||
|
model._reset_callbacks()
|
||||||
|
config.update(train_args)
|
||||||
|
model.train(**config)
|
||||||
|
|
||||||
|
# Get search space
|
||||||
|
if not space:
|
||||||
|
space = default_space
|
||||||
|
LOGGER.warning('WARNING ⚠️ search space not provided, using default search space.')
|
||||||
|
|
||||||
|
# Get dataset
|
||||||
|
data = train_args.get('data', TASK2DATA[model.task])
|
||||||
|
space['data'] = data
|
||||||
|
if 'data' not in train_args:
|
||||||
|
LOGGER.warning(f'WARNING ⚠️ data not provided, using default "data={data}".')
|
||||||
|
|
||||||
|
# Define the trainable function with allocated resources
|
||||||
|
trainable_with_resources = tune.with_resources(_tune, {'cpu': NUM_THREADS, 'gpu': gpu_per_trial or 0})
|
||||||
|
|
||||||
|
# Define the ASHA scheduler for hyperparameter search
|
||||||
|
asha_scheduler = ASHAScheduler(time_attr='epoch',
|
||||||
|
metric=TASK2METRIC[model.task],
|
||||||
|
mode='max',
|
||||||
|
max_t=train_args.get('epochs') or DEFAULT_CFG_DICT['epochs'] or 100,
|
||||||
|
grace_period=grace_period,
|
||||||
|
reduction_factor=3)
|
||||||
|
|
||||||
|
# Define the callbacks for the hyperparameter search
|
||||||
|
tuner_callbacks = [WandbLoggerCallback(project='YOLOv8-tune')] if wandb else []
|
||||||
|
|
||||||
|
# Create the Ray Tune hyperparameter search tuner
|
||||||
|
tuner = tune.Tuner(trainable_with_resources,
|
||||||
|
param_space=space,
|
||||||
|
tune_config=tune.TuneConfig(scheduler=asha_scheduler, num_samples=max_samples),
|
||||||
|
run_config=RunConfig(callbacks=tuner_callbacks, storage_path='./runs/tune'))
|
||||||
|
|
||||||
|
# Run the hyperparameter search
|
||||||
|
tuner.fit()
|
||||||
|
|
||||||
|
# Return the results of the hyperparameter search
|
||||||
|
return tuner.get_results()
|
||||||
|
Loading…
Reference in new issue