Reference for ultralytics/engine/tuner.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/tuner.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.engine.tuner.Tuner
Class responsible for hyperparameter tuning of YOLO models.
The class evolves YOLO model hyperparameters over a given number of iterations by mutating them according to the search space and retraining the model to evaluate their performance.
Attributes:
Name | Type | Description |
---|---|---|
space |
dict
|
Hyperparameter search space containing bounds and scaling factors for mutation. |
tune_dir |
Path
|
Directory where evolution logs and results will be saved. |
tune_csv |
Path
|
Path to the CSV file where evolution logs are saved. |
Methods:
Name | Description |
---|---|
_mutate |
dict) -> dict:
Mutates the given hyperparameters within the bounds specified in |
__call__ |
Executes the hyperparameter evolution across multiple iterations. |
Example
Tune hyperparameters for YOLOv8n on COCO8 at imgsz=640 and epochs=30 for 300 tuning iterations.
from ultralytics import YOLO
model = YOLO("yolo11n.pt")
model.tune(data="coco8.yaml", epochs=10, iterations=300, optimizer="AdamW", plots=False, save=False, val=False)
Tune with custom search space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
dict
|
Configuration for hyperparameter evolution. |
DEFAULT_CFG
|
Source code in ultralytics/engine/tuner.py
__call__
Executes the hyperparameter evolution process when the Tuner instance is called.
This method iterates through the number of iterations, performing the following steps in each iteration:
1. Load the existing hyperparameters or initialize new ones.
2. Mutate the hyperparameters using the mutate
method.
3. Train a YOLO model with the mutated hyperparameters.
4. Log the fitness score and mutated hyperparameters to a CSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model
|
A pre-initialized YOLO model to be used for training. |
None
|
iterations
|
int
|
The number of generations to run the evolution for. |
10
|
cleanup
|
bool
|
Whether to delete iteration weights to reduce storage space used during tuning. |
True
|
Note
The method utilizes the self.tune_csv
Path object to read and log hyperparameters and fitness scores.
Ensure this path is set correctly in the Tuner instance.
Source code in ultralytics/engine/tuner.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|