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.
180 lines
11 KiB
180 lines
11 KiB
2 years ago
|
---
|
||
|
comments: true
|
||
1 year ago
|
description: Discover how to streamline hyperparameter tuning for YOLOv8 models with Ray Tune. Learn to accelerate tuning, integrate with Weights & Biases, and analyze results.
|
||
|
keywords: Ultralytics, YOLOv8, Ray Tune, hyperparameter tuning, machine learning optimization, Weights & Biases integration, result analysis
|
||
2 years ago
|
---
|
||
|
|
||
1 year ago
|
# Efficient Hyperparameter Tuning with Ray Tune and YOLOv8
|
||
2 years ago
|
|
||
1 year ago
|
Hyperparameter tuning is vital in achieving peak model performance by discovering the optimal set of hyperparameters. This involves running trials with different hyperparameters and evaluating each trial’s performance.
|
||
2 years ago
|
|
||
1 year ago
|
## Accelerate Tuning with Ultralytics YOLOv8 and Ray Tune
|
||
2 years ago
|
|
||
1 year ago
|
[Ultralytics YOLOv8](https://ultralytics.com) incorporates Ray Tune for hyperparameter tuning, streamlining the optimization of YOLOv8 model hyperparameters. With Ray Tune, you can utilize advanced search strategies, parallelism, and early stopping to expedite the tuning process.
|
||
2 years ago
|
|
||
2 years ago
|
### Ray Tune
|
||
2 years ago
|
|
||
1 year ago
|
<p align="center">
|
||
|
<img width="640" src="https://docs.ray.io/en/latest/_images/tune_overview.png" alt="Ray Tune Overview">
|
||
|
</p>
|
||
2 years ago
|
|
||
1 year ago
|
[Ray Tune](https://docs.ray.io/en/latest/tune/index.html) is a hyperparameter tuning library designed for efficiency and flexibility. It supports various search strategies, parallelism, and early stopping strategies, and seamlessly integrates with popular machine learning frameworks, including Ultralytics YOLOv8.
|
||
2 years ago
|
|
||
1 year ago
|
### Integration with Weights & Biases
|
||
2 years ago
|
|
||
1 year ago
|
YOLOv8 also allows optional integration with [Weights & Biases](https://wandb.ai/site) for monitoring the tuning process.
|
||
2 years ago
|
|
||
|
## Installation
|
||
|
|
||
|
To install the required packages, run:
|
||
|
|
||
|
!!! tip "Installation"
|
||
|
|
||
1 year ago
|
=== "CLI"
|
||
1 year ago
|
|
||
1 year ago
|
```bash
|
||
|
# Install and update Ultralytics and Ray Tune packages
|
||
|
pip install -U ultralytics "ray[tune]"
|
||
1 year ago
|
|
||
1 year ago
|
# Optionally install W&B for logging
|
||
|
pip install wandb
|
||
|
```
|
||
2 years ago
|
|
||
|
## Usage
|
||
|
|
||
|
!!! example "Usage"
|
||
|
|
||
1 year ago
|
=== "Python"
|
||
2 years ago
|
|
||
1 year ago
|
```python
|
||
|
from ultralytics import YOLO
|
||
1 year ago
|
|
||
1 year ago
|
# Load a YOLOv8n model
|
||
|
model = YOLO('yolov8n.pt')
|
||
1 year ago
|
|
||
1 year ago
|
# Start tuning hyperparameters for YOLOv8n training on the COCO8 dataset
|
||
|
result_grid = model.tune(data='coco8.yaml', use_ray=True)
|
||
|
```
|
||
2 years ago
|
|
||
|
## `tune()` Method Parameters
|
||
|
|
||
|
The `tune()` method in YOLOv8 provides an easy-to-use interface for hyperparameter tuning with Ray Tune. It accepts several arguments that allow you to customize the tuning process. Below is a detailed explanation of each parameter:
|
||
|
|
||
1 year ago
|
| Parameter | Type | Description | Default Value |
|
||
|
|-----------------|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|
|
||
|
| `data` | `str` | The dataset configuration file (in YAML format) to run the tuner on. This file should specify the training and validation data paths, as well as other dataset-specific settings. | |
|
||
|
| `space` | `dict, optional` | A dictionary defining the hyperparameter search space for Ray Tune. Each key corresponds to a hyperparameter name, and the value specifies the range of values to explore during tuning. If not provided, YOLOv8 uses a default search space with various hyperparameters. | |
|
||
|
| `grace_period` | `int, optional` | The grace period in epochs for the [ASHA scheduler](https://docs.ray.io/en/latest/tune/api/schedulers.html) in Ray Tune. The scheduler will not terminate any trial before this number of epochs, allowing the model to have some minimum training before making a decision on early stopping. | 10 |
|
||
|
| `gpu_per_trial` | `int, optional` | The number of GPUs to allocate per trial during tuning. This helps manage GPU usage, particularly in multi-GPU environments. If not provided, the tuner will use all available GPUs. | None |
|
||
1 year ago
|
| `iterations` | `int, optional` | The maximum number of trials to run during tuning. This parameter helps control the total number of hyperparameter combinations tested, ensuring the tuning process does not run indefinitely. | 10 |
|
||
1 year ago
|
| `**train_args` | `dict, optional` | Additional arguments to pass to the `train()` method during tuning. These arguments can include settings like the number of training epochs, batch size, and other training-specific configurations. | {} |
|
||
2 years ago
|
|
||
|
By customizing these parameters, you can fine-tune the hyperparameter optimization process to suit your specific needs and available computational resources.
|
||
|
|
||
|
## Default Search Space Description
|
||
|
|
||
|
The following table lists the default search space parameters for hyperparameter tuning in YOLOv8 with Ray Tune. Each parameter has a specific value range defined by `tune.uniform()`.
|
||
|
|
||
1 year ago
|
| Parameter | Value Range | Description |
|
||
|
|-------------------|----------------------------|------------------------------------------|
|
||
|
| `lr0` | `tune.uniform(1e-5, 1e-1)` | Initial learning rate |
|
||
|
| `lrf` | `tune.uniform(0.01, 1.0)` | Final learning rate factor |
|
||
|
| `momentum` | `tune.uniform(0.6, 0.98)` | Momentum |
|
||
|
| `weight_decay` | `tune.uniform(0.0, 0.001)` | Weight decay |
|
||
|
| `warmup_epochs` | `tune.uniform(0.0, 5.0)` | Warmup epochs |
|
||
|
| `warmup_momentum` | `tune.uniform(0.0, 0.95)` | Warmup momentum |
|
||
|
| `box` | `tune.uniform(0.02, 0.2)` | Box loss weight |
|
||
|
| `cls` | `tune.uniform(0.2, 4.0)` | Class loss weight |
|
||
|
| `hsv_h` | `tune.uniform(0.0, 0.1)` | Hue augmentation range |
|
||
|
| `hsv_s` | `tune.uniform(0.0, 0.9)` | Saturation augmentation range |
|
||
|
| `hsv_v` | `tune.uniform(0.0, 0.9)` | Value (brightness) augmentation range |
|
||
|
| `degrees` | `tune.uniform(0.0, 45.0)` | Rotation augmentation range (degrees) |
|
||
|
| `translate` | `tune.uniform(0.0, 0.9)` | Translation augmentation range |
|
||
|
| `scale` | `tune.uniform(0.0, 0.9)` | Scaling augmentation range |
|
||
|
| `shear` | `tune.uniform(0.0, 10.0)` | Shear augmentation range (degrees) |
|
||
|
| `perspective` | `tune.uniform(0.0, 0.001)` | Perspective augmentation range |
|
||
|
| `flipud` | `tune.uniform(0.0, 1.0)` | Vertical flip augmentation probability |
|
||
|
| `fliplr` | `tune.uniform(0.0, 1.0)` | Horizontal flip augmentation probability |
|
||
|
| `mosaic` | `tune.uniform(0.0, 1.0)` | Mosaic augmentation probability |
|
||
|
| `mixup` | `tune.uniform(0.0, 1.0)` | Mixup augmentation probability |
|
||
|
| `copy_paste` | `tune.uniform(0.0, 1.0)` | Copy-paste augmentation probability |
|
||
2 years ago
|
|
||
|
## Custom Search Space Example
|
||
|
|
||
|
In this example, we demonstrate how to use a custom search space for hyperparameter tuning with Ray Tune and YOLOv8. By providing a custom search space, you can focus the tuning process on specific hyperparameters of interest.
|
||
|
|
||
|
!!! example "Usage"
|
||
|
|
||
|
```python
|
||
|
from ultralytics import YOLO
|
||
1 year ago
|
|
||
1 year ago
|
# Define a YOLO model
|
||
2 years ago
|
model = YOLO("yolov8n.pt")
|
||
1 year ago
|
|
||
|
# Run Ray Tune on the model
|
||
|
result_grid = model.tune(data="coco128.yaml",
|
||
|
space={"lr0": tune.uniform(1e-5, 1e-1)},
|
||
1 year ago
|
epochs=50,
|
||
|
use_ray=True)
|
||
2 years ago
|
```
|
||
|
|
||
1 year ago
|
In the code snippet above, we create a YOLO model with the "yolov8n.pt" pretrained weights. Then, we call the `tune()` method, specifying the dataset configuration with "coco128.yaml". We provide a custom search space for the initial learning rate `lr0` using a dictionary with the key "lr0" and the value `tune.uniform(1e-5, 1e-1)`. Finally, we pass additional training arguments, such as the number of epochs directly to the tune method as `epochs=50`.
|
||
|
|
||
1 year ago
|
## Processing Ray Tune Results
|
||
1 year ago
|
|
||
|
After running a hyperparameter tuning experiment with Ray Tune, you might want to perform various analyses on the obtained results. This guide will take you through common workflows for processing and analyzing these results.
|
||
|
|
||
1 year ago
|
### Loading Tune Experiment Results from a Directory
|
||
1 year ago
|
|
||
|
After running the tuning experiment with `tuner.fit()`, you can load the results from a directory. This is useful, especially if you're performing the analysis after the initial training script has exited.
|
||
|
|
||
|
```python
|
||
|
experiment_path = f"{storage_path}/{exp_name}"
|
||
|
print(f"Loading results from {experiment_path}...")
|
||
|
|
||
|
restored_tuner = tune.Tuner.restore(experiment_path, trainable=train_mnist)
|
||
|
result_grid = restored_tuner.get_results()
|
||
|
```
|
||
|
|
||
1 year ago
|
### Basic Experiment-Level Analysis
|
||
1 year ago
|
|
||
|
Get an overview of how trials performed. You can quickly check if there were any errors during the trials.
|
||
|
|
||
|
```python
|
||
|
if result_grid.errors:
|
||
|
print("One or more trials failed!")
|
||
|
else:
|
||
|
print("No errors!")
|
||
|
```
|
||
|
|
||
1 year ago
|
### Basic Trial-Level Analysis
|
||
1 year ago
|
|
||
|
Access individual trial hyperparameter configurations and the last reported metrics.
|
||
|
|
||
|
```python
|
||
|
for i, result in enumerate(result_grid):
|
||
|
print(f"Trial #{i}: Configuration: {result.config}, Last Reported Metrics: {result.metrics}")
|
||
|
```
|
||
|
|
||
1 year ago
|
### Plotting the Entire History of Reported Metrics for a Trial
|
||
1 year ago
|
|
||
|
You can plot the history of reported metrics for each trial to see how the metrics evolved over time.
|
||
|
|
||
|
```python
|
||
|
import matplotlib.pyplot as plt
|
||
|
|
||
|
for result in result_grid:
|
||
|
plt.plot(result.metrics_dataframe["training_iteration"], result.metrics_dataframe["mean_accuracy"], label=f"Trial {i}")
|
||
|
|
||
|
plt.xlabel('Training Iterations')
|
||
|
plt.ylabel('Mean Accuracy')
|
||
|
plt.legend()
|
||
|
plt.show()
|
||
|
```
|
||
|
|
||
|
## Summary
|
||
|
|
||
|
In this documentation, we covered common workflows to analyze the results of experiments run with Ray Tune using Ultralytics. The key steps include loading the experiment results from a directory, performing basic experiment-level and trial-level analysis and plotting metrics.
|
||
|
|
||
1 year ago
|
Explore further by looking into Ray Tune’s [Analyze Results](https://docs.ray.io/en/latest/tune/examples/tune_analyze_results.html) docs page to get the most out of your hyperparameter tuning experiments.
|