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
Tuner(args=DEFAULT_CFG, _callbacks: list | None = None)
A class 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. Supports both local CSV storage and distributed MongoDB Atlas coordination for multi-machine hyperparameter optimization.
Attributes:
Name | Type | Description |
---|---|---|
space |
dict[str, tuple]
|
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. |
args |
dict
|
Configuration arguments for the tuning process. |
callbacks |
list
|
Callback functions to be executed during tuning. |
prefix |
str
|
Prefix string for logging messages. |
mongodb |
MongoClient
|
Optional MongoDB client for distributed tuning. |
collection |
Collection
|
MongoDB collection for storing tuning results. |
Methods:
Name | Description |
---|---|
_mutate |
Mutate hyperparameters based on bounds and scaling factors. |
__call__ |
Execute the hyperparameter evolution across multiple iterations. |
Examples:
Tune hyperparameters for YOLO11n 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,
>>> plots=False,
>>> save=False,
>>> val=False
>>> )
Tune with distributed MongoDB Atlas coordination across multiple machines:
>>> model.tune(
>>> data="coco8.yaml",
>>> epochs=10,
>>> iterations=300,
>>> mongodb_uri="mongodb+srv://user:pass@cluster.mongodb.net/",
>>> mongodb_db="ultralytics",
>>> mongodb_collection="tune_results"
>>> )
Tune with custom search space:
>>> model.tune(space={"lr0": (1e-5, 1e-1), "momentum": (0.6, 0.98)})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
dict
|
Configuration for hyperparameter evolution. |
DEFAULT_CFG
|
_callbacks
|
list | None
|
Callback functions to be executed during tuning. |
None
|
Source code in ultralytics/engine/tuner.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
__call__
__call__(model=None, iterations: int = 10, cleanup: bool = True)
Execute the hyperparameter evolution process when the Tuner instance is called.
This method iterates through the specified number of iterations, performing the following steps: 1. Sync MongoDB results to CSV (if using distributed mode) 2. Mutate hyperparameters using the best previous results or defaults 3. Train a YOLO model with the mutated hyperparameters 4. Log fitness scores and hyperparameters to MongoDB and/or CSV 5. Track the best performing configuration across all iterations
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model | None
|
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 during tuning. |
True
|
Source code in ultralytics/engine/tuner.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|